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

import cn.estsh.i3plus.ext.mes.pcn.api.busi.jx.IJxProduceSnExtService;
import cn.estsh.i3plus.ext.mes.pcn.pojo.bean.MesProduceSnExt;
import cn.estsh.i3plus.ext.mes.pcn.pojo.repository.MesProduceSnExtRepository;
import cn.estsh.i3plus.ext.mes.pcn.pojo.util.MesPcnExtConstWords;
import cn.estsh.i3plus.mes.pcn.actor.shipping.dispatch.IFsmRouteDataService;
import cn.estsh.i3plus.pojo.base.bean.DdlPackBean;
import cn.estsh.i3plus.pojo.base.tool.DdlPreparedPack;
import cn.estsh.i3plus.pojo.mes.bean.MesRouteProcess;
import cn.estsh.i3plus.pojo.mes.model.StationRequestBean;
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;

/**
 * @Author: wangjie
 * @CreateDate: 2021/01/18 11:41 上午
 * @Description:
 **/
@Slf4j
@Service
public class JxProduceSnExtService implements IJxProduceSnExtService {

    @Autowired
    private MesProduceSnExtRepository produceSnExtRepository;

    @Autowired
    private IFsmRouteDataService fsmRouteDataService;

    @Override
    public MesProduceSnExt getProduceSnExtDb(String organizeCode, String serialNumber, String productSn) {
        if (StringUtils.isEmpty(organizeCode) || (StringUtils.isEmpty(serialNumber) && StringUtils.isEmpty(productSn))) return null;
        DdlPackBean packBean = DdlPackBean.getDdlPackBean(organizeCode);
        DdlPreparedPack.getStringEqualPack(serialNumber, MesPcnExtConstWords.SERIAL_NUMBER, packBean);
        DdlPreparedPack.getStringEqualPack(productSn, MesPcnExtConstWords.PRODUCT_SN, packBean);
        return produceSnExtRepository.getByProperty(packBean);
    }

    @Override
    public void doPackSnExtProcess(StationRequestBean reqBean, MesProduceSnExt produceSnExt) {
        if (null == reqBean || null == produceSnExt) return;

        List<MesRouteProcess> routeProcessList = fsmRouteDataService.getCachedFsmRouteProcessData(reqBean);

        if (CollectionUtils.isEmpty(routeProcessList)) return;

        produceSnExt.setRouteCode(reqBean.getRouteCode());
        produceSnExt.setProcessCode(reqBean.getProcessCode());
        produceSnExt.setNextProcessCode(getNextProcessCode(reqBean.getProcessCode(), routeProcessList));

        if (produceSnExt.getWorkCenterCode().equals(reqBean.getWorkCenterCode())) produceSnExt.setWorkCellCode(reqBean.getWorkCellCode());

    }

    @Override
    public void clearProduceSnProcess(MesProduceSnExt produceSnExt) {
        if (null == produceSnExt) return;
        produceSnExt.setRouteCode(null);
        produceSnExt.setProcessCode(null);
        produceSnExt.setWorkCellCode(null);
    }

    private String getNextProcessCode(String processCode, List<MesRouteProcess> routeProcessList) {
        Optional<MesRouteProcess> routeProcessOp = routeProcessList.stream().filter(o -> (null != o && o.getProcessCode().equals(processCode))).findFirst();
        if (null == routeProcessOp || !routeProcessOp.isPresent()) return null;
        return routeProcessOp.get().getNextProcess();
    }


}