package cn.estsh.i3plus.ext.mes.pcn.apiservice.daoimpl;

import cn.estsh.i3plus.ext.mes.pcn.apiservice.dao.IMesProdRuleSortCfgDao;
import cn.estsh.i3plus.pojo.base.enumutil.CommonEnumUtil;
import cn.estsh.i3plus.pojo.mes.bean.MesProdRuleSortCfg;
import groovy.util.logging.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.persistence.EntityManager;
import javax.persistence.Query;

/**
 * @Description : 排序加工规则
 * @Reference :
 * @Author : junsheng.li
 * @CreateDate 2024/10/15 13:29
 * @Modify:
 **/
@Service
@Slf4j
public class MesProdRuleSortCfgDaoImpl implements IMesProdRuleSortCfgDao {

    @Autowired
    private EntityManager entityManager;

    @Override
    public MesProdRuleSortCfg getMesProdRuleSortCfg(String organizeCode, String partNo, String workCenterCode) {
        if (StringUtils.isEmpty(organizeCode) || StringUtils.isEmpty(partNo) || StringUtils.isEmpty(workCenterCode)) return null;
        StringBuilder hql = new StringBuilder();
        hql.append(" select sortCfg from MesProdRuleSortCfg sortCfg where 1=1");
        hql.append(" and sortCfg.organizeCode = :organizeCode and sortCfg.isValid = :isValid and sortCfg.isDeleted = :isDeleted ");
        hql.append(" and sortCfg.workCenterCode = :workCenterCode and (sortCfg.productPartNo = :partNo or sortCfg.reportPartNo =:partNo)");
        Query hqlQuery = entityManager.createQuery(hql.toString(), MesProdRuleSortCfg.class);
        hqlQuery.setParameter("organizeCode", organizeCode);
        hqlQuery.setParameter("isValid", CommonEnumUtil.VALID);
        hqlQuery.setParameter("isDeleted", CommonEnumUtil.FALSE);
        hqlQuery.setParameter("workCenterCode", workCenterCode);
        hqlQuery.setParameter("partNo", partNo);
        return (MesProdRuleSortCfg) hqlQuery.getSingleResult();
    }
}
