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

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')")
    public CommonResult<Long> createInfo(@Valid @RequestBody InfoSaveReqVO createReqVO) {
        return success(infoService.createInfo(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新客户拜访记录")
    @PreAuthorize("@ss.hasPermission('visit:info:update')")
    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')")
    public CommonResult<Boolean> deleteInfo(@RequestParam("id") Long id) {
        infoService.deleteInfo(id);
        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")
    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("/getPrintListByCompanyName")
    @Operation(summary = "获得客户拜访打印信息根据公司名称")
    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));
    }

}