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

import cn.estsh.i3plus.ext.mes.pcn.apiservice.dao.ep.IEpOutPutStatisticsDao;
import cn.estsh.i3plus.ext.mes.pcn.pojo.bean.MesOutPutStatistics;
import cn.estsh.i3plus.ext.mes.pcn.pojo.model.ep.EpQmsOutPutStatisticsModel;
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 EpOutPutStatisticsDao implements IEpOutPutStatisticsDao {

    @Autowired
    private EntityManager entityManager;

    @Override
    public List<MesOutPutStatistics> queryEpQmsOutPutStatisticsList(EpQmsOutPutStatisticsModel model) {
        StringBuffer hql = new StringBuffer();
        hql.append(" select ops ");
        packHqlForQueryOutPutStatistics(hql, model);

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

        packParameterForQueryOutPutStatistics(query, model);

        return query.getResultList();
    }

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

    private void packParameterForQueryOutPutStatistics(Query query, EpQmsOutPutStatisticsModel 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());
        }
    }


}
