"use strict";
// oms/src/services/userTargetingServices
Object.defineProperty(exports, "__esModule", { value: true });
exports.UserTargetingService = void 0;
const userModel_1 = require("../models/userModel"); // 假设你有一个User模型
class UserTargetingService {
    /**
     * 内部方法：根据筛选条件数组构建MongoDB查询对象
     * @param filterData 筛选条件数组
     * @returns MongoDB查询对象
     */
    buildMongoQuery(filterData) {
        const query = {};
        // 默认添加 fmToken 非空条件，以筛选有效用户
        // query.fmToken = { $ne: null };
        if (!filterData || filterData.length === 0) {
            return {};
        }
        // 将筛选条件转换为MongoDB查询
        for (const condition of filterData) {
            const { field, operator, value } = condition;
            // 新增逻辑：如果 firstLoginAt 或 lastActiveAt 的值是包含两个元素的数组，
            // 则将其转换为日期范围查询 (between)，并跳过后续处理
            if ((field === "firstLoginAt" || field === "lastActiveAt") && Array.isArray(value) && value.length === 2) {
                const startDate = new Date();
                const endDate = new Date();
                startDate.setDate(startDate.getDate() - value[0]);
                endDate.setDate(endDate.getDate() - value[1]);
                query[field] = { $gt: endDate, $lt: startDate };
                continue; // 处理完此特殊情况，跳到下一个条件
            }
            let queryValue = value;
            // 现有逻辑：将 "n天前" 的数字转换为日期
            if ((field === "firstLoginAt" || field === "lastActiveAt") && typeof value === "number" && value >= 0) {
                const targetDate = new Date();
                targetDate.setDate(targetDate.getDate() - value);
                queryValue = targetDate;
            }
            switch (operator) {
                case "$eq": // 等于
                    query[field] = queryValue;
                    break;
                case "$ne": // 不等于
                    query[field] = { $ne: queryValue };
                    break;
                case "$gt": // 大于
                    query[field] = { $gt: queryValue };
                    break;
                case "$gte": // 大于等于
                    query[field] = { $gte: queryValue };
                    break;
                case "$lt": // 小于
                    query[field] = { $lt: queryValue };
                    break;
                case "$lte": // 小于等于
                    query[field] = { $lte: queryValue };
                    break;
                case "$in": // 包含在数组中
                    query[field] = { $in: queryValue };
                    break;
                case "$nin": // 不包含在数组中
                    query[field] = { $nin: queryValue };
                    break;
                default:
                    console.error(`Unknown operator: ${operator}`);
                    break;
            }
        }
        return query;
    }
    /**
     * 根据传入的筛选条件查询满足条件的用户个数
     * @param filterData 筛选条件数组，例如: [{ field: "country", operator: "eq", value: "US" }]
     * @returns 满足条件的用户个数
     */
    async countTargetUsers(filterData) {
        try {
            const query = this.buildMongoQuery(filterData);
            const count = await userModel_1.User.countDocuments(query);
            return count;
        }
        catch (error) {
            console.error("Error counting target users:", error);
            throw new Error("Failed to count target users.");
        }
    }
    /**
     * 根据传入的筛选条件查询满足条件的所有用户
     * @param filterData 筛选条件数组，例如: [{ field: "country", operator: "in", value: ["US", "CA"] }]
     * @returns 满足条件的用户列表
     */
    async findTargetUsers(filterData) {
        try {
            const query = this.buildMongoQuery(filterData);
            const users = await userModel_1.User.find(query);
            return users;
        }
        catch (error) {
            console.error("Error finding target users:", error);
            throw new Error("Failed to find target users.");
        }
    }
}
exports.UserTargetingService = UserTargetingService;
