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

import cn.estsh.i3plus.ext.mes.api.busi.bu3.ISxProdDataToMonthlyStatisticsService;
import cn.estsh.i3plus.ext.mes.pojo.model.bu3.SxProdDataToAutoStatisticsModel;
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.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;

@RestController
@RequestMapping(CommonConstWords.BASE_URL_MES)
@Api(tags = "生产数据月度统计控制层")
public class SxProdDataToMonthlyStatisticsController extends MesBaseController {

    @Autowired
    private ISxProdDataToMonthlyStatisticsService prodDataToMonthlyStatisticsService;

    @PostMapping(value = "/sx/ext/prod-data-to-monthly-statistics/exec")
    @ApiOperation(value = "生产数据月度统计")
    public ResultBean exec(@RequestBody SxProdDataToAutoStatisticsModel data) {
        try {

            ValidatorBean.beginValid(data)
                    .notNull("today", data.getToday())
                    .notNull(MesExtConstWords.FLAG, data.getFlag())
                    .notNull(MesExtConstWords.CREATE_USER, data.getCreateUser())
                    .notNull(MesExtConstWords.ORGANIZE_CODE, data.getOrganizeCode());

            StopWatch stopWatch = new StopWatch();

            stopWatch.start();

            LOGGER.info("MES工厂{}TODAY{}执行生产数据月度统计JOB --- API START --- FLAG:[{}] ---  THREAD:[{}]", data.getOrganizeCode(), data.getToday(), data.getFlag(), Thread.currentThread().getName());

            try {

                prodDataToMonthlyStatisticsService.exec(data);

            } finally {

                stopWatch.stop();

                LOGGER.info("MES工厂{}TODAY{}执行生产数据月度统计JOB --- API END --- JOB总耗时:[{}] --- THREAD:[{}]", data.getOrganizeCode(), data.getToday(), stopWatch.getTotalTimeMillis(), Thread.currentThread().getName());

            }
            return ResultBean.success("操作成功").setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode());
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }
}
