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

import cn.estsh.i3plus.ext.mes.apiservice.dao.IExteriorScratchProofDetailDao;
import cn.estsh.i3plus.ext.mes.pojo.bean.MesExteriorScratchProofDetail;
import cn.estsh.i3plus.ext.mes.pojo.model.MesExteriorScratchProofDetailModel;
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 ExteriorScratchProofDetailDao implements IExteriorScratchProofDetailDao {

    @Autowired
    private EntityManager entityManager;

    @Override
    public int queryExteriorScratchProofDetailByPagerCount(MesExteriorScratchProofDetail exteriorScratchProofDetail) {
        StringBuffer hql = new StringBuffer();
        hql.append(" select count(pe.id) ");

        packHqlForQuery(hql, exteriorScratchProofDetail);

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

        packParameterForQuery(query, exteriorScratchProofDetail);

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

    private void packHqlForQuery(StringBuffer hql, MesExteriorScratchProofDetail exteriorScratchProofDetail) {
        hql.append(" from MesExteriorScratchProofDetail as pe ");
        hql.append(" left join MesFile as f on (pe.fileId = f.id and f.organizeCode = :organizeCode and f.isValid = :isValid and f.isDeleted = :isDeleted) ");
        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");

        if (!StringUtils.isEmpty(exteriorScratchProofDetail.getWorkCenterCode())) {
            hql.append(" and pe.workCenterCode = :workCenterCode ");
        }
        if (!StringUtils.isEmpty(exteriorScratchProofDetail.getWorkCellCode())) {
            hql.append(" and pe.workCellCode = :workCellCode ");
        }
        if (!StringUtils.isEmpty(exteriorScratchProofDetail.getShiftGroupCheck())) {
            hql.append(" and pe.shiftGroupCheck = :shiftGroupCheck ");
        }
        if (!StringUtils.isEmpty(exteriorScratchProofDetail.getQualityCheck())) {
            hql.append(" and pe.qualityCheck = :qualityCheck ");
        }
        if (!StringUtils.isEmpty(exteriorScratchProofDetail.getCheckResult())) {
            hql.append(" and pe.checkResult = :checkResult ");
        } else {
            hql.append(" and pe.checkResult in :checkResultList");
        }
        if (!StringUtils.isEmpty(exteriorScratchProofDetail.getCheckStartTime()) && !StringUtils.isEmpty(exteriorScratchProofDetail.getCheckEndTime())) {
            hql.append(" and pe.checkTime >= :checkStartTime");
            hql.append(" and pe.checkTime <= :checkEndTime");
        }

    }

    private void packParameterForQuery(Query query, MesExteriorScratchProofDetail exteriorScratchProofDetail) {
        query.setParameter(MesExtConstWords.ORGANIZE_CODE, exteriorScratchProofDetail.getOrganizeCode());
        query.setParameter(MesExtConstWords.IS_VALID, exteriorScratchProofDetail.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(exteriorScratchProofDetail.getWorkCenterCode())) {
            query.setParameter(MesExtConstWords.WORK_CENTER_CODE, exteriorScratchProofDetail.getWorkCenterCode());
        }
        if (!StringUtils.isEmpty(exteriorScratchProofDetail.getWorkCellCode())) {
            query.setParameter(MesExtConstWords.WORK_CELL_CODE, exteriorScratchProofDetail.getWorkCellCode());
        }
        if (!StringUtils.isEmpty(exteriorScratchProofDetail.getShiftGroupCheck())) {
            query.setParameter(MesExtConstWords.SHIFT_GROUP_CHECK, exteriorScratchProofDetail.getShiftGroupCheck());
        }
        if (!StringUtils.isEmpty(exteriorScratchProofDetail.getQualityCheck())) {
            query.setParameter(MesExtConstWords.QUALITY_CHECK, exteriorScratchProofDetail.getQualityCheck());
        }
        if (!StringUtils.isEmpty(exteriorScratchProofDetail.getCheckResult())) {
            query.setParameter(MesExtConstWords.CHECK_RESULT, exteriorScratchProofDetail.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(exteriorScratchProofDetail.getCheckStartTime()) && !StringUtils.isEmpty(exteriorScratchProofDetail.getCheckEndTime())) {
            query.setParameter(MesExtConstWords.CHECK_START_TIME, exteriorScratchProofDetail.getCheckStartTime());
            query.setParameter(MesExtConstWords.CHECK_END_TIME, exteriorScratchProofDetail.getCheckEndTime());
        }
    }

    @Override
    public List<MesExteriorScratchProofDetailModel> queryExteriorScratchProofDetailByPager(MesExteriorScratchProofDetail exteriorScratchProofDetail, Pager pager) {
        StringBuffer hql = new StringBuffer();
        hql.append(" select new " + MesExteriorScratchProofDetailModel.class.getName() + "(pe.id, pe.workCenterCode, pe.workCellCode, " +
                " pe.orderTime, pe.orderNo, pe.checkResult, pe.checkUser, pe.checkTime, pe.shiftGroupCheck, pe.shiftGroupCheckUser, " +
                " pe.shiftGroupCheckTime, pe.qualityCheck, pe.qualityCheckUser, pe.qualityCheckTime, " +
                " pe.fileId, f.fileName, f.fileUrl, f.groupName, f.fileOriginName, f.fileSize, f.fileType, " +
                " pe.organizeCode, pe.isValid, pe.isDeleted, pe.createUser, pe.createDatetime, pe.modifyUser, pe.modifyDatetime) ");

        packHqlForQuery(hql, exteriorScratchProofDetail);

        hql.append(" order by pe.checkTime desc ");
        Query query = entityManager.createQuery(hql.toString(), MesExteriorScratchProofDetailModel.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();
    }
}
