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

import cn.estsh.i3plus.ext.mes.pcn.pojo.bean.MesWorkOrderExt;
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.pojo.base.bean.DdlPackBean;
import cn.estsh.i3plus.pojo.base.enumutil.MesEnumUtil;
import cn.estsh.i3plus.pojo.base.tool.DdlPreparedPack;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.quartz.DisallowConcurrentExecution;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;

import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 下达到产线的工单如果排在顺序第一时默认启动。
 *
 * @author yumingxing
 * @version 1.0
 * @date 2021/1/27 15:16
 **/
@DisallowConcurrentExecution
@ApiOperation("生产工单默认启动")
@Slf4j
public class WorkOrderAutoOpen implements Job {

    @Autowired
    private MesWorkOrderExtRepository workOrderExtRepository;


    @Override
    public void execute(JobExecutionContext jobExecutionContext) {
        Object param = jobExecutionContext.getJobDetail().getJobDataMap().get(MesPcnExtConstWords.PARAM);
        String organizeCode = param.toString();
        int openValue = MesEnumUtil.WORK_ORDER_STATUS.OPEN.getValue();

        int landedValue = MesEnumUtil.WORK_ORDER_STATUS.LANDED.getValue();

        DdlPackBean ddlPackBean = DdlPackBean.getDdlPackBean(organizeCode);
        DdlPreparedPack.getNumEqualPack(landedValue, "workOrderStatus", ddlPackBean);
        List<MesWorkOrderExt> mesWorkOrders = workOrderExtRepository.findByHqlWhere(ddlPackBean);

        if (!CollectionUtils.isEmpty(mesWorkOrders)) {
            mesWorkOrders.sort(Comparator.comparing(MesWorkOrderExt::getSeq));
            Map<String, List<MesWorkOrderExt>> map = mesWorkOrders.stream().collect(Collectors.groupingBy(MesWorkOrderExt::getWorkCenterCode));
            Set<Map.Entry<String, List<MesWorkOrderExt>>> entries = map.entrySet();
            for (Map.Entry<String, List<MesWorkOrderExt>> entry : entries) {
                List<MesWorkOrderExt> mesWorkOrderExts = entry.getValue();
                String workCenterCode = entry.getKey();
                //判断该产线下是否有启动的工单 没有则启动顺序第一的工单
                DdlPackBean ddlPackBean1 = DdlPackBean.getDdlPackBean(organizeCode.toString());
                DdlPreparedPack.getNumEqualPack(openValue, "workOrderStatus", ddlPackBean1);
                DdlPreparedPack.getStringEqualPack(workCenterCode, "workCenterCode", ddlPackBean1);
                List<MesWorkOrderExt> openWorkOrders = workOrderExtRepository.findByHqlWhere(ddlPackBean1);
                if (CollectionUtils.isEmpty(openWorkOrders) && mesWorkOrderExts.size() > 0) {
                    MesWorkOrderExt mesWorkOrderExt = mesWorkOrderExts.get(0);
                    mesWorkOrderExt.setWorkOrderStatus(openValue);
                    ConvertBean.serviceModelUpdate(mesWorkOrderExt, MesPcnExtConstWords.JOB);
                    mesWorkOrderExt.setSystemSyncStatus(MesPcnExtEnumUtil.IF_SYNC_STATUS.NO_SYNC.getValue());
                    new ConvertBeanExt(mesWorkOrderExt).convertBean(mesWorkOrderExt);
                    workOrderExtRepository.save(mesWorkOrderExt);
                    return;
                }
            }
        }

    }
}
