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

import cn.estsh.i3plus.ext.mes.pcn.api.busi.IMesMoveRuleService;
import cn.estsh.i3plus.ext.mes.pcn.pojo.util.MesPcnExtConstWords;
import cn.estsh.i3plus.pojo.base.bean.DdlPackBean;
import cn.estsh.i3plus.pojo.base.tool.DdlPreparedPack;
import cn.estsh.i3plus.pojo.mes.bean.MesMoveRule;
import cn.estsh.i3plus.pojo.mes.repository.IMesMoveRuleRepository;
import groovy.util.logging.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;

/**
 * @Description : 移库规则
 * @Reference :
 * @Author : junsheng.li
 * @CreateDate 2024/9/28 12:15
 * @Modify:
 **/
@Service
@Slf4j
public class MesMoveRuleServiceImpl implements IMesMoveRuleService {

    @Autowired
    private IMesMoveRuleRepository mesMoveRuleRepository;

    @Override
    public List<MesMoveRule> findMesMoveRuleByPartProdGroupCode(String code, String organizeCode,Integer groupType) {
        if (StringUtils.isEmpty(code) || StringUtils.isEmpty(organizeCode)) return null;
        DdlPackBean ddlPackBean = DdlPackBean.getDdlPackBean(organizeCode);
        DdlPreparedPack.getNumEqualPack(groupType, "groupType", ddlPackBean);
        DdlPreparedPack.getStringEqualPack(code, "code", ddlPackBean);
        return mesMoveRuleRepository.findByHqlWhere(ddlPackBean);
    }

    @Override
    public List<MesMoveRule> findMesMoveRuleByPartProdGroupCode(List<String> codeList, String organizeCode,Integer groupType) {
        if (CollectionUtils.isEmpty(codeList) || StringUtils.isEmpty(organizeCode)) return null;
        DdlPackBean ddlPackBean = DdlPackBean.getDdlPackBean(organizeCode);
        DdlPreparedPack.getNumEqualPack(groupType, "groupType", ddlPackBean);
        if (codeList.size() == MesPcnExtConstWords.ONE){
            DdlPreparedPack.getStringEqualPack(codeList.iterator().next(), "code", ddlPackBean);
        }else{
            DdlPreparedPack.getInPackList(codeList, "code", ddlPackBean);
        }
        return mesMoveRuleRepository.findByHqlWhere(ddlPackBean);
    }
}
