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

import cn.estsh.i3plus.ext.mes.pcn.apiservice.dao.ep.IEpBadCellDetailDao;
import cn.estsh.i3plus.ext.mes.pcn.pojo.bean.MesBadCellDetail;
import cn.estsh.i3plus.ext.mes.pcn.pojo.model.ep.EpQmsBadCellDetailModel;
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 org.springframework.util.StringUtils;

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

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

    @Autowired
    private EntityManager entityManager;

    @Override
    public List<MesBadCellDetail> queryEpQmsBadCellDetailList(EpQmsBadCellDetailModel model) {
        StringBuffer hql = new StringBuffer();
        hql.append(" select bcd ");
        packHqlForQueryBadCellDetail(hql, model);

        hql.append(" order by bcd.defectActionTime desc ");
        Query query = entityManager.createQuery(hql.toString(), MesBadCellDetail.class);

        packParameterForQueryBadCellDetail(query, model);

        return query.getResultList();
    }

    private void packHqlForQueryBadCellDetail(StringBuffer hql, EpQmsBadCellDetailModel model) {
        hql.append(" from MesBadCellDetail as bcd ");
        hql.append(" where bcd.organizeCode = :organizeCode ");
        hql.append(" and bcd.isValid = :isValid ");
        hql.append(" and bcd.isDeleted = :isDeleted ");
        hql.append(" and (( bcd.defectActionTime >= :defectActionTimeStart and bcd.defectActionTime < :defectActionTimeEnd ) or ( bcd.defectActionTime >= :lastDefectActionTimeStart and bcd.defectActionTime < :lastDefectActionTimeEnd ))");
        if (!StringUtils.isEmpty(model.getWorkCenterCode())) {
            hql.append(" and bcd.workCenterCode = :workCenterCode ");
        }
        if (!StringUtils.isEmpty(model.getPartNo())) {
            hql.append(" and bcd.partNo = :partNo ");
        }
    }

    private void packParameterForQueryBadCellDetail(Query query, EpQmsBadCellDetailModel model) {
        query.setParameter(MesPcnExtConstWords.ORGANIZE_CODE, model.getOrganizeCode());
        query.setParameter(MesPcnExtConstWords.IS_VALID, CommonEnumUtil.IS_VAILD.VAILD.getValue());
        query.setParameter(MesPcnExtConstWords.IS_DELETED, CommonEnumUtil.TRUE_OR_FALSE.FALSE.getValue());
        query.setParameter(MesPcnExtConstWords.DEFECT_ACTION_TIME_START, model.getDefectActionTimeStart());
        query.setParameter(MesPcnExtConstWords.DEFECT_ACTION_TIME_END, model.getDefectActionTimeEnd());
        query.setParameter(MesPcnExtConstWords.LAST_DEFECT_ACTION_TIME_START, model.getLastDefectActionTimeStart());
        query.setParameter(MesPcnExtConstWords.LAST_DEFECT_ACTION_TIME_END, model.getLastDefectActionTimeEnd());
        if (!StringUtils.isEmpty(model.getWorkCenterCode())) {
            query.setParameter(MesPcnExtConstWords.WORK_CENTER_CODE, model.getWorkCenterCode());
        }
        if (!StringUtils.isEmpty(model.getPartNo())) {
            query.setParameter(MesPcnExtConstWords.PART_NO, model.getPartNo());
        }
    }


}
