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

import cn.estsh.i3plus.ext.mes.pcn.pojo.bean.MesPartAutoLanded;
import cn.estsh.i3plus.ext.mes.pcn.pojo.bean.MesWorkOrderExt;
import cn.estsh.i3plus.ext.mes.pcn.pojo.repository.MesPartAutoLandedRepository;
import cn.estsh.i3plus.ext.mes.pcn.pojo.repository.MesWorkOrderExtRepository;
import cn.estsh.i3plus.ext.mes.pcn.pojo.util.ConvertBeanExt;
import cn.estsh.i3plus.ext.mes.pcn.pojo.util.MesPcnExtConstWords;
import cn.estsh.i3plus.ext.mes.pcn.pojo.util.MesPcnExtEnumUtil;
import cn.estsh.i3plus.platform.common.convert.ConvertBean;
import cn.estsh.i3plus.platform.common.util.MesConstWords;
import cn.estsh.i3plus.pojo.base.bean.DdlPackBean;
import cn.estsh.i3plus.pojo.base.enumutil.CommonEnumUtil;
import cn.estsh.i3plus.pojo.base.enumutil.MesEnumUtil;
import cn.estsh.i3plus.pojo.base.tool.DdlPreparedPack;
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.ObjectUtils;
import org.springframework.util.StringUtils;

import java.util.List;

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

    @Autowired
    private MesWorkOrderExtRepository workOrderExtRepository;

    @Autowired
    private MesPartAutoLandedRepository partAutoLandedRepository;

    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 = null;
        try {
            jsonObject = JSONObject.parseObject(jobParam);
        } catch (JSONException e) {
            LOGGER.error("Mes业务数据同步定时任务,参数异常，参数不是json格式");
            return;
        }

        Object organizeCode = jsonObject.get("organizationCode");
        int createValue = MesEnumUtil.WORK_ORDER_STATUS.CREATE.getValue();

        int landedValue = MesEnumUtil.WORK_ORDER_STATUS.LANDED.getValue();
        DdlPackBean ddlPackBean = DdlPackBean.getDdlPackBean(organizeCode.toString());
        DdlPreparedPack.getNumEqualPack(createValue, "workOrderStatus", ddlPackBean);
        List<MesWorkOrderExt> mesWorkOrders = workOrderExtRepository.findByHqlWhere(ddlPackBean);


        for (MesWorkOrderExt mesWorkOrder : mesWorkOrders) {
            String partNo = mesWorkOrder.getPartNo();
            String workCenterCode = mesWorkOrder.getWorkCenterCode();
            MesPartAutoLanded mesPartAutoLanded = partAutoLandedRepository.getByProperty(
                    new String[]{MesConstWords.ORGANIZE_CODE, MesConstWords.IS_DELETED, MesConstWords.IS_VALID, "partNo", "workCenterCode"},
                    new Object[]{organizeCode, CommonEnumUtil.TRUE_OR_FALSE.FALSE.getValue(), CommonEnumUtil.IS_VAILD.VAILD.getValue(), partNo, workCenterCode});
            if (!ObjectUtils.isEmpty(mesPartAutoLanded)) {
                mesWorkOrder.setWorkOrderStatus(landedValue);
                ConvertBean.serviceModelUpdate(mesWorkOrder, MesPcnExtConstWords.JOB);
                mesWorkOrder.setSystemSyncStatus(MesPcnExtEnumUtil.IF_SYNC_STATUS.NO_SYNC.getValue());
                new ConvertBeanExt(mesWorkOrder).convertBean(mesWorkOrder);
                workOrderExtRepository.save(mesWorkOrder);
            }
        }
    }
}
