package com.zjrs.ggfw.rlzysc.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 com.zjrs.ggfw.portal.bpo.CommBPO;
import com.zjrs.ggfw.rlzysc.bpo.YgbaBPO;
import com.zjrs.ggfw.rlzysc.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.Map;

@Api("用工备案")
@RestController
@RequestMapping("/api/rlzysc/ygba")
public class YgbaController {

    private static final Logger LOG = LoggerFactory.getLogger(YgbaController.class);

    @Resource
    private YgbaBPO ygbaBPO;
    @Resource
    private CommBPO commBPO;

    /**
     * 获取单位信息
     */
    @ApiOperation(value = "用工备案获取单位信息", notes = "用工备案获取单位信息")
    @PostMapping("/ygbaGetDwxx")
    public AjaxResponse ygbaGetDwxx() {
        AjaxResponse ajaxResponse = new AjaxResponse();
        ajaxResponse.setData("data", ygbaBPO.getDwxx());
        return ajaxResponse;
    }

    /**
     * 保存单位信息
     */
    @ApiOperation(value = "用工备案保存单位信息", notes = "用工备案保存单位信息")
    @PostMapping("/ygbaSaveDwxx")
    public AjaxResponse ygbaSaveDwxx(@RequestBody @Valid YgbadwUDTO dto, BindingResult bindingResult) {
        AjaxResponse ajaxResponse = new AjaxResponse();
        if(bindingResult.hasErrors()) {//校验入参
            return this.commBPO.checkRequestParameter(bindingResult);
        }
        ajaxResponse.setData("rsrlzyygbadwlsh", ygbaBPO.saveDwxx(dto));
        return ajaxResponse;
    }

    /**
     * 用工备案获取员工列表
     * @param dto
     * @param bindingResult
     * @return
     */
    @ApiOperation(value = "用工备案获取员工列表", notes = "用工备案获取员工列表")
    @PostMapping("/ygbaGetYgxxlist")
    public AjaxResponse ygbaGetYgxxlist(@RequestBody @Valid YgbaYgxxQueryDTO dto, BindingResult bindingResult) {
        AjaxResponse ajaxResponse = new AjaxResponse();

        if(bindingResult.hasErrors()) {//校验入参
            return this.commBPO.checkRequestParameter(bindingResult);
        }

        Integer pagenum = Math.toIntExact(dto.getPagenum());
        Integer pagesize = Math.toIntExact(dto.getPagesize());
        PageResult pageResult = ygbaBPO.getYgxxlist(dto, pagenum, pagesize);
        ajaxResponse.setData("data", pageResult);
        ajaxResponse.setData("pagesize", pagesize);
        ajaxResponse.setData("pagenum", pagenum);
        return ajaxResponse;
    }

    /**
     * 获取员工信息
     */
    @ApiOperation(value = "用工备案获取员工信息", notes = "用工备案获取员工信息")
    @PostMapping("/ygbaGetYgxx")
    public AjaxResponse ygbaGetYgxx(@RequestBody @Valid YgbaygCommDTO dto, BindingResult bindingResult) {
        AjaxResponse ajaxResponse = new AjaxResponse();
        if(bindingResult.hasErrors()) {//校验入参
            return this.commBPO.checkRequestParameter(bindingResult);
        }
        ajaxResponse.setData("data", ygbaBPO.getYgxx(dto));
        return ajaxResponse;
    }
    /**
     * 保存员工信息
     */
    @ApiOperation(value = "用工备案保存员工信息", notes = "用工备案保存员工信息")
    @PostMapping("/ygbaSaveYgxx")
    public AjaxResponse ygbaSaveYgxx(@RequestBody @Valid YgbaygUDTO dto, BindingResult bindingResult) {
        AjaxResponse ajaxResponse = new AjaxResponse();
        if(bindingResult.hasErrors()) {//校验入参
            return this.commBPO.checkRequestParameter(bindingResult);
        }
        ajaxResponse.setData("rsrlzyygbaygywlsh", ygbaBPO.saveYgxx(dto));
        return ajaxResponse;
    }
    /**
     * 员工批量提交
     */
    @ApiOperation(value = "用工备案员工批量提交", notes = "用工备案员工批量提交")
    @PostMapping("/ygbaBatchSubmitYgxx")
    public AjaxResponse ygbaBatchSubmitYgxx(@RequestBody @Valid YgbaygCommDTO dto, BindingResult bindingResult) {
        AjaxResponse ajaxResponse = new AjaxResponse();
        if(bindingResult.hasErrors()) {//校验入参
            return this.commBPO.checkRequestParameter(bindingResult);
        }
        ygbaBPO.batchSubmitYgxx(dto);
        return ajaxResponse;
    }

    /**
     * 员工信息删除
     */
    @ApiOperation(value = "用工备案员工信息删除", notes = "用工备案员工信息删除")
    @PostMapping("/ygbaDelYgxx")
    public AjaxResponse ygbaDelYgxx(@RequestBody @Valid YgbaygCommDTO dto, BindingResult bindingResult) {
        AjaxResponse ajaxResponse = new AjaxResponse();
        if(bindingResult.hasErrors()) {//校验入参
            return this.commBPO.checkRequestParameter(bindingResult);
        }
        ygbaBPO.delYgxx(dto);
        return ajaxResponse;
    }

    /**
     * 用工备案员工离职
     */
    @ApiOperation(value = "用工备案员工离职", notes = "用工备案员工离职")
    @PostMapping("/ygbaQuitYgxx")
    public AjaxResponse ygbaQuitYgxx(@RequestBody @Valid YgbaygQuitDTO dto, BindingResult bindingResult) {
        AjaxResponse ajaxResponse = new AjaxResponse();
        if(bindingResult.hasErrors()) {//校验入参
            return this.commBPO.checkRequestParameter(bindingResult);
        }
        ygbaBPO.quitYgxx(dto);
        return ajaxResponse;
    }

    /**
     * 用工备案下载导入模板
     * @param request
     * @param res
     * @throws IOException
     */
    @ApiOperation(value = "用工备案下载导入模板", notes = "用工备案下载导入模板")
    @GetMapping(value = "/ygbaDownloadTemplate")
    public void ygbaDownloadTemplate(HttpServletRequest request, HttpServletResponse res) throws IOException {
        LOG.info("下载导入模板开始");
        InputStream is = YgbaController.class.getClassLoader()
                .getResourceAsStream("templates" + File.separator + "rlzysc" + File.separator + "ygbaimport.xlsx");
        res.setContentType("application/octet-stream; charset=UTF-8");
        res.setHeader("Content-disposition",
                "attachment;filename=" + URLEncoder.encode("职工名册导入模板.xlsx", "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("下载导入模板结束");
    }

    /**
     * 用工备案员工导入
     * @param file
     * @param request
     * @return
     */
    @ApiOperation(value = "用工备案员工导入", notes = "用工备案员工导入")
    @PostMapping("/ygbaImportYgxx")
    @ResponseBody
    public AjaxResponse ygbaImportYgxx(MultipartFile file, HttpServletRequest request) {
        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.ygbaBPO.importYgxx(is);
                    ajaxResponse.setAppcode(resultMap.get("code"));
                    ajaxResponse.setMsg(resultMap.get("msg"));
                    ajaxResponse.setData("data", resultMap);
                }
            } catch (Exception e) {
                LOG.error(e.getMessage());
                ajaxResponse.setAppcode("1");
                ajaxResponse.setErrorMsg("导入失败，请检查导入文件是否正确。");
            }
        }
        LOG.info("员工导入结束");
        return ajaxResponse;
    }

    /**
     * 用工备案导入汇总列表
     * @param dto
     * @param bindingResult
     * @return
     */
    @ApiOperation(value = "用工备案导入汇总列表", notes = "用工备案导入汇总列表")
    @PostMapping("/ygbaImportHzList")
    public AjaxResponse ygbaImportHzList(@RequestBody @Valid YgbaygtempQueryDTO dto, BindingResult bindingResult) {
        LOG.info("导入汇总列表开始");
        AjaxResponse ajaxResponse = new AjaxResponse();
        if(bindingResult.hasErrors()) {//校验入参
            return this.commBPO.checkRequestParameter(bindingResult);
        }
        Integer pagenum = Math.toIntExact(dto.getPagenum());
        Integer pagesize = Math.toIntExact(dto.getPagesize());
        PageResult pageResult = this.ygbaBPO.importHzList(dto, pagenum, pagesize);
        ajaxResponse.setData("data", pageResult);
        ajaxResponse.setData("pagesize", pagesize);
        ajaxResponse.setData("pagenum", pagenum);
        LOG.info("导入汇总列表结束");
        return ajaxResponse;
    }

    @ApiOperation(value = "用工备案导入结果列表", notes = "用工备案导入结果列表")
    @PostMapping("/ygbaImportMxList")
    public AjaxResponse ygbaImportMxList(@RequestBody @Valid YgbaygtempQueryDTO dto, BindingResult bindingResult) {
        LOG.info("用工备案导入结果列表开始");
        AjaxResponse ajaxResponse = new AjaxResponse();
        if(bindingResult.hasErrors()) {//校验入参
            return this.commBPO.checkRequestParameter(bindingResult);
        }
        Integer pagenum = Math.toIntExact(dto.getPagenum());
        Integer pagesize = Math.toIntExact(dto.getPagesize());
        PageResult pageResult = this.ygbaBPO.importMxList(dto, pagenum, pagesize);
        ajaxResponse.setData("data", pageResult);
        ajaxResponse.setData("pagesize", pagesize);
        ajaxResponse.setData("pagenum", pagenum);
        LOG.info("用工备案导入结果列表结束");
        return ajaxResponse;
    }

    /**
     * 获取个人基本信息
     */
    @ApiOperation(value = "获取个人基本信息", notes = "获取个人基本信息")
    @PostMapping("/ygbaGetGrjbxx")
    public AjaxResponse ygbaGetGrjbxx(@RequestBody @Valid YgbaygCommDTO dto, BindingResult bindingResult) {
        AjaxResponse ajaxResponse = new AjaxResponse();
        if(bindingResult.hasErrors()) {//校验入参
            return this.commBPO.checkRequestParameter(bindingResult);
        }
        ajaxResponse.setData("data", ygbaBPO.ygbaGetGrjbxx(dto));
        return ajaxResponse;
    }

}

