package nckd.jxccl.base.orm.helper;

import kd.bos.common.enums.EnableEnum;
import kd.bos.entity.constant.StatusEnum;
import kd.bos.entity.validate.BillStatus;
import kd.bos.logging.Log;
import kd.bos.logging.LogFactory;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import nckd.jxccl.base.common.constant.FormConstant;
import nckd.jxccl.base.common.utils.ConvertUtil;

import java.util.Collection;
import java.util.Date;
import java.util.List;

/**
 * QFilter 通用辅助类
 * @author W.Y.C
 * @date 2025/7/7 9:17
 * @version 1.0
 */
public final class QFilterCommonHelper {

    private static final Log logger = LogFactory.getLog(QFilterCommonHelper.class);

    private QFilterCommonHelper() {
    }

    /**
     * 当前数据QFilter实例
     * @return: kd.bos.orm.query.QFilter
     * @author W.Y.C
     * @date: 2025/07/07 09:20
     */
    public static QFilter getIsCurrentData() {
        return new QFilter(FormConstant.ISCURRENTDATA, QCP.equals, EnableEnum.YES.getCode());
    }

    /**
     * 是否删除QFilter实例
     * @return: kd.bos.orm.query.QFilter
     * @author W.Y.C
     * @date: 2025/07/07 09:20
     */
    public static QFilter getIsDeleted() {
        return new QFilter(FormConstant.IS_DELETED, QCP.equals, EnableEnum.NO.getCode());
    }

    /**
     * 获取使用状态为可用的QFilter实例
     * @param
     * @return: kd.bos.orm.query.QFilter
     * @author W.Y.C
     * @date: 2025/07/07 09:20
     */
    public static QFilter getEnableFilter() {
        return getEnableFilter(true);
    }

    /**
     * 根据使用状态获取QFilter实例
     * @param enable 是否启用
     * @return: kd.bos.orm.query.QFilter
     * @author W.Y.C
     * @date: 2025/07/07 09:20
     */
    public static QFilter getEnableFilter(boolean enable) {
        return new QFilter(FormConstant.ENABLE, QCP.equals, enable ? EnableEnum.YES.getCode() : EnableEnum.NO.getCode());
    }

    /**
     * 获取当前版本QFilter实例
     * @param
     * @return: kd.bos.orm.query.QFilter
     * @author W.Y.C
     * @date: 2025/07/07 09:20
     */
    public static QFilter getCurrentVersionFilter() {
        return new QFilter(FormConstant.IS_CURRENT_VERSION, QCP.equals, EnableEnum.YES.getCode());
    }

    /**
     * 获取状态为已审核的QFilter实例
     * @param
     * @return: kd.bos.orm.query.QFilter
     * @author W.Y.C
     * @date: 2025/07/07 09:20
     */
    public static QFilter getStatusFilter() {
        return new QFilter(FormConstant.STATUS, QCP.equals, StatusEnum.C.toString());
    }

    /**
     * 获取数据版本状态为生效中的QFilter实例
     * @param
     * @return: kd.bos.orm.query.QFilter
     * @author W.Y.C
     * @date: 2025/07/07 09:20
     */
    public static QFilter getDataStatusFilter() {
        return new QFilter(FormConstant.DATA_STATUS, QCP.equals, EnableEnum.YES.getCode());
    }

    /**
     * 获取业务状态为生效中的QFilter实例
     * @param
     * @return: kd.bos.orm.query.QFilter
     * @author W.Y.C
     * @date: 2025/07/07 09:20
     */
    public static QFilter getBusinessStatusFilter() {
        return new QFilter(FormConstant.BUSINESS_STATUS, QCP.equals, EnableEnum.YES.getCode());
    }

    /**
     * 获取初始状态为完成的QFilter实例
     * @param
     * @return: kd.bos.orm.query.QFilter
     * @author W.Y.C
     * @date: 2025/07/07 09:20
     */
    public static QFilter getInitStatusFinishFilter() {
        return new QFilter(FormConstant.INIT_STATUS, QCP.equals, BillStatus.C.ordinal()+"");
    }

    /**
     * 获取业务状态为已审批的QFilter实例
     * @return: kd.bos.orm.query.QFilter
     * @author W.Y.C
     * @date: 2025/07/07 17:30
     */
    public static QFilter getBillStatusFilter() {
        return new QFilter(FormConstant.BILL_STATUS_KEY, QCP.equals, BillStatus.C.toString());
    }

    /**
     * 获取编号等于number的QFilter实例
     * @param number
     * @return: kd.bos.orm.query.QFilter
     * @author W.Y.C
     * @date: 2025/07/07 09:20
     */
    public static QFilter getNumberEqFilter(String number) {
        return new QFilter(FormConstant.NUMBER_KEY, QCP.equals, number);
    }

    /**
     * 获取主键等于id的QFilter实例
     * @param id
     * @return: kd.bos.orm.query.QFilter
     * @author W.Y.C
     * @date: 2025/07/07 09:20
     */
    public static QFilter getIdEqFilter(Object id) {
        return new QFilter(FormConstant.ID_KEY, QCP.equals, ConvertUtil.toLong(id));
    }

    /**
     * 获取主键等于idList的QFilter实例
     * @param idList
     * @return: kd.bos.orm.query.QFilter
     * @author W.Y.C
     * @date: 2025/07/07 09:20
     */
    public static QFilter getIdInFilter(Collection<Long> idList) {
        return new QFilter(FormConstant.ID_KEY, QCP.in, idList);
    }

    /**
     * 获取有效时间范围内的QFilter实例
     * 如果结束时间为空，则表示持续有效
     * @param beginDateProperty 开始时间字段名
     * @param endDateProperty 结束时间字段名
     * @return: kd.bos.orm.query.QFilter
     * @author W.Y.C
     * @date: 2025/07/07
     */
    public static QFilter getValidDateFilter(String beginDateProperty, String endDateProperty) {
        // 创建组合条件 (beginTime <= currentTime AND (endTime >= currentTime OR endTime IS NULL))
        Date currentDate = new Date();
        QFilter beginFilter = new QFilter(beginDateProperty, QCP.less_equals, currentDate);
        QFilter endFilter = new QFilter(endDateProperty, QCP.large_equals, currentDate);
        QFilter nullEndFilter = new QFilter(endDateProperty, QCP.is_null, null);

        return beginFilter.and(endFilter.or(nullEndFilter));
    }

    /**
     * 获取有效时间范围内的QFilter实例
     * 如果结束时间为空，则表示持续有效
     * @param beginDateProperty 开始时间字段名
     * @param endDateProperty 结束时间字段名
     * @param date 获取的日期
     * @return: kd.bos.orm.query.QFilter
     * @author W.Y.C
     * @date: 2025/07/07
     */
    public static QFilter getValidDateFilter(String beginDateProperty, String endDateProperty,Date date) {
        // 创建组合条件 (beginTime <= currentTime AND (endTime >= currentTime OR endTime IS NULL))
        Date currentDate = new Date();
        QFilter beginFilter = new QFilter(beginDateProperty, QCP.less_equals, date);
        QFilter endFilter = new QFilter(endDateProperty, QCP.large_equals, date);
        QFilter nullEndFilter = new QFilter(endDateProperty, QCP.is_null, null);

        return beginFilter.and(endFilter.or(nullEndFilter));
    }
}