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

import cn.estsh.i3plus.ext.mes.apiservice.dao.IOqcCheckDao;
import cn.estsh.i3plus.ext.mes.pojo.model.OqcCheckModel;
import cn.estsh.i3plus.ext.mes.pojo.model.OqcReportParamModel;
import cn.estsh.i3plus.ext.mes.pojo.util.MesExtConstWords;
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 OqcCheckDao implements IOqcCheckDao {

    @Autowired
    private EntityManager entityManager;

    @Override
    public int queryOqcCheckDetailPagerCount(OqcReportParamModel model) {
        StringBuffer hql = new StringBuffer();
        hql.append(" select count(distinct oc.id) ");

        packHqlForQueryOqcCheckDetail(hql, model);

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

        packParameterForQueryOqcCheckDetail(query, model);

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

    @Override
    public List<OqcCheckModel> queryOqcCheckDetailByPager(OqcReportParamModel model, Pager pager) {
        StringBuffer hql = new StringBuffer();
        hql.append(" select new " + OqcCheckModel.class.getName() + "(oc.id, oc.oqcNo, oc.oqcType, oc.orderNo, oc.partNo, " +
                " oc.partName, oc.categoryCode3, oc.serialNumber, oc.workCenterCode, oc.workTime, oc.shiftCode, oc.planQty, oc.spotCheckQty, " +
                " oc.batchLimit, oc.checkLimit, oc.checkResult, oc.checkUser, oc.checkTime, wo.workOrderType) ");

        packHqlForQueryOqcCheckDetail(hql, model);

        hql.append(" order by oc.modifyDatetime desc ");
        Query query = entityManager.createQuery(hql.toString(), OqcCheckModel.class);

        packParameterForQueryOqcCheckDetail(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 packHqlForQueryOqcCheckDetail(StringBuffer hql, OqcReportParamModel model) {
        hql.append(" from MesOqcCheck as oc ");
        hql.append(" left join MesWorkOrder as wo on (oc.orderNo = wo.orderNo and wo.organizeCode = :organizeCode and wo.isValid = :isValid and wo.isDeleted = :isDeleted) ");
        hql.append(" where oc.organizeCode = :organizeCode ");
        hql.append(" and oc.isValid = :isValid ");
        hql.append(" and oc.isDeleted = :isDeleted ");
        if (!StringUtils.isEmpty(model.getWorkOrderNo())) {
            hql.append(" and oc.orderNo = :workOrderNo ");
        }
        if (!StringUtils.isEmpty(model.getPartNo())) {
            hql.append(" and oc.partNo = :partNo ");
        }
        if (!StringUtils.isEmpty(model.getWorkCenterCode())) {
            hql.append(" and oc.workCenterCode = :workCenterCode ");
        }
        if (!StringUtils.isEmpty(model.getSerialNumber())) {
            hql.append(" and oc.serialNumber = :serialNumber ");
        }
        if (!StringUtils.isEmpty(model.getWorkOrderType())) {
            hql.append(" and wo.workOrderType = :workOrderType ");
        }
        if (!StringUtils.isEmpty(model.getWorkTimeStart())) {
            hql.append(" and oc.workTime >= :workTimeStart ");
        }
        if (!StringUtils.isEmpty(model.getWorkTimeEnd())) {
            hql.append(" and oc.workTime <= :workTimeEnd ");
        }
    }

    private void packParameterForQueryOqcCheckDetail(Query query, OqcReportParamModel model) {
        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());
        if (!StringUtils.isEmpty(model.getWorkOrderNo())) {
            query.setParameter(MesExtConstWords.WORK_ORDER_NO, model.getWorkOrderNo());
        }
        if (!StringUtils.isEmpty(model.getPartNo())) {
            query.setParameter(MesExtConstWords.PART_NO, model.getPartNo());
        }
        if (!StringUtils.isEmpty(model.getWorkCenterCode())) {
            query.setParameter(MesExtConstWords.WORK_CENTER_CODE, model.getWorkCenterCode());
        }
        if (!StringUtils.isEmpty(model.getSerialNumber())) {
            query.setParameter(MesExtConstWords.SERIAL_NUMBER, model.getSerialNumber());
        }
        if (!StringUtils.isEmpty(model.getWorkOrderType())) {
            query.setParameter(MesExtConstWords.WORK_ORDER_TYPE, model.getWorkOrderType());
        }
        if (!StringUtils.isEmpty(model.getWorkTimeStart())) {
            query.setParameter(MesExtConstWords.WORK_TIME_START, model.getWorkTimeStart());
        }
        if (!StringUtils.isEmpty(model.getWorkTimeEnd())) {
            query.setParameter(MesExtConstWords.WORK_TIME_END, model.getWorkTimeEnd());
        }
    }

    @Override
    public List<OqcCheckModel> queryOqcCheckListByPart(OqcReportParamModel model) {
        StringBuffer hql = new StringBuffer();
        hql.append(" select new " + OqcCheckModel.class.getName() + "(oc.id, oc.oqcNo, oc.oqcType, oc.partNo, " +
                " oc.serialNumber, oc.workTime, oc.checkResult, pe.categoryCode3, pe.salesTo) ");
        hql.append(" from MesOqcCheck as oc ");
        hql.append(" left join MesPartExt as pe on (oc.partNo = pe.partNo and pe.organizeCode = :organizeCode and pe.isValid = :isValid and pe.isDeleted = :isDeleted) ");
        hql.append(" where oc.organizeCode = :organizeCode ");
        hql.append(" and oc.isValid = :isValid ");
        hql.append(" and oc.isDeleted = :isDeleted ");
        if (!StringUtils.isEmpty(model.getPartNo())) {
            hql.append(" and oc.partNo = :partNo ");
        }
        if (!StringUtils.isEmpty(model.getWorkTimeStart())) {
            hql.append(" and oc.workTime >= :workTimeStart ");
        }
        if (!StringUtils.isEmpty(model.getWorkTimeEnd())) {
            hql.append(" and oc.workTime <= :workTimeEnd ");
        }
        if (!StringUtils.isEmpty(model.getCategoryCode3())) {
            hql.append(" and pe.categoryCode3 = :categoryCode3 ");
        }

        Query query = entityManager.createQuery(hql.toString(), OqcCheckModel.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());
        if (!StringUtils.isEmpty(model.getPartNo())) {
            query.setParameter(MesExtConstWords.PART_NO, model.getPartNo());
        }
        if (!StringUtils.isEmpty(model.getCategoryCode3())) {
            query.setParameter(MesExtConstWords.CATEGORY_CODE3, model.getCategoryCode3());
        }
        if (!StringUtils.isEmpty(model.getWorkTimeStart())) {
            query.setParameter(MesExtConstWords.WORK_TIME_START, model.getWorkTimeStart());
        }
        if (!StringUtils.isEmpty(model.getWorkTimeEnd())) {
            query.setParameter(MesExtConstWords.WORK_TIME_END, model.getWorkTimeEnd());
        }
        return query.getResultList();
    }
}
