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

import cn.estsh.i3plus.ext.mes.pcn.apiservice.dao.bu.IBuOutPutStatisticsDao;
import cn.estsh.i3plus.ext.mes.pcn.pojo.model.bu.BuOutPutStatisticsQtyModel;
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 BuOutPutStatisticsDao implements IBuOutPutStatisticsDao {

    @Autowired
    private EntityManager entityManager;

    @Override
    public List<BuOutPutStatisticsQtyModel> queryOutPutStatisticsListByPart(BuOutPutStatisticsQtyModel model) {
        StringBuffer hql = new StringBuffer();
        hql.append(" select new " + BuOutPutStatisticsQtyModel.class.getName() + "(ops.workOrderNo, ops.workTime, ops.shiftCode, ops.qty) ");
        packHqlForQueryOutPutStatisticsList(hql, model);
        hql.append(" order by ops.createDatetime desc ");
        Query query = entityManager.createQuery(hql.toString(), BuOutPutStatisticsQtyModel.class);

        packParameterForQueryOutPutStatisticsList(query, model);

        return query.getResultList();
    }

    private void packHqlForQueryOutPutStatisticsList(StringBuffer hql, BuOutPutStatisticsQtyModel model) {
        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 ");
        if (!StringUtils.isEmpty(model.getPartNo())) {
            hql.append(" and ops.partNo = :partNo ");
        }
        if (!StringUtils.isEmpty(model.getWorkTimeStart())) {
            hql.append(" and ops.workTime >= :workTimeStart ");
        }
        if (!StringUtils.isEmpty(model.getWorkTimeEnd())) {
            hql.append(" and ops.workTime <= :workTimeEnd ");
        }
        if (!StringUtils.isEmpty(model.getCategoryCode3())) {
            hql.append(" and pe.categoryCode3 = :categoryCode3 ");
        }
        if (!StringUtils.isEmpty(model.getSalesTo())) {
            hql.append(" and pe.salesTo = :salesTo ");
        }
        hql.append(" and pe.isOqc = :isOqc ");
    }

    private void packParameterForQueryOutPutStatisticsList(Query query, BuOutPutStatisticsQtyModel 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.IS_OQC, CommonEnumUtil.TRUE_OR_FALSE.TRUE.getValue());
        if (!StringUtils.isEmpty(model.getPartNo())) {
            query.setParameter(MesPcnExtConstWords.PART_NO, model.getPartNo());
        }
        if (!StringUtils.isEmpty(model.getWorkTimeStart())) {
            query.setParameter(MesPcnExtConstWords.WORK_TIME_START, model.getWorkTimeStart());
        }
        if (!StringUtils.isEmpty(model.getWorkTimeEnd())) {
            query.setParameter(MesPcnExtConstWords.WORK_TIME_END, model.getWorkTimeEnd());
        }
        if (!StringUtils.isEmpty(model.getCategoryCode3())) {
            query.setParameter(MesPcnExtConstWords.CATEGORY_CODE3, model.getCategoryCode3());
        }
        if (!StringUtils.isEmpty(model.getSalesTo())) {
            query.setParameter(MesPcnExtConstWords.SALES_TO, model.getSalesTo());
        }
    }
}
