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

import cn.estsh.i3plus.ext.mes.apiservice.dao.bu3.ISxTrendAnalysisDao;
import cn.estsh.i3plus.ext.mes.pojo.bean.MesEquipmentCheckDetail;
import cn.estsh.i3plus.ext.mes.pojo.bean.MesFirstOrderDetail;
import cn.estsh.i3plus.ext.mes.pojo.bean.MesQManageTaskDetail;
import cn.estsh.i3plus.ext.mes.pojo.model.EsopExtModel;
import cn.estsh.i3plus.ext.mes.pojo.model.SxPackSnReportModel;
import cn.estsh.i3plus.ext.mes.pojo.model.bu3.SxEquipModel;
import cn.estsh.i3plus.ext.mes.pojo.model.bu3.SxTrendAnalysisModel;
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.math.BigInteger;
import java.util.ArrayList;
import java.util.List;

/**
 * @PROJECT_NAME: i3plus-mes-panasonic-dev
 * @DESCRIPTION:
 * @USER: xinwang.yi
 * @DATE: 2023-07-06 15:43
 */
@Service
public class SxTrendAnalysisDao implements ISxTrendAnalysisDao {

    @Autowired
    private EntityManager entityManager;

    @Override
    public List<MesQManageTaskDetail> queryQManageTaskDetailList(SxTrendAnalysisModel mesQmanageTaskDetail) {
        String hql = "select  new " + MesQManageTaskDetail.class.getName() +
                "(qdetail.id,worder.orderNo,qorder.workCenterCode,qdetail.workCellCode,qdetail.workCellName," +
                "qorder.shiftCode,worder.partNo,qdetail.processName,qdetail.manageProjects,qdetail.screwType,qdetail.screwCutterNo," +
                "qdetail.qty,qdetail.tighteningTorque,qdetail.upLimit,qdetail.downLimit,qdetail.manualTightening,qdetail.isIntact,qdetail.spotCheckUser,qdetail.spotCheckTime,qdetail.teamConfirmUser," +
                "qdetail.teamConfirmTime,qdetail.qualityConfirmUser,qdetail.qualityConfirmTime,qdetail.isTeamConfirm,qdetail.isQualityConfirm,qorder.serialNumber, qdetail.checkType)" +
                " from MesQManageTaskDetail qdetail" +
                " LEFT JOIN MesOrderJob qorder on qdetail.orderNo = qorder.orderNo" +
                " and qdetail.organizeCode = qorder.organizeCode and qdetail.isValid = qorder.isValid " +
                " and qdetail.isDeleted = qorder.isDeleted " +
                " LEFT JOIN MesWorkOrder worder on qorder.workOrderNo = worder.orderNo" +
                " and qorder.organizeCode = worder.organizeCode and qorder.isValid = worder.isValid " +
                " and qorder.isDeleted = worder.isDeleted " +
                " where qdetail.organizeCode = :organizeCode " +
                " and qdetail.isValid = :isValid and qdetail.isDeleted = :isDeleted and qorder.orderStatus in ( :orderStatusList )";

        if(!StringUtils.isEmpty(mesQmanageTaskDetail.getWorkCenterCode())){
            hql += " and qorder.workCenterCode = :workCenterCode ";
        }

        if(!StringUtils.isEmpty(mesQmanageTaskDetail.getProcessCode())){
            hql += " and qdetail.processCode = :processCode ";
        }

        if (!StringUtils.isEmpty(mesQmanageTaskDetail.getSpotCheckTimeStart())) {
            hql += " and qdetail.spotCheckTime >= :spotCheckTimeStart ";
        }

        if (!StringUtils.isEmpty(mesQmanageTaskDetail.getSpotCheckTimeEnd())) {
            hql += " and qdetail.spotCheckTime <= :spotCheckTimeEnd ";
        }

        if (!StringUtils.isEmpty(mesQmanageTaskDetail.getManageProjects())) {
            hql += " and qdetail.manageProjects = :manageProjects ";
        }

        List<Integer> orderStatusList = new ArrayList<>();
        orderStatusList.add(MesExtEnumUtil.MES_ORJ_ORDER_STATUS.CHECK_COMPLETE.getValue());
        orderStatusList.add(MesExtEnumUtil.MES_ORJ_ORDER_STATUS.CLOSE.getValue());
        Query queryObject = entityManager.createQuery(hql);
        queryObject.setParameter("organizeCode", mesQmanageTaskDetail.getOrganizeCode());
        queryObject.setParameter("isDeleted", CommonEnumUtil.TRUE_OR_FALSE.FALSE.getValue());
        queryObject.setParameter("isValid", CommonEnumUtil.TRUE_OR_FALSE.TRUE.getValue());
        queryObject.setParameter("orderStatusList", orderStatusList);

        if(!StringUtils.isEmpty(mesQmanageTaskDetail.getWorkCenterCode())){
            queryObject.setParameter("workCenterCode", mesQmanageTaskDetail.getWorkCenterCode());
        }

        if(!StringUtils.isEmpty(mesQmanageTaskDetail.getProcessCode())){
            queryObject.setParameter("processCode", mesQmanageTaskDetail.getProcessCode());
        }

        if (!StringUtils.isEmpty(mesQmanageTaskDetail.getSpotCheckTimeStart())) {
            queryObject.setParameter("spotCheckTimeStart", mesQmanageTaskDetail.getSpotCheckTimeStart());
        }

        if (!StringUtils.isEmpty(mesQmanageTaskDetail.getSpotCheckTimeEnd())) {
            queryObject.setParameter("spotCheckTimeEnd", mesQmanageTaskDetail.getSpotCheckTimeEnd());
        }

        if (!StringUtils.isEmpty(mesQmanageTaskDetail.getManageProjects())) {
            queryObject.setParameter("manageProjects", mesQmanageTaskDetail.getManageProjects());
        }

        return queryObject.getResultList();
    }

    @Override
    public List<MesFirstOrderDetail> queryFirstOrderDetailList(SxTrendAnalysisModel mesFirstOrderDetail) {
        String hql = "select  new " + MesFirstOrderDetail.class.getName() +
                "(fdetail.id,forder.workOrderNo,forder.workCenterCode,forder.serialNumber,fdetail.workCellName," +
                "forder.shiftCode,fdetail.processCode,fdetail.processName,fdetail.checkMessage,fdetail.checkStand,fdetail.checkValue," +
                "fdetail.checkResult,fdetail.spotCheckUser,fdetail.spotCheckTime,fdetail.teamConfirmUser," +
                "fdetail.teamConfirmTime,fdetail.qualityConfirmUser,fdetail.qualityConfirmTime,fdetail.orderNo,forder.createDatetime," +
                "fdetail.isQualityConfirm,fdetail.isTeamConfirm,fdetail.upLimit,fdetail.downLimit,fdetail.resultValue,worder.partNo)" +
                " from MesFirstOrderDetail fdetail" +
                " INNER JOIN MesOrderJob forder on fdetail.orderNo = forder.orderNo" +
                " and fdetail.organizeCode = forder.organizeCode and fdetail.isValid = forder.isValid " +
                " and fdetail.isDeleted = forder.isDeleted " +
                " INNER JOIN MesWorkOrder worder on forder.workOrderNo = worder.orderNo" +
                " and forder.organizeCode = worder.organizeCode and forder.isValid = worder.isValid " +
                " and forder.isDeleted = worder.isDeleted " +
                " where fdetail.organizeCode = :organizeCode " +
                " and fdetail.isValid = :isValid and fdetail.isDeleted = :isDeleted and forder.orderStatus in ( :orderStatusList )";

        if(!StringUtils.isEmpty(mesFirstOrderDetail.getProcessCode())){
            hql += " and fdetail.processCode = :processCode ";
        }

        if(!StringUtils.isEmpty(mesFirstOrderDetail.getManageProjects())){
            hql += " and fdetail.checkMessage = :checkMessage ";
        }

        if (!StringUtils.isEmpty(mesFirstOrderDetail.getSpotCheckTimeStart())) {
            hql += " and fdetail.spotCheckTime >= :spotCheckTimeStart ";
        }

        if (!StringUtils.isEmpty(mesFirstOrderDetail.getSpotCheckTimeEnd())) {
            hql += " and fdetail.spotCheckTime <= :spotCheckTimeEnd ";
        }

        if(!StringUtils.isEmpty(mesFirstOrderDetail.getWorkCenterCode())){
            hql += " and forder.workCenterCode = :workCenterCode ";
        }

        if(!StringUtils.isEmpty(mesFirstOrderDetail.getPartNo())){
            hql += " and worder.partNo = :partNo ";
        }

        List<Integer> orderStatusList = new ArrayList<>();
        orderStatusList.add(MesExtEnumUtil.MES_ORJ_ORDER_STATUS.CHECK_COMPLETE.getValue());
        orderStatusList.add(MesExtEnumUtil.MES_ORJ_ORDER_STATUS.CLOSE.getValue());
        Query queryObject = entityManager.createQuery(hql);
        queryObject.setParameter(MesExtConstWords.ORGANIZE_CODE, mesFirstOrderDetail.getOrganizeCode());
        queryObject.setParameter(MesExtConstWords.IS_DELETED, CommonEnumUtil.TRUE_OR_FALSE.FALSE.getValue());
        queryObject.setParameter(MesExtConstWords.IS_VALID, CommonEnumUtil.TRUE_OR_FALSE.TRUE.getValue());
        queryObject.setParameter("orderStatusList", orderStatusList);

        if(!StringUtils.isEmpty(mesFirstOrderDetail.getProcessCode())){
            queryObject.setParameter(MesExtConstWords.PROCESS_CODE, mesFirstOrderDetail.getProcessCode());
        }

        if (!StringUtils.isEmpty(mesFirstOrderDetail.getSpotCheckTimeStart())) {
            queryObject.setParameter(MesExtConstWords.SPOT_CHECK_TIME_START, mesFirstOrderDetail.getSpotCheckTimeStart());
        }

        if (!StringUtils.isEmpty(mesFirstOrderDetail.getSpotCheckTimeEnd())) {
            queryObject.setParameter(MesExtConstWords.SPOT_CHECK_TIME_END, mesFirstOrderDetail.getSpotCheckTimeEnd());
        }

        if(!StringUtils.isEmpty(mesFirstOrderDetail.getManageProjects())){
            queryObject.setParameter(MesExtConstWords.CHECK_MESSAGE, mesFirstOrderDetail.getManageProjects());
        }

        if(!StringUtils.isEmpty(mesFirstOrderDetail.getWorkCenterCode())){
            queryObject.setParameter(MesExtConstWords.WORK_CENTER_CODE, mesFirstOrderDetail.getWorkCenterCode());
        }

        if(!StringUtils.isEmpty(mesFirstOrderDetail.getPartNo())){
            queryObject.setParameter(MesExtConstWords.PART_NO, mesFirstOrderDetail.getPartNo());
        }

        return queryObject.getResultList();
    }

    @Override
    public List<MesEquipmentCheckDetail> queryEquipmentCheckDetailList(SxTrendAnalysisModel equipmentCheckDetail) {
        StringBuffer hql = new StringBuffer();
        hql.append(" select ecd");

        hql.append(" from MesEquipmentCheckDetail 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.getEquNo())) {
            hql.append(" and ecd.equNo = :equNo ");
        }
        if (!StringUtils.isEmpty(equipmentCheckDetail.getWorkCenterCode())) {
            hql.append(" and ecd.workCenterCode = :workCenterCode ");
        }
        if (!StringUtils.isEmpty(equipmentCheckDetail.getWorkCellCode())) {
            hql.append(" and ecd.workCellCode = :workCellCode ");
        }

        //hql.append(" and ecd.checkResult in :checkResult ");

        if (!StringUtils.isEmpty(equipmentCheckDetail.getManageProjects())) {
            hql.append(" and ecd.projectContent = :projectContent ");
        }
        if (!StringUtils.isEmpty(equipmentCheckDetail.getSpotCheckTimeStart())) {
            hql.append(" and ecd.spotCheckTime >= :spotCheckTimeStart ");
        }
        if (!StringUtils.isEmpty(equipmentCheckDetail.getSpotCheckTimeEnd())) {
            hql.append(" and ecd.spotCheckTime <= :spotCheckTimeEnd ");
        }
        Query query = entityManager.createQuery(hql.toString(), MesEquipmentCheckDetail.class);

        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.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());
        }
        //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.getManageProjects())) {
            query.setParameter(MesExtConstWords.PROJECT_CONTENT, equipmentCheckDetail.getManageProjects());
        }
        if (!StringUtils.isEmpty(equipmentCheckDetail.getSpotCheckTimeStart())) {
            query.setParameter(MesExtConstWords.SPOT_CHECK_TIME_START, equipmentCheckDetail.getSpotCheckTimeStart());
        }

        if (!StringUtils.isEmpty(equipmentCheckDetail.getSpotCheckTimeEnd())) {
            query.setParameter(MesExtConstWords.SPOT_CHECK_TIME_END, equipmentCheckDetail.getSpotCheckTimeEnd());
        }
        return query.getResultList();
    }

    @Override
    public int queryQManageProjectsListPagerCount(String manageProjects, String processCode, String partNo, String organizeCode) {
        StringBuffer hql = new StringBuffer();
        hql.append(" select count( DISTINCT manageProjects ) ");

        packHqlForQueryQManageProjects(hql, manageProjects, processCode, partNo);

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

        packParameterForQueryQManageProjects(query, manageProjects, processCode, partNo, organizeCode);

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

    @Override
    public List<String> queryQManageProjectsListPager(String manageProjects, String processCode, String partNo, String organizeCode, Pager pager) {
        StringBuffer hql = new StringBuffer();
        hql.append(" select DISTINCT manageProjects ");

        packHqlForQueryQManageProjects(hql, manageProjects, processCode, partNo);

        hql.append(" order by manageProjects ");
        Query query = entityManager.createQuery(hql.toString(), String.class);

        packParameterForQueryQManageProjects(query, manageProjects, processCode, partNo, organizeCode);

        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 queryFirstOrderCheckMessageListPagerCount(String manageProjects, String processCode, String partNo, String organizeCode) {
        StringBuffer hql = new StringBuffer();
        hql.append(" select count( DISTINCT checkMessage ) ");

        packHqlForQueryFirstOrderCheckMessage(hql, manageProjects, processCode, partNo);

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

        packParameterForQueryFirstOrderCheckMessage(query, manageProjects, processCode, partNo, organizeCode);

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

    @Override
    public List<String> queryFirstOrderCheckMessageListPager(String manageProjects, String processCode, String partNo, String organizeCode, Pager pager) {
        StringBuffer hql = new StringBuffer();
        hql.append(" select DISTINCT checkMessage ");

        packHqlForQueryFirstOrderCheckMessage(hql, manageProjects, processCode, partNo);

        hql.append(" order by checkMessage ");
        Query query = entityManager.createQuery(hql.toString(), String.class);

        packParameterForQueryFirstOrderCheckMessage(query, manageProjects, processCode, partNo, organizeCode);

        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 queryEquipCheckProjectContentPagerCount(String manageProjects, String workCenterCode, String workCellCode, String equNo, String organizeCode) {
        StringBuffer hql = new StringBuffer();
        hql.append(" select count( DISTINCT projectContent ) ");

        packHqlForQueryEquipCheckProjectContent(hql, manageProjects, workCenterCode, workCellCode, equNo);

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

        packParameterForQueryEquipCheckProjectContent(query, manageProjects, workCenterCode, workCellCode, equNo, organizeCode);

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

    @Override
    public List<String> queryEquipCheckProjectContentListPager(String manageProjects, String workCenterCode, String workCellCode, String equNo, String organizeCode, Pager pager) {
        StringBuffer hql = new StringBuffer();
        hql.append(" select DISTINCT projectContent ");

        packHqlForQueryEquipCheckProjectContent(hql, manageProjects, workCenterCode, workCellCode, equNo);

        hql.append(" order by projectContent ");
        Query query = entityManager.createQuery(hql.toString(), String.class);

        packParameterForQueryEquipCheckProjectContent(query, manageProjects, workCenterCode, workCellCode, equNo, organizeCode);

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

    @Override
    public BigInteger queryEquipListPagerCount(String equNo, String equName, String organizeCode) {
        StringBuffer hql = new StringBuffer();
        hql.append(" select count( DISTINCT CONCAT(equ_No, equ_name)) ");

        packHqlForNativeQueryEquip(hql, equNo, equName);

        Query query = entityManager.createNativeQuery(hql.toString());

        packParameterForNativeQueryEquip(query, equNo, equName, organizeCode);

        return (BigInteger)query.getSingleResult();
    }

    @Override
    public List<SxEquipModel> queryEquipListPager(String equNo, String equName, String organizeCode, Pager pager) {
        StringBuffer hql = new StringBuffer();
        hql.append(" select DISTINCT new " + SxEquipModel.class.getName() + "( equNo, equName ) ");

        packHqlForQueryEquip(hql, equNo, equName);

        hql.append(" order by equNo ");
        Query query = entityManager.createQuery(hql.toString(), SxEquipModel.class);

        packParameterForQueryEquip(query, equNo, equName, organizeCode);

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

        return query.getResultList();
    }

    private void packHqlForQueryEquipCheckProjectContent(StringBuffer hql, String manageProjects, String workCenterCode, String workCellCode, String equNo) {
        hql.append(" from MesEquipmentCheck ");
        hql.append(" where organizeCode = :organizeCode ");
        hql.append(" and isValid = :isValid ");
        hql.append(" and isDeleted = :isDeleted ");
        if (!StringUtils.isEmpty(manageProjects)) {
            hql.append(" and projectContent like :projectContent ");
        }
        if (!StringUtils.isEmpty(workCenterCode)) {
            hql.append(" and workCenterCode = :workCenterCode ");
        }
        if (!StringUtils.isEmpty(workCellCode)) {
            hql.append(" and workCellCode = :workCellCode ");
        }
        if (!StringUtils.isEmpty(equNo)) {
            hql.append(" and equNo = :equNo ");
        }
    }

    private void packParameterForQueryEquipCheckProjectContent(Query query, String manageProjects, String workCenterCode, String workCellCode, String equNo, String organizeCode) {
        query.setParameter(MesExtConstWords.ORGANIZE_CODE, organizeCode);
        query.setParameter(MesExtConstWords.IS_VALID, CommonEnumUtil.IS_VAILD.VAILD.getValue());
        query.setParameter(MesExtConstWords.IS_DELETED, CommonEnumUtil.TRUE_OR_FALSE.FALSE.getValue());
        if (!StringUtils.isEmpty(manageProjects)) {
            query.setParameter(MesExtConstWords.PROJECT_CONTENT, "%" + manageProjects + "%");
        }
        if (!StringUtils.isEmpty(workCenterCode)) {
            query.setParameter(MesExtConstWords.WORK_CENTER_CODE, workCenterCode);
        }
        if (!StringUtils.isEmpty(workCellCode)) {
            query.setParameter(MesExtConstWords.WORK_CELL_CODE, workCellCode);
        }
        if (!StringUtils.isEmpty(equNo)) {
            query.setParameter(MesExtConstWords.EQU_NO, equNo);
        }
    }

    private void packHqlForQueryFirstOrderCheckMessage(StringBuffer hql, String manageProjects, String processCode, String partNo) {
        hql.append(" from MesProcessFirstItem ");
        hql.append(" where organizeCode = :organizeCode ");
        hql.append(" and isValid = :isValid ");
        hql.append(" and isDeleted = :isDeleted ");
        if (!StringUtils.isEmpty(manageProjects)) {
            hql.append(" and checkMessage like :checkMessage ");
        }
        if (!StringUtils.isEmpty(processCode)) {
            hql.append(" and processCode = :processCode ");
        }
        if (!StringUtils.isEmpty(partNo)) {
            hql.append(" and partNo = :partNo ");
        }
    }

    private void packParameterForQueryFirstOrderCheckMessage(Query query, String manageProjects, String processCode, String partNo, String organizeCode) {
        query.setParameter(MesExtConstWords.ORGANIZE_CODE, organizeCode);
        query.setParameter(MesExtConstWords.IS_VALID, CommonEnumUtil.IS_VAILD.VAILD.getValue());
        query.setParameter(MesExtConstWords.IS_DELETED, CommonEnumUtil.TRUE_OR_FALSE.FALSE.getValue());
        if (!StringUtils.isEmpty(manageProjects)) {
            query.setParameter(MesExtConstWords.CHECK_MESSAGE, "%" + manageProjects + "%");
        }

        if (!StringUtils.isEmpty(processCode)) {
            query.setParameter(MesExtConstWords.PROCESS_CODE,  processCode);
        }

        if (!StringUtils.isEmpty(partNo)) {
            query.setParameter(MesExtConstWords.PART_NO,  partNo);
        }
    }

    private void packHqlForQueryQManageProjects(StringBuffer hql, String manageProjects, String processCode, String partNo) {
        hql.append(" from MesProcessQManage ");
        hql.append(" where organizeCode = :organizeCode ");
        hql.append(" and isValid = :isValid ");
        hql.append(" and isDeleted = :isDeleted ");
        if (!StringUtils.isEmpty(manageProjects)) {
            hql.append(" and manageProjects like :manageProjects ");
        }
        if (!StringUtils.isEmpty(processCode)) {
            hql.append(" and processCode = :processCode ");
        }
        if (!StringUtils.isEmpty(partNo)) {
            hql.append(" and partNo = :partNo ");
        }
    }

    private void packParameterForQueryQManageProjects(Query query, String manageProjects, String processCode, String partNo, String organizeCode) {
        query.setParameter(MesExtConstWords.ORGANIZE_CODE, organizeCode);
        query.setParameter(MesExtConstWords.IS_VALID, CommonEnumUtil.IS_VAILD.VAILD.getValue());
        query.setParameter(MesExtConstWords.IS_DELETED, CommonEnumUtil.TRUE_OR_FALSE.FALSE.getValue());
        if (!StringUtils.isEmpty(manageProjects)) {
            query.setParameter(MesExtConstWords.MANAGE_PROJECTS, "%" + manageProjects + "%");
        }
        if (!StringUtils.isEmpty(processCode)) {
            query.setParameter(MesExtConstWords.PROCESS_CODE,  processCode);
        }

        if (!StringUtils.isEmpty(partNo)) {
            query.setParameter(MesExtConstWords.PART_NO,  partNo);
        }
    }

    private void packHqlForNativeQueryEquip(StringBuffer hql, String equNo, String equName) {
        hql.append(" from mes_equipment_check ");
        hql.append(" where organize_code = :organizeCode ");
        hql.append(" and is_valid = :isValid ");
        hql.append(" and is_deleted = :isDeleted ");
        if (!StringUtils.isEmpty(equNo)) {
            hql.append(" and equ_no like :equNo ");
        }

        if (!StringUtils.isEmpty(equName)) {
            hql.append(" and equ_name like :equName ");
        }
    }

    private void packHqlForQueryEquip(StringBuffer hql, String equNo, String equName) {
        hql.append(" from MesEquipmentCheck ");
        hql.append(" where organizeCode = :organizeCode ");
        hql.append(" and isValid = :isValid ");
        hql.append(" and isDeleted = :isDeleted ");
        if (!StringUtils.isEmpty(equNo)) {
            hql.append(" and equNo like :equNo ");
        }

        if (!StringUtils.isEmpty(equName)) {
            hql.append(" and equName like :equName ");
        }
    }

    private void packParameterForNativeQueryEquip(Query query, String equNo, String equName, String organizeCode) {
        query.setParameter(MesExtConstWords.ORGANIZE_CODE, organizeCode);
        query.setParameter(MesExtConstWords.IS_VALID, CommonEnumUtil.IS_VAILD.VAILD.getValue());
        query.setParameter(MesExtConstWords.IS_DELETED, CommonEnumUtil.TRUE_OR_FALSE.FALSE.getValue());
        if (!StringUtils.isEmpty(equNo)) {
            query.setParameter(MesExtConstWords.EQU_NO, "%" + equNo + "%");
        }

        if (!StringUtils.isEmpty(equName)) {
            query.setParameter(MesExtConstWords.EQU_NAME, "%" + equName + "%");
        }
    }

    private void packParameterForQueryEquip(Query query, String equNo, String equName, String organizeCode) {
        query.setParameter(MesExtConstWords.ORGANIZE_CODE, organizeCode);
        query.setParameter(MesExtConstWords.IS_VALID, CommonEnumUtil.IS_VAILD.VAILD.getValue());
        query.setParameter(MesExtConstWords.IS_DELETED, CommonEnumUtil.TRUE_OR_FALSE.FALSE.getValue());
        if (!StringUtils.isEmpty(equNo)) {
            query.setParameter(MesExtConstWords.EQU_NO, "%" + equNo + "%");
        }

        if (!StringUtils.isEmpty(equName)) {
            query.setParameter(MesExtConstWords.EQU_NAME, "%" + equName + "%");
        }
    }
}
