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.seres.MesCimSeresJisRfid;
import cn.estsh.i3plus.pojo.mes.bean.shipping.MesShippingQueue;
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;

@Service
@Slf4j
public class MesShippingQueueService implements IMesShippingQueueService {

    @Autowired
    private MesShippingQueueRepository shippingQueueRepository;

    @Autowired
    private IMesCimSeresJisRfidRepository cimSeresJisRfidRepository;

    @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 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()});
    }
}
