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

import cn.estsh.i3plus.ext.mes.apiservice.dao.IEquipmentCheckDetailDao;
import cn.estsh.i3plus.ext.mes.apiservice.dao.ep.IEpEquipmentCheckDetailDao;
import cn.estsh.i3plus.ext.mes.pojo.bean.MesEpEquipmentCheckDetail;
import cn.estsh.i3plus.ext.mes.pojo.bean.MesEquipmentCheckDetail;
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.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 EpEquipmentCheckDetailDao implements IEpEquipmentCheckDetailDao {
    @Autowired
    private EntityManager entityManager;

    @Override
    public List<MesEpEquipmentCheckDetail> queryEquipmentCheckDetailByPager(MesEpEquipmentCheckDetail equipmentCheckDetail, Pager pager) {
        StringBuffer hql = new StringBuffer();
        hql.append(" select ecd");

        packHqlForQuery(hql, equipmentCheckDetail);

        hql.append(" order by ecd.spotCheckTime desc ");
        Query query = entityManager.createQuery(hql.toString(), MesEpEquipmentCheckDetail.class);

        packParameterForQuery(query, equipmentCheckDetail);

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

    @Override
    public int queryEquipmentCheckDetailCount(MesEpEquipmentCheckDetail equipmentCheckDetail) {
        StringBuffer hql = new StringBuffer();
        hql.append(" select count(distinct ecd.id) ");

        packHqlForQuery(hql, equipmentCheckDetail);

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

        packParameterForQuery(query, equipmentCheckDetail);

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

    private void packHqlForQuery(StringBuffer hql, MesEpEquipmentCheckDetail equipmentCheckDetail) {
        hql.append(" from MesEpEquipmentCheckDetail as ecd ");
        hql.append(" left join MesOrderJob as oj on (ecd.orderJobNo = oj.orderNo and oj.organizeCode = :organizeCode and oj.isValid = :isValid and oj.isDeleted = :isDeleted) ");
        hql.append(" where ecd.organizeCode = :organizeCode ");
        hql.append(" and ecd.isValid = :isValid ");
        hql.append(" and ecd.isDeleted = :isDeleted ");
        hql.append(" and oj.orderStatus in :orderStatus ");
        if (!StringUtils.isEmpty(equipmentCheckDetail.getOrderJobNo())) {
            hql.append(" and ecd.orderJobNo like :orderJobNo ");
        }
        if (!StringUtils.isEmpty(equipmentCheckDetail.getEquNo())) {
            hql.append(" and ecd.equNo like :equNo ");
        }
        if (!StringUtils.isEmpty(equipmentCheckDetail.getWorkCenterCode())) {
            hql.append(" and ecd.workCenterCode = :workCenterCode ");
        }
        if (!StringUtils.isEmpty(equipmentCheckDetail.getWorkCellCode())) {
            hql.append(" and ecd.workCellCode = :workCellCode ");
        }
        if (!StringUtils.isEmpty(equipmentCheckDetail.getCheckType())) {
            hql.append(" and ecd.checkType = :checkType ");
        }
        if (!StringUtils.isEmpty(equipmentCheckDetail.getIsQualityConfirm())) {
            hql.append(" and ecd.isQualityConfirm = :isQualityConfirm ");
        }
        if (!StringUtils.isEmpty(equipmentCheckDetail.getIsTeamConfirm())) {
            hql.append(" and ecd.isTeamConfirm = :isTeamConfirm ");
        }
        if (!StringUtils.isEmpty(equipmentCheckDetail.getCheckResult())) {
            hql.append(" and ecd.checkResult = :checkResult ");
        } else {
            hql.append(" and ecd.checkResult in :checkResult ");
        }
        if (!StringUtils.isEmpty(equipmentCheckDetail.getCreateDateTimeStart())) {
            hql.append(" and ecd.spotCheckTime >= :spotCheckTimeStart ");
        }
        if (!StringUtils.isEmpty(equipmentCheckDetail.getCreateDateTimeEnd())) {
            hql.append(" and ecd.spotCheckTime <= :spotCheckTimeEnd ");
        }
    }

    private void packParameterForQuery(Query query, MesEpEquipmentCheckDetail equipmentCheckDetail) {
        query.setParameter(MesExtConstWords.ORGANIZE_CODE, equipmentCheckDetail.getOrganizeCode());
        query.setParameter(MesExtConstWords.IS_VALID, CommonEnumUtil.IS_VAILD.VAILD.getValue());
        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(MesExtConstWords.ORDER_STATUS, orderStatusList);
        if (!StringUtils.isEmpty(equipmentCheckDetail.getOrderJobNo())) {
            query.setParameter(MesExtConstWords.ORDER_JOB_NO, "%" + equipmentCheckDetail.getOrderJobNo() + "%");
        }
        if (!StringUtils.isEmpty(equipmentCheckDetail.getEquNo())) {
            query.setParameter(MesExtConstWords.EQU_NO, "%" + equipmentCheckDetail.getEquNo() + "%");
        }
        if (!StringUtils.isEmpty(equipmentCheckDetail.getWorkCenterCode())) {
            query.setParameter(MesExtConstWords.WORK_CENTER_CODE, equipmentCheckDetail.getWorkCenterCode());
        }
        if (!StringUtils.isEmpty(equipmentCheckDetail.getWorkCellCode())) {
            query.setParameter(MesExtConstWords.WORK_CELL_CODE, equipmentCheckDetail.getWorkCellCode());
        }
        if (!StringUtils.isEmpty(equipmentCheckDetail.getCheckType())) {
            query.setParameter(MesExtConstWords.CHECK_TYPE, equipmentCheckDetail.getCheckType());
        }
        if (!StringUtils.isEmpty(equipmentCheckDetail.getIsQualityConfirm())) {
            query.setParameter(MesExtConstWords.IS_QUALITY_CONFIRM, equipmentCheckDetail.getIsQualityConfirm());
        }
        if (!StringUtils.isEmpty(equipmentCheckDetail.getIsTeamConfirm())) {
            query.setParameter(MesExtConstWords.IS_TEAM_CONFIRM, equipmentCheckDetail.getIsTeamConfirm());
        }
        if (!StringUtils.isEmpty(equipmentCheckDetail.getCheckResult())) {
            query.setParameter(MesExtConstWords.CHECK_RESULT, equipmentCheckDetail.getCheckResult());
        } else {
            List<Integer> checkResultList = new ArrayList<>(3);
            checkResultList.add(MesExtEnumUtil.EQU_CHECK_RESULT.QUALIFIED.getValue());
            checkResultList.add(MesExtEnumUtil.EQU_CHECK_RESULT.EQUIPMENT_ABNORMAL.getValue());
            checkResultList.add(MesExtEnumUtil.EQU_CHECK_RESULT.EQUIPMENT_FAILURE.getValue());
            query.setParameter(MesExtConstWords.CHECK_RESULT, checkResultList);
        }
        if (!StringUtils.isEmpty(equipmentCheckDetail.getCreateDateTimeStart())) {
            query.setParameter(MesExtConstWords.SPOT_CHECK_TIME_START, equipmentCheckDetail.getCreateDateTimeStart());
        }
        if (!StringUtils.isEmpty(equipmentCheckDetail.getCreateDateTimeEnd())) {
            query.setParameter(MesExtConstWords.SPOT_CHECK_TIME_END, equipmentCheckDetail.getCreateDateTimeEnd());
        }

    }
}
