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

import cn.estsh.i3plus.ext.mes.api.busi.bu.IBuSxReportFormService;
import cn.estsh.i3plus.ext.mes.pojo.bean.SxMesDataTravel;
import cn.estsh.i3plus.ext.mes.pojo.model.CompleteRateChartModel;
import cn.estsh.i3plus.ext.mes.pojo.model.ProjectContainersComputeChartParamModel;
import cn.estsh.i3plus.ext.mes.pojo.model.UnWorkTimeStatisticsModel;
import cn.estsh.i3plus.ext.mes.pojo.model.bu.BuBusiErrorMessageModel;
import cn.estsh.i3plus.ext.mes.pojo.model.bu.MesOuterPackageRetrospectModel;
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/bu-ext/report-form")
@Api(tags = "报表")
public class BuSxReportFormController extends MesBaseController {

    @Autowired
    private IBuSxReportFormService reportFormService;

    @GetMapping(value = "/attainment-rate-chart/query")
    @ApiOperation(value = "达成率一览表")
    public ResultBean queryAttainmentRateChart(CompleteRateChartModel model) {
        try {
            ValidatorBean.beginValid(model)
                    .notNull(MesExtConstWords.DIMENSION, model.getDimension());
            model.setOrganizeCode(AuthUtil.getOrganize().getOrganizeCode());
            return ResultBean.success("查询成功").setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode())
                    .setResultList(reportFormService.queryAttainmentRateChart(model));
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @GetMapping(value = "/complete-rate-chart/query")
    @ApiOperation(value = "完结率一览表")
    public ResultBean queryCompleteRateChart(CompleteRateChartModel model) {
        try {
            ValidatorBean.beginValid(model)
                    .notNull(MesExtConstWords.DIMENSION, model.getDimension());
            model.setOrganizeCode(AuthUtil.getOrganize().getOrganizeCode());
            return ResultBean.success("查询成功").setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode())
                    .setResultList(reportFormService.queryCompleteRateChart(model));
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @GetMapping(value = "/project-bad-rate/query")
    @ApiOperation(value = "工程不良率")
    public ResultBean queryProjectBadRate(ProjectContainersComputeChartParamModel paramModel, Pager pager) {
        try {
            paramModel.setOrganizeCode(AuthUtil.getOrganize().getOrganizeCode());
            return ResultBean.success("查询成功").setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode())
                    .setListPager(reportFormService.queryProjectBadRate(paramModel, pager));
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @GetMapping(value = "/project-containers-compute/query")
    @ApiOperation(value = "工程集计表")
    public ResultBean queryProjectContainersComputeChart(ProjectContainersComputeChartParamModel model, Pager pager) {
        try {
            return ResultBean.success("查询成功").setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode())
                    .setListPager(reportFormService.queryProjectContainersComputeChart(model,pager));
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @GetMapping(value = "/work-time-monthly-performance/query")
    @ApiOperation(value = "作业时间月绩")
    public ResultBean queryWorkTimeMonthlyPerformance(UnWorkTimeStatisticsModel model) {
        try {
            ValidatorBean.beginValid(model)
                    .notNull(MesExtConstWords.DIMENSION, model.getDimension());
            model.setOrganizeCode(AuthUtil.getOrganize().getOrganizeCode());
            return ResultBean.success("查询成功").setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode())
                    .setResultList(reportFormService.queryWorkTimeMonthlyPerformance(model));
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @GetMapping(value = "/busi-error-message/query")
    @ApiOperation(value = "关键件错误记录查询")
    public ResultBean queryMesErrorMassageByListPager(BuBusiErrorMessageModel model, Pager pager) {
        try {
            model.setOrganizeCode(AuthUtil.getOrganize().getOrganizeCode());
            return ResultBean.success("查询成功").setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode())
                    .setListPager(reportFormService.queryBusiErrorMassageByListPager(model, pager));
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @GetMapping(value = "/data-travel/query")
    @ApiOperation(value = "数据履历查询")
    public ResultBean queryMesDataTravelByListPager(SxMesDataTravel model, Pager pager) {
        try {
            model.setOrganizeCode(AuthUtil.getOrganize().getOrganizeCode());
            return ResultBean.success("查询成功").setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode())
                    .setListPager(reportFormService.queryMesDataTravelByListPager(model, pager));
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @PutMapping(value = "/data-travel/update")
    @ApiOperation(value = "数据履历编辑")
    public ResultBean queryMesDataTravelByListPager(SxMesDataTravel model) {
        try {
            ValidatorBean.checkNotNull(model.getId(), "id不能为空");
            reportFormService.updateDataTravel(model);
            return ResultBean.success("查询成功").setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode());
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @PutMapping(value = "/data-travel/confirm")
    @ApiOperation(value = "数据履历结果确认")
    public ResultBean queryMesDataTravelByListPager(Long[] ids, Integer confirmType) {
        try {
            ValidatorBean.checkNotNull(ids, "ids不能为空");
            ValidatorBean.checkNotNull(confirmType, "确认类型不能为空");
            reportFormService.dataTravelResultConfirm(ids, confirmType, AuthUtil.getSessionUser().getUserName());
            return ResultBean.success("查询成功").setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode());
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @GetMapping(value = "/outer-package-retrospect/query")
    @ApiOperation(value = "大小包装条码追溯报表")
    public ResultBean queryMesDataTravelByListPager(MesOuterPackageRetrospectModel model, Pager pager) {
        try {
            model.setOrganizeCode(AuthUtil.getOrganize().getOrganizeCode());
            return ResultBean.success("查询成功").setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode())
                    .setListPager(reportFormService.queryMesOuterPackageRetrospectPager(model, pager));
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }
}
