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

import cn.estsh.i3plus.ext.mes.apiservice.dao.bu3.ISxIotOutPutStatisticsDao;
import cn.estsh.i3plus.ext.mes.pojo.model.bu3.SxProdDataBzzgsModel;
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 SxIotOutPutStatisticsDao implements ISxIotOutPutStatisticsDao {

    @Autowired
    private EntityManager entityManager;

    @Override
    public List<SxProdDataBzzgsModel> queryIotOutPutStatisticsList(SxProdDataBzzgsModel model) {
        StringBuffer hql = new StringBuffer();
        hql.append(" select new " + SxProdDataBzzgsModel.class.getName() + "(ops.workTime, ops.workCenterCode, ops.shiftCode, ops.qty, ops.remakeQty, pe.standardWork) ");
        hql.append(" from MesOutPutStatistics as ops ");
        hql.append(" left join MesPartExt as pe on (ops.partNo = pe.partNo and pe.organizeCode = :organizeCode and pe.isValid = :isValid and pe.isDeleted = :isDeleted) ");
        hql.append(" where ops.organizeCode = :organizeCode ");
        hql.append(" and ops.isValid = :isValid ");
        hql.append(" and ops.isDeleted = :isDeleted ");
        hql.append(" and ops.workOrderType = :workOrderType ");
        hql.append(" and ops.modifyDatetime >= :modifyDatetimeStart ");
        hql.append(" and ops.modifyDatetime <= :modifyDatetimeEnd ");

        Query query = entityManager.createQuery(hql.toString(), SxProdDataBzzgsModel.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.WORK_ORDER_TYPE, model.getWorkOrderType());
        query.setParameter(MesExtConstWords.MODIFY_DATE_TIME_START, model.getModifyDatetimeStart());
        query.setParameter(MesExtConstWords.MODIFY_DATE_TIME_END, model.getModifyDatetimeEnd());

        return query.getResultList();
    }

}
