const AdPricingRuleModel = require('../models/AdPricingRuleModel') /** * دقیق‌ترین قانونِ منطبق رو پیدا می‌کنه — یه قانون منطبقه اگه هر فیلدِ غیرِnullش دقیقاً با * سناریوی داده‌شده یکی باشه (فیلدِ null همیشه صدق می‌کنه، یعنی وایلدکارت). بینِ قانون‌های * منطبق، اونی که بیشترین فیلدِ غیرِnull (دقیق‌ترین) رو داره برنده‌ست. * gender/expertise/provinceId که خودِ کاربر انتخاب نکرده (یعنی «هرکسی»/«فرقی نمی‌کنه») باید * null پاس داده بشه، نه رشته‌ی خالی — وگرنه هیچ قانونِ وایلدکاردی هم منطبق نمی‌شه. * subExpertise آرایه‌ست (تبلیغ می‌تونه چند زیرتخصص هدف‌گذاری کنه) — قانونِ subExpertise * وقتی منطبقه که همون یکی مقدارش توی این آرایه باشه */ async function resolveAdPrice (adType, { provinceId, gender, expertise, subExpertise } = {}) { const rules = await AdPricingRuleModel.find({ ad_type: adType }).lean() const subExpertiseList = Array.isArray(subExpertise) ? subExpertise : [] let best = null let bestScore = -1 for (const rule of rules) { if (rule.province_id != null && rule.province_id !== provinceId) continue if (rule.gender != null && rule.gender !== gender) continue if (rule.expertise != null && rule.expertise !== expertise) continue if (rule.sub_expertise != null && !subExpertiseList.includes(rule.sub_expertise)) continue const score = (rule.province_id != null ? 1 : 0) + (rule.gender != null ? 1 : 0) + (rule.expertise != null ? 1 : 0) + (rule.sub_expertise != null ? 1 : 0) if (score > bestScore) { bestScore = score best = rule } } return best ? best.price : null } module.exports = { resolveAdPrice }