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

import cn.estsh.i3plus.ext.mes.api.busi.bu3.ISxOfficialLossAnalysisReportService;
import cn.estsh.i3plus.ext.mes.pojo.model.bu3.SxOfficialLossAnalysisReportModel;
import cn.estsh.i3plus.ext.mes.pojo.util.MesExtConstWords;
import cn.estsh.i3plus.platform.common.util.CommonConstWords;
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.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;

@RestController
@RequestMapping(CommonConstWords.BASE_URL_MES)
@Api(tags = "工场损失分析报表控制层")
public class SxOfficialLossAnalysisReportController extends MesBaseController {

    @Autowired
    private ISxOfficialLossAnalysisReportService officialLossAnalysisReportService;

    @PostMapping(value = "/sx/ext/career-official-loss-analysis/query")
    @ApiOperation(value = "工场损失明细分析报表报表")
    public ResultBean queryCareerOfficialLossAnalysis(@RequestBody SxOfficialLossAnalysisReportModel data) {
        try {
            ValidatorBean.beginValid(data)
                    .notNull(MesExtConstWords.DIMENSION, data.getDimension())
                    .notNull(MesExtConstWords.WORK_TIME_START, data.getWorkTimeStart())
                    .notNull(MesExtConstWords.WORK_TIME_END, data.getWorkTimeEnd());
            data.setOrganizeCode(AuthUtil.getOrganize().getOrganizeCode());
            return ResultBean.success("查询成功").setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode())
                    .setResultObject(officialLossAnalysisReportService.queryCareerOfficialLossAnalysis(data));
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @PostMapping(value = "/sx/ext/career-official-loss-analysis/download")
    @ApiOperation(value = "工场损失明细分析报表报表导出")
    public ResultBean doDownLoadCareerOfficialLossAnalysis(HttpServletResponse resp, @RequestBody SxOfficialLossAnalysisReportModel data) {
        try {
            ValidatorBean.beginValid(data)
                    .notNull(MesExtConstWords.DIMENSION, data.getDimension())
                    .notNull(MesExtConstWords.WORK_TIME_START, data.getWorkTimeStart())
                    .notNull(MesExtConstWords.WORK_TIME_END, data.getWorkTimeEnd());
            data.setOrganizeCode(AuthUtil.getOrganize().getOrganizeCode());
            officialLossAnalysisReportService.doDownLoadCareerOfficialLossAnalysis(resp, data);
            return ResultBean.success("操作成功").setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode());
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @PostMapping(value = "/sx/ext/official-loss-trend-chart/query")
    @ApiOperation(value = "工场损失推移管理报表")
    public ResultBean queryOfficialLossTrendChart(@RequestBody SxOfficialLossAnalysisReportModel data) {
        try {
            ValidatorBean.beginValid(data)
                    .notNull(MesExtConstWords.DIMENSION, data.getDimension())
                    .notNull(MesExtConstWords.WORK_TIME_START, data.getWorkTimeStart())
                    .notNull(MesExtConstWords.WORK_TIME_END, data.getWorkTimeEnd());
            data.setOrganizeCode(AuthUtil.getOrganize().getOrganizeCode());
            return ResultBean.success("查询成功").setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode())
                    .setResultObject(officialLossAnalysisReportService.queryOfficialLossTrendChart(data));
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @PostMapping(value = "/sx/ext/official-loss-trend-chart/download")
    @ApiOperation(value = "工场损失推移管理报表导出")
    public ResultBean doDownLoadOfficialLossTrendChart(HttpServletResponse resp, @RequestBody SxOfficialLossAnalysisReportModel data) {
        try {
            ValidatorBean.beginValid(data)
                    .notNull(MesExtConstWords.DIMENSION, data.getDimension())
                    .notNull(MesExtConstWords.WORK_TIME_START, data.getWorkTimeStart())
                    .notNull(MesExtConstWords.WORK_TIME_END, data.getWorkTimeEnd());
            data.setOrganizeCode(AuthUtil.getOrganize().getOrganizeCode());
            officialLossAnalysisReportService.doDownLoadOfficialLossTrendChart(resp, data);
            return ResultBean.success("操作成功").setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode());
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }


}
