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

import cn.estsh.i3plus.ext.mes.pcn.apiservice.dao.ep.IEpRepairTaskDao;
import cn.estsh.i3plus.ext.mes.pcn.pojo.bean.MesRepairTask;
import cn.estsh.i3plus.ext.mes.pcn.pojo.model.ep.EpQmsRepirTaskModel;
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 EpRepairTaskDao implements IEpRepairTaskDao {

    @Autowired
    private EntityManager entityManager;

    @Override
    public List<MesRepairTask> queryEpQmsRepairTaskList(EpQmsRepirTaskModel model) {
        StringBuffer hql = new StringBuffer();
        hql.append(" select rt ");
        packHqlForQueryRepairTask(hql, model);

        hql.append(" order by rt.workTime desc ");
        Query query = entityManager.createQuery(hql.toString(), MesRepairTask.class);

        packParameterForQueryRepairTask(query, model);

        return query.getResultList();
    }

    private void packHqlForQueryRepairTask(StringBuffer hql, EpQmsRepirTaskModel model) {
        hql.append(" from MesRepairTask as rt ");
        hql.append(" where rt.organizeCode = :organizeCode ");
        hql.append(" and rt.isValid = :isValid ");
        hql.append(" and rt.isDeleted = :isDeleted ");
        hql.append(" and (( rt.workTime >= :curWorkTimeStart and rt.workTime < :curWorkTimeEnd ) or ( rt.workTime >= :lastWorkTimeStart and rt.workTime < :lastWorkTimeEnd ))");
        if (!StringUtils.isEmpty(model.getWorkCenterCode())) {
            hql.append(" and rt.workCenterCode = :workCenterCode ");
        }
        if (!StringUtils.isEmpty(model.getPartNo())) {
            hql.append(" and rt.partNo = :partNo ");
        }
    }

    private void packParameterForQueryRepairTask(Query query, EpQmsRepirTaskModel 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.CUR_WORK_TIME_START, model.getCurWorkTimeStart());
        query.setParameter(MesPcnExtConstWords.CUR_WORK_TIME_END, model.getCurWorkTimeEnd());
        query.setParameter(MesPcnExtConstWords.LAST_WORK_TIME_START, model.getLastWorkTimeStart());
        query.setParameter(MesPcnExtConstWords.LAST_WORK_TIME_END, model.getLastWorkTimeEnd());
        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());
        }
    }


}
