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

import cn.estsh.i3plus.ext.mes.api.busi.ISxReportFormService;
import cn.estsh.i3plus.ext.mes.pojo.bean.MesContainerJobBooking;
import cn.estsh.i3plus.ext.mes.pojo.bean.MesProduceSnTravelExt;
import cn.estsh.i3plus.ext.mes.pojo.bean.MesRepairRecordExt;
import cn.estsh.i3plus.ext.mes.pojo.bean.MesReplacementTravel;
import cn.estsh.i3plus.ext.mes.pojo.model.*;
import cn.estsh.i3plus.ext.mes.pojo.util.MesExtConstWords;
import cn.estsh.i3plus.platform.common.util.CommonConstWords;
import cn.estsh.i3plus.pojo.base.bean.ListPager;
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.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
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 SxReportFormController extends MesBaseController {

    @Autowired
    private ISxReportFormService reportFormService;

    @GetMapping(value = "/sn-travel/query")
    @ApiOperation(value = "条码履历报表")
    public ResultBean queryProduceSnTravelExtPager(MesProduceSnTravelExt produceSnTravelExt, Pager pager) {
        try {
            produceSnTravelExt.setOrganizeCode(AuthUtil.getOrganize().getOrganizeCode());
            ListPager listPager = reportFormService.queryProduceSnTravelExtByPager(produceSnTravelExt, pager);
            return ResultBean.success("操作成功").setListPager(listPager)
                    .setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode());
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @GetMapping(value = "/standard-work-order/pack-sn/query")
    @ApiOperation(value = "正常工单包装条码查询报表")
    public ResultBean queryStandardWorkOrderPackSnPager(SxPackSnReportModel model, Pager pager) {
        try {
            model.setOrganizeCode(AuthUtil.getOrganize().getOrganizeCode());
            return ResultBean.success("查询成功").setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode())
                    .setListPager(reportFormService.queryStandardWorkOrderPackSnPager(model, pager));
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @GetMapping(value = "/repair-work-order/pack-sn/query")
    @ApiOperation(value = "返工工单包装条码查询报表")
    public ResultBean queryRepairWorkOrderPackSnPager(SxPackSnReportModel model, Pager pager) {
        try {
            model.setOrganizeCode(AuthUtil.getOrganize().getOrganizeCode());
            return ResultBean.success("查询成功").setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode())
                    .setListPager(reportFormService.queryRepairWorkOrderPackSnPager(model, pager));
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @GetMapping({"/standard-work-order-sn/query"})
    @ApiOperation("正常工单条码")
    public ResultBean queryStandardWorkOrderSnPager(ProduceSnExtModel model, Pager pager) {
        try {
            model.setOrganizeCode(AuthUtil.getOrganize().getOrganizeCode());
            ListPager listPager = reportFormService.queryStandardWorkOrderSnPager(model, pager);
            return ResultBean.success("操作成功").setListPager(listPager).setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode());
        } catch (ImppBusiException var4) {
            return ResultBean.fail(var4);
        } catch (Exception var5) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(var5);
        }
    }

    @GetMapping(value = "/repair-work-order-sn/query")
    @ApiOperation(value = "返工工单条码")
    public ResultBean queryRepairWorkOrderSnPager(ProduceSnRepairModel model, Pager pager) {
        try {
            model.setOrganizeCode(AuthUtil.getOrganize().getOrganizeCode());
            return ResultBean.success("查询成功").setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode())
                    .setListPager(reportFormService.queryRepairWorkOrderSnPager(model, pager));
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @GetMapping(value = "/un-work-time-detail/query")
    @ApiOperation(value = "非作业时间明细")
    public ResultBean queryUnWorkTimeDetailPager(UnWorkTimeDetailModel model, Pager pager) {
        try {
            model.setOrganizeCode(AuthUtil.getOrganize().getOrganizeCode());
            return ResultBean.success("查询成功").setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode())
                    .setListPager(reportFormService.queryUnWorkTimeDetailPager(model, pager));
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @GetMapping(value = "/un-work-time-statistics/query")
    @ApiOperation(value = "非作业时间汇总")
    public ResultBean queryUnWorkTimeStatistics(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.queryUnWorkTimeStatistics(model));
        } 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 = "/work-time-monthly-performance/eam-query")
    @ApiOperation(value = "作业时间月绩eam")
    public ResultBean queryWorkTimeMonthlyPerformanceEam(@RequestParam("organizeCode") String organizeCode,
                                                         @RequestParam("workCenterCode") String workCenterCode,
                                                         @RequestParam("dimension") Integer dimension,
                                                         @RequestParam("dimension1") Integer dimension1,
                                                         @RequestParam("workTimeStart") String workTimeStart,
                                                         @RequestParam("workTimeEnd") String workTimeEnd) {
        try {
            UnWorkTimeStatisticsModel model =new UnWorkTimeStatisticsModel();
            model.setOrganizeCode(organizeCode);
//            model.setWorkCenterCode(workCenterCode);//EAM查全部产线 不要改 todo
            model.setDimension(dimension);
            model.setDimension1(dimension1);
            model.setWorkTimeStart(workTimeStart);
            model.setWorkTimeEnd(workTimeEnd);
            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 = "/un-work-time-category-statistics/query")
    @ApiOperation(value = "非作业时间类别统计")
    public ResultBean queryUnWorkTimeCategoryStatistics(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.queryUnWorkTimeCategoryStatistics(model));
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @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({"/mes-replacement-travel/query"})
    @ApiOperation("换件作业报表")
    public ResultBean queryReplacementTravel(MesReplacementTravel replacementTravel, Pager pager) {
        try {
            replacementTravel.setOrganizeCode(AuthUtil.getOrganize().getOrganizeCode());
            ListPager listPager = reportFormService.queryReplacementTravel(replacementTravel, pager);
            return ResultBean.success("操作成功").setListPager(listPager).setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode());
        } catch (ImppBusiException var4) {
            return ResultBean.fail(var4);
        } catch (Exception var5) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(var5);
        }
    }

    @GetMapping({"/mes-repair-task/query"})
    @ApiOperation("维修记录报表")
    public ResultBean queryRepairTask(MesRepairRecordExt repairRecordExt, Pager pager) {
        try {
            repairRecordExt.setOrganizeCode(AuthUtil.getOrganize().getOrganizeCode());
            ListPager listPager = reportFormService.queryRepairTask(repairRecordExt, pager);
            return ResultBean.success("操作成功").setListPager(listPager).setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode());
        } catch (ImppBusiException var4) {
            return ResultBean.fail(var4);
        } catch (Exception var5) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(var5);
        }
    }

    @GetMapping({"/mes-job-booking/query"})
    @ApiOperation("数据录入报表")
    public ResultBean queryJobBooking(MesContainerJobBooking jobBooking, Pager pager) {
        try {
            jobBooking.setOrganizeCode(AuthUtil.getOrganize().getOrganizeCode());
            ListPager listPager = reportFormService.queryJobBooking(jobBooking, pager);
            return ResultBean.success("操作成功").setListPager(listPager).setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode());
        } catch (ImppBusiException var4) {
            return ResultBean.fail(var4);
        } catch (Exception var5) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(var5);
        }
    }

}
