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

import cn.estsh.i3plus.ext.mes.api.busi.bu3.ISxDailyCheckConfirmRecordService;
import cn.estsh.i3plus.ext.mes.pojo.model.bu3.SxDailyCheckConfirmRecordDataModel;
import cn.estsh.i3plus.ext.mes.pojo.model.bu3.SxDailyCheckConfirmRecordModel;
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.util.StopWatch;
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 SxDailyCheckConfirmRecordController extends MesBaseController {

    @Autowired
    private ISxDailyCheckConfirmRecordService dailyCheckConfirmRecordService;

    @PostMapping(value = "/sx/ext/daily-check-confirm-record/data/query")
    @ApiOperation(value = "日常点检报表确认记录报表")
    public ResultBean queryDailyCheckConfirmRecordData(@RequestBody SxDailyCheckConfirmRecordDataModel data) {

        StopWatch stopWatch = new StopWatch();
        stopWatch.start();
        try {
            LOGGER.info("MES日常点检报表确认记录报表 --- QUERY START --- THREAD:[{}]", Thread.currentThread().getName());
            ValidatorBean.beginValid(data)
                    .notNull(MesExtConstWords.ORDER_TYPE, data.getOrderType())
                    .notNull(MesExtConstWords.WORK_CENTER_CODE, data.getWorkCenterCode())
                    .notNull(MesExtConstWords.WORK_TIME, data.getWorkTime())
                    .notNull(MesExtConstWords.SHIFT_CODE, data.getShiftCode());
            data.basic(AuthUtil.getOrganize().getOrganizeCode(), AuthUtil.getSessionUser().getUserName());
            return ResultBean.success("操作成功").setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode())
                    .setResultObject(dailyCheckConfirmRecordService.queryDailyCheckConfirmRecordData(data));
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        } finally {
            stopWatch.stop();
            LOGGER.info("MES日常点检报表确认记录报表 --- QUERY STOP --- 总耗时:[{}] --- THREAD:[{}]", stopWatch.getTotalTimeMillis(), Thread.currentThread().getName());
        }
    }

    @PostMapping(value = "/sx/ext/daily-check-confirm-record/data/confirm")
    @ApiOperation(value = "日常点检报表确认")
    public ResultBean doConfirmDailyCheckConfirmRecordData(@RequestBody SxDailyCheckConfirmRecordModel data) {
        try {
            ValidatorBean.beginValid(data)
                    .notNull(MesExtConstWords.ORDER_TYPE, data.getOrderType())
                    .notNull(MesExtConstWords.WORK_CENTER_CODE, data.getWorkCenterCode())
                    .notNull(MesExtConstWords.WORK_TIME, data.getWorkTime())
                    .notNull(MesExtConstWords.SHIFT_CODE, data.getShiftCode())
                    .notNull(MesExtConstWords.LEVEL, data.getLevel());
            data.basic(AuthUtil.getOrganize().getOrganizeCode(), AuthUtil.getSessionUser().getUserName());
            dailyCheckConfirmRecordService.doConfirmDailyCheckConfirmRecordData(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/daily-check-confirm-record/data/download")
    @ApiOperation(value = "日常点检报表确认记录报表导出")
    public ResultBean doDownLoadDailyCheckConfirmRecordData(HttpServletResponse resp, @RequestBody SxDailyCheckConfirmRecordDataModel data) {
        StopWatch stopWatch = new StopWatch();
        stopWatch.start();
        try {
            LOGGER.info("MES日常点检报表确认记录报表 --- DOWNLOAD START --- THREAD:[{}]", Thread.currentThread().getName());
            ValidatorBean.beginValid(data)
                    .notNull(MesExtConstWords.ORDER_TYPE, data.getOrderType())
                    .notNull(MesExtConstWords.WORK_CENTER_CODE, data.getWorkCenterCode())
                    .notNull(MesExtConstWords.WORK_TIME, data.getWorkTime())
                    .notNull(MesExtConstWords.SHIFT_CODE, data.getShiftCode());
            data.basic(AuthUtil.getOrganize().getOrganizeCode(), AuthUtil.getSessionUser().getUserName());
            dailyCheckConfirmRecordService.doDownLoadDailyCheckConfirmRecordData(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);
        } finally {
            stopWatch.stop();
            LOGGER.info("MES日常点检报表确认记录报表 --- DOWNLOAD STOP --- 总耗时:[{}] --- THREAD:[{}]", stopWatch.getTotalTimeMillis(), Thread.currentThread().getName());
        }
    }
}
