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

import cn.estsh.i3plus.ext.mes.pcn.api.base.IMesPackingRuleService;
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.MesPackingDefine;
import cn.estsh.i3plus.pojo.mes.bean.MesPackingRule;
import cn.estsh.i3plus.pojo.mes.bean.MesPackingRuleDetail;
import cn.estsh.i3plus.pojo.mes.repository.MesPackingDefineRepository;
import cn.estsh.i3plus.pojo.mes.repository.MesPackingRuleDetailRepository;
import cn.estsh.i3plus.pojo.mes.repository.MesPackingRuleRepository;
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.stream.Collectors;

/**
 * @Description : 成品包装规则
 * @Reference :
 * @Author : wangjie
 * @CreateDate 2024/6/13 15:18
 * @Modify:
 **/
@Service
public class MesPackingRuleService implements IMesPackingRuleService {

    @Autowired
    private MesPackingRuleDetailRepository packingRuleDetailRepository;

    @Autowired
    private MesPackingRuleRepository packingRuleRepository;

    @Autowired
    private MesPackingDefineRepository packingDefineRepository;

    @Override
    public List<MesPackingRuleDetail> getPackingRuleDetailList(String organizeCode, String partNo, String partType) {
        if (StringUtils.isEmpty(organizeCode) || StringUtils.isEmpty(partNo) || StringUtils.isEmpty(partType)) return null;
        return packingRuleDetailRepository.findByProperty(
                new String[]{MesPcnExtConstWords.ORGANIZE_CODE, MesPcnExtConstWords.IS_DELETED, MesPcnExtConstWords.IS_VALID, MesPcnExtConstWords.PART_NO, MesPcnExtConstWords.PART_TYPE},
                new Object[]{organizeCode, CommonEnumUtil.TRUE_OR_FALSE.FALSE.getValue(), CommonEnumUtil.IS_VAILD.VAILD.getValue(), partNo, partType});
    }

    @Override
    public List<MesPackingRule> getPackingRuleList(String organizeCode, List<String> packageCodeList) {
        if (StringUtils.isEmpty(organizeCode) || CollectionUtils.isEmpty(packageCodeList)) return null;
        packageCodeList = packageCodeList.stream().filter(o -> !StringUtils.isEmpty(o)).distinct().collect(Collectors.toList());
        DdlPackBean packBean = DdlPackBean.getDdlPackBean(organizeCode);
        if (packageCodeList.size() == 1) DdlPreparedPack.getStringEqualPack(packageCodeList.get(0), MesPcnExtConstWords.PACKAGE_CODE, packBean);
        else DdlPreparedPack.getInPackList(packageCodeList, MesPcnExtConstWords.PACKAGE_CODE, packBean);
        return packingRuleRepository.findByHqlWhere(packBean);
    }

    @Override
    public MesPackingDefine getPackingDefine(String organizeCode, String packCode) {
        if (StringUtils.isEmpty(organizeCode) || StringUtils.isEmpty(packCode)) return null;
        return packingDefineRepository.getByProperty(
                new String[]{MesPcnExtConstWords.ORGANIZE_CODE, MesPcnExtConstWords.IS_DELETED, MesPcnExtConstWords.IS_VALID, MesPcnExtConstWords.PACK_CODE},
                new Object[]{organizeCode, CommonEnumUtil.TRUE_OR_FALSE.FALSE.getValue(), CommonEnumUtil.IS_VAILD.VAILD.getValue(), packCode});
    }

}
