package cn.iocoder.yudao.module.visit.controller.admin.info;

import cn.iocoder.yudao.framework.idempotent.core.annotation.Idempotent;
import com.fhs.common.utils.StringUtil;
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.visit.controller.admin.info.vo.*;
import cn.iocoder.yudao.module.visit.dal.dataobject.info.InfoDO;
import cn.iocoder.yudao.module.visit.service.info.InfoService;

@Tag(name = "管理后台 - 客户拜访记录")
@RestController
@RequestMapping("/visit/info")
@Validated
public class  InfoController {

    @Resource
    private InfoService infoService;

    @PostMapping("/create")
    @Operation(summary = "创建客户拜访记录")
    @PreAuthorize("@ss.hasPermission('visit:info:create')")
    @Idempotent(timeout = 10, message = "重复请求，请稍后重试")
    public CommonResult<Long> createInfo(@Valid @RequestBody InfoSaveReqVO createReqVO) {
        return success(infoService.createInfo(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新客户拜访记录")
    @PreAuthorize("@ss.hasPermission('visit:info:update')")
    @Idempotent(timeout = 10, message = "重复请求，请稍后重试")
    public CommonResult<Boolean> updateInfo(@Valid @RequestBody InfoSaveReqVO updateReqVO) {
        infoService.updateInfo(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除客户拜访记录")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('visit:info:delete')")
    @Idempotent(timeout = 10, message = "重复请求，请稍后重试")
    public CommonResult<Boolean> deleteInfo(@RequestParam("id") Long id) {
        infoService.deleteInfo(id);
        return success(true);
    }

    @DeleteMapping("/deleteByIds")
    @Operation(summary = "删除客户拜访记录")
    @Parameter(name = "ids", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('visit:info:delete')")
    @Idempotent(timeout = 10, message = "重复请求，请稍后重试")
    public CommonResult<Boolean> deleteInfoByIds(@RequestParam("ids") String ids) {
        //如果ids是空的，返回异常提示，请先选择需要打印的记录
        if (StringUtil.isEmpty(ids)) {
            throw  new RuntimeException("请先选择需要删除的记录");
        }
        String[] split = ids.split(",");
        //转为List
        List<Long> idList = new ArrayList<>();
        for (String s : split) {
            idList.add(Long.parseLong(s));
        }
        infoService.deleteInfoByIds(idList);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得客户拜访记录")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('visit:info:query')")
    public CommonResult<InfoRespVO> getInfo(@RequestParam("id") Long id) {
        InfoDO info = infoService.getInfo(id);
        return success(BeanUtils.toBean(info, InfoRespVO.class));
    }

    @GetMapping("/getPrintListByIds")
    @Operation(summary = "获得客户拜访打印信息根据Ids")
    @PreAuthorize("@ss.hasPermission('visit:info:print')")
    public CommonResult<List<InfoPrintVO>> getPrintListByIds(@RequestParam("ids") String ids) {
        //如果ids是空的，返回异常提示，请先选择需要打印的记录
        if (StringUtil.isEmpty(ids)) {
            throw  new RuntimeException("请先选择需要打印的记录");
        }
        String[] split = ids.split(",");
        //转为List
        List<Long> idList = new ArrayList<>();
        for (String s : split) {
            idList.add(Long.parseLong(s));
        }
        List<InfoPrintVO> info = infoService.getInfoByIds(idList);
        return success(info);
    }

    @GetMapping("/getUnionPrintListByIds")
    @Operation(summary = "获得合并客户拜访打印信息根据Ids")
    @PreAuthorize("@ss.hasPermission('visit:info:print')")
    public CommonResult<List<InfoPrintVO>> getUnionPrintListByIds(@RequestParam("ids") String ids) {
        //如果ids是空的，返回异常提示，请先选择需要打印的记录
        if (StringUtil.isEmpty(ids)) {
            throw  new RuntimeException("请先选择需要合并的打印的记录");
        }
        String[] split = ids.split(",");
        //转为List
        List<Long> idList = new ArrayList<>();
        for (String s : split) {
            idList.add(Long.parseLong(s));
        }
        List<InfoPrintVO> info = infoService.getUnionInfoByIds(idList);
        return success(info);
    }

    @GetMapping("/getUnionAllPrintListByIds")
    @Operation(summary = "获得合并客户拜访打印信息根据Ids")
    @PreAuthorize("@ss.hasPermission('visit:info:print')")
    public CommonResult<List<InfoPrintVO>> getUnionAllPrintListByIds(@Valid InfoPageReqVO pageReqVO) {
        PageResult<InfoDO> pageResult = infoService.getInfoPage(pageReqVO);
        if (pageResult.getTotal()==0){
            throw new RuntimeException("当前不存在可打印数据");
        }
        List<InfoDO> list = pageResult.getList();

        List<InfoPrintVO> info = infoService.getUnionAllInfoByIds(list);
        return success(info);
    }

    @GetMapping("/getPrintListByCompanyName")
    @Operation(summary = "获得客户拜访打印信息根据公司名称")
    @PreAuthorize("@ss.hasPermission('visit:customer-info:print')")
    public CommonResult<List<InfoPrintVO>> getPrintListByCompanyName(@RequestParam("companyName") String companyName) {
        InfoPrintVO info = infoService.getInfoByCompanyName(companyName);
        List<InfoPrintVO> infoPrintVO = new ArrayList<>();
        if (info == null){
            return success(infoPrintVO);
        }
        infoPrintVO.add(info);
        return success(infoPrintVO);
    }

    @GetMapping("/page")
    @Operation(summary = "获得客户拜访记录分页")
    @PreAuthorize("@ss.hasPermission('visit:info:query')")
    public CommonResult<PageResult<InfoRespVO>> getInfoPage(@Valid InfoPageReqVO pageReqVO) {
        PageResult<InfoDO> pageResult = infoService.getInfoPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, InfoRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出客户拜访记录 Excel")
    @PreAuthorize("@ss.hasPermission('visit:info:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportInfoExcel(@Valid InfoPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<InfoDO> list = infoService.getInfoPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "客户拜访记录.xls", "数据", InfoRespVO.class,
                        BeanUtils.toBean(list, InfoRespVO.class));
    }

}