package com.zjrs.zwnw.bsdb.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.bsdb.bpo.DbgzBPO;
import com.zjrs.zwnw.bsdb.bpo.DbsxBPO;
import com.zjrs.zwnw.bsdb.dto.*;
import com.zjrs.zwnw.bsdb.util.AppUtils;
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.validation.Valid;
import java.util.Map;

@Api("督办管理")
@RestController
@RequestMapping("/api/dbgl")
public class DbglController {

    private static final Logger logger = LoggerFactory.getLogger(DbglController.class);

    @Resource
    DbsxBPO dbsxBPO;

    @Resource
    DbgzBPO dbgzBPO;

    /**
     * 网上办事督办待办列表
     *
     * @param dto 查询条件
     * @return 分页list
     */
    @ApiOperation(value = "网上办事督办待办列表", notes = "网上办事督办待办列表")
    @PostMapping("/workspace/db/list")
    public AjaxResponse queryDbglWorkspaceDbList(@RequestBody DbglPageQueryDTO dto) {
        logger.info("网上办事督办待办列表");
        AjaxResponse ajaxResponse = new AjaxResponse();
        Integer pagenum = dto.getPagenum();
        Integer pagesize = dto.getPagesize();
        try {
            PageResult pageResult = dbsxBPO.queryDbglWorkspaceDbList(dto);
            ajaxResponse.setData("data", pageResult);
            ajaxResponse.setData("pagesize", pagesize);
            ajaxResponse.setData("pagenum", pagenum);
        } catch (Exception e) {
            logger.error(e.getMessage());
            ajaxResponse.setAppcode("1");
            ajaxResponse.setErrorMsg("系统异常");
        }
        return ajaxResponse;
    }

    /**
     * 网上办事督办已办列表
     *
     * @param dto 查询条件
     * @return 分页list
     */
    @ApiOperation(value = "网上办事督办已办列表", notes = "网上办事督办已办列表")
    @PostMapping("/workspace/yb/list")
    public AjaxResponse queryDbglWorkspaceYbList(@RequestBody DbglPageQueryDTO dto) {
        logger.info("网上办事督办已办列表");
        AjaxResponse ajaxResponse = new AjaxResponse();
        Integer pagenum = dto.getPagenum();
        Integer pagesize = dto.getPagesize();
        try {
            PageResult pageResult = dbsxBPO.queryDbglWorkspaceYbList(dto);
            ajaxResponse.setData("data", pageResult);
            ajaxResponse.setData("pagesize", pagesize);
            ajaxResponse.setData("pagenum", pagenum);
        } catch (Exception e) {
            logger.error(e.getMessage());
            ajaxResponse.setAppcode("1");
            ajaxResponse.setErrorMsg("系统异常");
        }
        return ajaxResponse;
    }

    /**
     * 网上办事督办超期列表
     *
     * @param dto 查询条件
     * @return 分页list
     */
    @ApiOperation(value = "网上办事督办超期列表", notes = "网上办事督办超期列表")
    @PostMapping("/timeout/list")
    public AjaxResponse queryDbglTimeoutList(@RequestBody VRsSlxxRemaindatePageQueryDTO dto) {
        logger.info("网上办事督办超期列表");
        AjaxResponse ajaxResponse = new AjaxResponse();
        Integer pagenum = dto.getPagenum();
        Integer pagesize = dto.getPagesize();
        try {
            PageResult pageResult = dbsxBPO.queryDbglTimeoutList(dto);
            ajaxResponse.setData("data", pageResult);
            ajaxResponse.setData("pagesize", pagesize);
            ajaxResponse.setData("pagenum", pagenum);
        } catch (Exception e) {
            logger.error(e.getMessage());
            ajaxResponse.setAppcode("1");
            ajaxResponse.setErrorMsg("系统异常");
        }
        return ajaxResponse;
    }
    
    /**
     * 网上办事督办待办列表
     *
     * @param dto 查询条件
     * @return 分页list
     */
    @ApiOperation(value = "网上办事督办待办列表", notes = "网上办事督办待办列表")
    @PostMapping("/db/list")
    public AjaxResponse queryDbglDbList(@RequestBody DbglPageQueryDTO dto) {
        logger.info("网上办事督办待办列表");
        AjaxResponse ajaxResponse = new AjaxResponse();
        Integer pagenum = dto.getPagenum();
        Integer pagesize = dto.getPagesize();
        try {
            PageResult pageResult = dbsxBPO.queryDbglDbList(dto);
            ajaxResponse.setData("data", pageResult);
            ajaxResponse.setData("pagesize", pagesize);
            ajaxResponse.setData("pagenum", pagenum);
        } catch (Exception e) {
            logger.error(e.getMessage());
            ajaxResponse.setAppcode("1");
            ajaxResponse.setErrorMsg("系统异常");
        }
        return ajaxResponse;
    }

    /**
     * 网上办事督办已办列表
     *
     * @param dto 查询条件
     * @return 分页list
     */
    @ApiOperation(value = "网上办事督办已办列表", notes = "网上办事督办已办列表")
    @PostMapping("/yb/list")
    public AjaxResponse queryDbglYbList(@RequestBody DbglPageQueryDTO dto) {
        logger.info("网上办事督办已办列表");
        AjaxResponse ajaxResponse = new AjaxResponse();
        Integer pagenum = dto.getPagenum();
        Integer pagesize = dto.getPagesize();
        try {
            PageResult pageResult = dbsxBPO.queryDbglYbList(dto);
            ajaxResponse.setData("data", pageResult);
            ajaxResponse.setData("pagesize", pagesize);
            ajaxResponse.setData("pagenum", pagenum);
        } catch (Exception e) {
            logger.error(e.getMessage());
            ajaxResponse.setAppcode("1");
            ajaxResponse.setErrorMsg("系统异常");
        }
        return ajaxResponse;
    }

    /**
     * 网上办事督办列表
     *
     * @param dto 查询条件
     * @return 分页list
     */
    @ApiOperation(value = "网上办事督办列表", notes = "网上办事督办列表")
    @PostMapping("/list")
    public AjaxResponse queryDbgList(@RequestBody DbglPageQueryDTO dto) {
        logger.info("网上办事督办已办列表");
        AjaxResponse ajaxResponse = new AjaxResponse();
        Integer pagenum = dto.getPagenum();
        Integer pagesize = dto.getPagesize();
        try {
            PageResult pageResult = dbsxBPO.queryDbglList(dto);
            ajaxResponse.setData("data", pageResult);
            ajaxResponse.setData("pagesize", pagesize);
            ajaxResponse.setData("pagenum", pagenum);
        } catch (Exception e) {
            logger.error(e.getMessage());
            ajaxResponse.setAppcode("1");
            ajaxResponse.setErrorMsg("系统异常");
        }
        return ajaxResponse;
    }

    /**
     * 网上办事督办事项信息查询
     *
     * @param dto 查询条件
     * @return
     */
    @ApiOperation(value = "网上办事督办事项信息", notes = "网上办事督办事项信息")
    @PostMapping("/query")
    public AjaxResponse dbglQuery(@RequestBody @Valid DbglQueryDTO dto, BindingResult bindingResult) {
        logger.info("网上办事督办事项信息");
        AjaxResponse ajaxResponse = new AjaxResponse();

        if(bindingResult.hasErrors())
            return AppUtils.getValidAjaxResponse(ajaxResponse, bindingResult);

        try {
            RsBsdbDbsxDTO result =  dbsxBPO.dbglQuery(dto);
            ajaxResponse.setData("data", result);
        } catch (Exception e) {
            logger.error(e.getMessage());
            ajaxResponse.setAppcode("1");
            ajaxResponse.setErrorMsg("系统异常");
        }
        return ajaxResponse;
    }

    /**
     * 网上办事督办事项信息保存
     *
     * @param dto 保存信息
     * @return
     */
    @ApiOperation(value = "网上办事督办事项信息保存", notes = "网上办事督办事项信息保存")
    @PostMapping("/save")
    public AjaxResponse dbglSave(@RequestBody @Valid RsBsdbDbsxSaveDTO dto, BindingResult bindingResult) {
        logger.info("网上办事督办事项信息保存");
        AjaxResponse ajaxResponse = new AjaxResponse();

        if(bindingResult.hasErrors())
            return AppUtils.getValidAjaxResponse(ajaxResponse, bindingResult);

        try {
            RsBsdbDbsxDTO result =  dbsxBPO.dbglSave(dto);
            ajaxResponse.setData("data", result);
            ajaxResponse.setMsg("网上办事督办事项信息, 保存成功");
        } catch (Exception e) {
            logger.error(e.getMessage());
            ajaxResponse.setAppcode("1");
            ajaxResponse.setErrorMsg("系统异常");
        }
        return ajaxResponse;
    }

    /**
     * 网上办事督办事项信息删除
     *
     * @param dto 删除条件
     * @return
     */
    @ApiOperation(value = "网上办事督办事项信息删除", notes = "网上办事督办事项信息删除")
    @PostMapping("/delete")
    public AjaxResponse dbglDelete(@RequestBody @Valid DbglDeleteDTO dto, BindingResult bindingResult) {
        logger.info("网上办事督办事项信息删除");
        AjaxResponse ajaxResponse = new AjaxResponse();

        if(bindingResult.hasErrors())
            return AppUtils.getValidAjaxResponse(ajaxResponse, bindingResult);

        try {
            dbsxBPO.dbglDelete(dto);
            ajaxResponse.setData("data", null);
            ajaxResponse.setMsg("网上办事督办事项信息, 删除成功");
        } catch (Exception e) {
            logger.error(e.getMessage());
            ajaxResponse.setAppcode("1");
            ajaxResponse.setErrorMsg("系统异常");
        }
        return ajaxResponse;
    }

    /**
     * 网上办事督办事项信息发布下架
     *
     * @param dto 删除条件
     * @return
     */
    @ApiOperation(value = "网上办事督办事项信息发布下架", notes = "网上办事督办事项信息发布下架")
    @PostMapping("/publish")
    public AjaxResponse dbglPublish(@RequestBody @Valid DbglPublishDTO dto, BindingResult bindingResult) {
        logger.info("网上办事督办事项信息发布下架");
        AjaxResponse ajaxResponse = new AjaxResponse();

        if(bindingResult.hasErrors())
            return AppUtils.getValidAjaxResponse(ajaxResponse, bindingResult);

        try {
            dbsxBPO.dbglPublish(dto);
            ajaxResponse.setData("data", null);
            ajaxResponse.setMsg("网上办事督办事项信息, " + ("1".equals(dto.getRsbsdbfbzt()) ? "发布成功" : "取消发布成功"));
        }
        catch (AppException e) {
            logger.error(e.getMessage());
            ajaxResponse.setAppcode("1");
            ajaxResponse.setErrorMsg(e.getMessage());
        }
        catch (Exception e) {
            logger.error(e.getMessage());
            ajaxResponse.setAppcode("1");
            ajaxResponse.setErrorMsg("系统异常");
        }
        return ajaxResponse;
    }

    /**
     * 网上办事督办事项信息发布下架
     *
     * @param dto 删除条件
     * @return
     */
    @ApiOperation(value = "网上办事督办事项信息派发", notes = "网上办事督办事项信息派发")
    @PostMapping("/assign")
    public AjaxResponse dbglAssign(@RequestBody @Valid DbglAssignDTO dto, BindingResult bindingResult) {
        logger.info("网上办事督办事项信息派发");
        AjaxResponse ajaxResponse = new AjaxResponse();

        if(bindingResult.hasErrors())
            return AppUtils.getValidAjaxResponse(ajaxResponse, bindingResult);

        try {
            dbsxBPO.dbglAssign(dto);
            ajaxResponse.setData("data", null);
            ajaxResponse.setMsg("网上办事督办事项信息, 派发成功");
        }
        catch (AppException e) {
            logger.error(e.getMessage());
            ajaxResponse.setAppcode("1");
            ajaxResponse.setErrorMsg(e.getMessage());
        }
        catch (Exception e) {
            logger.error(e.getMessage());
            ajaxResponse.setAppcode("1");
            ajaxResponse.setErrorMsg("系统异常");
        }
        return ajaxResponse;
    }

    /**
     * 网上办事督办事项信息发布下架
     *
     * @param dto 删除条件
     * @return
     */
    @ApiOperation(value = "网上办事督办事项信息催办", notes = "网上办事督办事项信息催办")
    @PostMapping("/urge")
    public AjaxResponse dbglUrge(@RequestBody @Valid DbglQueryDTO dto, BindingResult bindingResult) {
        logger.info("网上办事督办事项信息催办");
        AjaxResponse ajaxResponse = new AjaxResponse();

        if(bindingResult.hasErrors())
            return AppUtils.getValidAjaxResponse(ajaxResponse, bindingResult);

        try {
            dbsxBPO.dbglUrge(dto);
            ajaxResponse.setData("data", null);
            ajaxResponse.setMsg("网上办事督办事项信息, 催办成功");
        }
        catch (AppException e) {
            logger.error(e.getMessage());
            ajaxResponse.setAppcode("1");
            ajaxResponse.setErrorMsg(e.getMessage());
        }
        catch (Exception e) {
            e.printStackTrace();
            logger.error(e.getMessage());
            ajaxResponse.setAppcode("1");
            ajaxResponse.setErrorMsg("系统异常");
        }
        return ajaxResponse;
    }

    /**
     * 网上办事督办事项信息反馈
     *
     * @param dto 删除条件
     * @return
     */
    @ApiOperation(value = "网上办事督办事项信息反馈", notes = "网上办事督办事项信息反馈")
    @PostMapping("/feedback")
    public AjaxResponse dbglFeedback(@RequestBody @Valid DbglFeedbackSaveDTO dto, BindingResult bindingResult) {
        logger.info("网上办事督办事项信息评价");
        AjaxResponse ajaxResponse = new AjaxResponse();

        if(bindingResult.hasErrors())
            return AppUtils.getValidAjaxResponse(ajaxResponse, bindingResult);

        try {
            dbsxBPO.dbglFeedback(dto);
            ajaxResponse.setMsg("网上办事督办事项信息, 反馈成功");
        } catch(AppException e) {
            logger.error(e.getMessage());
            ajaxResponse.setAppcode("1");
            ajaxResponse.setErrorMsg(e.getMessage());
        }
        catch (Exception e) {
            logger.error(e.getMessage());
            ajaxResponse.setAppcode("1");
            ajaxResponse.setErrorMsg("系统异常");
        }
        return ajaxResponse;
    }

    /**
     * 网上办事督办事项信息反馈提交
     *
     * @param dto 删除条件
     * @return
     */
    @ApiOperation(value = "网上办事督办事项信息反馈提交", notes = "网上办事督办事项信息反馈提交")
    @PostMapping("/feedback/submit")
    public AjaxResponse dbglFeedbackSubmit(@RequestBody @Valid DbglFeedbackSaveDTO dto, BindingResult bindingResult) {
        logger.info("网上办事督办事项信息评价");
        AjaxResponse ajaxResponse = new AjaxResponse();

        if(bindingResult.hasErrors())
            return AppUtils.getValidAjaxResponse(ajaxResponse, bindingResult);

        try {
            dbsxBPO.dbglFeedbackSubmit(dto);
            ajaxResponse.setMsg("网上办事督办事项信息, 提交成功");
        } catch(AppException e) {
            logger.error(e.getMessage());
            ajaxResponse.setAppcode("1");
            ajaxResponse.setErrorMsg(e.getMessage());
        }
        catch (Exception e) {
            logger.error(e.getMessage());
            ajaxResponse.setAppcode("1");
            ajaxResponse.setErrorMsg("系统异常");
        }
        return ajaxResponse;
    }

    /**
     * 网上办事督办事项信息发布下架
     *
     * @param dto 删除条件
     * @return
     */
    @ApiOperation(value = "网上办事督办事项信息评价", notes = "网上办事督办事项信息评价")
    @PostMapping("/appraise")
    public AjaxResponse dbglAppraise(@RequestBody @Valid DbglAppraiseSaveDTO dto, BindingResult bindingResult) {
        logger.info("网上办事督办事项信息评价");
        AjaxResponse ajaxResponse = new AjaxResponse();

        if(bindingResult.hasErrors())
            return AppUtils.getValidAjaxResponse(ajaxResponse, bindingResult);

        try {
            RsBsdbDbsxDTO result =  dbsxBPO.dbglAppraise(dto);
            ajaxResponse.setData("data", result);
            ajaxResponse.setMsg("网上办事督办事项信息, 评价成功");
        } catch(AppException e) {
            logger.error(e.getMessage());
            ajaxResponse.setAppcode("1");
            ajaxResponse.setErrorMsg(e.getMessage());
        }
        catch (Exception e) {
            logger.error(e.getMessage());
            ajaxResponse.setAppcode("1");
            ajaxResponse.setErrorMsg("系统异常");
        }
        return ajaxResponse;
    }


    /**
     * 网上办事督办事项信息环节获取
     *
     * @param dto 删除条件
     * @return
     */
    @ApiOperation(value = "网上办事督办事项信息环节获取", notes = "网上办事督办事项信息环节获取")
    @PostMapping("/process/query")
    public AjaxResponse queryProcess(@RequestBody @Valid DbglQueryDTO dto, BindingResult bindingResult) {
        logger.info("网上办事督办事项信息环节获取");
        AjaxResponse ajaxResponse = new AjaxResponse();

        if(bindingResult.hasErrors())
            return AppUtils.getValidAjaxResponse(ajaxResponse, bindingResult);

        try {
            Map<String, String> result =  dbsxBPO.queryProcess(dto);
            ajaxResponse.setData("data", result);
        } catch (Exception e) {
            logger.error(e.getMessage());
            ajaxResponse.setAppcode("1");
            ajaxResponse.setErrorMsg("系统异常");
        }
        return ajaxResponse;
    }


    /**
     * 网上办事督办任务跟踪
     *
     * @param dto 查询条件
     * @return 分页list
     */
    @ApiOperation(value = "网上办事督办任务跟踪", notes = "网上办事督办任务跟踪")
    @PostMapping("/track/list")
    public AjaxResponse queryDbglTrackList(@RequestBody DbglPageQueryDTO dto) {
        logger.info("网上办事督办任务跟踪");
        AjaxResponse ajaxResponse = new AjaxResponse();
        Integer pagenum = dto.getPagenum();
        Integer pagesize = dto.getPagesize();

        try {
            PageResult pageResult = dbgzBPO.queryDbglTrackList(dto);
            ajaxResponse.setData("data", pageResult);
            ajaxResponse.setData("pagesize", pagesize);
            ajaxResponse.setData("pagenum", pagenum);
        } catch (Exception e) {
            logger.error(e.getMessage());
            ajaxResponse.setAppcode("1");
            ajaxResponse.setErrorMsg("系统异常");
        }
        return ajaxResponse;
    }

    /**
     * 网上办事督办任务反馈
     *
     * @param dto 查询条件
     * @return 分页list
     */
    @ApiOperation(value = "网上办事督办任务反馈", notes = "网上办事督办任务反馈")
    @PostMapping("/feedback/list")
    public AjaxResponse queryDbglFeedbackList(@RequestBody DbglPageQueryDTO dto) {
        logger.info("网上办事督办任务反馈");
        AjaxResponse ajaxResponse = new AjaxResponse();
        Integer pagenum = dto.getPagenum();
        Integer pagesize = dto.getPagesize();

        try {
            PageResult pageResult = dbgzBPO.queryDbglFeedbackList(dto);
            ajaxResponse.setData("data", pageResult);
            ajaxResponse.setData("pagesize", pagesize);
            ajaxResponse.setData("pagenum", pagenum);
        } catch (Exception e) {
            logger.error(e.getMessage());
            ajaxResponse.setAppcode("1");
            ajaxResponse.setErrorMsg("系统异常");
        }
        return ajaxResponse;
    }

    /**
     * 首页-督办催办提醒
     *
     * @param dto 查询条件
     * @return 分页list
     */
    @ApiOperation(value = "首页-督办催办提醒", notes = "首页-督办催办提醒")
    @PostMapping("/task/list")
    public AjaxResponse queryDbglTaskList(@RequestBody DbglPageQueryDTO dto) {
        logger.info("首页-督办催办提醒");
        AjaxResponse ajaxResponse = new AjaxResponse();
        Integer pagenum = dto.getPagenum();
        Integer pagesize = dto.getPagesize();

        try {
            PageResult pageResult = dbgzBPO.queryDbglTaskList(dto);
            ajaxResponse.setData("data", pageResult);
            ajaxResponse.setData("pagesize", pagesize);
            ajaxResponse.setData("pagenum", pagenum);
        } catch (Exception e) {
            logger.error(e.getMessage());
            ajaxResponse.setAppcode("1");
            ajaxResponse.setErrorMsg("系统异常");
        }
        return ajaxResponse;
    }

}
