陶杰
2024-10-28 021496babb52a859294a8475ffef330009b0167c
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
package com.mzl.flower.service.impl.wallet;
 
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.mzl.flower.config.security.SecurityUtils;
import com.mzl.flower.dto.request.wallet.QueryWalletAmountDTO;
import com.mzl.flower.dto.response.supplier.SupplierDTO;
import com.mzl.flower.entity.coupon.CouponTemplateDO;
import com.mzl.flower.entity.wallet.WalletDO;
import com.mzl.flower.enums.TrueOrFalseEnum;
import com.mzl.flower.mapper.wallet.WalletMapper;
import com.mzl.flower.service.supplier.SupplierService;
import com.mzl.flower.service.wallet.WalletReduceService;
import com.mzl.flower.service.wallet.WalletService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.regex.Pattern;
 
/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author @TaoJie
 * @since 2024-10-22
 */
@Service
public class WalletServiceImpl extends ServiceImpl<WalletMapper, WalletDO> implements WalletService {
 
    @Autowired
    private SupplierService supplierService;
 
    @Autowired
    private WalletReduceService walletReduceService;
 
    private static final String DATE_TIME_PATTERN = "yyyy-MM-dd HH:mm:ss";
    private static final Pattern dateTimeRegex = Pattern.compile("^\\d{4}-\\d{2}-\\d{2} \\d{2}:\\d{2}:\\d{2}$");
 
 
    @Override
    public String getWalletOnLineTime() {
        String walletOnlineTime =baseMapper.selectWalletOnlineTime();
        // 这里需要判断walletOnlineTime是不是一个正确的yyyy-mm-dd hh24:mi:ss的时间
        String startTime;
        if (isValidDateTime(walletOnlineTime)) {
            startTime = walletOnlineTime;
        } else {
            // 如果 walletOnlineTime 无效,则使用当前时间的字符串
            startTime = getCurrentDateTime();
        }
        return startTime;
    }
 
    @Override
    public WalletDO getCurrentSupplier() {
        final SupplierDTO currentSupplier = supplierService.getCurrentSupplier();
        // 根据供应商ID获取钱包信息
 
            // 查询的条件的deleted的字段得为0
            // 下面的单独封装成一个方法
            WalletDO walletDO = getBySupplierId(currentSupplier.getId());
 
            if(null==walletDO){
                // 先创建一个钱包
                walletDO=new WalletDO();
                walletDO.setUserId(SecurityUtils.getUserId());
                walletDO.setSupplierId(currentSupplier.getId());
                walletDO.setTotalAmount(BigDecimal.ZERO);
                walletDO.setWithdrawableAmount(BigDecimal.ZERO);
                //把所有涉及BigDecimal的金额都设置为0
                walletDO.setWithdrawingAmount(BigDecimal.ZERO);
                walletDO.setWithdrawnAmount(BigDecimal.ZERO);
                walletDO.setSettlingAmount(BigDecimal.ZERO);
                walletDO.setTotalDeduction(BigDecimal.ZERO);
                walletDO.setTotalTransactionAmount(BigDecimal.ZERO);
                walletDO.setSettlingAmount(BigDecimal.ZERO);
                walletDO.create(SecurityUtils.getUserId());
                baseMapper.insert(walletDO);
                // 将再次查询的结果返回
                walletDO=getBySupplierId(currentSupplier.getId());
            }
 
 
        return walletDO;
    }
 
    @Override
    public WalletDO getBySupplierId(Long supplierId) {
        // 不用getOne,用获取list的第一个元素
 
        List<WalletDO> walletDOS = baseMapper.selectList(new LambdaQueryWrapper<WalletDO>()
                .eq(WalletDO::getDeleted, TrueOrFalseEnum.FALSE.isFlag())
                .eq(WalletDO::getSupplierId, supplierId));
        if(!CollectionUtils.isEmpty(walletDOS)){
            return walletDOS.get(0);
        }else return null;
    }
 
    @Override
    public BigDecimal getWaittingSettlementAmount(WalletDO walletDO) {
 
        return baseMapper.getWaittingSettlementAmount(walletDO);
    }
 
    @Override
    public BigDecimal getSupplierTotalTransactionAmount(WalletDO walletDO) {
 
        QueryWalletAmountDTO queryWalletAmountDTO=new QueryWalletAmountDTO();
        queryWalletAmountDTO.setSupplierId(walletDO.getSupplierId());
        queryWalletAmountDTO.setStartTime(getWalletOnLineTime());
        queryWalletAmountDTO.setUserId(walletDO.getUserId());
 
        BigDecimal totalTransactionAmount=baseMapper.getSupplierTotalTransactionAmount(queryWalletAmountDTO);
 
        return totalTransactionAmount;
    }
 
    @Override
    public BigDecimal getSupplierDeductAmount(WalletDO walletDO) {
        QueryWalletAmountDTO queryWalletAmountDTO=new QueryWalletAmountDTO();
        queryWalletAmountDTO.setSupplierId(walletDO.getSupplierId());
        queryWalletAmountDTO.setStartTime(getWalletOnLineTime());
        queryWalletAmountDTO.setUserId(walletDO.getUserId());
 
        // 质检扣款
        BigDecimal checkDeduceAmount = walletReduceService.getCheckReduceAmount(queryWalletAmountDTO);
        checkDeduceAmount = checkDeduceAmount != null ? checkDeduceAmount : BigDecimal.ZERO;
        // 售后扣款
        BigDecimal saleDeduceAmount = walletReduceService.getSaleReduceAmount(queryWalletAmountDTO);
        saleDeduceAmount = saleDeduceAmount != null ? saleDeduceAmount : BigDecimal.ZERO;
 
        // 总扣款
        BigDecimal deduceAmount = checkDeduceAmount.add(saleDeduceAmount);
 
        return deduceAmount;
    }
 
 
    public static boolean isValidDateTime(String dateTime) {
        if (dateTime == null || !dateTimeRegex.matcher(dateTime).matches()) {
            return false;
        }
        SimpleDateFormat sdf = new SimpleDateFormat(DATE_TIME_PATTERN);
        sdf.setLenient(false);
        try {
            sdf.parse(dateTime);
            return true;
        } catch (Exception e) {
            return false;
        }
    }
 
    public static String getCurrentDateTime() {
        SimpleDateFormat sdf = new SimpleDateFormat(DATE_TIME_PATTERN);
        return sdf.format(new Date());
    }
}