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

import cn.estsh.i3plus.ext.mes.pcn.apiservice.dao.IPackageExtDao;
import cn.estsh.i3plus.ext.mes.pcn.pojo.model.SnAndPackageNoCorrelationModel;
import cn.estsh.i3plus.ext.mes.pcn.pojo.util.MesPcnExtConstWords;
import cn.estsh.i3plus.pojo.base.enumutil.CommonEnumUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.persistence.EntityManager;
import javax.persistence.Query;
import java.util.List;

/**
 * @Author: wangjie
 * @CreateDate: 2019/11/21 11:29 上午
 * @Description:
 **/
@Service
public class PackageExtDao implements IPackageExtDao {

    @Autowired
    private EntityManager entityManager;

    @Override
    public List<SnAndPackageNoCorrelationModel> getSnAndPackCorrelationListByOrderNo(String organizeCode, String orderNo) {
        StringBuffer hql = new StringBuffer();
        hql.append(" select new " + SnAndPackageNoCorrelationModel.class.getName() + "(pe.workOrderNo, pe.packageNo, pe.createDatetime, pd.serialNumber) ");
        hql.append(" from MesPackageExt as pe ");
        hql.append(" left join MesPackageDetail as pd on (pe.packageNo = pd.packageNo and pd.organizeCode = :organizeCode and pd.isValid = :isValid and pd.isDeleted = :isDeleted) ");
        hql.append(" where pe.organizeCode = :organizeCode ");
        hql.append(" and pe.isValid = :isValid ");
        hql.append(" and pe.isDeleted = :isDeleted ");
        hql.append(" and pe.workOrderNo = :workOrderNo ");

        hql.append(" order by pe.createDatetime desc ");
        Query query = entityManager.createQuery(hql.toString(), SnAndPackageNoCorrelationModel.class);

        query.setParameter(MesPcnExtConstWords.ORGANIZE_CODE, organizeCode);
        query.setParameter(MesPcnExtConstWords.IS_VALID, CommonEnumUtil.IS_VAILD.VAILD.getValue());
        query.setParameter(MesPcnExtConstWords.IS_DELETED, CommonEnumUtil.TRUE_OR_FALSE.FALSE.getValue());
        query.setParameter(MesPcnExtConstWords.WORK_ORDER_NO, orderNo);

        return query.getResultList();
    }

}
