package cn.iocoder.yudao.module.tkdata.controller.admin.customserviceinfo;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.tkdata.controller.admin.customserviceinfo.vo.*;
import cn.iocoder.yudao.module.tkdata.dal.dataobject.customserviceinfo.CustomServiceInfoDO;
import cn.iocoder.yudao.module.tkdata.service.customserviceinfo.CustomServiceInfoService;

@Tag(name = "管理后台 - 客服信息")
@RestController
@RequestMapping("/server/custom-service-info")
@Validated
public class CustomServiceInfoController {

    @Resource
    private CustomServiceInfoService customServiceInfoService;

    @PostMapping("/create")
    @Operation(summary = "创建客服信息")
    @PreAuthorize("@ss.hasPermission('server:custom-service-info:create')")
    public CommonResult<Long> createCustomServiceInfo(@Valid @RequestBody CustomServiceInfoSaveReqVO createReqVO) {
        return success(customServiceInfoService.createCustomServiceInfo(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新客服信息")
    @PreAuthorize("@ss.hasPermission('server:custom-service-info:update')")
    public CommonResult<Boolean> updateCustomServiceInfo(@Valid @RequestBody CustomServiceInfoSaveReqVO updateReqVO) {
        customServiceInfoService.updateCustomServiceInfo(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除客服信息")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('server:custom-service-info:delete')")
    public CommonResult<Boolean> deleteCustomServiceInfo(@RequestParam("id") Long id) {
        customServiceInfoService.deleteCustomServiceInfo(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除客服信息")
                @PreAuthorize("@ss.hasPermission('server:custom-service-info:delete')")
    public CommonResult<Boolean> deleteCustomServiceInfoList(@RequestParam("ids") List<Long> ids) {
        customServiceInfoService.deleteCustomServiceInfoListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得客服信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('server:custom-service-info:query')")
    public CommonResult<CustomServiceInfoRespVO> getCustomServiceInfo(@RequestParam("id") Long id) {
        CustomServiceInfoDO customServiceInfo = customServiceInfoService.getCustomServiceInfo(id);
        return success(BeanUtils.toBean(customServiceInfo, CustomServiceInfoRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得客服信息分页")
    @PreAuthorize("@ss.hasPermission('server:custom-service-info:query')")
    public CommonResult<PageResult<CustomServiceInfoRespVO>> getCustomServiceInfoPage(@Valid CustomServiceInfoPageReqVO pageReqVO) {
        PageResult<CustomServiceInfoDO> pageResult = customServiceInfoService.getCustomServiceInfoPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, CustomServiceInfoRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出客服信息 Excel")
    @PreAuthorize("@ss.hasPermission('server:custom-service-info:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportCustomServiceInfoExcel(@Valid CustomServiceInfoPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<CustomServiceInfoDO> list = customServiceInfoService.getCustomServiceInfoPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "客服信息.xls", "数据", CustomServiceInfoRespVO.class,
                        BeanUtils.toBean(list, CustomServiceInfoRespVO.class));
    }

}