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

import cn.estsh.i3plus.ext.mes.apiservice.dao.IProjectTourConfirmDao;
import cn.estsh.i3plus.ext.mes.pojo.bean.MesProjectTourDetail;
import cn.estsh.i3plus.ext.mes.pojo.util.MesExtConstWords;
import cn.estsh.i3plus.ext.mes.pojo.util.MesExtEnumUtil;
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.ObjectUtils;
import org.springframework.util.StringUtils;

import javax.persistence.EntityManager;
import javax.persistence.Query;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author: xiangming
 * @CreateDate: 2021-03-12
 * @Description:
 **/
@Service
public class ProjectTourConfirmDao implements IProjectTourConfirmDao {

    @Autowired
    private EntityManager entityManager;

    @Override
    public int queryProjectTourDetailByPagerCount(MesProjectTourDetail projectTourDetail) {
        StringBuffer hql = new StringBuffer();
        hql.append(" select count(distinct pe.id) ");

        packHqlForQuery(hql, projectTourDetail);

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

        packParameterForQuery(query, projectTourDetail);

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

    private void packHqlForQuery(StringBuffer hql, MesProjectTourDetail projectTourDetail) {
        hql.append(" from MesProjectTourDetail as pe ");
        hql.append(" left join MesOrderJob as oj on (pe.orderNo = oj.orderNo and oj.organizeCode = :organizeCode and oj.isValid = :isValid and oj.isDeleted = :isDeleted) ");
        hql.append(" where pe.organizeCode = :organizeCode ");
        hql.append(" and pe.isValid = :isValid ");
        hql.append(" and pe.isDeleted = :isDeleted ");
        hql.append(" and oj.orderStatus in :orderStatusList");
//        hql.append(" and pe.checkResult in :checkResultList");
        if (!StringUtils.isEmpty(projectTourDetail.getWorkCenterCode())) {
            hql.append(" and pe.workCenterCode = :workCenterCode ");
        }
        if (!StringUtils.isEmpty(projectTourDetail.getProdCfgTypeClassify())) {
            hql.append(" and pe.prodCfgTypeClassify = :prodCfgTypeClassify ");
        }
        if (!StringUtils.isEmpty(projectTourDetail.getShiftGroupCheck())) {
            hql.append(" and pe.shiftGroupCheck = :shiftGroupCheck ");
        }
        if (!StringUtils.isEmpty(projectTourDetail.getQualityCheck())) {
            hql.append(" and pe.qualityCheck = :qualityCheck ");
        }
        if (!StringUtils.isEmpty(projectTourDetail.getCheckStartTime()) && !StringUtils.isEmpty(projectTourDetail.getCheckEndTime())) {
            hql.append(" and pe.checkTime >= :checkStartTime");
            hql.append(" and pe.checkTime <= :checkEndTime");
        }
        if (!ObjectUtils.isEmpty(projectTourDetail.getCheckResult())) {
            hql.append(" and pe.checkResult = :checkResult ");
        }
        hql.append(" order by pe.checkTime desc ");
    }

    private void packParameterForQuery(Query query, MesProjectTourDetail projectTourDetail) {
        query.setParameter(MesExtConstWords.ORGANIZE_CODE, projectTourDetail.getOrganizeCode());
        query.setParameter(MesExtConstWords.IS_VALID, projectTourDetail.getIsValid());
        query.setParameter(MesExtConstWords.IS_DELETED, CommonEnumUtil.TRUE_OR_FALSE.FALSE.getValue());
        List<Integer> orderStatusList = new ArrayList<>(2);
        orderStatusList.add(MesExtEnumUtil.MES_ORJ_ORDER_STATUS.CHECK_COMPLETE.getValue());
        orderStatusList.add(MesExtEnumUtil.MES_ORJ_ORDER_STATUS.CLOSE.getValue());
        query.setParameter("orderStatusList", orderStatusList);

        if (!StringUtils.isEmpty(projectTourDetail.getWorkCenterCode())) {
            query.setParameter(MesExtConstWords.WORK_CENTER_CODE, projectTourDetail.getWorkCenterCode());
        }
        if (!StringUtils.isEmpty(projectTourDetail.getProdCfgTypeClassify())) {
            query.setParameter(MesExtConstWords.PROD_CFG_TYPE_CLASSIFY, projectTourDetail.getProdCfgTypeClassify());
        }
        if (!StringUtils.isEmpty(projectTourDetail.getShiftGroupCheck())) {
            query.setParameter(MesExtConstWords.SHIFT_GROUP_CHECK, projectTourDetail.getShiftGroupCheck());
        }
        if (!StringUtils.isEmpty(projectTourDetail.getQualityCheck())) {
            query.setParameter(MesExtConstWords.QUALITY_CHECK, projectTourDetail.getQualityCheck());
        }
        if (!StringUtils.isEmpty(projectTourDetail.getCheckStartTime()) && !StringUtils.isEmpty(projectTourDetail.getCheckEndTime())) {
            query.setParameter(MesExtConstWords.CHECK_START_TIME, projectTourDetail.getCheckStartTime());
            query.setParameter(MesExtConstWords.CHECK_END_TIME, projectTourDetail.getCheckEndTime());
        }
        if (!ObjectUtils.isEmpty(projectTourDetail.getCheckResult())) {
            query.setParameter("checkResult", projectTourDetail.getCheckResult());
        }
    }

    @Override
    public List<MesProjectTourDetail> queryProjectTourDetailByPager(MesProjectTourDetail exteriorScratchProofDetail, Pager pager) {
        StringBuffer hql = new StringBuffer();
        hql.append(" select distinct pe");

        packHqlForQuery(hql, exteriorScratchProofDetail);

        //hql.append(" order by pe.createDatetime,pe.orderNo desc ");
        Query query = entityManager.createQuery(hql.toString(), MesProjectTourDetail.class);

        packParameterForQuery(query, exteriorScratchProofDetail);

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