package cn.estsh.i3plus.ext.mes.apiservice.controller.busi;

import cn.estsh.i3plus.ext.mes.api.busi.ISxBadStatisticsReportFormService;
import cn.estsh.i3plus.ext.mes.pojo.model.BadStatisticsReportParamModel;
import cn.estsh.i3plus.ext.mes.pojo.model.SxRepairRecordReportModel;
import cn.estsh.i3plus.ext.mes.pojo.util.MesExtConstWords;
import cn.estsh.i3plus.platform.common.util.CommonConstWords;
import cn.estsh.i3plus.pojo.base.common.Pager;
import cn.estsh.i3plus.pojo.base.enumutil.ResourceEnumUtil;
import cn.estsh.impp.framework.base.controller.MesBaseController;
import cn.estsh.impp.framework.boot.auth.AuthUtil;
import cn.estsh.impp.framework.boot.exception.ImppBusiException;
import cn.estsh.impp.framework.boot.exception.ImppExceptionBuilder;
import cn.estsh.impp.framework.boot.util.ResultBean;
import cn.estsh.impp.framework.boot.util.ValidatorBean;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author: wangjie
 * @CreateDate: 2021/01/19 15:37
 * @Description:
 **/
@RestController
@RequestMapping(CommonConstWords.BASE_URL_MES + "/sx/report-form")
@Api(tags = "不良统计报表")
public class SxBadStatisticsReportFormController extends MesBaseController {

    @Autowired
    private ISxBadStatisticsReportFormService badStatisticsReportFormService;

    @GetMapping(value = "/project-bad-performance/query")
    @ApiOperation(value = "工程不良实绩报表")
    public ResultBean queryProjectBadPerformance(BadStatisticsReportParamModel model) {
        try {
            ValidatorBean.beginValid(model)
                    .notNull(MesExtConstWords.DIMENSION, model.getDimension());
            model.setOrganizeCode(AuthUtil.getOrganize().getOrganizeCode());
            return ResultBean.success("查询成功").setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode())
                    .setResultList(badStatisticsReportFormService.queryProjectBadPerformance(model));
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @GetMapping(value = "/bad-statistics/query")
    @ApiOperation(value = "不良统计报表")
    public ResultBean queryBadStatistics(BadStatisticsReportParamModel model) {
        try {
            ValidatorBean.beginValid(model)
                    .notNull(MesExtConstWords.DIMENSION, model.getDimension())
                    .notNull(MesExtConstWords.DIMENSION_1, model.getDimension1());
            model.setOrganizeCode(AuthUtil.getOrganize().getOrganizeCode());
            return ResultBean.success("查询成功").setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode())
                    .setResultList(badStatisticsReportFormService.queryBadStatistics(model));
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @GetMapping(value = "/repair-record-detail/query")
    @ApiOperation(value = "修理品票明细报表")
    public ResultBean queryRepairDetailReport(SxRepairRecordReportModel model, Pager pager) {
        try {
            model.setOrganizeCode(AuthUtil.getOrganize().getOrganizeCode());
            return ResultBean.success("查询成功").setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode())
                    .setListPager(badStatisticsReportFormService.queryRepairDetailReport(model, pager));
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    /**
     * 根据ID修改审批状态
     *
     * @param id
     * @param status
     * @return
     */
    @PutMapping(value = "/approval-status-update")
    @ApiOperation(value = "根据ID修改审批状态")
    public ResultBean updateApprovalStatusById(long id, Integer status) {
        try {
            badStatisticsReportFormService.updateApprovalStatusById(id, status);
            return ResultBean.success("操作成功")
                    .setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode());
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

}
