package cn.estsh.i3plus.pojo.sweb.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.SwebEnumUtil;
import cn.estsh.i3plus.pojo.base.tool.DdlPreparedPack;
import cn.estsh.i3plus.pojo.base.tool.HqlPack;
import cn.estsh.i3plus.pojo.base.util.StringUtil;
import cn.estsh.i3plus.pojo.sweb.bean.*;
import cn.estsh.i3plus.pojo.sweb.modelbean.SwebPOForPubListEnterModel;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description : hql 封装
 * @Reference :
 * @Author : jack.lv
 * @CreateDate : 2019-04-01 17:59
 * @Modify:
 **/
public class SwebHqlPack {

    /**
     * 通用封装isValid、isDeleted等参数
     *
     * @param bean
     * @param hqlStr
     * @return
     */
    public static DdlPackBean buildHql(BaseBean bean, DdlPackBean hqlStr) {
        // 判断工厂代码是否为空
        if (StringUtils.isNotBlank(bean.getOrganizeCode())) {
            DdlPreparedPack.getStringEqualPack(bean.getOrganizeCode(), "organizeCode", hqlStr);
        }

        // 封装有效状态和删除状态
        Integer isValid = bean.getIsValid();
        //默认查有效数据
        if (isValid != null && isValid != 0) {
            DdlPreparedPack.getNumEqualPack(isValid, "isValid", hqlStr);
        } else {
            DdlPreparedPack.getNumEqualPack(CommonEnumUtil.IS_VAILD.VAILD.getValue(), "isValid", hqlStr);
        }
        DdlPreparedPack.getNumEqualPack(CommonEnumUtil.TRUE_OR_FALSE.FALSE.getValue(), "isDeleted", hqlStr);
        return hqlStr;
    }

    public static DdlPackBean getPurchaseOrderWhereHql(SwebPurchaseOrder purchaseOrder, List<String> vendorNos) {
        DdlPackBean result = new DdlPackBean();
        DdlPreparedPack.getStringLikerPack(purchaseOrder.getOrderNo(), "orderNo", result);
        DdlPreparedPack.getNumEqualPack(purchaseOrder.getOrderSource(), "orderSource", result);
        DdlPreparedPack.getInPackList(vendorNos, "vendorNo", result);
        if (!StringUtil.isEmpty(purchaseOrder.getExpectedTimeStart()) && !StringUtil.isEmpty(purchaseOrder.getExpectedTimeEnd())) {
            DdlPreparedPack.timeBuilder(purchaseOrder.getExpectedTimeStart(),
                    purchaseOrder.getExpectedTimeEnd(), "expectedTime", result, false);
        }
        DdlPreparedPack.getNumEqualPack(purchaseOrder.getOrderType(), "orderType", result);
        DdlPreparedPack.getNumEqualPack(purchaseOrder.getPoStatus(), "poStatus", result);
        DdlPreparedPack.getOrderByPack(new Object[]{CommonEnumUtil.ASC_OR_DESC.DESC.getValue()}, new String[]{"createDatetime"}, result);
        return buildHql(purchaseOrder, result);
    }

    public static DdlPackBean getPurchaseOrderForSnPrintWhereHql(SwebPurchaseOrder purchaseOrder, List<String> vendorNos) {
        DdlPackBean result = new DdlPackBean();
        DdlPreparedPack.getStringLikerPack(purchaseOrder.getOrderNo(), "orderNo", result);
        DdlPreparedPack.getNumEqualPack(purchaseOrder.getOrderSource(), "orderSource", result);
        if (StringUtil.isEmpty(purchaseOrder.getVendorNo())) {
            DdlPreparedPack.getInPackList(vendorNos, "vendorNo", result);
        } else {
            DdlPreparedPack.getStringEqualPack(purchaseOrder.getVendorNo(), "vendorNo", result);
        }
        if (!StringUtil.isEmpty(purchaseOrder.getExpectedTimeStart()) && !StringUtil.isEmpty(purchaseOrder.getExpectedTimeEnd())) {
            DdlPreparedPack.timeBuilder(purchaseOrder.getExpectedTimeStart(),
                    purchaseOrder.getExpectedTimeEnd(), "expectedTime", result, false);
        }

        List<Integer> poStatus = new ArrayList<>();
        poStatus.add(SwebEnumUtil.ORDER_MASTER_STATUS.SURE.getValue());
        poStatus.add(SwebEnumUtil.ORDER_MASTER_STATUS.PRINTED.getValue());
        poStatus.add(SwebEnumUtil.ORDER_MASTER_STATUS.DELIVERY.getValue());
        DdlPreparedPack.getInPackList(poStatus, "poStatus", result);

        DdlPreparedPack.getNumEqualPack(purchaseOrder.getOrderType(), "orderType", result);
        DdlPreparedPack.getOrderByPack(new Object[]{CommonEnumUtil.ASC_OR_DESC.DESC.getValue()}, new String[]{"createDatetime"}, result);
        return buildHql(purchaseOrder, result);
    }

    public static DdlPackBean getPackageWhereHql(SwebPackage swebPackage) {
        DdlPackBean result = new DdlPackBean();
        DdlPreparedPack.getStringEqualPack(swebPackage.getPackageNo(), "packageNo", result);
        DdlPreparedPack.getStringEqualPack(swebPackage.getParentPackageNo(), "parentPackageNo", result);
        DdlPreparedPack.getNumEqualPack(swebPackage.getPackTypeId(), "packTypeId", result);
        DdlPreparedPack.getOrderByPack(new Object[]{CommonEnumUtil.ASC_OR_DESC.DESC.getValue()}, new String[]{"createDatetime"}, result);
        return buildHql(swebPackage, result);
    }

    public static DdlPackBean getConfigWhereHql(SwebConfig swebConfig) {
        DdlPackBean result = new DdlPackBean();
        DdlPreparedPack.getStringEqualPack(swebConfig.getConfigCode(), "configCode", result);
        DdlPreparedPack.getStringEqualPack(swebConfig.getConfigValue(), "configValue", result);
        DdlPreparedPack.getOrderByPack(new Object[]{CommonEnumUtil.ASC_OR_DESC.DESC.getValue()}, new String[]{"createDatetime"}, result);
        return buildHql(swebConfig, result);
    }


    // DdlPackBean方法中的 getNumNOEqualPack（） 方法存在问题，所有先用  HqlPack
    public static String getPurchaseOrderDetailsWhereHqlByItemStatus(SwebPOForPubListEnterModel model) {
        StringBuffer result = new StringBuffer();
        HqlPack.getNumNOEqualPack(SwebEnumUtil.ORDER_MASTER_STATUS.CREATE.getValue(),"itemStatus",result);
        HqlPack.getNumNOEqualPack(SwebEnumUtil.ORDER_MASTER_STATUS.RECEIPT.getValue(),"itemStatus",result);

        HqlPack.getStringEqualPack(model.getOrderNo(), "orderNo", result);
        HqlPack.getStringEqualPack(model.getVendorNo(), "vendorNo", result);
        HqlPack.getNumEqualPack(model.getOrderType(), "orderType", result);
        HqlPack.getStringEqualPack(model.getPartNo(), "partNo", result);
        // 封装有效状态和删除状态
        HqlPack.getNumEqualPack(CommonEnumUtil.IS_VAILD.VAILD.getValue(), "isValid", result);
        HqlPack.getNumEqualPack(CommonEnumUtil.TRUE_OR_FALSE.FALSE.getValue(), "isDeleted", result);
        HqlPack.getStringEqualPack(model.getOrganizeCode(), "organizeCode", result);
        HqlPack.getOrderByPack(new Object[]{CommonEnumUtil.ASC_OR_DESC.DESC.getValue()}, new String[]{"createDatetime"}, result);
        return  result.toString();
    }

    public static DdlPackBean getPurchaseOrderDetailsWhereHql(SwebPOForPubListEnterModel model) {
        DdlPackBean result = new DdlPackBean();
        DdlPreparedPack.getStringEqualPack(model.getOrderNo(), "orderNo", result);
        DdlPreparedPack.getStringEqualPack(model.getVendorNo(), "vendorNo", result);
        DdlPreparedPack.getNumEqualPack(model.getOrderType(), "orderType", result);
        DdlPreparedPack.getStringEqualPack(model.getPartNo(), "partNo", result);
        // 封装有效状态和删除状态
        DdlPreparedPack.getNumEqualPack(CommonEnumUtil.IS_VAILD.VAILD.getValue(), "isValid", result);
        DdlPreparedPack.getNumEqualPack(CommonEnumUtil.TRUE_OR_FALSE.FALSE.getValue(), "isDeleted", result);
        DdlPreparedPack.getOrderByPack(new Object[]{CommonEnumUtil.ASC_OR_DESC.DESC.getValue()}, new String[]{"createDatetime"}, result);
        return buildHql(model, result);
    }

    public static DdlPackBean getProcurementPlanOrderWhereHql(SwebProcurementPlanOrder swebProcurementPlanOrder) {
        DdlPackBean result = new DdlPackBean();
        DdlPreparedPack.getStringEqualPack(swebProcurementPlanOrder.getVendorNo(), "vendorNo", result);
        DdlPreparedPack.getStringLikerPack(swebProcurementPlanOrder.getOrderNo(), "orderNo", result);
        DdlPreparedPack.getOrderByPack(new Object[]{CommonEnumUtil.ASC_OR_DESC.DESC.getValue()}, new String[]{"createDatetime"}, result);
        return buildHql(swebProcurementPlanOrder, result);
    }

    public static DdlPackBean getProcurementPlanOrderDeatilsWhereHql(SwebProcurementPlanOrderDetails swebProcurementPlanOrderDetails) {
        DdlPackBean result = new DdlPackBean();
        DdlPreparedPack.getStringLikerPack(swebProcurementPlanOrderDetails.getOrderNo(), "orderNo", result);
        DdlPreparedPack.getStringLikerPack(swebProcurementPlanOrderDetails.getPartNo(), "partNo", result);
        DdlPreparedPack.getStringLikerPack(swebProcurementPlanOrderDetails.getPartName(), "partName", result);
        DdlPreparedPack.getOrderByPack(new Object[]{CommonEnumUtil.ASC_OR_DESC.DESC.getValue()}, new String[]{"createDatetime"}, result);
        return buildHql(swebProcurementPlanOrderDetails, result);
    }

    public static DdlPackBean getVendorRelWhereHql(SwebVendorRel vendorRel) {
        DdlPackBean result = new DdlPackBean();
        DdlPreparedPack.getStringLikerPack(vendorRel.getVendorNo(), "vendorNo", result);
        DdlPreparedPack.getStringLikerPack(vendorRel.getPlannerCode(), "plannerCode", result);
        DdlPreparedPack.getOrderByPack(new Object[]{CommonEnumUtil.ASC_OR_DESC.DESC.getValue()}, new String[]{"createDatetime"}, result);
        return buildHql(vendorRel, result);
    }

    public static DdlPackBean getPurchaseOrderDetailsPublishListQtyWhereHql(SwebPurchaseOrderDetails details) {
        DdlPackBean result = new DdlPackBean();
        DdlPreparedPack.getStringEqualPack(details.getOrderNo(), "orderNo", result);
        DdlPreparedPack.getStringEqualPack(details.getVendorNo(), "vendorNo", result);
        DdlPreparedPack.getStringEqualPack(details.getPartNo(), "partNo", result);
        DdlPreparedPack.getNumEqualPack(details.getItemStatus(), "itemStatus", result);
        return buildHql(details, result);
    }

    public static DdlPackBean getPurchaseOrderDetailsPublishListStatusWhereHql(SwebPurchaseOrderDetails details) {
        DdlPackBean result = new DdlPackBean();
        DdlPreparedPack.getStringEqualPack(details.getOrderNo(), "orderNo", result);
        DdlPreparedPack.getStringEqualPack(details.getVendorNo(), "vendorNo", result);
        DdlPreparedPack.getStringEqualPack(details.getPartNo(), "partNo", result);
        return buildHql(details, result);
    }

    /**
     * 将该供应商下该单号状态全部改为已发布状态 where sql（只查询状态为新建的订单）
     * @param details
     * @return
     */
    public static DdlPackBean getPODetailsPublishListUpdatePublishStatusWhereHql(SwebPurchaseOrderDetails details) {
        DdlPackBean result = new DdlPackBean();
        DdlPreparedPack.getStringEqualPack(details.getOrderNo(), "orderNo", result);
        DdlPreparedPack.getStringEqualPack(details.getVendorNo(), "vendorNo", result);
        return buildHql(details, result);
    }

    /**
     * 判断订单详情表是否有非新建状态的订单 where sql
     * @param details
     * @return
     */
    public static DdlPackBean getPODetailsNotCreateStatusWhereHql(SwebPurchaseOrderDetails details) {
        DdlPackBean result = new DdlPackBean();
        DdlPreparedPack.getStringEqualPack(details.getOrderNo(), "orderNo", result);
        DdlPreparedPack.getNumNOEqualPack(SwebEnumUtil.ORDER_DETAILS_STATUS.CREATE.getValue(), "itemStatus", result);
        return buildHql(details, result);
    }

    public static DdlPackBean getDocMovementDetails(SwebDocMovementDetails details) {
        DdlPackBean result = new DdlPackBean();
        DdlPreparedPack.getStringLikerPack(details.getOrderNo(), "orderNo", result);
        DdlPreparedPack.getStringLikerPack(details.getPartNo(), "partNo", result);
        DdlPreparedPack.getNumEqualPack(details.getItemStatus(), "itemStatus", result);
        DdlPreparedPack.getStringLikerPack(details.getVendorNo(), "vendorNo", result);
        DdlPreparedPack.getOrderByPack(new Object[]{CommonEnumUtil.ASC_OR_DESC.DESC.getValue()}, new String[]{"createDatetime"}, result);
        return buildHql(details, result);
    }

    public static DdlPackBean getVendorSupplierCapcity(SwebVendorSupplierCapcity capcity, List<String> vendorNos) {
        DdlPackBean result = new DdlPackBean();
        if (vendorNos.size() == 1) {
            DdlPreparedPack.getStringLikerPack(vendorNos.get(0), "vendorNo", result);
        } else if (vendorNos.size() > 1){
            DdlPreparedPack.getInPackList(vendorNos, "vendorNo", result);
        }
        DdlPreparedPack.getStringLikerPack(capcity.getPartNo(), "partNo", result);
        DdlPreparedPack.getNumEqualPack(capcity.getStatus(), "status", result);
        if (!StringUtil.isEmpty(capcity.getDateStart())) {
            DdlPreparedPack.getStringBiggerPack(capcity.getDateStart(), "trackDate", result);
        }
        if (!StringUtil.isEmpty(capcity.getDateEnd())) {
            DdlPreparedPack.getStringSmallerPack(capcity.getDateEnd(), "trackDate", result);
        }
        DdlPreparedPack.getOrderByPack(new Object[]{CommonEnumUtil.ASC_OR_DESC.DESC.getValue()}, new String[]{"createDatetime"}, result);
        return buildHql(capcity, result);
    }

    public static DdlPackBean getSwebRequireAlarm(SwebRequireAlarm alarm, List<String> vendorNos) {
        DdlPackBean result = new DdlPackBean();
        if (vendorNos.size() == 1) {
            DdlPreparedPack.getStringLikerPack(vendorNos.get(0), "vendorNo", result);
        } else if (vendorNos.size() > 1){
            DdlPreparedPack.getInPackList(vendorNos, "vendorNo", result);
        }
        DdlPreparedPack.getStringLikerPack(alarm.getPartNo(), "partNo", result);
        if (!StringUtil.isEmpty(alarm.getDateStart())) {
            DdlPreparedPack.getStringBiggerPack(alarm.getDateStart(), "countDate", result);
        }
        if (!StringUtil.isEmpty(alarm.getDateEnd())) {
            DdlPreparedPack.getStringSmallerPack(alarm.getDateEnd(), "countDate", result);
        }
        DdlPreparedPack.getOrderByPack(new Object[]{CommonEnumUtil.ASC_OR_DESC.DESC.getValue()}, new String[]{"createDatetime"}, result);
        return buildHql(alarm, result);
    }

    public static DdlPackBean getSwebVendorStock(SwebVendorStock stock, List<String> vendorNos) {
        DdlPackBean result = new DdlPackBean();
        if (vendorNos.size() == 1) {
            DdlPreparedPack.getStringLikerPack(vendorNos.get(0), "vendorNo", result);
        } else if (vendorNos.size() > 1){
            DdlPreparedPack.getInPackList(vendorNos, "vendorNo", result);
        }
        DdlPreparedPack.getStringLikerPack(stock.getPartNo(), "partNo", result);
        DdlPreparedPack.getOrderByPack(new Object[]{CommonEnumUtil.ASC_OR_DESC.DESC.getValue()}, new String[]{"createDatetime"}, result);
        return buildHql(stock, result);
    }

    public static DdlPackBean getSwebVendorAlarm(SwebVendorAlarm alarm, List<String> vendorNos) {
        DdlPackBean result = new DdlPackBean();
        if (vendorNos.size() == 1) {
            DdlPreparedPack.getStringLikerPack(vendorNos.get(0), "vendorNo", result);
        } else if (vendorNos.size() > 1){
            DdlPreparedPack.getInPackList(vendorNos, "vendorNo", result);
        }
        DdlPreparedPack.getStringLikerPack(alarm.getOrderNo(), "orderNo", result);
        DdlPreparedPack.getStringLikerPack(alarm.getPartNo(), "partNo", result);
        DdlPreparedPack.getOrderByPack(new Object[]{CommonEnumUtil.ASC_OR_DESC.DESC.getValue()}, new String[]{"createDatetime"}, result);
        return buildHql(alarm, result);
    }
}
