package com.zjrs.zwnw.sxgl.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.mohrss.leaf.core.framework.persistens.PageResult;
import org.mohrss.leaf.core.framework.web.controller.AjaxResponse;
import com.zjrs.zwnw.sxgl.bpo.SxpzxxBPO;
import com.zjrs.zwnw.sxgl.dto.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import java.util.List;
import java.util.Map;

@Api("事项配置信息")
@RestController
@RequestMapping("/api/sxgl/sxpzxx")
public class SxpzxxController {
    private static final Logger LOG = LoggerFactory.getLogger(SxpzxxController.class);

    @Resource
    private SxpzxxBPO sxpzxxBPO;

    /**
     * 根据条件查询事项列表
     *
     * @param dto 查询条件
     * @return 分页list
     */
    @ApiOperation(value = "根据条件查询事项列表", notes = "根据条件查询事项列表")
    @PostMapping("/querySxList")
    public AjaxResponse querySxList(@RequestBody SxpzxxQueryDTO dto) {
        LOG.info("查询事项列表开始");
        AjaxResponse ajaxResponse = new AjaxResponse();
        Integer pagenum = dto.getPagenum();
        Integer pagesize = dto.getPagesize();
        try {
            PageResult pageResult = sxpzxxBPO.querySxList(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("系统异常");
        }
        LOG.info("查询事项列表结束");
        return ajaxResponse;
    }

    /**
     * 批量更新事项状态(实施状态/发布状态)
     *
     * @param dto
     * @return
     */
    @ApiOperation(value = "批量更新事项状态", notes = "批量更新事项状态")
    @PostMapping("/updateSxztByIds")
    public AjaxResponse updateSxztByIds(@RequestBody SxpzxxQueryDTO dto) {
        LOG.info("批量更新事项状态开始");
        AjaxResponse ajaxResponse = new AjaxResponse();
        try {
            Map<String, String> resultMap = this.sxpzxxBPO.updateSxztByIds(dto);
            ajaxResponse.setAppcode(resultMap.get("code"));
            ajaxResponse.setMsg(resultMap.get("msg"));
        } catch (Exception e) {
            LOG.error(e.getMessage());
            ajaxResponse.setAppcode("1");
            ajaxResponse.setErrorMsg("系统异常");
        }
        LOG.info("批量更新事项状态结束");
        return ajaxResponse;
    }

    @ApiOperation(value = "获取事项详细信息", notes = "获取事项详细信息")
    @PostMapping("/getSxpzxxById")
    public AjaxResponse getSxpzxxById(@RequestBody SxpzxxXsQueryDTO dto) {
        LOG.info("获取事项详细信息开始");
        AjaxResponse ajaxResponse = new AjaxResponse();
        try {
            SxpzxxXsDTO resrul = this.sxpzxxBPO.getSxpzxxById(dto);
            ajaxResponse.setData("data", resrul);
        } catch (Exception e) {
            LOG.error(e.getMessage());
            ajaxResponse.setAppcode("1");
            ajaxResponse.setErrorMsg("系统异常");
        }
        LOG.info("获取事项详细信息结束");
        return ajaxResponse;
    }

    @ApiOperation(value = "获取事项关联材料分页列表", notes = "获取事项关联材料分页列表")
    @PostMapping("/getSxglclList")
    public AjaxResponse getSxglclList(@RequestBody @Valid SxglclQueryDTO dto) {
        LOG.info("获取事项关联材料列表开始");
        AjaxResponse ajaxResponse = new AjaxResponse();
        Integer pagenum = dto.getPagenum();
        Integer pagesize = dto.getPagesize();
        try {
            PageResult pageResult = sxpzxxBPO.getSxglclList(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("系统异常");
        }
        LOG.info("获取事项关联材料列表结束");
        return ajaxResponse;
    }

    @ApiOperation(value = "获取事项未关联材料分页列表", notes = "获取事项未关联材料分页列表")
    @PostMapping("/getSxWglclList")
    public AjaxResponse getSxWglclList(@RequestBody @Valid SxWglclQueryDTO dto) {
        LOG.info("获取事项未关联材料列表开始");
        AjaxResponse ajaxResponse = new AjaxResponse();
        Integer pagenum = dto.getPagenum();
        Integer pagesize = dto.getPagesize();
        try {
            PageResult pageResult = sxpzxxBPO.getSxWglclList(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("系统异常");
        }
        LOG.info("获取事项未关联材料列表结束");
        return ajaxResponse;
    }

    @ApiOperation(value = "批量删除关联材料", notes = "批量删除关联材料")
    @PostMapping("/deleteSxglclByIds")
    public AjaxResponse deleteSxglclByIds(@RequestBody @Valid SxglclDeleteDTO dto) {
        LOG.info("批量删除关联材料开始");
        AjaxResponse ajaxResponse = new AjaxResponse();
        try {
            this.sxpzxxBPO.deleteSxglclByIds(dto);
            ajaxResponse.setMsg("操作成功");
        } catch (Exception e) {
            LOG.error(e.getMessage());
            ajaxResponse.setAppcode("1");
            ajaxResponse.setErrorMsg("系统异常");
        }
        LOG.info("批量删除关联材料结束");
        return ajaxResponse;
    }

    @ApiOperation(value = "更新事项关联材料信息", notes = "更新事项关联材料信息")
    @PostMapping("/updateSxglcl")
    public AjaxResponse updateSxglcl(@RequestBody @Valid SdszzcxxDTO dto) {
        LOG.info("更新事项关联材料信息开始");
        AjaxResponse ajaxResponse = new AjaxResponse();
        try {
            Map<String, String> resultMap = this.sxpzxxBPO.updateSxglcl(dto);
            ajaxResponse.setAppcode(resultMap.get("code"));
            ajaxResponse.setMsg(resultMap.get("msg"));
        } catch (Exception e) {
            LOG.error(e.getMessage());
            ajaxResponse.setAppcode("1");
            ajaxResponse.setErrorMsg("系统异常");
        }
        LOG.info("更新事项关联材料信息结束");
        return ajaxResponse;
    }

    @ApiOperation(value = "获取事项流程环节分页列表", notes = "获取事项流程环节分页列表")
    @PostMapping("/getLchjList")
    public AjaxResponse getLchjList(@RequestBody @Valid SxgllcQueryDTO dto) {
        LOG.info("获取事项流程环节列表开始");
        AjaxResponse ajaxResponse = new AjaxResponse();
        Integer pagenum = dto.getPagenum();
        Integer pagesize = dto.getPagesize();

        try {
            PageResult pageResult = new PageResult();
            pageResult = sxpzxxBPO.getLchjList(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("系统异常");
        }

        LOG.info("获取事项流程环节列表结束");
        return ajaxResponse;
    }

    @ApiOperation(value = "更新事项详细信息", notes = "更新事项详细信息")
    @PostMapping("/updateSxpzxxById")
    public AjaxResponse updateSxpzxxById(@RequestBody @Valid SxpzxxXsUpdateDTO dto) {
        LOG.info("更新事项详细信息开始");
        AjaxResponse ajaxResponse = new AjaxResponse();
        try {
            Map<String, String> resultMap = this.sxpzxxBPO.updateSxpzxxById(dto);
            ajaxResponse.setAppcode(resultMap.get("code"));
            ajaxResponse.setMsg(resultMap.get("msg"));
        } catch (Exception e) {
            LOG.error(e.getMessage());
            ajaxResponse.setAppcode("1");
            ajaxResponse.setErrorMsg("系统异常");
        }
        LOG.info("更新事项详细信息结束");
        return ajaxResponse;
    }

    @ApiOperation(value = "批量关联材料", notes = "批量关联材料")
    @PostMapping("/addSxglclByIds")
    public AjaxResponse addSxglclByIds(@RequestBody @Valid SxglclAddDTO dto) {
        LOG.info("批量关联材料开始");
        AjaxResponse ajaxResponse = new AjaxResponse();
        try {
            Map<String, String> resultMap = this.sxpzxxBPO.addSxglclByIds(dto);
            ajaxResponse.setAppcode(resultMap.get("code"));
            ajaxResponse.setMsg(resultMap.get("msg"));
        } catch (Exception e) {
            LOG.error(e.getMessage());
            ajaxResponse.setAppcode("1");
            ajaxResponse.setErrorMsg("系统异常");
        }
        LOG.info("批量关联材料结束");
        return ajaxResponse;
    }

    @ApiOperation(value = "获取事项关联详细信息", notes = "获取事项关联详细信息")
    @PostMapping("/getSxglcXx")
    public AjaxResponse getSxglcXx(@RequestBody @Valid SxglclXxQueryDTO dto) {
        LOG.info("获取事项关联详细信息开始");
        AjaxResponse ajaxResponse = new AjaxResponse();
        try {
            SxglclListDTO sxglclListDTO = this.sxpzxxBPO.getSxglcXx(dto);
            ajaxResponse.setData("data", sxglclListDTO);
        } catch (Exception e) {
            LOG.error(e.getMessage());
            ajaxResponse.setAppcode("1");
            ajaxResponse.setErrorMsg("系统异常");
        }
        LOG.info("获取事项关联详细信息结束");
        return ajaxResponse;
    }

    @ApiOperation(value = "下一步校验", notes = "下一步校验")
    @PostMapping("/checkNext")
    public AjaxResponse checkNext(@RequestBody @Valid CheckDTO dto) {
        LOG.info("下一步校验开始");
        AjaxResponse ajaxResponse = new AjaxResponse();
        try {
            Map<String, String> resultMap = this.sxpzxxBPO.checkNext(dto);
            ajaxResponse.setAppcode(resultMap.get("code"));
            ajaxResponse.setMsg(resultMap.get("msg"));
        } catch (Exception e) {
            LOG.error(e.getMessage());
            ajaxResponse.setAppcode("1");
            ajaxResponse.setErrorMsg("系统异常");
        }
        LOG.info("下一步校验结束");
        return ajaxResponse;
    }
    @ApiOperation(value = "事项流程配置列表", notes = "事项流程配置列表")
    @PostMapping("/getSxlcpzList")
    public AjaxResponse getSxlcpzList(@RequestBody @Valid SxpzxxLcpzQueryDTO dto) {
        LOG.info("事项流程配置列表开始");
        AjaxResponse ajaxResponse = new AjaxResponse();
        Integer pagenum = dto.getPagenum();
        Integer pagesize = dto.getPagesize();
        try {
            //返回流程环节列表
            ajaxResponse.setData("data", this.sxpzxxBPO.getSxlcpzList(dto, pagenum, pagesize));
            ajaxResponse.setData("pagesize", pagesize);
            ajaxResponse.setData("pagenum", pagenum);

        } catch (Exception e) {
            LOG.error(e.getMessage());
            ajaxResponse.setAppcode("1");
            ajaxResponse.setErrorMsg("系统异常");
        }
        LOG.info("事项流程配置列表结束");
        return ajaxResponse;
    }

    @ApiOperation(value = "新增环节", notes = "新增环节")
    @PostMapping("/addHj")
    public AjaxResponse addHj(@RequestBody @Valid SxpzxxLcpzAddHjQueryDTO dto) {
        LOG.info("新增环节开始");
        AjaxResponse ajaxResponse = new AjaxResponse();
        try {
            Map<String, String> resultMap = this.sxpzxxBPO.addHj(dto);
            ajaxResponse.setAppcode("0");
            ajaxResponse.setData("processdefid", dto.getProcessdefid());
            ajaxResponse.setData("isAddKyls", resultMap.get("isAddKyls"));
        } catch (Exception e) {
            LOG.error(e.getMessage());
            ajaxResponse.setAppcode("1");
            ajaxResponse.setErrorMsg("系统异常");
        }
        LOG.info("新增环节结束");
        return ajaxResponse;
    }
    @ApiOperation(value = "保存环节", notes = "保存环节")
    @PostMapping("/saveHj")
    public AjaxResponse saveHj(@RequestBody @Valid SxpzxxLcpzSaveHjDTO dto) {
        LOG.info("保存环节开始");
        AjaxResponse ajaxResponse = new AjaxResponse();
        try {
            Map<String, String> resultMap = this.sxpzxxBPO.saveHj(dto);
            ajaxResponse.setAppcode(resultMap.get("code"));
            ajaxResponse.setMsg(resultMap.get("msg"));
        } catch (Exception e) {
            LOG.error(e.getMessage());
            ajaxResponse.setAppcode("1");
            ajaxResponse.setErrorMsg("系统异常");
        }
        LOG.info("保存环节结束");
        return ajaxResponse;
    }
    @ApiOperation(value = "删除环节", notes = "删除环节")
    @PostMapping("/delHj")
    public AjaxResponse delHj(@RequestBody @Valid SxpzxxLcpzDelHjDTO dto) {
        LOG.info("删除环节开始");
        AjaxResponse ajaxResponse = new AjaxResponse();
        try {
            this.sxpzxxBPO.delHj(dto);
            ajaxResponse.setMsg("操作成功");
        } catch (Exception e) {
            LOG.error(e.getMessage());
            ajaxResponse.setAppcode("1");
            ajaxResponse.setErrorMsg("系统异常");
        }
        LOG.info("删除环节结束");
        return ajaxResponse;
    }
    @ApiOperation(value = "环节设置列表", notes = "环节设置列表")
    @PostMapping("/getLcpzSetList")
    public AjaxResponse getLcpzSetList(@RequestBody @Valid SxpzxxLcpzSetDTO dto) {
        LOG.info("环节设置列表开始");
        AjaxResponse ajaxResponse = new AjaxResponse();
        try {
            List<SxpzxxLcpzSetListDTO> resultList = this.sxpzxxBPO.getLcpzSetList(dto);
            ajaxResponse.setAppcode("0");
            ajaxResponse.setData("data", resultList);

        } catch (Exception e) {
            LOG.error(e.getMessage());
            e.printStackTrace();
            ajaxResponse.setAppcode("1");
            ajaxResponse.setErrorMsg("系统异常");
        }
        LOG.info("环节设置列表结束");
        return ajaxResponse;
    }
    @ApiOperation(value = "环节设置列表保存", notes = "环节设置列表保存")
    @PostMapping("/saveSetHj")
    public AjaxResponse saveSetHj(@RequestBody @Valid SxpzxxLcpzSetListSaveDTO dto) {
        LOG.info("环节设置列表保存开始");
        AjaxResponse ajaxResponse = new AjaxResponse();
        try {
            Map<String, String> resultMap = this.sxpzxxBPO.saveSetHj(dto);
            ajaxResponse.setAppcode(resultMap.get("code"));
            ajaxResponse.setMsg(resultMap.get("msg"));
        } catch (Exception e) {
            LOG.error(e.getMessage());
            e.printStackTrace();
            ajaxResponse.setAppcode("1");
            ajaxResponse.setErrorMsg("系统异常");
        }
        LOG.info("环节设置列表保存结束");
        return ajaxResponse;
    }
    @ApiOperation(value = "还原流程", notes = "还原流程")
    @PostMapping("/gobackLcpz")
    public AjaxResponse gobackLcpz(@RequestBody @Valid SxpzxxLcpzGobackDTO dto) {
        LOG.info("还原流程开始");
        AjaxResponse ajaxResponse = new AjaxResponse();
        try {
            this.sxpzxxBPO.gobackLcpz(dto.getRsSxConfigLsh());
            ajaxResponse.setMsg("操作成功");
        } catch (Exception e) {
            LOG.error(e.getMessage());
            ajaxResponse.setAppcode("1");
            ajaxResponse.setErrorMsg("系统异常");
        }
        LOG.info("还原流程结束");
        return ajaxResponse;
    }
    @ApiOperation(value = "确认流程", notes = "确认流程")
    @PostMapping("/confirmLcpz")
    public AjaxResponse confirmLcpz(@RequestBody @Valid SxpzxxLcpzConfirmDTO dto) {
        LOG.info("确认流程开始");
        AjaxResponse ajaxResponse = new AjaxResponse();
        try {
            Map<String, String> resultMap = this.sxpzxxBPO.confirmLcpz(dto);
            ajaxResponse.setAppcode(resultMap.get("code"));
            ajaxResponse.setMsg(resultMap.get("msg"));
        } catch (Exception e) {
            LOG.error(e.getMessage());
            e.printStackTrace();
            ajaxResponse.setAppcode("1");
            ajaxResponse.setErrorMsg("系统异常");
        }
        LOG.info("确认流程结束");
        return ajaxResponse;
    }

    @ApiOperation(value = "获取动态字典环节名称", notes = "获取动态字典环节名称")
    @GetMapping("/getHjmcDict")
    public AjaxResponse getHjmcDict() {
        LOG.info("获取动态字典环节名称开始");
        AjaxResponse ajaxResponse = new AjaxResponse();
        try {
            List<HjmcDictDTO> result = this.sxpzxxBPO.getHjmcDict();
            ajaxResponse.setData("data", result);
        } catch (Exception e) {
            LOG.error(e.getMessage());
            ajaxResponse.setAppcode("1");
            ajaxResponse.setErrorMsg("系统异常");
        }
        LOG.info("获取动态字典环节名称结束");
        return ajaxResponse;
    }

    @ApiOperation(value = "获取流程图信息", notes = "获取流程图信息")
    @PostMapping("/getLcpzLct")
    public AjaxResponse getLcpzLct(@RequestBody @Valid SxpzxxLcpzGetLctDTO dto) {
        LOG.info("获取流程图信息开始");
        AjaxResponse ajaxResponse = new AjaxResponse();
        try {
            Map<String,List> resultMap = this.sxpzxxBPO.getLcpzLct(dto);
            ajaxResponse.setAppcode("0");
            ajaxResponse.setData("data", resultMap);
        } catch (Exception e) {
            LOG.error(e.getMessage());
            e.printStackTrace();
            ajaxResponse.setAppcode("1");
            ajaxResponse.setErrorMsg("系统异常");
        }
        LOG.info("获取流程图信息结束");
        return ajaxResponse;
    }

    @ApiOperation(value = "获取事项配置附件样表列表", notes = "获取事项配置附件样表列表")
    @PostMapping("/getFjybList")
    public AjaxResponse getFjybList(@RequestBody @Valid SxpzxxFjybQueryDTO dto, BindingResult bindingResult) {
        LOG.info("获取事项配置附件样表列表开始");
        AjaxResponse ajaxResponse = new AjaxResponse();
        Integer pagenum = dto.getPagenum();
        Integer pagesize = dto.getPagesize();

        //校验入参
        if(bindingResult.hasErrors()) {
            ajaxResponse.setAppcode("1");
            ajaxResponse.setErrorMsg(bindingResult.getFieldError().getDefaultMessage());
            return ajaxResponse;
        }

        try {
            PageResult pageResult = sxpzxxBPO.getFjybList(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("系统异常");
        }
        LOG.info("获取事项配置附件样表列表结束");
        return ajaxResponse;
    }

    @ApiOperation(value = "保存事项配置附件样表", notes = "保存事项配置附件样表")
    @PostMapping("/saveFjyb")
    public AjaxResponse saveFjyb(@RequestBody @Valid SxpzxxFjybSaveDTO dto, BindingResult bindingResult) {
        LOG.info("保存事项配置附件样表开始");
        AjaxResponse ajaxResponse = new AjaxResponse();        //校验入参
        if(bindingResult.hasErrors()) {
            ajaxResponse.setAppcode("1");
            ajaxResponse.setErrorMsg(bindingResult.getFieldError().getDefaultMessage());
            return ajaxResponse;
        }

        try {
            Map<String, String> resultMap = this.sxpzxxBPO.saveFjyb(dto);
            ajaxResponse.setAppcode(resultMap.get("code"));
            ajaxResponse.setMsg(resultMap.get("msg"));
        } catch (Exception e) {
            LOG.error(e.getMessage());
            ajaxResponse.setAppcode("1");
            ajaxResponse.setErrorMsg("系统异常");
        }
        LOG.info("保存事项配置附件样表结束");
        return ajaxResponse;
    }
    @ApiOperation(value = "删除事项配置附件样表", notes = "删除事项配置附件样表")
    @PostMapping("/delFjybByIds")
    public AjaxResponse delFjybByIds(@RequestBody @Valid SxpzxxFjybDelDTO dto, BindingResult bindingResult) {
        LOG.info("删除事项配置附件样表开始");
        AjaxResponse ajaxResponse = new AjaxResponse();        //校验入参
        if(bindingResult.hasErrors()) {
            ajaxResponse.setAppcode("1");
            ajaxResponse.setErrorMsg(bindingResult.getFieldError().getDefaultMessage());
            return ajaxResponse;
        }

        try {
            this.sxpzxxBPO.delFjybByIds(dto);
            ajaxResponse.setMsg("操作成功");
        } catch (Exception e) {
            LOG.error(e.getMessage());
            ajaxResponse.setAppcode("1");
            ajaxResponse.setErrorMsg("系统异常");
        }
        LOG.info("删除事项配置附件样表结束");
        return ajaxResponse;
    }

    @ApiOperation(value = "批量修改领导信息", notes = "批量修改领导信息")
    @PostMapping("/saveBatchLdxx")
    public AjaxResponse saveBatchLdxx(@RequestBody @Valid SxpzxxBatchUpdateLdxxDTO dto, BindingResult bindingResult) {
        LOG.info("批量修改领导信息开始");
        AjaxResponse ajaxResponse = new AjaxResponse();        //校验入参
        if(bindingResult.hasErrors()) {
            ajaxResponse.setAppcode("1");
            ajaxResponse.setErrorMsg(bindingResult.getFieldError().getDefaultMessage());
            return ajaxResponse;
        }
        if(StringUtils.isBlank(dto.getFgld()) && StringUtils.isBlank(dto.getKz())){
            ajaxResponse.setAppcode("1");
            ajaxResponse.setMsg("请至少修改其中一项内容。");
            return ajaxResponse;
        }

        this.sxpzxxBPO.saveBatchLdxx(dto);
        ajaxResponse.setMsg("操作成功！");
        LOG.info("批量修改领导信息结束");
        return ajaxResponse;
    }

    @ApiOperation(value = "获取动态字典适用事项", notes = "获取动态字典适用事项")
    @PostMapping("/getSysx")
    public AjaxResponse getSysx(@RequestBody SxglclXxQueryDTO dto) {
        LOG.info("获取动态字典适用事项开始");
        AjaxResponse ajaxResponse = new AjaxResponse();
        List<HjmcDictDTO> result = this.sxpzxxBPO.getSysx(dto.getSxbm());
        ajaxResponse.setData("data", result);
        LOG.info("获取动态字典适用事项结束");
        return ajaxResponse;
    }
}
