package cn.estsh.i3plus.ext.mes.apiservice.serviceimpl.busi;

import cn.estsh.i3plus.ext.mes.api.busi.ISxWorkOrderAutoLandedService;
import cn.estsh.i3plus.ext.mes.api.busi.IWorkOrderLogExtService;
import cn.estsh.i3plus.ext.mes.icloud.sdk.IWmsErpHouseCheckCloud;
import cn.estsh.i3plus.ext.mes.pojo.bean.MesPartAutoLanded;
import cn.estsh.i3plus.ext.mes.pojo.bean.MesWorkOrderExt;
import cn.estsh.i3plus.ext.mes.pojo.repository.MesPartAutoLandedRepository;
import cn.estsh.i3plus.ext.mes.pojo.repository.MesWorkOrderExtRepository;
import cn.estsh.i3plus.ext.mes.pojo.util.ConvertBeanExt;
import cn.estsh.i3plus.ext.mes.pojo.util.MesExtConstWords;
import cn.estsh.i3plus.ext.mes.pojo.util.MesExtEnumUtil;
import cn.estsh.i3plus.platform.common.convert.ConvertBean;
import cn.estsh.i3plus.pojo.base.bean.BaseResultBean;
import cn.estsh.i3plus.pojo.base.bean.DdlPackBean;
import cn.estsh.i3plus.pojo.base.enumutil.CommonEnumUtil;
import cn.estsh.i3plus.pojo.base.tool.DdlPreparedPack;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * @author wangjie
 * @version 1.0
 * @date 2021/1/13 9:28
 **/
@Slf4j
@Service
public class SxWorkOrderAutoLandedService implements ISxWorkOrderAutoLandedService {

    @Autowired
    private MesWorkOrderExtRepository workOrderExtRepository;

    @Autowired
    private MesPartAutoLandedRepository partAutoLandedRepository;

    @Autowired
    private IWorkOrderLogExtService workOrderLogExtService;

    @Autowired
    private IWmsErpHouseCheckCloud wmsErpHouseCheckCloud;

    @Override
    public void doWorkOrderAutoLanded(String organizeCode) {
        int landedValue = MesExtEnumUtil.WORK_ORDER_STATUS.LANDED.getValue();
        DdlPackBean packBean = DdlPackBean.getDdlPackBean(organizeCode);
        DdlPreparedPack.getNumEqualPack(MesExtEnumUtil.WORK_ORDER_STATUS.CREATE.getValue(), MesExtConstWords.WORK_ORDER_STATUS, packBean);
        List<MesWorkOrderExt> workOrderExtList = workOrderExtRepository.findByHqlWhere(packBean);
        if (CollectionUtils.isEmpty(workOrderExtList)) {
            log.info("Mes生产工单自动下达定时任务,未查询到创建状态的工单");
        }

        List<String> workOrderNoList = workOrderExtList.stream().filter(o -> null != o).map(MesWorkOrderExt::getOrderNo).collect(Collectors.toList());
        log.info("Mes生产工单自动下达定时任务,查询到的工单编号包含:{}", workOrderNoList.toString());

        for (MesWorkOrderExt workOrderExt : workOrderExtList) {
            if (null == workOrderExt) {
                continue;
            }
            if (StringUtils.isEmpty(workOrderExt.getWorkCenterCode())) {
                log.info("Mes生产工单自动下达定时任务,工单编号[{}]未关联生产线,不执行自动下达", workOrderNoList.toString());
                continue;
            }

            List<MesPartAutoLanded> partAutoLandedList = partAutoLandedRepository.findByProperty(
                    new String[]{MesExtConstWords.ORGANIZE_CODE, MesExtConstWords.IS_DELETED, MesExtConstWords.IS_VALID, MesExtConstWords.PART_NO},
                    new Object[]{organizeCode, CommonEnumUtil.TRUE_OR_FALSE.FALSE.getValue(), CommonEnumUtil.IS_VAILD.VAILD.getValue(), workOrderExt.getPartNo()});
            Optional<MesPartAutoLanded> partAutoLanded = CollectionUtils.isEmpty(partAutoLandedList) ? null :
                    partAutoLandedList.stream().filter(o -> (null != o && (StringUtils.isEmpty(o.getWorkCenterCode()) || o.getWorkCenterCode().equals(workOrderExt.getWorkCenterCode())))).findFirst();
            if (null == partAutoLanded || !partAutoLanded.isPresent()) {
                log.info("Mes生产工单自动下达定时任务,工单编号[{}]不支持自动下达,自动发布物料配置未维护物料[{}]", workOrderExt.getOrderNo(), workOrderExt.getPartNo());
                continue;
            }

            log.info("Mes生产工单自动下达定时任务,工单编号[{}]下达中", workOrderExt.getOrderNo());

            //校验WMS的存储区是否存在
            BaseResultBean baseResultBean =  wmsErpHouseCheckCloud.getWmsZone(workOrderExt.getErpWarehouse(), organizeCode);
            if (null == baseResultBean) {
                log.info("Mes生产工单自动下达定时任务,调用校验WMS存储区微服接口失败,BaseResultBean对象为NULL,工单编号[{}]", workOrderExt.getOrderNo());
                continue;
            }
            if (!baseResultBean.success) {
                log.info("Mes生产工单自动下达定时任务,工单编号[{}]下达失败,原因:[{}]", workOrderExt.getOrderNo(), baseResultBean.errorMsg);
                continue;
            }

            workOrderExt.setWorkOrderStatus(landedValue);
            ConvertBean.serviceModelUpdate(workOrderExt, MesExtConstWords.JOB);
            workOrderExt.setSystemSyncStatus(MesExtEnumUtil.IF_SYNC_STATUS.NO_SYNC.getValue());
            new ConvertBeanExt(workOrderExt).convertBean(workOrderExt);
            workOrderExtRepository.save(workOrderExt);
            workOrderLogExtService.insertMesWorkOrderLogExt(workOrderExt);
            log.info("Mes生产工单自动下达定时任务,工单编号[{}]已下达", workOrderExt.getOrderNo());
        }
    }
}
