更新
This commit is contained in:
110
src/hooks/usePurchaseRules.ts
Normal file
110
src/hooks/usePurchaseRules.ts
Normal file
@@ -0,0 +1,110 @@
|
||||
import { ref, computed, watch } from 'vue';
|
||||
import { evaluateRules, getEnabledRules } from '/@/api/purchase/purchasingRuleConfig';
|
||||
|
||||
const RULE_CACHE_KEY = 'purchase_rule_cache';
|
||||
const CACHE_EXPIRE_TIME = 5 * 60 * 1000;
|
||||
|
||||
interface RuleCache {
|
||||
data: any[];
|
||||
timestamp: number;
|
||||
}
|
||||
|
||||
interface RuleResult {
|
||||
purchaseMode: string;
|
||||
purchaseSchool: string;
|
||||
bidTemplate: string;
|
||||
requiredFiles: { fieldName: string; fileTypeName: string; required: boolean }[];
|
||||
matchedRules: any[];
|
||||
}
|
||||
|
||||
const ruleCache = ref<RuleCache | null>(null);
|
||||
|
||||
export function usePurchaseRules() {
|
||||
const loading = ref(false);
|
||||
const rules = ref<any[]>([]);
|
||||
|
||||
const loadRules = async (forceRefresh = false) => {
|
||||
if (!forceRefresh && ruleCache.value) {
|
||||
const now = Date.now();
|
||||
if (now - ruleCache.value.timestamp < CACHE_EXPIRE_TIME) {
|
||||
rules.value = ruleCache.value.data;
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
loading.value = true;
|
||||
try {
|
||||
const res = await getEnabledRules();
|
||||
rules.value = res.data || [];
|
||||
ruleCache.value = {
|
||||
data: rules.value,
|
||||
timestamp: Date.now()
|
||||
};
|
||||
} catch (e) {
|
||||
console.error('加载采购规则失败', e);
|
||||
} finally {
|
||||
loading.value = false;
|
||||
}
|
||||
};
|
||||
|
||||
const evaluate = async (params: {
|
||||
budget: number;
|
||||
isCentralized?: string;
|
||||
isSpecial?: string;
|
||||
hasSupplier?: string;
|
||||
projectType?: string;
|
||||
}): Promise<RuleResult | null> => {
|
||||
try {
|
||||
const res = await evaluateRules(params);
|
||||
return res.data;
|
||||
} catch (e) {
|
||||
console.error('评估规则失败', e);
|
||||
return null;
|
||||
}
|
||||
};
|
||||
|
||||
const getThresholds = () => {
|
||||
const thresholds: Record<string, number> = {
|
||||
deptPurchase: 50000,
|
||||
feasibility: 300000,
|
||||
publicSelect: 400000,
|
||||
govPurchase: 1000000
|
||||
};
|
||||
|
||||
rules.value.forEach(rule => {
|
||||
if (rule.ruleCode === 'DEPT_PURCHASE_THRESHOLD' && rule.amountMax) {
|
||||
thresholds.deptPurchase = Number(rule.amountMax);
|
||||
}
|
||||
if (rule.ruleCode === 'FEASIBILITY_REPORT' && rule.amountMin) {
|
||||
thresholds.feasibility = Number(rule.amountMin);
|
||||
}
|
||||
if (rule.ruleCode === 'PUBLIC_BID_40W_100W' && rule.amountMin) {
|
||||
thresholds.publicSelect = Number(rule.amountMin);
|
||||
}
|
||||
if (rule.ruleCode === 'GOV_PURCHASE_THRESHOLD' && rule.amountMin) {
|
||||
thresholds.govPurchase = Number(rule.amountMin);
|
||||
}
|
||||
});
|
||||
|
||||
return thresholds;
|
||||
};
|
||||
|
||||
loadRules();
|
||||
|
||||
return {
|
||||
loading,
|
||||
rules,
|
||||
loadRules,
|
||||
evaluate,
|
||||
getThresholds
|
||||
};
|
||||
}
|
||||
|
||||
let globalRulesInstance: ReturnType<typeof usePurchaseRules> | null = null;
|
||||
|
||||
export function usePurchaseRulesSingleton() {
|
||||
if (!globalRulesInstance) {
|
||||
globalRulesInstance = usePurchaseRules();
|
||||
}
|
||||
return globalRulesInstance;
|
||||
}
|
||||
Reference in New Issue
Block a user