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

import cn.estsh.i3plus.ext.mes.apiservice.dao.IConsistencyAffirmRecordDao;
import cn.estsh.i3plus.ext.mes.pojo.bean.MesConsistencyAffirmRecord;
import cn.estsh.i3plus.ext.mes.pojo.model.EsopExtModel;
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 ConsistencyAffirmRecordDao implements IConsistencyAffirmRecordDao {
    @Autowired
    private EntityManager entityManager;

    @Override
    public List<MesConsistencyAffirmRecord> queryConsistencyAffirmRecordByPager(MesConsistencyAffirmRecord consistencyAffirmRecord, Pager pager) {
        StringBuffer hql = new StringBuffer();
        hql.append(" select car");

        packHqlForQuery(hql, consistencyAffirmRecord);

        hql.append(" order by car.checkTime desc ");
        Query query = entityManager.createQuery(hql.toString(), MesConsistencyAffirmRecord.class);

        packParameterForQuery(query, consistencyAffirmRecord);

        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 queryConsistencyAffirmRecordCount(MesConsistencyAffirmRecord consistencyAffirmRecord) {
        StringBuffer hql = new StringBuffer();
        hql.append(" select count(distinct car.id) ");

        packHqlForQuery(hql, consistencyAffirmRecord);

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

        packParameterForQuery(query, consistencyAffirmRecord);

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

    private void packHqlForQuery(StringBuffer hql, MesConsistencyAffirmRecord consistencyAffirmRecord) {
        hql.append(" from MesConsistencyAffirmRecord as car ");
        hql.append(" left join MesOrderJob as oj on (car.orderNo = oj.orderNo and oj.organizeCode = :organizeCode and oj.isValid = :isValid and oj.isDeleted = :isDeleted) ");
        hql.append(" where car.organizeCode = :organizeCode ");
        hql.append(" and car.isValid = :isValid ");
        hql.append(" and car.isDeleted = :isDeleted ");
        hql.append(" and oj.orderStatus in :orderStatusList");
        if (!StringUtils.isEmpty(consistencyAffirmRecord.getWorkOrderNo())) {
            hql.append(" and car.workOrderNo like :workOrderNo ");
        }
        if (!StringUtils.isEmpty(consistencyAffirmRecord.getPartNo())) {
            hql.append(" and car.partNo = :partNo ");
        }
        if (!StringUtils.isEmpty(consistencyAffirmRecord.getShiftGroupCheck())) {
            hql.append(" and car.shiftGroupCheck = :shiftGroupCheck ");
        }
        if (!StringUtils.isEmpty(consistencyAffirmRecord.getQualityCheck())) {
            hql.append(" and car.qualityCheck = :qualityCheck ");
        }
        if (!StringUtils.isEmpty(consistencyAffirmRecord.getCheckResult())) {
            hql.append(" and car.checkResult = :checkResult ");
        } else {
            hql.append(" and car.checkResult in :checkResultList");
        }
        if (!StringUtils.isEmpty(consistencyAffirmRecord.getCheckStartTime())) {
            hql.append(" and car.checkTime >= :checkStartTime ");
        }
        if (!StringUtils.isEmpty(consistencyAffirmRecord.getCheckEndTime())) {
            hql.append(" and car.checkTime <= :checkEndTime ");
        }
    }

    private void packParameterForQuery(Query query, MesConsistencyAffirmRecord consistencyAffirmRecord) {
        query.setParameter(MesExtConstWords.ORGANIZE_CODE, consistencyAffirmRecord.getOrganizeCode());
        query.setParameter(MesExtConstWords.IS_VALID, consistencyAffirmRecord.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(consistencyAffirmRecord.getWorkOrderNo())) {
            query.setParameter(MesExtConstWords.WORK_ORDER_NO, "%" + consistencyAffirmRecord.getWorkOrderNo() + "%");
        }
        if (!StringUtils.isEmpty(consistencyAffirmRecord.getPartNo())) {
            query.setParameter(MesExtConstWords.PART_NO, consistencyAffirmRecord.getPartNo());
        }
        if (!StringUtils.isEmpty(consistencyAffirmRecord.getShiftGroupCheck())) {
            query.setParameter(MesExtConstWords.SHIFT_GROUP_CHECK, consistencyAffirmRecord.getShiftGroupCheck());
        }
        if (!StringUtils.isEmpty(consistencyAffirmRecord.getQualityCheck())) {
            query.setParameter(MesExtConstWords.QUALITY_CHECK, consistencyAffirmRecord.getQualityCheck());
        }
        if (!StringUtils.isEmpty(consistencyAffirmRecord.getCheckResult())) {
            query.setParameter(MesExtConstWords.CHECK_RESULT, consistencyAffirmRecord.getCheckResult());
        } else {
            List<Integer> checkResultList = new ArrayList<>(2);
            checkResultList.add(MesExtEnumUtil.CHECK_RESULT.OK.getValue());
            checkResultList.add(MesExtEnumUtil.CHECK_RESULT.NG.getValue());
            query.setParameter("checkResultList", checkResultList);
        }
        if (!StringUtils.isEmpty(consistencyAffirmRecord.getCheckStartTime())) {
            query.setParameter(MesExtConstWords.CHECK_START_TIME, consistencyAffirmRecord.getCheckStartTime());
        }
        if (!StringUtils.isEmpty(consistencyAffirmRecord.getCheckEndTime())) {
            query.setParameter(MesExtConstWords.CHECK_END_TIME, consistencyAffirmRecord.getCheckEndTime());
        }
    }
}
