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

import cn.estsh.i3plus.ext.mes.pcn.apiservice.dao.jx.IJxOutPutStatisticsDao;
import cn.estsh.i3plus.ext.mes.pcn.pojo.model.OqcLastCheckModel;
import cn.estsh.i3plus.ext.mes.pcn.pojo.util.MesPcnExtConstWords;
import cn.estsh.i3plus.ext.mes.pcn.pojo.util.MesPcnExtEnumUtil;
import cn.estsh.i3plus.pojo.base.common.Pager;
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 JxOutPutStatisticsDao implements IJxOutPutStatisticsDao {

    @Autowired
    private EntityManager entityManager;

    @Override
    public int queryOutPutStatisticsOqcByPagerCount(OqcLastCheckModel model) {
        StringBuffer hql = new StringBuffer();
        hql.append(" select count(distinct opso.id) ");

        packHqlForQueryOutPutStatisticsOqc(hql, model);

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

        packParameterForQueryOutPutStatisticsOqc(query, model);

        return ((Long) query.getSingleResult()).intValue();
    }

    @Override
    public List<OqcLastCheckModel> queryOutPutStatisticsOqcByPager(OqcLastCheckModel model, Pager pager) {
        StringBuffer hql = new StringBuffer();
        hql.append(" select new " + OqcLastCheckModel.class.getName() + "( opso.id, opso.pid, opso.organizeCode, opso.workOrderNo, opso.workCenterCode, " +
                " opso.partNo, opso.partNameRdd, opso.workTime, opso.shiftCode, opso.spotCheckQty, opso.checkLimit, opso.checkNo, opso.checkResult, " +
                " opso.remark, opso.confirmTime, opso.checkUser, opso.workOrderType ) ");
        packHqlForQueryOutPutStatisticsOqc(hql, model);
        hql.append(" order by opso.createDatetime desc ");
        Query query = entityManager.createQuery(hql.toString(), OqcLastCheckModel.class);

        packParameterForQueryOutPutStatisticsOqc(query, model);

        query.setMaxResults(pager.getPageSize());
        if (pager.getCurrentPage() == 0) {
            pager.setCurrentPage(1);
        }
        query.setFirstResult(pager.getCurrentPage() * pager.getPageSize() - pager.getPageSize());
        return query.getResultList();
    }

    private void packHqlForQueryOutPutStatisticsOqc(StringBuffer hql, OqcLastCheckModel model) {
        hql.append(" from MesOutPutStatisticsOqc as opso ");
        hql.append(" left join MesPartExt as p on (opso.partNo = p.partNo and p.organizeCode = :organizeCode and p.isValid = :isValid and p.isDeleted = :isDeleted) ");
        hql.append(" where opso.organizeCode = :organizeCode ");
        hql.append(" and opso.isValid = :isValid ");
        hql.append(" and opso.isDeleted = :isDeleted ");
        if (!StringUtils.isEmpty(model.getWorkOrderNo())) {
            hql.append(" and opso.workOrderNo = :workOrderNo ");
        }
        if (!StringUtils.isEmpty(model.getPartNo())) {
            hql.append(" and opso.partNo = :partNo ");
        }
        if (!StringUtils.isEmpty(model.getWorkCenterCode())) {
            hql.append(" and opso.workCenterCode = :workCenterCode ");
        }
        if (!StringUtils.isEmpty(model.getShiftCode())) {
            hql.append(" and opso.shiftCode = :shiftCode ");
        }
        if (!StringUtils.isEmpty(model.getCheckResult())) {
            hql.append(" and opso.checkResult = :checkResult ");
        }
        if (!StringUtils.isEmpty(model.getWorkTimeStart())) {
            hql.append(" and opso.workTime >= :workTimeStart ");
        }
        if (!StringUtils.isEmpty(model.getWorkTimeEnd())) {
            hql.append(" and opso.workTime <= :workTimeEnd ");
        }
        if (!StringUtils.isEmpty(model.getWorkOrderType())) {
            hql.append(" and opso.workOrderType = :workOrderType ");
        }
        hql.append(" and p.isOqc = :isOqc ");
    }

    private void packParameterForQueryOutPutStatisticsOqc(Query query, OqcLastCheckModel model) {
        query.setParameter(MesPcnExtConstWords.ORGANIZE_CODE, model.getOrganizeCode());
        query.setParameter(MesPcnExtConstWords.IS_VALID, CommonEnumUtil.IS_VAILD.VAILD.getValue());
        query.setParameter(MesPcnExtConstWords.IS_DELETED, MesPcnExtEnumUtil.TRUE_OR_FALSE.FALSE.getValue());
        query.setParameter(MesPcnExtConstWords.IS_OQC, MesPcnExtEnumUtil.TRUE_OR_FALSE.TRUE.getValue());
        if (!StringUtils.isEmpty(model.getWorkOrderNo())) {
            query.setParameter(MesPcnExtConstWords.WORK_ORDER_NO, model.getWorkOrderNo());
        }
        if (!StringUtils.isEmpty(model.getPartNo())) {
            query.setParameter(MesPcnExtConstWords.PART_NO, model.getPartNo());
        }
        if (!StringUtils.isEmpty(model.getWorkCenterCode())) {
            query.setParameter(MesPcnExtConstWords.WORK_CENTER_CODE, model.getWorkCenterCode());
        }
        if (!StringUtils.isEmpty(model.getShiftCode())) {
            query.setParameter(MesPcnExtConstWords.SHIFT_CODE, model.getShiftCode());
        }
        if (!StringUtils.isEmpty(model.getCheckResult())) {
            query.setParameter(MesPcnExtConstWords.CHECK_RESULT, model.getCheckResult());
        }
        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.getWorkOrderType())) {
            query.setParameter(MesPcnExtConstWords.WORK_ORDER_TYPE, model.getWorkOrderType());
        }
    }

}
