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

import cn.estsh.i3plus.ext.mes.pcn.api.busi.IMesShippingQueueService;
import cn.estsh.i3plus.ext.mes.pcn.pojo.util.MesPcnExtConstWords;
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 cn.estsh.i3plus.pojo.mes.bean.MesProdRouteOptParam;
import cn.estsh.i3plus.pojo.mes.bean.seres.MesCimSeresJisRfid;
import cn.estsh.i3plus.pojo.mes.bean.shipping.MesShippingOrderManagement;
import cn.estsh.i3plus.pojo.mes.bean.shipping.MesShippingOrderManagementDetail;
import cn.estsh.i3plus.pojo.mes.bean.shipping.MesShippingQueue;
import cn.estsh.i3plus.pojo.mes.repository.MesShippingOrderManagementDetailRepository;
import cn.estsh.i3plus.pojo.mes.repository.MesShippingOrderManagementRepository;
import cn.estsh.i3plus.pojo.mes.repository.MesShippingQueueRepository;
import cn.estsh.i3plus.pojo.mes.repository.seres.IMesCimSeresJisRfidRepository;
import cn.estsh.i3plus.pojo.mes.util.MesExtEnumUtil;
import groovy.util.logging.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Map;
import java.util.Optional;

@Service
@Slf4j
public class MesShippingQueueService implements IMesShippingQueueService {

    @Autowired
    private MesShippingQueueRepository shippingQueueRepository;

    @Autowired
    private IMesCimSeresJisRfidRepository cimSeresJisRfidRepository;

    @Autowired
    private MesShippingOrderManagementRepository shippingOrderManagementRepository;

    @Autowired
    private MesShippingOrderManagementDetailRepository shippingOrderManagementDetailRepository;

    @Override
    public MesShippingQueue getShippingQueue(String organizeCode, String shippingGroupCode) {
        if (StringUtils.isEmpty(organizeCode) || StringUtils.isEmpty(shippingGroupCode)) return null;
        DdlPackBean packBean = DdlPackBean.getDdlPackBean(organizeCode);
        DdlPreparedPack.getStringEqualPack(shippingGroupCode, MesPcnExtConstWords.SHIPPING_GROUP_CODE, packBean);
        DdlPreparedPack.getNumEqualPack(MesExtEnumUtil.QUEUE_ORDER_STATUS.CREATE.getValue(), MesPcnExtConstWords.QUEUE_STATUS, packBean);
        DdlPreparedPack.getOrderByPack(new Object[]{CommonEnumUtil.ASC_OR_DESC.ASC.getValue()}, new String[]{MesPcnExtConstWords.CREATE_DATE_TIME}, packBean);
        return shippingQueueRepository.getByProperty(packBean);
    }

    @Override
    public MesShippingQueue getShippingQueueByCode(String organizeCode, String shippingCode) {
        if (StringUtils.isEmpty(organizeCode) || StringUtils.isEmpty(shippingCode)) return null;
        DdlPackBean packBean = DdlPackBean.getDdlPackBean(organizeCode);
        DdlPreparedPack.getStringEqualPack(shippingCode, MesPcnExtConstWords.SHIPPING_CODE, packBean);
        DdlPreparedPack.getNumEqualPack(MesExtEnumUtil.QUEUE_ORDER_STATUS.CREATE.getValue(), MesPcnExtConstWords.QUEUE_STATUS, packBean);
        DdlPreparedPack.getOrderByPack(new Object[]{CommonEnumUtil.ASC_OR_DESC.ASC.getValue()}, new String[]{MesPcnExtConstWords.CREATE_DATE_TIME}, packBean);
        return shippingQueueRepository.getByProperty(packBean);
    }

    @Override
    public void saveShippingQueue(String organizeCode, Long id, String[] propertyName, Object[] propertyValue) {
        if (StringUtils.isEmpty(organizeCode) || StringUtils.isEmpty(id)) return;
        shippingQueueRepository.updateByPropertiesNoSync(new String[]{MesPcnExtConstWords.ID, MesPcnExtConstWords.ORGANIZE_CODE}, new Object[]{id, organizeCode}, propertyName, propertyValue);
    }

    @Override
    public MesCimSeresJisRfid getCimSeresJisRfid(String organizeCode, String vin) {
        if (StringUtils.isEmpty(organizeCode) || StringUtils.isEmpty(vin)) return null;
        return cimSeresJisRfidRepository.getByProperty(
                new String[]{MesPcnExtConstWords.ORGANIZE_CODE, MesPcnExtConstWords.IS_DELETED, MesPcnExtConstWords.IS_VALID, MesPcnExtConstWords.VIN, MesPcnExtConstWords.STATUS},
                new Object[]{organizeCode, CommonEnumUtil.TRUE_OR_FALSE.FALSE.getValue(), CommonEnumUtil.IS_VAILD.VAILD.getValue(), vin, CommonEnumUtil.TRUE_OR_FALSE.TRUE.getValue()});
    }

    @Override
    public MesShippingOrderManagement getShipping(String organizeCode, String shippingCode) {
        if (StringUtils.isEmpty(organizeCode) || StringUtils.isEmpty(shippingCode)) return null;
        return shippingOrderManagementRepository.getByProperty(
                new String[]{MesPcnExtConstWords.ORGANIZE_CODE, MesPcnExtConstWords.IS_DELETED, MesPcnExtConstWords.IS_VALID, MesPcnExtConstWords.SHIPPING_CODE},
                new Object[]{organizeCode, CommonEnumUtil.TRUE_OR_FALSE.FALSE.getValue(), CommonEnumUtil.IS_VAILD.VAILD.getValue(), shippingCode});
    }

    @Override
    public MesShippingOrderManagementDetail getShippingDetail(String organizeCode, Long pid) {
        if (StringUtils.isEmpty(organizeCode) || StringUtils.isEmpty(pid)) return null;
        return shippingOrderManagementDetailRepository.getByProperty(
                new String[]{MesPcnExtConstWords.ORGANIZE_CODE, MesPcnExtConstWords.IS_DELETED, MesPcnExtConstWords.IS_VALID, MesPcnExtConstWords.PID},
                new Object[]{organizeCode, CommonEnumUtil.TRUE_OR_FALSE.FALSE.getValue(), CommonEnumUtil.IS_VAILD.VAILD.getValue(), pid});
    }

    /**
     * ##################  【拉动单号18位】
     * 3263                【供应商代码 4位】
     * 1                   【-个VIN对应几个托盘 1位】
     * 2                   【左右区分,0代表做,1代表右;不区分左右为2 1位】
     * JSG1DJ05            【LMS零件类 8位】
     * 默认替换说明: 截取96位后面的32位替换为上面拼接的字符串
     */
    //解析rfidSn
    @Override
    public String getRfidSn(Optional<Map<String, MesProdRouteOptParam>> stepParamMap, String rfid) {
        //replaceIndex=替换起始位置; replaceLength=替换长度; replaceInfo=替换信息
        String replaceIndex = (null != stepParamMap && stepParamMap.isPresent() && stepParamMap.get().containsKey(MesPcnExtConstWords.REPLACE_INDEX)) ? stepParamMap.get().get(MesPcnExtConstWords.REPLACE_INDEX).getParamValue() : null;
        String replaceLength = (null != stepParamMap && stepParamMap.isPresent() && stepParamMap.get().containsKey(MesPcnExtConstWords.REPLACE_LENGTH)) ? stepParamMap.get().get(MesPcnExtConstWords.REPLACE_LENGTH).getParamValue() : null;
        String replaceInfo = (null != stepParamMap && stepParamMap.isPresent() && stepParamMap.get().containsKey(MesPcnExtConstWords.REPLACE_INFO)) ? stepParamMap.get().get(MesPcnExtConstWords.REPLACE_INFO).getParamValue() : null;
        if (StringUtils.isEmpty(replaceIndex)) replaceIndex = "96";
        if (StringUtils.isEmpty(replaceLength)) replaceLength = "32";
        if (StringUtils.isEmpty(replaceInfo)) replaceInfo = "##################326312JSG1DJ05";
        try {
            return new StringBuilder().append(rfid, MesPcnExtConstWords.ZERO, Integer.parseInt(replaceIndex)).append(replaceInfo).append(rfid, Integer.parseInt(replaceIndex) + Integer.parseInt(replaceLength), rfid.length()).toString();
        } catch (Exception e) {
            return null;
        }
    }

}
