package com.zjrs.zwnw.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 org.mohrss.leaf.core.framework.web.exception.AppException;
import com.zjrs.zwnw.portal.bpo.CommonBPO;
import com.zjrs.zwnw.rlzysc.bpo.YgbaBPO;
import com.zjrs.zwnw.rlzysc.dto.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import java.io.IOException;

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

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

    @Resource
    private YgbaBPO ygbaBPO;
    @Resource
    private CommonBPO commBPO;

    /**
     * 用工备案用工信息审核列表
     * @param dto
     * @param bindingResult
     * @return
     */
    @ApiOperation(value = "用工备案用工信息审核列表", notes = "用工备案用工信息审核列表")
    @PostMapping("/ygbaGetYgxxshList")
    public AjaxResponse ygbaGetYgxxshList(@RequestBody @Valid YgbaDwxxQueryDTO 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());
        dto.setLx("1");
        PageResult pageResult = ygbaBPO.ygbaGetYgxxList(dto, pagenum, pagesize);
        ajaxResponse.setData("data", pageResult);
        ajaxResponse.setData("pagesize", pagesize);
        ajaxResponse.setData("pagenum", pagenum);
        return ajaxResponse;
    }

    /**
     * 用工备案用工信息管理列表
     * @param dto
     * @param bindingResult
     * @return
     */
    @ApiOperation(value = "用工备案用工信息管理列表", notes = "用工备案用工信息管理列表")
    @PostMapping("/ygbaGetYgxxglList")
    public AjaxResponse ygbaGetYgxxglList(@RequestBody @Valid YgbaDwxxQueryDTO 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.ygbaGetYgxxList(dto, pagenum, pagesize);
        ajaxResponse.setData("data", pageResult);
        ajaxResponse.setData("pagesize", pagesize);
        ajaxResponse.setData("pagenum", pagenum);
        return ajaxResponse;
    }


    /**
     * 用工备案获取单位用工情况
     */
    @ApiOperation(value = "用工备案获取单位用工情况", notes = "用工备案获取单位用工情况")
    @PostMapping("/ygbaGetDwxx")
    public AjaxResponse ygbaGetDwxx(@RequestBody @Valid YgbaygCommDTO dto, BindingResult bindingResult) {
        AjaxResponse ajaxResponse = new AjaxResponse();
        if(bindingResult.hasErrors()) {//校验入参
            return this.commBPO.checkRequestParameter(bindingResult);
        }
        ajaxResponse.setData("data", ygbaBPO.getDwxx(dto));
        return ajaxResponse;
    }

    /**
     * 用工备案审核职工名册列表
     * @param dto
     * @param bindingResult
     * @return
     */
    @ApiOperation(value = "用工备案审核职工名册列表", notes = "用工备案审核职工名册列表")
    @PostMapping("/ygbaGetZgmcShList")
    public AjaxResponse ygbaGetZgmcShList(@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.ygbaGetZgmcShList(dto, pagenum, pagesize);
        ajaxResponse.setData("data", pageResult);
        ajaxResponse.setData("pagesize", pagesize);
        ajaxResponse.setData("pagenum", pagenum);
        return ajaxResponse;
    }


    /**
     * 用工备案职工信息审核
     */
    @ApiOperation(value = "用工备案职工信息审核", notes = "用工备案职工信息审核")
    @PostMapping("/ygbaYgxxSh")
    public AjaxResponse ygbaBatchSubmitYgxx(@RequestBody @Valid YgbaygShDTO dto, BindingResult bindingResult) {
        AjaxResponse ajaxResponse = new AjaxResponse();
        if(bindingResult.hasErrors()) {//校验入参
            return this.commBPO.checkRequestParameter(bindingResult);
        }
        ygbaBPO.ygbaYgxxSh(dto);
        return ajaxResponse;
    }
    /**
     * 用工备案审核职工名册列表
     * @param dto
     * @param bindingResult
     * @return
     */
    @ApiOperation(value = "用工备案查看职工名册列表", notes = "用工备案查看职工名册列表")
    @PostMapping("/ygbaGetZgmcCkList")
    public AjaxResponse ygbaGetZgmcCkList(@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.ygbaGetZgmcCkList(dto, pagenum, pagesize);
        ajaxResponse.setData("data", pageResult);
        ajaxResponse.setData("pagesize", pagesize);
        ajaxResponse.setData("pagenum", pagenum);
        return ajaxResponse;
    }


    @ApiOperation(value = "用工备案职工名册导出", notes = "用工备案职工名册导出")
    @PostMapping(value = "/ygbaZgmcExport")
    public void ygbaZgmcExport(@RequestBody YgbaYgxxQueryDTO dto, HttpServletResponse res) throws IOException {
        ygbaBPO.ygbaZgmcExport(dto, res);
    }

    /**
     * 用工备案职工名册离职
     */
    @ApiOperation(value = "用工备案职工名册离职", notes = "用工备案职工名册离职")
    @PostMapping("/ygbaZgmcQuit")
    public AjaxResponse ygbaZgmcQuit(@RequestBody @Valid YgbaygQuitDTO dto, BindingResult bindingResult) {
        AjaxResponse ajaxResponse = new AjaxResponse();
        if(bindingResult.hasErrors()) {//校验入参
            return this.commBPO.checkRequestParameter(bindingResult);
        }
        try{
            ygbaBPO.ygbaZgmcQuit(dto);
        }catch (AppException e){
            ajaxResponse.setAppcode("1");
            ajaxResponse.setMsg(e.getMessage());
        }
        return ajaxResponse;
    }

    //职工名册查询
    @ApiOperation(value = "用工备案职工名册查询列表", notes = "用工备案职工名册查询列表")
    @PostMapping("/ygbaGetZgmccxList")
    public AjaxResponse ygbaGetZgmccxList(@RequestBody @Valid YgbaZgmcQueryDTO dto, BindingResult bindingResult) {
        AjaxResponse ajaxResponse = new AjaxResponse();

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

    @ApiOperation(value = "用工备案职工名册查询职工名册导出", notes = "用工备案职工名册查询职工名册导出")
    @PostMapping(value = "/ygbaZgmccxExport")
    public void ygbaZgmccxExport(@RequestBody YgbaZgmcQueryDTO dto, HttpServletResponse res) throws IOException {
        ygbaBPO.ygbaZgmccxExport(dto, res);
    }
}
