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

import cn.estsh.i3plus.ext.mes.pcn.apiservice.dao.ep.IEpProduceSnRepairDao;
import cn.estsh.i3plus.ext.mes.pcn.pojo.model.ep.EpProduceSnExtModel;
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 EpProduceSnRepairDao implements IEpProduceSnRepairDao {

    @Autowired
    private EntityManager entityManager;

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

        packHqlForQuerySnListByPackNo4Oqc(hql);

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

        packParameterForQuerySnListByPackNo4Oqc(query, organizeCode, packageNo, workOrderNo);

        return query.getResultList();
    }

    private void packHqlForQuerySnListByPackNo4Oqc(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 (pse.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 pd.packageNo = :packageNo ");
    }

    private void packParameterForQuerySnListByPackNo4Oqc(Query query, String organizeCode, String packageNo, String workOrderNo) {
        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.PACKAGENO, packageNo);
    }

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

        packHqlForQuerySnListByOrderNoAndWorkTime4Oqc(hql);

        hql.append(" order by pse.serialNumber asc ");
        Query query = entityManager.createQuery(hql.toString(), EpProduceSnExtModel.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);
    }

}
