P15GEN2\59518
2024-05-29 d4210c7c4b04abde20037ea8aa0f54ef8a2649aa
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
package policy;
 
import java.util.Date;
import java.util.List;
 
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
 
import foundation.action.ActionProvider;
import foundation.dao.DataPackage;
import foundation.dao.DataSource;
import foundation.data.entity.Entity;
import foundation.data.object.DataObject;
import foundation.persist.NamedSQL;
import foundation.token.IOnlineUser;
import foundation.util.Util;
import policy.price.OnsitesResult;
import policy.price.PolicyBucket;
import policy.price.PricesResult;
import policy.price.outline.DomainOnsiteResult;
import policy.price.outline.DomainPriceResult;
import policy.price.outline.OnsiteOutline;
import policy.price.outline.PricesOutline;
import policy.rule.CheckBoard;
import policy.rule.RuleBucket;
import policy.rule.RuleRuntimes;
 
public class PolicyCenter extends ActionProvider {
    
    
    protected static Logger logger;
    
    static {
        logger = LogManager.getLogger(PolicyCenter.class);
    }
    
    @Override
    protected void publishMethod() {
        //1. 获取价格列表
        addMethod("getPriceList");
        
        //2. 获取即时政策
        addMethod("getOnsiteDiscounts");
        
        //3. 下单前获取下单上下文(是否可下单,可下单公司、BU等)
        addMethod("getOrderContext");
        
        //4.1 订单计算
        addMethod("orderCalculate");
        
        //4.2 订单检查(保存)
        addMethod("policyCheckSave");
        
        //4.3 订单检查(提交)
        addMethod("policyCheckCommit");
        
        //4.4 订单检查(生效)
        addMethod("policyCheckOpen");
        
        //4.5 订单检查看板
        addMethod("policyCheckBoard");        
        
        //5. 获取价格概况
        addMethod("getPriceOutline");
        
        //6. 获取折扣概况 
        addMethod("getOnsiteOutline");
        
        //7. 获取当前BU下的订单检查规则 
        addMethod("getChenckRulesByBU");
        
        //8. 刷新价格列表
        addMethod("refreshPriceList");
        
        //9. 刷新即时政策
        addMethod("refreshOnsiteList");
        
        //10. 根据终止日期刷新状态(备案,协议,价格)
        addMethod("refreshStateBySuspendDate");
    }
 
    public void getPriceList() throws Exception {
        String customerId = dataReader.getString("customer_id");
        List<String> skuIdList = dataReader.getStringList("sku_id");
        boolean includeNotActive = dataReader.getBoolean("include_not_active", false);
 
        if (Util.isEmpty(customerId) && skuIdList.isEmpty()) {
            return;
        }
        
        //1. 刷新价格
        PolicyLoader.refreshPriceList();
        
        //2. 获取价格
        PricesResult pricesResult = new PricesResult(skuIdList, includeNotActive);
        
        PolicyBucket policyBucket = PolicyBucket.getInstance();
        policyBucket.getPriceList(customerId, pricesResult);
        
        //3. 返回价格
        dataWriter.addValue(pricesResult);
    }
    
    public void getOnsiteDiscounts() throws Exception {
        String customerId = dataReader.getString("customer_id");
        String productLineId = dataReader.getString("bu_id");
        boolean includeNotActive = dataReader.getBoolean("include_not_active", false);
        
        if (Util.isEmpty(customerId) || Util.isEmpty(productLineId)) {
            dataWriter.reportOneError("getOnsiteDiscounts", "产品线或客户为空,无法获取当前客户优惠政策...");
            return;
        }
        
        //1. 刷新政策
        PolicyLoader.refreshOnsiteList();
        
        //2. 获取政策
        OnsitesResult qtyResult = new OnsitesResult(includeNotActive);
        
        PolicyBucket policyBucket = PolicyBucket.getInstance();
        policyBucket.getOnsiteDiscounts(customerId, productLineId, qtyResult);
        
        //3. 返回政策
        dataWriter.addValue("onsiteDiscounts", qtyResult);
    }
    
    public void getOrderContext() throws Exception {
        
    }
    
    public void orderCalculate() throws Exception {
        DataPackage dataPackage = dataReader.getDataPackage();
        
        //1. 获取BU
        String buId = getBUId(dataPackage);
        
        if (Util.isEmpty(buId)) {
            logger.error("无法获得订单BU");
            dataWriter.reportOneError("policyCheckOpen", "无法获得订单BU");
        }
        
        //2. 获取校验规则,进行校验
        RuleRuntimes rules = RuleBucket.getRuleRuntimes(buId);
        rules.calculateData(dataPackage, dataWriter);
        dataPackage.setResultData(DataSource.DB);
        
        //3 返回结果
        dataWriter.setDataPackage(dataPackage);
    }
    
    public void policyCheckSave() throws Exception {
        DataPackage dataPackage = dataReader.getDataPackage();
        
        //1. 获取BU
        String buId = getBUId(dataPackage);
        
        if (Util.isEmpty(buId)) {
            logger.error("无法获得订单BU");
            dataWriter.reportOneError("policyCheckOpen", "无法获得订单BU");
        }
        
        //2. 获取校验规则,进行校验
        RuleRuntimes rules = RuleBucket.getRuleRuntimes(buId);
        rules.check(dataPackage, OrderEvent.Save, dataWriter);
    }
    
    public void policyCheckCommit() throws Exception {
        DataPackage dataPackage = dataReader.getDataPackage();
        
        //1. 获取BU
        String buId = getBUId(dataPackage);
        if (Util.isEmpty(buId)) {
            logger.error("无法获得订单BU");
            dataWriter.reportOneError("policyCheckOpen", "无法获得订单BU");
        }
        
        //2. 获取校验规则,进行校验
        RuleRuntimes rules = RuleBucket.getRuleRuntimes(buId);
        boolean success = rules.check(dataPackage, OrderEvent.Commit, dataWriter);
        
        if (!success) {
            task.terminate();
        }
    }
    
    public void policyCheckOpen() throws Exception {
        DataPackage dataPackage = dataReader.getDataPackage();
        
        //1. 获取BU
        String buId = getBUId(dataPackage);
        
        if (Util.isEmpty(buId)) {
            logger.error("无法获得订单BU");
            dataWriter.reportOneError("policyCheckOpen", "无法获得订单BU");
        }
        
        //2. 获取校验规则,进行校验
        RuleRuntimes rules = RuleBucket.getRuleRuntimes(buId);
        rules.check(dataPackage, OrderEvent.Open, dataWriter);
    }
    
    public void policyCheckBoard() throws Exception {
        DataPackage dataPackage = dataReader.getDataPackage();
        IOnlineUser onlineUser = IOnlineUser.getInstance();
        
        CheckBoard result = new CheckBoard();
        
        //1. 获取BU
        String buId = onlineUser.getBUId();
        if (Util.isEmpty(buId)) {
            Entity master = dataPackage.getMasterEntity();
            buId = master.getString("bu_id");
        }
        
        if (Util.isEmpty(buId)) {
            logger.error("无法获得订单BU");
            dataWriter.addValue(result);
        }
        
        //2. 获取校验规则,进行校验
        RuleRuntimes rules = RuleBucket.getRuleRuntimes(buId);
        rules.createCheckBoard(dataPackage, result, dataWriter);
        
        //3 返回结果
        dataWriter.addValue(result);        
    }
    
    public void getPriceOutline() throws Exception {
        String buId = dataReader.getString("bu_id");
        String productId = dataReader.getString("product_id");
        String skuId = dataReader.getString("sku_id");
        
        PolicyBucket policyBucket = PolicyBucket.getInstance();
        
        //1. 刷新价格
        PolicyLoader.refreshPriceList();
        
        //2. 查询某个SKU的价格概览
        if (!Util.isEmpty(skuId)) {
            DomainPriceResult result = policyBucket.getSKUPriceList(skuId);
            dataWriter.addValue(result);
            return;
        }
        
        //3. 查询某个产品的价格概览
        if (!Util.isEmpty(productId)) {
            DomainPriceResult result = policyBucket.getProductPriceList(productId);
            dataWriter.addValue(result);
            return;
        }
        
        //4. 查询某个产品线的价格概览
        if (!Util.isEmpty(buId)) {
            DomainPriceResult result = policyBucket.getProductLinePriceList(buId);
            dataWriter.addValue(result);
            return;
        }
        
        //5. 获取全部价格概览
        PricesOutline pricesOutline = new PricesOutline();
        policyBucket.getPriceOutline(pricesOutline);
        dataWriter.addValue(pricesOutline);
    }
    
    public void getOnsiteOutline() throws Exception {
        String buId = dataReader.getString("bu_id");
        String productId = dataReader.getString("product_id");
        String skuId = dataReader.getString("sku_id");
        
        PolicyBucket policyBucket = PolicyBucket.getInstance();
        
        //1. 刷新政策
        PolicyLoader.refreshOnsiteList();
        
        //2. 查询某个SKU的价格概览
        if (!Util.isEmpty(skuId)) {
            DomainOnsiteResult result = policyBucket.getSKUOnsiteList(skuId);
            dataWriter.addValue(result);
            return;
        }
        
        //3. 查询某个产品的价格概览
        if (!Util.isEmpty(productId)) {
            DomainOnsiteResult result = policyBucket.getProductOnsiteList(productId);
            dataWriter.addValue(result);
            return;
        }
        
        //4. 查询某个产品线的价格概览
        if (!Util.isEmpty(buId)) {
            DomainOnsiteResult result = policyBucket.getProductLineOnsiteList(buId);
            dataWriter.addValue(result);
            return;
        }
        
        //5. 获取政策
        OnsiteOutline onsiteOutline = new OnsiteOutline();
        policyBucket.getOnsiteOutline(onsiteOutline);
        dataWriter.addValue(onsiteOutline);
    }
    
    public void getChenckRulesByBU() throws Exception {
        CheckBoard result = new CheckBoard();
        
        //1. 获取BU
        String buId = dataReader.getString("bu_id");
        
        if (Util.isEmpty(buId)) {
            logger.error("无法获得订单BU");
            dataWriter.addValue(result);
        }
        
        //2. 获取校验规则
        RuleRuntimes rules = RuleBucket.getRuleRuntimes(buId);
        
        //3 返回结果
        dataWriter.addValue(rules);    
    }
    
    public void refreshPriceList() throws Exception {
        int cnt = PolicyLoader.refreshPriceList();
        dataWriter.addValue(cnt);
    }
    
    public void refreshOnsiteList() throws Exception {
        int cnt = PolicyLoader.refreshOnsiteList();
        dataWriter.addValue(cnt);
    }
    
    private String getBUId(DataPackage dataPackage) throws Exception {
        String buId = null;
        Entity master = dataPackage.getMasterEntity(DataSource.DB, DataSource.Request);
        
        if (master != null) {
            buId = master.getString("bu_id");
        }
        
        return buId;
    }
 
    public void refreshStateBySuspendDate() throws Exception {
        String dataName = null;
        
        //1. 页面请求会执行
        if (dataReader != null) {
            dataName = dataReader.getDomain().getDataName();
        }
        
        //2. 定时任务会执行
        if (Util.isEmpty(dataName)) {
            dataName = context.getStepParam();
        }
        
        //3. 获取表名
        if (Util.isEmpty(dataName)) {
            logger.error("dataname is empty, skip refreshStateBySuspendDate");
            return;
        }
        
        DataObject dataObject = DataObject.getInstance(dataName);
        String tableName = dataObject.getTableName();
        
        //3. 
        NamedSQL namedSQL = NamedSQL.getInstance("refreshStateBySuspendDate");
        namedSQL.setParam("tableName", tableName);
        namedSQL.setParam("currentDate", new Date(), true);
        int exeCount = namedSQL.execute();
        
        logger.info("失效 dataName:{}, {} 条", dataName, exeCount);
    }
}