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

import cn.estsh.i3plus.ext.mes.api.busi.bu3.ISxProdDataToSoftAdapterService;
import cn.estsh.i3plus.ext.mes.apiservice.serviceimpl.busi.bu3.SxProdDataToSoftAdapterService;
import cn.estsh.i3plus.ext.mes.pojo.model.bu3.SxProdDataToSoftAdapterDataModel;
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.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 SxProdDataToSoftAdapterController extends MesBaseController {

    @PostMapping(value = "/sx/ext/prod-data-to-soft-adapter/exec")
    @ApiOperation(value = "生产数据回传软适配")
    public ResultBean execProdDataToSoftAdapter(@RequestBody SxProdDataToSoftAdapterDataModel data) {
        try {

            ValidatorBean.beginValid(data)
                    .notNull("date", data.getDate())
                    .notNull(MesExtConstWords.ORGANIZE_CODE, data.getOrganizeCode());

            StopWatch stopWatch = new StopWatch();

            stopWatch.start();

            String currentThreadName = Thread.currentThread().getName();

            LOGGER.info("MES工厂{}DATE{}执行生产数据回传软适配JOB --- API EXEC START --- THREAD:[{}]", data.getOrganizeCode(), data.getDate(), currentThreadName);

            try {
                data.setCurrentThreadName(currentThreadName);

                ISxProdDataToSoftAdapterService prodDataToSoftAdapterService = new SxProdDataToSoftAdapterService();
                prodDataToSoftAdapterService.exec(data);

            } finally {

                stopWatch.stop();

                LOGGER.info("MES工厂{}DATE{}执行生产数据回传软适配JOB --- API EXEC END --- JOB总耗时:[{}] --- THREAD:[{}]", data.getOrganizeCode(), data.getDate(), stopWatch.getTotalTimeMillis(), currentThreadName);

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