package com.zjrs.zwnw.zj.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.mohrss.leaf.core.framework.persistens.PageResult;
import org.mohrss.leaf.core.framework.web.controller.AjaxResponse;
import org.mohrss.leaf.core.framework.web.exception.AppException;
import com.zjrs.zwnw.jbxxgl.po.VSAc01PO;
import com.zjrs.zwnw.zj.bpo.GccrcBPO;
import com.zjrs.zwnw.zj.dto.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.StreamUtils;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLEncoder;
import java.util.List;
import java.util.Map;

@Api("高层次人才信息管理")
@RestController
@RequestMapping("/api/zj/gccrc")
public class GccrcController {
    private static final Logger LOG = LoggerFactory.getLogger(GccrcController.class);
    @Resource
    private GccrcBPO gccrcBPO;

    @ApiOperation(value = "高层次人才信息明细")
    @PostMapping(value = "/getGccrcInfo")
    public AjaxResponse getGccrcInfo(@RequestBody @Valid GccrcQueryByIdDTO dto, BindingResult bindingResult) {
        AjaxResponse ajaxResponse = new AjaxResponse();
        //校验入参
        if(bindingResult.hasErrors()) {
            ajaxResponse.setAppcode("1");
            ajaxResponse.setErrorMsg(bindingResult.getFieldError().getDefaultMessage());
            return ajaxResponse;
        }
        try {
            GccrcInfoDTO gccrcInfoDTO = gccrcBPO.getGccrcInfo(dto);
            ajaxResponse.setData("data", gccrcInfoDTO);
        } catch (Exception e) {
            LOG.error(e.getMessage());
            ajaxResponse.setAppcode("1");
            ajaxResponse.setErrorMsg("系统异常");
        }
        return ajaxResponse;
    }
    @ApiOperation(value = "高层次人才信息查询列表")
    @PostMapping(value = "/getGccrcList")
    public AjaxResponse getGccrcList(@RequestBody @Valid GccrcQueryDTO dto, BindingResult bindingResult) {
        AjaxResponse ajaxResponse = new AjaxResponse();
        //校验入参
        if(bindingResult.hasErrors()) {
            ajaxResponse.setAppcode("1");
            ajaxResponse.setErrorMsg(bindingResult.getFieldError().getDefaultMessage());
            return ajaxResponse;
        }
        Integer pagenum = dto.getPagenum();
        Integer pagesize = dto.getPagesize();
        try {
            PageResult pageResult = gccrcBPO.getGccrcList(dto, pagenum, pagesize);
            ajaxResponse.setData("data", pageResult);
            ajaxResponse.setData("pagesize", pagesize);
            ajaxResponse.setData("pagenum", pagenum);
        } catch (Exception e) {
            LOG.error(e.getMessage());
            ajaxResponse.setAppcode("1");
            ajaxResponse.setErrorMsg("系统异常");
        }
        return ajaxResponse;
    }
    @ApiOperation(value = "新增/修改高层次人才信息")
    @PostMapping(value = "/saveOrUpdGccrc")
    public AjaxResponse saveOrUpdGccrc(@RequestBody @Valid GccrcSaveDTO dto, BindingResult bindingResult) {
        AjaxResponse ajaxResponse = new AjaxResponse();
        //校验入参
        if(bindingResult.hasErrors()) {
            ajaxResponse.setAppcode("1");
            ajaxResponse.setErrorMsg(bindingResult.getFieldError().getDefaultMessage());
            return ajaxResponse;
        }
        try {
            ajaxResponse=gccrcBPO.saveOrUpdGccrc(dto);
        } catch (Exception e) {
            LOG.error(e.toString());
            ajaxResponse.setAppcode("1");
            ajaxResponse.setErrorMsg(e.toString());
        }
        return ajaxResponse;
    }
    @ApiOperation(value = "高层次人才信息变更历史查询")
    @PostMapping(value = "/getBglsList")
    public AjaxResponse getBglsList(@RequestBody @Valid GccrcQueryByIdPageDTO dto, BindingResult bindingResult) {
        AjaxResponse ajaxResponse = new AjaxResponse();
        //校验入参
        if(bindingResult.hasErrors()) {
            ajaxResponse.setAppcode("1");
            ajaxResponse.setErrorMsg(bindingResult.getFieldError().getDefaultMessage());
            return ajaxResponse;
        }
        Integer pagenum = dto.getPagenum();
        Integer pagesize = dto.getPagesize();
        try {
            PageResult pageResult = gccrcBPO.getBglsList(dto, pagenum, pagesize);
            ajaxResponse.setData("data", pageResult);
            ajaxResponse.setData("pagesize", pagesize);
            ajaxResponse.setData("pagenum", pagenum);
        } catch (Exception e) {
            LOG.error(e.getMessage());
            ajaxResponse.setAppcode("1");
            ajaxResponse.setErrorMsg("系统异常");
        }
        return ajaxResponse;
    }
    @ApiOperation(value = "注销/取消注销高层次人才信息")
    @PostMapping(value = "/saveIsZxGccrc")
    public AjaxResponse saveIsZxGccrc(@RequestBody @Valid GccrcSaveIsZxDTO dto, BindingResult bindingResult) {
        AjaxResponse ajaxResponse = new AjaxResponse();
        //校验入参
        if(bindingResult.hasErrors()) {
            ajaxResponse.setAppcode("1");
            ajaxResponse.setErrorMsg(bindingResult.getFieldError().getDefaultMessage());
            return ajaxResponse;
        }
        try {
            ajaxResponse=gccrcBPO.saveIsZxGccrc(dto);
        } catch (Exception e) {
            LOG.error(e.toString());
            ajaxResponse.setAppcode("1");
            ajaxResponse.setErrorMsg(e.toString());
        }
        return ajaxResponse;
    }
    @ApiOperation(value = "签发电子卡")
    @PostMapping(value = "/saveQfzt")
    public AjaxResponse saveQfzt(@RequestBody @Valid GccrcQfQueryListDTO dto, BindingResult bindingResult) {
        AjaxResponse ajaxResponse = new AjaxResponse();
        //校验入参
        if(bindingResult.hasErrors()) {
            ajaxResponse.setAppcode("1");
            ajaxResponse.setErrorMsg(bindingResult.getFieldError().getDefaultMessage());
            return ajaxResponse;
        }
        try {
            ajaxResponse=gccrcBPO.saveQfzt(dto);
        } catch (Exception e) {
            LOG.error(e.toString());
            ajaxResponse.setAppcode("1");
            ajaxResponse.setErrorMsg(e.toString());
        }
        return ajaxResponse;
    }
    @ApiOperation(value = "查询电子卡信息")
    @PostMapping(value = "/getDzkxx")
    public AjaxResponse getDzkxx(@RequestBody @Valid GccrcQfQueryDTO dto, BindingResult bindingResult) {
        AjaxResponse ajaxResponse = new AjaxResponse();
        //校验入参
        if(bindingResult.hasErrors()) {
            ajaxResponse.setAppcode("1");
            ajaxResponse.setErrorMsg(bindingResult.getFieldError().getDefaultMessage());
            return ajaxResponse;
        }
        try {
            GccrcQfInfoDTO gccrcInfoDTO = gccrcBPO.getDzkxx(dto);
            ajaxResponse.setData("data", gccrcInfoDTO);
        } catch (Exception e) {
            LOG.error(e.getMessage());
            ajaxResponse.setAppcode("1");
            ajaxResponse.setErrorMsg("系统异常");
        }
        return ajaxResponse;
    }
    @ApiOperation(value = "查询电子卡签发记录")
    @PostMapping(value = "/getDzkqfList")
    public AjaxResponse getDzkqfList(@RequestBody @Valid GccrcQueryByIdPageDTO dto, BindingResult bindingResult) {
        AjaxResponse ajaxResponse = new AjaxResponse();
        //校验入参
        if(bindingResult.hasErrors()) {
            ajaxResponse.setAppcode("1");
            ajaxResponse.setErrorMsg(bindingResult.getFieldError().getDefaultMessage());
            return ajaxResponse;
        }
        Integer pagenum = dto.getPagenum();
        Integer pagesize = dto.getPagesize();
        try {
            PageResult pageResult = gccrcBPO.getDzkqfList(dto, pagenum, pagesize);
            ajaxResponse.setData("data", pageResult);
            ajaxResponse.setData("pagesize", pagesize);
            ajaxResponse.setData("pagenum", pagenum);
        } catch (Exception e) {
            LOG.error(e.getMessage());
            ajaxResponse.setAppcode("1");
            ajaxResponse.setErrorMsg("系统异常");
        }
        return ajaxResponse;
    }
    @ApiOperation(value = "导出高层次人才信息")
    @PostMapping(value = "/exportGccrc")
    public void exportGccrc(@RequestBody GccrcQueryDTO dto, HttpServletResponse res) throws IOException {
        LOG.info("导出事项列表开始");
        try{
            gccrcBPO.exportGccrc(dto, res);
        }catch (Exception e){
            LOG.error(e.getMessage());
        }
        LOG.info("导出事项列表结束");
    }
    @ApiOperation(value = "获取人员信息")
    @PostMapping(value = "/queryRyxx")
    public AjaxResponse queryRyxx(@RequestBody @Valid AC01DTO dto, BindingResult bindingResult) {
        AjaxResponse ajaxResponse = new AjaxResponse();
        //校验入参
        if(bindingResult.hasErrors()) {
            ajaxResponse.setAppcode("1");
            ajaxResponse.setErrorMsg(bindingResult.getFieldError().getDefaultMessage());
            return ajaxResponse;
        }
        try {
            VSAc01PO vsAc01PO = gccrcBPO.queryRyxx(dto);
            ajaxResponse.setData("data", vsAc01PO);
        } catch (Exception e) {
            LOG.error(e.getMessage());
            ajaxResponse.setAppcode("1");
            ajaxResponse.setErrorMsg("系统异常");
        }
        return ajaxResponse;
    }
    @ApiOperation(value = "查询附件记录")
    @PostMapping(value = "/getFjList")
    public AjaxResponse getFjList(@RequestBody @Valid GccrcQueryByIdPageDTO dto, BindingResult bindingResult) {
        AjaxResponse ajaxResponse = new AjaxResponse();
        //校验入参
        if(bindingResult.hasErrors()) {
            ajaxResponse.setAppcode("1");
            ajaxResponse.setErrorMsg(bindingResult.getFieldError().getDefaultMessage());
            return ajaxResponse;
        }
        Integer pagenum = dto.getPagenum();
        Integer pagesize = dto.getPagesize();
        try {
            PageResult pageResult = gccrcBPO.getFjList(dto, pagenum, pagesize);
            ajaxResponse.setData("data", pageResult);
            ajaxResponse.setData("pagesize", pagesize);
            ajaxResponse.setData("pagenum", pagenum);
        } catch (Exception e) {
            LOG.error(e.getMessage());
            ajaxResponse.setAppcode("1");
            ajaxResponse.setErrorMsg("系统异常");
        }
        return ajaxResponse;
    }

    @ApiOperation(value = "保存附件信息")
    @PostMapping(value = "/saveFj")
    public AjaxResponse saveFj(@RequestBody @Valid GccrcFjDTO dto, BindingResult bindingResult) {
        AjaxResponse ajaxResponse = new AjaxResponse();
        //校验入参
        if(bindingResult.hasErrors()) {
            ajaxResponse.setAppcode("1");
            ajaxResponse.setErrorMsg(bindingResult.getFieldError().getDefaultMessage());
            return ajaxResponse;
        }
        try {
            ajaxResponse=gccrcBPO.saveFj(dto);
        } catch (Exception e) {
            LOG.error(e.toString());
            ajaxResponse.setAppcode("1");
            ajaxResponse.setErrorMsg(e.toString());
        }
        return ajaxResponse;
    }

    @ApiOperation(value = "删除附件信息")
    @PostMapping(value = "/delFj")
    public AjaxResponse delFj(@RequestBody @Valid GccrcFjDTO dto, BindingResult bindingResult) {
        AjaxResponse ajaxResponse = new AjaxResponse();
        //校验入参
        if(bindingResult.hasErrors()) {
            ajaxResponse.setAppcode("1");
            ajaxResponse.setErrorMsg(bindingResult.getFieldError().getDefaultMessage());
            return ajaxResponse;
        }
        try {
            ajaxResponse=gccrcBPO.delFj(dto);
        } catch (Exception e) {
            LOG.error(e.toString());
            ajaxResponse.setAppcode("1");
            ajaxResponse.setErrorMsg(e.toString());
        }
        return ajaxResponse;
    }

    @ApiOperation(value = "批量填写公示期数")
    @PostMapping(value = "/batchGsqs")
    public AjaxResponse batchGsqs(@RequestBody @Valid GccrcGsqsDTO dto, BindingResult bindingResult) {
        AjaxResponse ajaxResponse = new AjaxResponse();
        //校验入参
        if(bindingResult.hasErrors()) {
            ajaxResponse.setAppcode("1");
            ajaxResponse.setErrorMsg(bindingResult.getFieldError().getDefaultMessage());
            return ajaxResponse;
        }
        try {
            ajaxResponse=gccrcBPO.batchGsqs(dto);
        } catch (Exception e) {
            LOG.error(e.toString());
            ajaxResponse.setAppcode("1");
            ajaxResponse.setErrorMsg(e.toString());
        }
        return ajaxResponse;
    }
    @ApiOperation(value = "导入高层次人才信息", notes = "导入高层次人才信息")
    @PostMapping("/importGccrc")
    public AjaxResponse importGccrc(MultipartFile file) {
        LOG.info("导入高层次人才信息开始");
        AjaxResponse ajaxResponse = new AjaxResponse();
        if (file == null) {
            ajaxResponse.setAppcode("1");
            ajaxResponse.setMsg("导入失败，导入的文件无效。");
        } else {
            try {
                String suffix = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf(".") + 1);
                if (!"xls".equals(suffix) && !"xlsx".equals(suffix)) {
                    ajaxResponse.setAppcode("1");
                    ajaxResponse.setMsg("导入失败，导入的文件无效，文件格式只能为：xls或xlsx。");

                } else if (file.getSize() > 2 * 1048576) {
                    ajaxResponse.setAppcode("1");
                    ajaxResponse.setMsg("导入失败，导入的文件大小不能超过2M。");
                } else {
                    InputStream is = file.getInputStream();
                    Map<String, String> resultMap = this.gccrcBPO.importGccrc(is);
                    ajaxResponse.setAppcode(resultMap.get("code"));
                    ajaxResponse.setMsg(resultMap.get("msg"));
                    ajaxResponse.setData("data", resultMap);
                }
            } catch (AppException e) {
                LOG.error(e.getMessage());
                ajaxResponse.setAppcode("1");
                ajaxResponse.setErrorMsg(e.getMessage());
            }catch (Exception e) {
                LOG.error(e.getMessage());
                ajaxResponse.setAppcode("1");
                ajaxResponse.setErrorMsg("导入失败，请检查导入文件是否正确。");
            }
        }
        LOG.info("导入高层次人才信息结束");
        return ajaxResponse;
    }
    @ApiOperation(value = "高层次人才信息导入汇总", notes = "高层次人才信息导入汇总")
    @PostMapping("/queryGccrcDrhz")
    public AjaxResponse queryGccrcDrhz(@RequestBody DrDTO dto) {
        AjaxResponse ajaxResponse = new AjaxResponse();
        try {
            List<Map<String,Integer>> list = gccrcBPO.queryGccrcDrhz(dto);
            ajaxResponse.setData("data", list);
        } catch (Exception e) {
            LOG.error(e.getMessage());
            ajaxResponse.setAppcode("1");
            ajaxResponse.setErrorMsg("系统异常");
        }
        return ajaxResponse;
    }
    @ApiOperation(value = "高层次人才信息导入失败", notes = "高层次人才信息导入失败")
    @PostMapping("/queryGccrcDrsb")
    public AjaxResponse queryGccrcDrsb(@RequestBody @Valid DrDTO dto, BindingResult bindingResult) {
        AjaxResponse ajaxResponse = new AjaxResponse();
        //校验入参
        if(bindingResult.hasErrors()) {
            ajaxResponse.setAppcode("1");
            ajaxResponse.setErrorMsg(bindingResult.getFieldError().getDefaultMessage());
            return ajaxResponse;
        }
        Integer pagenum = dto.getPagenum();
        Integer pagesize = dto.getPagesize();
        try {
            PageResult pageResult = gccrcBPO.queryGccrcDrsb(dto, pagenum, pagesize);
            ajaxResponse.setData("data", pageResult);
            ajaxResponse.setData("pagesize", pagesize);
            ajaxResponse.setData("pagenum", pagenum);
        } catch (Exception e) {
            LOG.error(e.getMessage());
            ajaxResponse.setAppcode("1");
            ajaxResponse.setErrorMsg("系统异常");
        }
        return ajaxResponse;
    }
    @ApiOperation(value = "高层次人才信息导入成功", notes = "高层次人才信息导入成功")
    @PostMapping("/queryGccrcDrcg")
    public AjaxResponse queryGccrcDrcg(@RequestBody @Valid DrDTO dto, BindingResult bindingResult) {
        AjaxResponse ajaxResponse = new AjaxResponse();
        //校验入参
        if(bindingResult.hasErrors()) {
            ajaxResponse.setAppcode("1");
            ajaxResponse.setErrorMsg(bindingResult.getFieldError().getDefaultMessage());
            return ajaxResponse;
        }
        Integer pagenum = dto.getPagenum();
        Integer pagesize = dto.getPagesize();
        try {
            PageResult pageResult = gccrcBPO.queryGccrcDrcg(dto, pagenum, pagesize);
            ajaxResponse.setData("data", pageResult);
            ajaxResponse.setData("pagesize", pagesize);
            ajaxResponse.setData("pagenum", pagenum);
        } catch (Exception e) {
            LOG.error(e.getMessage());
            ajaxResponse.setAppcode("1");
            ajaxResponse.setErrorMsg("系统异常");
        }
        return ajaxResponse;
    }

    @ApiOperation(value = "导入照片", notes = "导入照片")
    @PostMapping("/importPhoto")
    public AjaxResponse importGccrcPhoto(MultipartFile file) {
        LOG.info("导入照片开始");
        AjaxResponse ajaxResponse = new AjaxResponse();
        if (file == null) {
            ajaxResponse.setAppcode("1");
            ajaxResponse.setMsg("导入失败，导入的文件无效。");
        } else {
            try {
                String suffix = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf(".") + 1);
                if (!"rar".equals(suffix.toLowerCase()) && !"zip".equals(suffix.toLowerCase())) {
                    ajaxResponse.setAppcode("1");
                    ajaxResponse.setMsg("导入失败，导入的文件无效，文件格式只能为：rar或zip。");
                } else if (file.getSize() > 10 * 1048576) {
                    ajaxResponse.setAppcode("1");
                    ajaxResponse.setMsg("导入失败，导入的文件大小不能超过10M。");
                } else {
                    Map<String, String> resultMap = this.gccrcBPO.importPhoto(file,suffix);
                    ajaxResponse.setAppcode(resultMap.get("code"));
                    ajaxResponse.setMsg(resultMap.get("msg"));
                    ajaxResponse.setData("data", resultMap);
                }
            } catch (Exception e) {
                e.printStackTrace();
                LOG.error(e.getMessage());
                ajaxResponse.setAppcode("1");
                ajaxResponse.setErrorMsg("导入失败，请检查导入文件是否正确。");
            }
        }
        LOG.info("导入照片结束");
        return ajaxResponse;
    }

    @ApiOperation(value = "导入照片汇总", notes = "导入照片汇总")
    @PostMapping("/queryPhotoDrhz")
    public AjaxResponse queryPhotoDrhz(@RequestBody DrDTO dto) {
        AjaxResponse ajaxResponse = new AjaxResponse();
        try {
            List<Map<String,Integer>> list = gccrcBPO.queryPhotoDrhz(dto);
            ajaxResponse.setData("data", list);
        } catch (Exception e) {
            LOG.error(e.getMessage());
            ajaxResponse.setAppcode("1");
            ajaxResponse.setErrorMsg("系统异常");
        }
        return ajaxResponse;
    }
    @ApiOperation(value = "导入照片失败", notes = "导入照片失败")
    @PostMapping("/queryPhotoDrsb")
    public AjaxResponse queryPhotoDrsb(@RequestBody @Valid DrDTO dto, BindingResult bindingResult) {
        AjaxResponse ajaxResponse = new AjaxResponse();
        //校验入参
        if(bindingResult.hasErrors()) {
            ajaxResponse.setAppcode("1");
            ajaxResponse.setErrorMsg(bindingResult.getFieldError().getDefaultMessage());
            return ajaxResponse;
        }
        Integer pagenum = dto.getPagenum();
        Integer pagesize = dto.getPagesize();
        try {
            PageResult pageResult = gccrcBPO.queryPhotoDrsb(dto, pagenum, pagesize);
            ajaxResponse.setData("data", pageResult);
            ajaxResponse.setData("pagesize", pagesize);
            ajaxResponse.setData("pagenum", pagenum);
        } catch (Exception e) {
            LOG.error(e.getMessage());
            ajaxResponse.setAppcode("1");
            ajaxResponse.setErrorMsg("系统异常");
        }
        return ajaxResponse;
    }
    @ApiOperation(value = "导入照片成功", notes = "导入照片成功")
    @PostMapping("/queryPhotoDrcg")
    public AjaxResponse queryPhotoDrcg(@RequestBody @Valid DrDTO dto, BindingResult bindingResult) {
        AjaxResponse ajaxResponse = new AjaxResponse();
        //校验入参
        if(bindingResult.hasErrors()) {
            ajaxResponse.setAppcode("1");
            ajaxResponse.setErrorMsg(bindingResult.getFieldError().getDefaultMessage());
            return ajaxResponse;
        }
        Integer pagenum = dto.getPagenum();
        Integer pagesize = dto.getPagesize();
        try {
            PageResult pageResult = gccrcBPO.queryPhotoDrcg(dto, pagenum, pagesize);
            ajaxResponse.setData("data", pageResult);
            ajaxResponse.setData("pagesize", pagesize);
            ajaxResponse.setData("pagenum", pagenum);
        } catch (Exception e) {
            LOG.error(e.getMessage());
            ajaxResponse.setAppcode("1");
            ajaxResponse.setErrorMsg("系统异常");
        }
        return ajaxResponse;
    }
    @ApiOperation(value = "下载导入模板", notes = "下载导入模板")
    @GetMapping(value = "/downloadTemplate")
    public void downloadTemplate(HttpServletRequest request, HttpServletResponse res) throws IOException {
        LOG.info("下载导入模板开始");
        InputStream is = GccrcController.class.getClassLoader()
                .getResourceAsStream("templates" + File.separator + "zj" + File.separator + "importGccrc.xls");
        res.setContentType("application/octet-stream; charset=UTF-8");
        res.setHeader("Content-disposition",
                "attachment;filename=" + URLEncoder.encode("湛江市高层次人才数据导入模板.xls", "UTF-8"));
        try {
            StreamUtils.copy(is, res.getOutputStream());
        } catch (IOException e) {
            LOG.error(e.getMessage());
        } finally {
            if (is != null) {
                is.close();
            }
            if (res.getOutputStream() != null) {
                res.getOutputStream().close();
            }
        }
        LOG.info("下载导入模板结束");
    }

}
