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

import cn.estsh.i3plus.ext.mes.apiservice.dao.bu3.ISxEquipmentCheckDetailDao;
import cn.estsh.i3plus.ext.mes.pojo.model.bu3.SxDailyCheckEquCheckDataModel;
import cn.estsh.i3plus.ext.mes.pojo.util.MesExtConstWords;
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 SxEquipmentCheckDetailDao implements ISxEquipmentCheckDetailDao {

    @Autowired
    private EntityManager entityManager;

    @Override
    public List<SxDailyCheckEquCheckDataModel> queryDailyCheckEquipmentCheckDetailList(SxDailyCheckEquCheckDataModel model) {
        StringBuffer hql = new StringBuffer();
        hql.append(" select new " + SxDailyCheckEquCheckDataModel.class.getName() + "(ecd.equNo, ecd.equName, ecd.projectContent, ecd.checkMethod, ecd.checkType, ecd.spotCheckUser, ecd.spotCheckTime, " +
                " ecd.teamConfirmUser, ecd.teamConfirmTime, ecd.checkResult, ecd.resultValue, ecd.remark) ");
        hql.append(" from MesEquipmentCheckDetail as ecd ");
        hql.append(" left join MesOrderJob as oj on (ecd.orderJobNo = oj.orderNo and oj.organizeCode = :organizeCode and oj.isValid = :isValid and oj.isDeleted = :isDeleted) ");
        hql.append(" where ecd.organizeCode = :organizeCode ");
        hql.append(" and ecd.isValid = :isValid ");
        hql.append(" and ecd.isDeleted = :isDeleted ");
        hql.append(" and ecd.equNo = :equNo ");
        hql.append(" and oj.workCenterCode = :workCenterCode ");
        hql.append(" and oj.shiftCode = :shiftCode ");
        hql.append(" and oj.orderStatus = :orderStatus ");
        hql.append(" and oj.workTime >= :workTimeStart ");
        hql.append(" and oj.workTime < :workTimeEnd ");

        Query query = entityManager.createQuery(hql.toString(), SxDailyCheckEquCheckDataModel.class);

        query.setParameter(MesExtConstWords.ORGANIZE_CODE, model.getOrganizeCode());
        query.setParameter(MesExtConstWords.IS_VALID, CommonEnumUtil.IS_VAILD.VAILD.getValue());
        query.setParameter(MesExtConstWords.IS_DELETED, CommonEnumUtil.TRUE_OR_FALSE.FALSE.getValue());
        query.setParameter(MesExtConstWords.EQU_NO, model.getEquNo());
        query.setParameter(MesExtConstWords.WORK_CENTER_CODE, model.getWorkCenterCode());
        query.setParameter(MesExtConstWords.SHIFT_CODE, model.getShiftCode());
        query.setParameter(MesExtConstWords.ORDER_STATUS, model.getOrderStatus());
        query.setParameter(MesExtConstWords.WORK_TIME_START, model.getWorkTimeStart());
        query.setParameter(MesExtConstWords.WORK_TIME_END, model.getWorkTimeEnd());

        return query.getResultList();
    }
}
