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

import cn.estsh.i3plus.ext.mes.pcn.apiservice.dao.bu.IBuProduceSnRepairDao;
import cn.estsh.i3plus.ext.mes.pcn.pojo.model.bu.BuOqcProduceSnModel;
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 BuProduceSnRepairDao implements IBuProduceSnRepairDao {

    @Autowired
    private EntityManager entityManager;

    @Override
    public List<BuOqcProduceSnModel> querySnRepairListByOrderNoAndWorkTime4Oqc(String organizeCode, String workOrderNo, String workTime, String shiftCode) {
        StringBuffer hql = new StringBuffer();
        hql.append(" select new " + BuOqcProduceSnModel.class.getName() + "(pse.serialNumber, pse.workOrderNo, pse.qcStatus, pse.snStatus, psr.outWorkCenterTime, pse.lastWorkOrderNo, pd.packageNo, pse.workCenterCode) ");

        packHqlForQuerySnListByOrderNoAndWorkTime4Oqc(hql);

        hql.append(" order by pse.serialNumber asc ");
        Query query = entityManager.createQuery(hql.toString(), BuOqcProduceSnModel.class);

        packParameterForQuerySnListByOrderNoAndWorkTime4Oqc(query, organizeCode, workOrderNo, workTime, shiftCode);

        return query.getResultList();
    }

    private void packHqlForQuerySnListByOrderNoAndWorkTime4Oqc(StringBuffer hql) {
        hql.append(" from MesProduceSnExt as pse ");
        hql.append(" left join MesProduceSnRepair as psr on (pse.lastWorkOrderNo = psr.workOrderNo and pse.serialNumber = psr.serialNumber and psr.organizeCode = :organizeCode and psr.isValid = :isValid and psr.isDeleted = :isDeleted) ");
        hql.append(" left join MesPackageDetail as pd on (psr.serialNumber = pd.serialNumber and pd.organizeCode = :organizeCode and pd.isValid = :isValid and pd.isDeleted = :isDeleted) ");
        hql.append(" where pse.organizeCode = :organizeCode ");
        hql.append(" and pse.isValid = :isValid ");
        hql.append(" and pse.isDeleted = :isDeleted ");
        hql.append(" and pse.lastWorkOrderNo = :lastWorkOrderNo ");
        hql.append(" and psr.workTime = :workTime ");
        hql.append(" and psr.shiftCode = :shiftCode ");
    }

    private void packParameterForQuerySnListByOrderNoAndWorkTime4Oqc(Query query, String organizeCode, String workOrderNo, String workTime, String shiftCode) {
        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.LAST_WORK_ORDER_NO, workOrderNo);
        query.setParameter(MesPcnExtConstWords.WORK_TIME, workTime);
        query.setParameter(MesPcnExtConstWords.SHIFT_CODE, shiftCode);
    }

}
