package cn.estsh.i3plus.ext.mes.pcn.apiservice.serviceimpl.print.sortshipping.strategy;

import cn.estsh.i3plus.ext.mes.pcn.apiservice.serviceimpl.print.sortshipping.ISortShippingDispatchStrategyService;
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.base.util.StringUtil;
import cn.estsh.i3plus.pojo.mes.bean.shipping.MesShippingOrderManagement;
import cn.estsh.i3plus.pojo.mes.bean.shipping.MesShippingOrderManagementDetail;
import cn.estsh.i3plus.pojo.mes.repository.MesShippingOrderManagementDetailRepository;
import cn.estsh.i3plus.pojo.mes.repository.MesShippingOrderManagementRepository;
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.Arrays;
import java.util.List;

/**
 * @Description : 发运单查询
 **/
@Service
@Slf4j
public abstract class SortShippingDispatchStrategyService implements ISortShippingDispatchStrategyService {

    @Autowired
    public MesShippingOrderManagementRepository shippingOrderManagementRepository;

    @Autowired
    public MesShippingOrderManagementDetailRepository shippingOrderManagementDetailRepository;

    @Override
    public List<MesShippingOrderManagement> getShippingOrderManagementList(MesShippingOrderManagement bean) {
        DdlPackBean packBean = DdlPackBean.getDdlPackBean(bean.getOrganizeCode());
        List<String> shippingGroupCodeList = !StringUtil.isEmpty(bean.getShippingGroupCode()) ? Arrays.asList(bean.getShippingGroupCode().split(MesPcnExtConstWords.COMMA)) : null;
        if (!CollectionUtils.isEmpty(shippingGroupCodeList)) {
            if (shippingGroupCodeList.size() == 1) DdlPreparedPack.getStringEqualPack(shippingGroupCodeList.get(0), MesPcnExtConstWords.SHIPPING_GROUP_CODE, packBean);
            else  DdlPreparedPack.getInPackList(shippingGroupCodeList, MesPcnExtConstWords.SHIPPING_GROUP_CODE, packBean);
        }
        DdlPreparedPack.getStringEqualPack(bean.getShippingCode(), MesPcnExtConstWords.SHIPPING_CODE, packBean);
        DdlPreparedPack.getIsNull(MesPcnExtConstWords.LAST_PRINT_TIME, packBean);
        DdlPreparedPack.getOrderByPack(new Object[]{CommonEnumUtil.ASC_OR_DESC.ASC.getValue()}, new String[]{MesPcnExtConstWords.CREATE_DATE_TIME}, packBean);
        return shippingOrderManagementRepository.findByHqlTopWhere(packBean,10);
    }

    @Override
    public List<MesShippingOrderManagementDetail> getShippingOrderManagementDetailList(String organizeCode, List<Long> idList, Object[] obj, String[] str) {
        if (StringUtils.isEmpty(organizeCode) || CollectionUtils.isEmpty(idList)) return null;
        DdlPackBean packBean = DdlPackBean.getDdlPackBean(organizeCode);
        if (idList.size() == 1) DdlPreparedPack.getNumEqualPack(idList.get(0), MesPcnExtConstWords.PID, packBean);
        else DdlPreparedPack.getInPackList(idList, MesPcnExtConstWords.PID, packBean);
        if (null != obj && null != str) DdlPreparedPack.getOrderByPack(obj, str, packBean);
        return shippingOrderManagementDetailRepository.findByHqlWhere(packBean);
    }

    @Override
    public void saveShippingOrderManagementList(List<MesShippingOrderManagement> shippingOrderManagementList) {
        if (CollectionUtils.isEmpty(shippingOrderManagementList)) return;
        shippingOrderManagementRepository.saveAll(shippingOrderManagementList);
    }

}
