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.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;

@Api("督办规则管理")
@RestController
@RequestMapping("/api/dbgl/rule")
public class DbglRuleController {

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

    @Resource
    DbgzBPO dbgzBPO;

    /**
     * 网上办事督办事项选择列表
     *
     * @param dto 查询条件
     * @return 分页list
     */
    @ApiOperation(value = "网上办事督办事项选择列表", notes = "网上办事督办事项选择列表")
    @PostMapping("/select/list")
    public AjaxResponse queryDbglRuleSelectList(@RequestBody DbglPageQueryDTO dto) {
        logger.info("网上办事督办事项选择列表");
        AjaxResponse ajaxResponse = new AjaxResponse();
        Integer pagenum = dto.getPagenum();
        Integer pagesize = dto.getPagesize();

        try {
            PageResult pageResult = dbgzBPO.queryDbglRuleSelectList(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 queryDbglRuleList(@RequestBody DbglPageQueryDTO dto) {
        logger.info("网上办事督办规则列表");
        AjaxResponse ajaxResponse = new AjaxResponse();
        Integer pagenum = dto.getPagenum();
        Integer pagesize = dto.getPagesize();

        try {
            PageResult pageResult = dbgzBPO.queryDbglRuleList(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("/query")
    public AjaxResponse queryDbglRule(@RequestBody DbglRuleQueryDTO dto) {
        logger.info("网上办事督办规则列表");
        AjaxResponse ajaxResponse = new AjaxResponse();

        try {
            DbglDTO result = dbgzBPO.queryDbglRule(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 dbglRuleSave(@RequestBody DbglRuleSaveDTO dto, BindingResult bindingResult) {
        logger.info("网上办事督办规则信息保存");
        AjaxResponse ajaxResponse = new AjaxResponse();
        if(bindingResult.hasErrors())
            return AppUtils.getValidAjaxResponse(ajaxResponse, bindingResult);

        try {
            DbglDTO dbglDTO = dbgzBPO.dbglRuleSave(dto);
            ajaxResponse.setData("data", dbglDTO);
            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("/delete")
    public AjaxResponse dbglRuleDelete(@RequestBody DbglRuleDeleteDTO dto, BindingResult bindingResult) {

        logger.info("网上办事督办规则信息删除");
        AjaxResponse ajaxResponse = new AjaxResponse();
        if(bindingResult.hasErrors())
            return AppUtils.getValidAjaxResponse(ajaxResponse, bindingResult);

        try {
            dbgzBPO.dbglRuleDelete(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;
    }

}
