Cui Zhi Feng
2024-08-30 89307d4d3a9c2a278d4aaf9ee472a0f33ae8a05d
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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
package com.mzl.flower.service;
 
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.mzl.flower.base.cache.MarkupCacheClient;
import com.mzl.flower.config.exception.ValidationException;
import com.mzl.flower.config.security.SecurityUtils;
import com.mzl.flower.constant.Constants;
import com.mzl.flower.dto.PriceDTO;
import com.mzl.flower.dto.response.payment.OrderSalesItemChargeDTO;
import com.mzl.flower.entity.customer.Customer;
import com.mzl.flower.entity.flower.*;
import com.mzl.flower.entity.partner.Partner;
import com.mzl.flower.entity.payment.FeeService;
import com.mzl.flower.entity.payment.OrderItemSales;
import com.mzl.flower.entity.payment.OrderSalesItem;
import com.mzl.flower.entity.supplier.Supplier;
import com.mzl.flower.mapper.customer.CustomerMapper;
import com.mzl.flower.mapper.flower.FlowerCategoryMapper;
import com.mzl.flower.mapper.partner.PartnerMapper;
import com.mzl.flower.mapper.supplier.SupplierMapper;
import com.mzl.flower.mapper.system.UserMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
 
import java.math.BigDecimal;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;
 
import static java.time.format.DateTimeFormatter.BASIC_ISO_DATE;
 
public class BaseService {
 
    @Autowired
    protected UserMapper userMapper;
 
    @Autowired
    protected RedisTemplate<String, String> stringRedisTemplate;
 
    @Autowired
    protected SupplierMapper supplierMapper;
 
    @Autowired
    protected PartnerMapper partnerMapper;
 
    @Autowired
    protected CustomerMapper customerMapper;
 
    @Autowired
    protected MarkupCacheClient markupCacheClient;
 
    @Autowired
    private FlowerCategoryMapper flowerCategoryMapper;
 
    protected double getServiceFeeRate(List<FeeService> fees, Integer saleCount){
        int sc = saleCount == null ? 0 : saleCount;
        double rate = 0D;
        if(fees != null && fees.size() > 0){
            for(FeeService f : fees){
                Integer lowerNum = f.getLowerNum();//下限
                Integer upperNum = f.getUpperNum();//上限
                Double feeRate = f.getFeeRate();//费率 %
                if (lowerNum == null && upperNum == null) {
                    continue;
                }
 
                if (lowerNum == null) {
                    if(sc <= upperNum){
                        rate = feeRate;
                        break;
                    }
                } else if (upperNum == null) {
                    if(sc > lowerNum){
                        rate = feeRate;
                        break;
                    }
                } else if (sc <= upperNum && sc > lowerNum) {
                    rate = feeRate;
                    break;
                }
            }
        }
 
        return rate;
    }
 
    protected void prepareSalesAmount(OrderItemSales s
            , Map<String, BigDecimal> supplierSalesMap
            , Map<String, BigDecimal> partnerSalesMap
            , Map<String, BigDecimal> platformSalesMap){
        String orderItemId = s.getOrderItemId();
        if (supplierSalesMap != null) {
            BigDecimal feeSupplier = s.getFeeSupplier();
            BigDecimal b = supplierSalesMap.get(orderItemId);
            if (b == null) {
                b = new BigDecimal(0);
            }
            supplierSalesMap.put(orderItemId, b.add(feeSupplier));
        }
 
        if (partnerSalesMap != null) {
            BigDecimal feePartner = s.getFeePartner();
            BigDecimal b = partnerSalesMap.get(orderItemId);
            if (b == null) {
                b = new BigDecimal(0);
            }
            partnerSalesMap.put(orderItemId, b.add(feePartner));
        }
 
        if (platformSalesMap != null) {
            BigDecimal feePlatform = getAmount(s.getFeePlatform());
            BigDecimal feePlatformPack = getAmount(s.getFeePlatformPack());
            BigDecimal feePlatformCheck = getAmount(s.getFeePlatformCheck());
            BigDecimal feePlatformTransport = getAmount(s.getFeePlatformTransport());
 
            BigDecimal b = platformSalesMap.get(orderItemId);
            if (b == null) {
                b = new BigDecimal(0);
            }
            b = b.add(feePlatform).add(feePlatformPack).add(feePlatformCheck).add(feePlatformTransport);
            platformSalesMap.put(orderItemId, b);
        }
    }
 
    protected BigDecimal getAmount(BigDecimal amount){
        return amount == null ? new BigDecimal(0) : amount;
    }
 
    protected void prepareSalesAmount(OrderSalesItem s
            , Map<String, BigDecimal> supplierSalesMap
            , Map<String, BigDecimal> partnerSalesMap
            , Map<String, BigDecimal> platformSalesMap){//旧
        String orderItemId = s.getOrderItemId();
 
        String chargePersons = s.getChargePersons();
        List<OrderSalesItemChargeDTO> charges = parseArray(chargePersons, OrderSalesItemChargeDTO.class);
        if(charges != null && charges.size() > 0){
            for(OrderSalesItemChargeDTO c : charges){
                BigDecimal amount = c.getAmount() == null ? new BigDecimal(0) : c.getAmount();
                setChargePersonAmount(orderItemId, c.getPersonInCharge(), supplierSalesMap
                        , partnerSalesMap, platformSalesMap, amount);
            }
        } else {
            BigDecimal amount = s.getAmount() == null ? new BigDecimal(0) : s.getAmount();
            setChargePersonAmount(orderItemId, s.getPersonInCharge(), supplierSalesMap
                    , partnerSalesMap, platformSalesMap, amount);
        }
    }
 
    private void setChargePersonAmount(String orderItemId, String personInCharge
            , Map<String, BigDecimal> supplierSalesMap
            , Map<String, BigDecimal> partnerSalesMap
            , Map<String, BigDecimal> platformSalesMap
            , BigDecimal amount){//旧
        if (Constants.CHARGE_PERSON.supplier.name().equals(personInCharge)) {
            if (supplierSalesMap == null) {
                return;
            }
            BigDecimal b = supplierSalesMap.get(orderItemId);
            if (b == null) {
                b = new BigDecimal(0);
            }
            supplierSalesMap.put(orderItemId, b.add(amount));
        } else if (Constants.CHARGE_PERSON.partner.name().equals(personInCharge)) {
            if (partnerSalesMap == null) {
                return;
            }
            BigDecimal b = partnerSalesMap.get(orderItemId);
            if (b == null) {
                b = new BigDecimal(0);
            }
            partnerSalesMap.put(orderItemId, b.add(amount));
        } else if (Constants.CHARGE_PERSON.platform.name().equals(personInCharge)) {
            if (platformSalesMap == null) {
                return;
            }
            BigDecimal b = platformSalesMap.get(orderItemId);
            if (b == null) {
                b = new BigDecimal(0);
            }
            platformSalesMap.put(orderItemId, b.add(amount));
        }
    }
 
    protected BigDecimal getFinalPrice(Long partnerId, Long categoryId, Long flowerId, BigDecimal price, String level){
        PriceDTO dto = getFinalPriceDTO(partnerId, categoryId, flowerId, price, level);
 
        return dto.getPrice();
    }
 
    protected PriceDTO getFinalPriceDTO(Long partnerId, Long categoryId, Long flowerId, BigDecimal price, String level){
        PriceDTO dto = new PriceDTO();
        dto.setOrgPrice(price);
 
        List<FlowerMarkupPl> plList = markupCacheClient.getMarkupPl();
        BigDecimal fp = new BigDecimal(0);
        fp = fp.add(price);
 
        if(plList != null && plList.size() > 0){//区间加价
            BigDecimal b = new BigDecimal(0);
            for(FlowerMarkupPl pl : plList){
                BigDecimal lowerPrice = pl.getLowerPrice();
                BigDecimal upperPrice = pl.getUpperPrice();
                if(lowerPrice == null && upperPrice == null){
                    continue;
                }
 
                if (lowerPrice == null) {
                    if(fp.compareTo(upperPrice) <= 0) {
                        b = pl.getFee();
                        break;
                    }
                } else if (upperPrice == null) {
                    if(fp.compareTo(lowerPrice) > 0){
                        b = pl.getFee();
                        break;
                    }
                } else if (fp.compareTo(upperPrice) <= 0 && fp.compareTo(lowerPrice) > 0) {
                    b = pl.getFee();
                    break;
                }
            }
            fp = fp.add(b);
            dto.setFirstFee(b);
        }
 
        if(partnerId != null){//二次加价和合伙人加价
            FlowerCategory fc = flowerCategoryMapper.selectById(categoryId);
            Long fcpId = fc == null ? null : fc.getParentId();
            FlowerMarkupPsSpCg ppsc = null;
            if(fcpId != null){//父类加价
                ppsc = markupCacheClient.getMarkupPsSpCg(partnerId, fcpId);
            }
            FlowerMarkupPsSpCg psc = markupCacheClient.getMarkupPsSpCg(partnerId, categoryId);
            FlowerMarkupPsSp ps = markupCacheClient.getMarkupPsSp(partnerId, flowerId);
            BigDecimal b = new BigDecimal(0);
            if(ps != null){
                b = ps.getFee();
            } else if (psc != null) {
                switch (level) {
                    case "A":
                        b = psc.getLevelA();
                        break;
                    case "B":
                        b = psc.getLevelB();
                        break;
                    case "C":
                        b = psc.getLevelC();
                        break;
                    case "D":
                        b = psc.getLevelD();
                        break;
                    case "E":
                        b = psc.getLevelE();
                        break;
                    case "O":
                        b = psc.getLevelO();
                        break;
                }
            } else if (ppsc != null) {
                switch (level) {
                    case "A":
                        b = ppsc.getLevelA();
                        break;
                    case "B":
                        b = ppsc.getLevelB();
                        break;
                    case "C":
                        b = ppsc.getLevelC();
                        break;
                    case "D":
                        b = ppsc.getLevelD();
                        break;
                    case "E":
                        b = ppsc.getLevelE();
                        break;
                    case "O":
                        b = ppsc.getLevelO();
                        break;
                }
            }
            fp = fp.add(b);
            dto.setSecondFee(b);
 
            FlowerMarkupSpCg fmpsc = null;
            if(fcpId != null){//父类加价
                fmpsc = markupCacheClient.getMarkupSpCg(partnerId, fcpId);
            }
            FlowerMarkupSpCg sc = markupCacheClient.getMarkupSpCg(partnerId, categoryId);
            FlowerMarkupSp s = markupCacheClient.getMarkupSp(partnerId, flowerId);
            b = new BigDecimal(0);
            if(s != null){
                b = s.getFee();
            } else if (sc != null) {
                switch (level) {
                    case "A":
                        b = sc.getLevelA();
                        break;
                    case "B":
                        b = sc.getLevelB();
                        break;
                    case "C":
                        b = sc.getLevelC();
                        break;
                    case "D":
                        b = sc.getLevelD();
                        break;
                    case "E":
                        b = sc.getLevelE();
                        break;
                    case "O":
                        b = sc.getLevelO();
                        break;
                }
            } else if (fmpsc != null) {
                switch (level) {
                    case "A":
                        b = fmpsc.getLevelA();
                        break;
                    case "B":
                        b = fmpsc.getLevelB();
                        break;
                    case "C":
                        b = fmpsc.getLevelC();
                        break;
                    case "D":
                        b = fmpsc.getLevelD();
                        break;
                    case "E":
                        b = fmpsc.getLevelE();
                        break;
                    case "O":
                        b = fmpsc.getLevelO();
                        break;
                }
            }
            fp = fp.add(b);
            dto.setPartnerFee(b);
        }
 
        dto.setPrice(fp);
        return dto;
    }
 
    protected BigDecimal getFinalPriceForPartner(Long partnerId, Long categoryId, Long flowerId, BigDecimal price, String level){
 
        List<FlowerMarkupPl> plList = markupCacheClient.getMarkupPl();
        BigDecimal fp = new BigDecimal(0);
        fp = fp.add(price);
 
        if(plList != null && plList.size() > 0){//区间加价
            BigDecimal b = new BigDecimal(0);
            for(FlowerMarkupPl pl : plList){
                BigDecimal lowerPrice = pl.getLowerPrice();
                BigDecimal upperPrice = pl.getUpperPrice();
                if(lowerPrice == null && upperPrice == null){
                    continue;
                }
 
                if (lowerPrice == null) {
                    if(fp.compareTo(upperPrice) <= 0) {
                        b = pl.getFee();
                        break;
                    }
                } else if (upperPrice == null) {
                    if(fp.compareTo(lowerPrice) > 0){
                        b = pl.getFee();
                        break;
                    }
                } else if (fp.compareTo(upperPrice) <= 0 && fp.compareTo(lowerPrice) > 0) {
                    b = pl.getFee();
                    break;
                }
            }
            fp = fp.add(b);
        }
 
        //二次加价
        FlowerCategory fc = flowerCategoryMapper.selectById(categoryId);
        Long fcpId = fc == null ? null : fc.getParentId();
        FlowerMarkupPsSpCg ppsc = null;
        if(fcpId != null){//父类加价
            ppsc = markupCacheClient.getMarkupPsSpCg(partnerId, fcpId);
        }
        FlowerMarkupPsSpCg psc = markupCacheClient.getMarkupPsSpCg(partnerId, categoryId);
        FlowerMarkupPsSp ps = markupCacheClient.getMarkupPsSp(partnerId, flowerId);
        BigDecimal b = new BigDecimal(0);
        if(ps != null){
            b = ps.getFee();
        } else if (psc != null) {
            switch (level) {
                case "A":
                    b = psc.getLevelA();
                    break;
                case "B":
                    b = psc.getLevelB();
                    break;
                case "C":
                    b = psc.getLevelC();
                    break;
                case "D":
                    b = psc.getLevelD();
                    break;
                case "E":
                    b = psc.getLevelE();
                    break;
                case "O":
                    b = psc.getLevelO();
                    break;
            }
        } else if (ppsc != null) {
            switch (level) {
                case "A":
                    b = ppsc.getLevelA();
                    break;
                case "B":
                    b = ppsc.getLevelB();
                    break;
                case "C":
                    b = ppsc.getLevelC();
                    break;
                case "D":
                    b = ppsc.getLevelD();
                    break;
                case "E":
                    b = ppsc.getLevelE();
                    break;
                case "O":
                    b = ppsc.getLevelO();
                    break;
            }
        }
        fp = fp.add(b);
 
        return fp;
    }
 
    protected Long getCurrentCustomerPartner(){
        Customer p = getCurrentCustomerWithoutCheck();
        return p == null ? null : p.getPartnerId();
    }
 
    protected Customer getCurrentCustomerWithoutCheck(){
        String userId = SecurityUtils.getUserId();
        Customer p = customerMapper.selectOne(new QueryWrapper<Customer>()
                .eq("user_id", userId));
 
        return p;
    }
 
    protected Customer getCurrentCustomer(){
        Customer p = getCurrentCustomerWithoutCheck();
 
        if(p == null){
            throw new ValidationException("客户不存在");
        }
 
        return p;
    }
 
    public Partner getCurrentPartner(){
        String userId = SecurityUtils.getUserId();
        Partner p = partnerMapper.selectOne(new QueryWrapper<Partner>()
                .eq("user_id", userId));
 
        if(p == null){
            throw new ValidationException("合伙人信息未认证,请到个人中心进行认证");
        }
 
        return p;
    }
 
    public Supplier getCurrentSupplier(){
        String userId = SecurityUtils.getUserId();
        Supplier s = supplierMapper.selectOne(new QueryWrapper<Supplier>()
                .eq("user_id", userId));
        if(s == null){
            throw new ValidationException("供应商信息未认证,请到个人中心进行认证");
        }
 
        return s;
    }
 
    protected String formatTime(LocalDateTime start, LocalDateTime end){
        long s = start.toEpochSecond(ZoneOffset.UTC);
        long e = end.toEpochSecond(ZoneOffset.UTC);
 
        long du = e - s;
        long m = du / 60;
        if(m == 0){
            return du + "秒";
        }
        long dl = du % 60;
        long h = m / 60;
        if(h == 0){
            return m + "分钟" +  dl + "秒";
        }
        long ml = m % 60;
        long d = h / 24;
        if(d == 0){
            return h + "小时" + ml + "分钟" +  dl + "秒";
        }
        long hl = h % 24;
        return d + "天" + hl + "小时" + ml + "分钟" +  dl + "秒";
    }
 
    public String format(ZonedDateTime dateTime, String format) {
        if (dateTime == null || format == null) {
            return null;
        }
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(format).withZone(ZoneId.of("Asia/Shanghai"));
        return dateTimeFormatter.format(dateTime);
    }
 
    public String format(LocalDateTime dateTime, String format) {
        if (dateTime == null || format == null) {
            return null;
        }
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(format).withZone(ZoneId.of("Asia/Shanghai"));
        return dateTimeFormatter.format(dateTime);
    }
 
    protected LocalDateTime parseLocalDateTime(final String dateStr, String format) {
        if (StringUtils.isBlank(dateStr)) {
            return null;
        }
        DateTimeFormatter dateTimeFormatter = BASIC_ISO_DATE;
        if (StringUtils.isNotBlank(format)) {
            dateTimeFormatter = DateTimeFormatter.ofPattern(format);
        }
        return LocalDateTime.parse(dateStr,dateTimeFormatter);
    }
 
    public LocalDate parseLocalDate(final String dateStr){
        if(StringUtils.isEmpty(dateStr)){
            return null;
        }
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        LocalDate date = LocalDate.parse(dateStr, dateTimeFormatter);
 
        return date;
    }
 
    public LocalDateTime parseLocalDateTime(final String dateStr, final boolean isStart) {
        return parseLocalDateTime(dateStr, isStart, "yyyy-MM-dd");
    }
 
    protected LocalDateTime parseLocalDateTime(final String dateStr, final boolean isStart, String format) {
        LocalDateTime dateTime = null;
        if (StringUtils.isNotBlank(dateStr)) {
            DateTimeFormatter dateTimeFormatter = BASIC_ISO_DATE;
            if (StringUtils.isNotBlank(format)) {
                dateTimeFormatter = DateTimeFormatter.ofPattern(format);
            }
            LocalDate date = LocalDate.parse(dateStr, dateTimeFormatter);
            if (isStart) {
                dateTime = date.atTime(0, 0, 0);
            } else {
                dateTime = date.atTime(23, 59, 59);
            }
        }
        return dateTime;
    }
 
    protected LocalDateTime parseLocalDateTime(final String dateStr, int hour
            , int minute, int second, long plusDays) {
        LocalDateTime dateTime = null;
        if (StringUtils.isNotBlank(dateStr)) {
            DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
            LocalDate date = LocalDate.parse(dateStr, dateTimeFormatter).plusDays(plusDays);
            dateTime = date.atTime(hour, minute, second);
        }
 
        return dateTime;
    }
 
    protected LocalDateTime parseLocalDateTime(String dateStr){
        if(StringUtils.isEmpty(dateStr)){
            return null;
        }
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        return LocalDateTime.parse(dateStr, dateTimeFormatter);
    }
 
    protected List<String> splitParam(String param) {
        if (StringUtils.isNotEmpty(param)) {
            String[] params = param.split(",");
            List<String> levelList = new ArrayList<>();
            for (String l : params) {
                levelList.add(l.trim());
            }
 
            return levelList;
        }
 
        return null;
    }
 
    protected  <T> List<T> parseArray(String text, Class<T> clazz) {
        List<T> result = new ArrayList<>();
        if(StringUtils.isNotEmpty(text)){
            try {
                result = JSON.parseArray(text, clazz);
            }catch (Exception e){
            }
        }
 
        return result;
    }
 
    protected String toJSONString(Object o){
        try{
            return JSON.toJSONString(o);
        }catch (Exception e){
        }
 
        return null;
    }
 
    protected <T> T parseObject(String text, Class<T> clazz){
        try{
            return JSON.parseObject(text, clazz);
        }catch (Exception e){
        }
 
        return null;
    }
 
}