package cn.estsh.i3plus.ext.mes.apiservice.schedulejob;

import cn.estsh.i3plus.ext.mes.api.busi.ISxWorkOrderAutoLandedService;
import cn.estsh.impp.framework.base.schedule.BaseImppScheduleJob;
import cn.estsh.impp.framework.boot.init.ApplicationProperties;
import com.alibaba.fastjson.JSONException;
import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.ApiOperation;
import org.quartz.DisallowConcurrentExecution;
import org.quartz.JobExecutionContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;

/**
 * 生产版本：指生产线。有本体产线、内置产线和组件生产线，本体产线为整机（成品）生产线，
 * 组件生产线生产出来的产品为销售件（可视为成品），内置生产线为半成品生产线，给本体线提供产品。
 * 计划下发到MES后部件生成顺序后可以自动下达
 * @author yumingxing
 * @version 1.0
 * @date 2021/1/27 15:16
 **/
@DisallowConcurrentExecution
@ApiOperation("生产工单自动下达")
public class WorkOrderAutoLanded extends BaseImppScheduleJob {

    @Autowired
    private ISxWorkOrderAutoLandedService workOrderAutoLandedService;

    public WorkOrderAutoLanded() {
        super(WorkOrderAutoLanded.class, "生产工单自动下达job");
    }


    @Override
    public void executeImppJob(JobExecutionContext context, ApplicationProperties applicationProperties) {
        String jobParam = this.getJobParam();
        if (StringUtils.isEmpty(jobParam)) {
            LOGGER.error("Mes生产工单自动下达定时任务,无法获取组织代码和方法代码");
            return;
        }

        JSONObject jsonObject;
        try {
            jsonObject = JSONObject.parseObject(jobParam);
        } catch (JSONException e) {
            LOGGER.error("Mes生产工单自动下达定时任务,参数异常，参数不是json格式");
            return;
        }

        Object organizeCode = jsonObject.get("organizationCode");


        long startTime = System.currentTimeMillis();

        workOrderAutoLandedService.doWorkOrderAutoLanded(organizeCode.toString());

        long endTime = System.currentTimeMillis();
        LOGGER.info(this.getClass().getSimpleName() + " :ALL:耗时: " + (endTime - startTime) + "ms");

    }
}
