package cn.estsh.i3plus.pojo.ptl.sqlpack;

import cn.estsh.i3plus.pojo.base.bean.BaseBean;
import cn.estsh.i3plus.pojo.base.bean.DdlPackBean;
import cn.estsh.i3plus.pojo.base.enumutil.CommonEnumUtil;
import cn.estsh.i3plus.pojo.base.enumutil.PtlPcnEnumUtil;
import cn.estsh.i3plus.pojo.base.tool.DdlPreparedPack;
import cn.estsh.i3plus.pojo.ptl.bean.*;
import org.springframework.util.StringUtils;

/**
 * @Description : PTL对象封装
 * @Reference :
 * @Author : silliter.yuan
 * @CreateDate : 2020-02-28 10:45
 * @Modify:
 **/
public class PtlHqlPack {

    /**
     * 封装公用查询参数
     *
     * @param bean   实体对象
     * @param hqlStr 查询语句
     * @return 封装完成的查询语句
     */
    public static DdlPackBean getStringBuilderPack(BaseBean bean, DdlPackBean hqlStr) {

        // 判断工厂代码是否为空
        if (!StringUtils.isEmpty(bean.getOrganizeCode())) {
            DdlPreparedPack.getStringEqualPack(bean.getOrganizeCode(), "organizeCode", hqlStr);
        }


        DdlPreparedPack.getStringEqualPack(bean.getCreateUser(), "createUser", hqlStr);
        if (!StringUtils.isEmpty(bean.getCreateDateTimeStart()) && !StringUtils.isEmpty(bean.getCreateDateTimeEnd())) {
            DdlPreparedPack.timeBuilder(bean.getCreateDateTimeStart(), bean.getCreateDateTimeEnd(), "createDatetime", hqlStr, true);
        }
        // 封装有效状态、删除状态、创建人和创建时间
        DdlPreparedPack.getNumEqualPack(bean.getIsValid(), "isValid", hqlStr);
        DdlPreparedPack.getNumEqualPack(CommonEnumUtil.TRUE_OR_FALSE.FALSE.getValue(), "isDeleted", hqlStr);

        return hqlStr;
    }

    /**
     * 通用查询条件
     *
     * @param organizeCode
     * @return
     */
    public static DdlPackBean getAllBaseDataByNormalPro(BaseBean baseBean, String organizeCode) {
        DdlPackBean packBean = new DdlPackBean();
        DdlPreparedPack.getStringEqualPack(organizeCode, "organizeCode", packBean);
        DdlPreparedPack.getNumEqualPack(baseBean.getIsValid(), "isValid", packBean);
        DdlPreparedPack.getNumEqualPack(CommonEnumUtil.IS_DEAL.NO.getValue(), "isDeleted", packBean);
        return packBean;
    }

    /**
     * 打印机管理信息分页查询条件
     *
     * @param ptlPrinterConfigure
     * @return
     */
    public static DdlPackBean packHqlPtlPrinterConfigure(PtlPrinterConfigure ptlPrinterConfigure) {
        DdlPackBean result = new DdlPackBean();

        DdlPreparedPack.getStringLikerPack(ptlPrinterConfigure.getPrinterNo(), "printerNo", result);
        DdlPreparedPack.getStringLikerPack(ptlPrinterConfigure.getIp(), "ip", result);

        getStringBuilderPack(ptlPrinterConfigure, result);

        return result;
    }

    /**
     * 待打印队列信息分页查询条件
     *
     * @param ptlPrintingQueue
     * @return
     */
    public static DdlPackBean packHqlPtlPrintingQueueByPrintNo(PtlPrintingQueue ptlPrintingQueue) {
        DdlPackBean result = new DdlPackBean();

        DdlPreparedPack.getNumEqualPack(PtlPcnEnumUtil.ETC_PRINT_STATUS.NOT_HIT.getValue(), "printStatus", result);
        DdlPreparedPack.getStringLikerPack(ptlPrintingQueue.getPrintNo(), "printNo", result);
        DdlPreparedPack.getNumEqualPack(PtlPcnEnumUtil.PRINT_TYPE.PART_NO.getValue(), "printType", result);

        getStringBuilderPack(ptlPrintingQueue, result);

        return result;
    }

    /**
     * 通用查询条件
     *
     * @param organizeCode
     * @return
     */
    public static DdlPackBean getAllBaseData(String organizeCode) {
        DdlPackBean packBean = new DdlPackBean();
        DdlPreparedPack.getStringEqualPack(organizeCode, "organizeCode", packBean);
        DdlPreparedPack.getNumEqualPack(CommonEnumUtil.IS_DEAL.NO.getValue(), "isDeleted", packBean);
        return packBean;
    }

    /**
     * 配置信息
     * @param ptlConfig
     * @return
     */
    public static DdlPackBean getPtlConfig(PtlConfig ptlConfig) {
        DdlPackBean packBean = getAllBaseDataByNormalPro(ptlConfig, ptlConfig.getOrganizeCode());
        if (!StringUtils.isEmpty(ptlConfig.getCfgCode())) {
            DdlPreparedPack.getStringEqualPack(ptlConfig.getCfgCode(), "cfgCode", packBean);
        }
        if (!StringUtils.isEmpty(ptlConfig.getCfgType())) {
            DdlPreparedPack.getStringEqualPack(ptlConfig.getCfgType(), "cfgType", packBean);
        }
        if (!StringUtils.isEmpty(ptlConfig.getCfgKey())) {
            DdlPreparedPack.getStringEqualPack(ptlConfig.getCfgKey(), "cfgKey", packBean);
        }
        return packBean;
    }

    /**
     * PTL PCN系统配置信息条件查询
     *
     * @param ptlConfig
     * @return
     */
    public static DdlPackBean getPtlSyncConfig(PtlConfig ptlConfig, String organizeCode) {
        DdlPackBean packBean = getAllBaseData(organizeCode);
        DdlPreparedPack.getStringLikerPack(ptlConfig.getCfgCode(), "cfgCode", packBean);
        DdlPreparedPack.getStringLikerPack(ptlConfig.getCfgName(), "cfgName", packBean);
        DdlPreparedPack.getStringEqualPack(ptlConfig.getCfgKey(), "cfgKey", packBean);
        DdlPreparedPack.getStringEqualPack(ptlConfig.getCfgType(), "cfgType", packBean);
        DdlPreparedPack.getNumEqualPack(ptlConfig.getIsValid(), "isValid", packBean);
        return packBean;
    }


    /**
     * PTL-PCN节点查询条件封装
     *
     * @param ptlPcn
     * @param organizeCode
     * @return
     */
    public static DdlPackBean getPtlPcnByCondition(PtlPcn ptlPcn, String organizeCode) {
        DdlPackBean packBean = getAllBaseData(organizeCode);
        if (!StringUtils.isEmpty(ptlPcn.getConnectIp())) {
            DdlPreparedPack.getStringLikerPack(ptlPcn.getConnectIp(), "connectIp", packBean);
        }
        if (!StringUtils.isEmpty(ptlPcn.getPcnName())) {
            DdlPreparedPack.getStringLikerPack(ptlPcn.getPcnName(), "pcnName", packBean);
        }
        if (!StringUtils.isEmpty(ptlPcn.getPcnCode())) {
            DdlPreparedPack.getStringLikerPack(ptlPcn.getPcnCode(), "pcnCode", packBean);
        }
        if (!StringUtils.isEmpty(ptlPcn.getAreaNo())) {
            DdlPreparedPack.getStringLikerPack(ptlPcn.getAreaNo(), "areaNo", packBean);
        }
        if (ptlPcn.getIsValid() != null) {
            DdlPreparedPack.getNumEqualPack(ptlPcn.getIsValid(), "isValid", packBean);
        }
        return packBean;
    }

    /**
     * PTL-PCN同步配置查询条件封装
     *
     * @param pcnSyncCfg
     * @return
     */
    public static DdlPackBean getPtlPcnSyncCfg(PtlPcnSyncCfg pcnSyncCfg, String organizeCode) {
        DdlPackBean packBean = getAllBaseDataByNormalPro(pcnSyncCfg, organizeCode);
        DdlPreparedPack.getStringLikerPack(pcnSyncCfg.getObjectCode(), "objectCode", packBean);
        DdlPreparedPack.getStringLikerPack(pcnSyncCfg.getPcnCode(), "pcnCode", packBean);
        DdlPreparedPack.getNumEqualPack(pcnSyncCfg.getSyncType(), "syncType", packBean);
        return packBean;
    }

    /**
     * PTL对象与dao对应关系查询条件封装
     *
     * @param objectDao
     * @return
     */
    public static DdlPackBean getPtlObjectDao(PtlObjectDao objectDao, String organizeCode) {
        DdlPackBean packBean = getAllBaseDataByNormalPro(objectDao, organizeCode);
        if (!StringUtils.isEmpty(objectDao.getObjectCode())) {
            DdlPreparedPack.getStringLikerPack(objectDao.getObjectCode(), "objectCode", packBean);
        }
        if (!StringUtils.isEmpty(objectDao.getDaoClass())) {
            DdlPreparedPack.getStringLikerPack(objectDao.getDaoClass(), "daoClass", packBean);
        }

        return packBean;
    }

    /**
     * ptl-pcn定时任务清单
     *
     * @param ptlPcnTask
     * @param organizeCode
     * @return
     */
    public static DdlPackBean getPtlPcnTask(PtlPcnTask ptlPcnTask, String organizeCode) {
        DdlPackBean packBean = getAllBaseDataByNormalPro(ptlPcnTask, organizeCode);
        if (!StringUtils.isEmpty(ptlPcnTask.getTaskCode())) {
            DdlPreparedPack.getStringLikerPack(ptlPcnTask.getTaskCode(), "taskCode", packBean);
        }
        if (!StringUtils.isEmpty(ptlPcnTask.getTaskName())) {
            DdlPreparedPack.getStringLikerPack(ptlPcnTask.getTaskName(), "taskName", packBean);
        }
        return packBean;
    }

    /**
     * 区域任务主表
     *
     * @param ptlAreaTask 区域任务主表
     * @return
     */
    public static DdlPackBean packPtlAreaTask(PtlAreaTask ptlAreaTask) {
        DdlPackBean result = new DdlPackBean();
        //查询参数封装
        DdlPreparedPack.getStringLikerPack(ptlAreaTask.getTaskNo(), "taskNo", result);
        DdlPreparedPack.getStringLikerPack(ptlAreaTask.getPartNo(), "partNo", result);
        if (!StringUtils.isEmpty(ptlAreaTask.getCreateDateTimeStart())
                || !StringUtils.isEmpty(ptlAreaTask.getCreateDateTimeEnd())) {
            DdlPreparedPack.timeBuilder(ptlAreaTask.getCreateDateTimeStart(), ptlAreaTask.getCreateDateTimeEnd()
                    , "createDatetime", result, true);
        }
        DdlPreparedPack.getStringEqualPack(ptlAreaTask.getAreaNo(), "areaNo", result);
        DdlPreparedPack.getNumEqualPack(ptlAreaTask.getStatus(), "status", result);
        DdlPreparedPack.getStringEqualPack(ptlAreaTask.getTaskType(), "taskType", result);
        DdlPreparedPack.getStringEqualPack(ptlAreaTask.getOrganizeCode(), "organizeCode", result);
        DdlPreparedPack.getNumEqualPack(ptlAreaTask.getIsValid(), "isValid", result);
        DdlPreparedPack.getNumEqualPack(CommonEnumUtil.IS_DEAL.NO.getValue(), "isDeleted", result);
        return result;
    }

    /**
     * 主任务表
     *
     * @param ptlMainTask 主任务表
     * @return
     */
    public static DdlPackBean packPtlMainTask(PtlMainTask ptlMainTask) {
        DdlPackBean result = new DdlPackBean();
        //查询参数封装
        DdlPreparedPack.getStringLikerPack(ptlMainTask.getTaskNo(), "taskNo", result);
        DdlPreparedPack.getStringLikerPack(ptlMainTask.getPartNo(), "partNo", result);
        if (!StringUtils.isEmpty(ptlMainTask.getCreateDateTimeStart())
                || !StringUtils.isEmpty(ptlMainTask.getCreateDateTimeEnd())) {
            DdlPreparedPack.timeBuilder(ptlMainTask.getCreateDateTimeStart(), ptlMainTask.getCreateDateTimeEnd()
                    , "createDatetime", result, true);
        }
        DdlPreparedPack.getNumEqualPack(ptlMainTask.getStatus(), "status", result);
        DdlPreparedPack.getStringEqualPack(ptlMainTask.getTaskType(), "taskType", result);
        DdlPreparedPack.getStringEqualPack(ptlMainTask.getOrganizeCode(), "organizeCode", result);
        DdlPreparedPack.getNumEqualPack(ptlMainTask.getIsValid(), "isValid", result);
        DdlPreparedPack.getNumEqualPack(CommonEnumUtil.IS_DEAL.NO.getValue(), "isDeleted", result);
        return result;
    }
    /**
     * 区段
     *
     * @param ptlAreaSectionTask 区段任务
     * @return
     */
    public static DdlPackBean packPtlAreaSectionTask(PtlAreaSectionTask ptlAreaSectionTask) {
        DdlPackBean result = new DdlPackBean();
        //查询参数封装
        DdlPreparedPack.getStringLikerPack(ptlAreaSectionTask.getAreaTaskNo(), "areaTaskNo", result);
        DdlPreparedPack.getStringLikerPack(ptlAreaSectionTask.getPartNo(), "partNo", result);
        if (!StringUtils.isEmpty(ptlAreaSectionTask.getCreateDateTimeStart())
                || !StringUtils.isEmpty(ptlAreaSectionTask.getCreateDateTimeEnd())) {
            DdlPreparedPack.timeBuilder(ptlAreaSectionTask.getCreateDateTimeStart(), ptlAreaSectionTask.getCreateDateTimeEnd()
                    , "createDatetime", result, true);
        }
        DdlPreparedPack.getStringEqualPack(ptlAreaSectionTask.getAreaNo(), "areaNo", result);
        DdlPreparedPack.getStringEqualPack(ptlAreaSectionTask.getSectionNo(), "sectionNo", result);
        DdlPreparedPack.getNumEqualPack(ptlAreaSectionTask.getStatus(), "status", result);
        DdlPreparedPack.getStringEqualPack(ptlAreaSectionTask.getOrganizeCode(), "organizeCode", result);
        DdlPreparedPack.getNumEqualPack(ptlAreaSectionTask.getIsValid(), "isValid", result);
        DdlPreparedPack.getNumEqualPack(CommonEnumUtil.IS_DEAL.NO.getValue(), "isDeleted", result);
        return result;
    }

    /**
     * 区段任务明细
     *
     * @param ptlAreaSectionTaskDetail 区段任务明细
     * @return
     */
    public static DdlPackBean packPtlAreaSectionTaskDetail(PtlAreaSectionTaskDetail ptlAreaSectionTaskDetail) {
        DdlPackBean result = new DdlPackBean();
        //查询参数封装
        DdlPreparedPack.getStringEqualPack(ptlAreaSectionTaskDetail.getSectionTaskNo(), "sectionTaskNo", result);
        DdlPreparedPack.getStringEqualPack(ptlAreaSectionTaskDetail.getOrganizeCode(), "organizeCode", result);
        DdlPreparedPack.getNumEqualPack(ptlAreaSectionTaskDetail.getIsValid(), "isValid", result);
        DdlPreparedPack.getNumEqualPack(CommonEnumUtil.IS_DEAL.NO.getValue(), "isDeleted", result);
        return result;
    }

    /**
     * 流程查询条件封装
     *
     * @param actionRoute
     * @return
     */
    public static DdlPackBean packHqlPtlActionRoute(PtlActionRoute actionRoute, String organizeCode) {
        DdlPackBean packBean = getAllBaseDataByNormalPro(actionRoute, organizeCode);
        DdlPreparedPack.getStringLikerPack(actionRoute.getRouteCode(), "routeCode", packBean);
        DdlPreparedPack.getStringLikerPack(actionRoute.getRouteName(), "routeName", packBean);
        DdlPreparedPack.getNumEqualPack(actionRoute.getRouteType(), "routeType", packBean);
        return packBean;
    }

    /**
     * 区域流程查询条件封装
     * @param areaRoute
     * @param organizeCode
     * @return
     */
    public static DdlPackBean packHqlPtlAreaRoute(PtlAreaRoute areaRoute, String organizeCode) {
        DdlPackBean packBean = getAllBaseDataByNormalPro(areaRoute, organizeCode);
        DdlPreparedPack.getStringLikerPack(areaRoute.getRouteCode(), "routeCode", packBean);
        DdlPreparedPack.getStringLikerPack(areaRoute.getAreaNo(), "areaNo", packBean);
        DdlPreparedPack.getNumEqualPack(areaRoute.getRouteType(), "routeType", packBean);
        return packBean;
    }

    /**
     * 区域
     * @param area
     * @param organizeCode
     * @return
     */
    public static DdlPackBean getPtlArea(PtlArea area, String organizeCode) {
        DdlPackBean packBean = getAllBaseDataByNormalPro(area, organizeCode);
        DdlPreparedPack.getStringLikerPack(area.getAreaNo(), "areaNo", packBean);
        DdlPreparedPack.getStringLikerPack(area.getAreaName(), "areaName", packBean);
        return packBean;
    }

    /**
     * 流程状态异常记录
     * @param routeStatusErrorRecord
     * @param organizeCode
     * @return
     */
    public static DdlPackBean getPtlRouteStatusErrorRecord(PtlRouteStatusErrorRecord routeStatusErrorRecord, String organizeCode) {
        DdlPackBean packBean = getAllBaseDataByNormalPro(routeStatusErrorRecord, organizeCode);
        DdlPreparedPack.getStringLikerPack(routeStatusErrorRecord.getRouteCode(), "routeCode", packBean);
        DdlPreparedPack.getStringLikerPack(routeStatusErrorRecord.getStatusCode(), "statusCode", packBean);
        DdlPreparedPack.getStringLikerPack(routeStatusErrorRecord.getTaskNo(), "taskNo", packBean);
        DdlPreparedPack.getStringEqualPack(routeStatusErrorRecord.getAreaNo(), "areaNo", packBean);
        DdlPreparedPack.getNumEqualPack(routeStatusErrorRecord.getHandleStatus(), "handleStatus", packBean);
        return packBean;
    }

    /**
     * IF_拣货数据
     * @param pickData
     * @param organizeCode
     * @return
     */
    public static DdlPackBean getIfPickData(IfPickData pickData, String organizeCode) {
        DdlPackBean packBean = DdlPackBean.getDdlPackBean(organizeCode);
        DdlPreparedPack.getNumEqualPack(pickData.getSyncFlag(), "syncFlag", packBean);
        return packBean;
    }
}
