cloudroam
2024-10-23 843dd3a17de187191cf7c442062b3de678bdad65
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
package com.mzl.flower.service.impl.wallet;
 
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mzl.flower.config.PyamentV3Configurer;
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.request.payment.TransferDetailReqDTO;
import com.mzl.flower.dto.request.payment.TransferReqDTO;
import com.mzl.flower.dto.request.wallet.CreateWalletWithdrawRecordDTO;
import com.mzl.flower.dto.request.wallet.QueryWalletDTO;
import com.mzl.flower.dto.request.wallet.WalletWithdrawRecordDTO;
import com.mzl.flower.dto.response.supplier.SupplierDTO;
import com.mzl.flower.dto.response.wallet.WalletWithdrawRecordVO;
import com.mzl.flower.entity.supplier.Supplier;
import com.mzl.flower.entity.system.UserWechat;
import com.mzl.flower.entity.wallet.WalletBillRecordDO;
import com.mzl.flower.entity.wallet.WalletDO;
import com.mzl.flower.entity.wallet.WalletWithdrawRecordDO;
import com.mzl.flower.mapper.supplier.SupplierMapper;
import com.mzl.flower.mapper.system.UserWechatMapper;
import com.mzl.flower.mapper.wallet.WalletBillRecordMapper;
import com.mzl.flower.mapper.wallet.WalletWithdrawRecordMapper;
import com.mzl.flower.service.payment.UserPaymentV3Service;
import com.mzl.flower.service.supplier.SupplierService;
import com.mzl.flower.service.wallet.WalletService;
import com.mzl.flower.service.wallet.WalletWithdrawRecordService;
import com.mzl.flower.utils.UUIDGenerator;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
 
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
 
/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author @TaoJie
 * @since 2024-10-22
 */
@Slf4j
@Service
@Transactional
@RequiredArgsConstructor
public class WalletWithdrawRecordServiceImpl extends ServiceImpl<WalletWithdrawRecordMapper, WalletWithdrawRecordDO> implements WalletWithdrawRecordService {
    private final WalletWithdrawRecordMapper walletWithdrawRecordMapper;
    private final WalletService walletService;
    private final SupplierService supplierService;
    private final SupplierMapper supplierMapper;
    private final UserWechatMapper wechatMapper;
    private final UserPaymentV3Service paymentV3Service;
 
    private final WalletBillRecordMapper walletBillRecordMapper;
 
    @Transactional
    @Override
    public boolean create(CreateWalletWithdrawRecordDTO dto) {
        SupplierDTO supplierDTO = supplierService.getCurrentSupplier();
        if (null == supplierDTO) {
            throw new ValidationException("供应商不能为空");
        }
 
        final WalletDO walletDO = walletService.getBySupplierId();
        if (null == walletDO) {
            throw new ValidationException("钱包不能为空");
        }
        // 查看钱包的金额是不是大于要提现的金额
        if (null != walletDO.getWithdrawableAmount() && null != dto.getAmount() && walletDO.getWithdrawableAmount().compareTo(dto.getAmount()) < 0) {
            throw new ValidationException("钱包金额不足");
        }
        WalletWithdrawRecordDO withdrawRecordDO = new WalletWithdrawRecordDO();
        withdrawRecordDO.setAmount(dto.getAmount());
        withdrawRecordDO.setSupplierId(supplierDTO.getId());
        withdrawRecordDO.setWithdrawState(Constants.WALLET_WITHDRAW_STATE.WAITING.name());
        withdrawRecordDO.setMethod(Constants.WALLET_WITHDRAW_METHOD.WEIXIN.name());
        withdrawRecordDO.create(SecurityUtils.getUserId());
        return save(withdrawRecordDO);
    }
 
    @Override
    public Page<WalletWithdrawRecordVO> queryPage(QueryWalletDTO queryWalletDTO, Page page) {
        List<WalletWithdrawRecordVO> list = walletWithdrawRecordMapper.queryPage(queryWalletDTO, page);
        page.setRecords(list);
        return page;
    }
 
    @Override
    public void updateWallet(WalletWithdrawRecordDTO walletWithdrawRecordDTO) {
        WalletWithdrawRecordDO withdrawRecordDO = walletWithdrawRecordMapper.selectById(walletWithdrawRecordDTO.getId());
        if (withdrawRecordDO == null) {
            throw new ValidationException("提现记录信息不存在");
        }
        if (StringUtils.isEmpty(walletWithdrawRecordDTO.getApproveState())) {
            throw new ValidationException("审批状态不能为空");
        }
        if (Constants.WALLET_APPROVE_STATE.REJECT.name().equals(walletWithdrawRecordDTO.getApproveState())) {
           if(StringUtils.isEmpty(walletWithdrawRecordDTO.getRejectReason())){
               throw new ValidationException("不通过理由不能为空");
           }
        }
        BeanUtils.copyProperties(walletWithdrawRecordDTO, withdrawRecordDO);
        withdrawRecordDO.update(SecurityUtils.getUserId());
        withdrawRecordDO.setApproveState(walletWithdrawRecordDTO.getApproveState());
        withdrawRecordDO.setApproveBy(SecurityUtils.getUserId());
        withdrawRecordDO.setApproveTime(LocalDateTime.now());
        walletWithdrawRecordMapper.updateById(withdrawRecordDO);
 
        //审批通过之后写转账信息
        if(Constants.WALLET_APPROVE_STATE.APPROVE.name().equals(walletWithdrawRecordDTO.getApproveState())){
            Supplier s = supplierMapper.selectById(withdrawRecordDO.getSupplierId());
            String name = "";
            name = "供应商" + s.getName();
            UserWechat wechat = wechatMapper.selectOne(new QueryWrapper<UserWechat>().eq("user_id", s.getUserId()));
            if(wechat == null){
                String msg = name + "未绑定账号无法结算";
                log.error(msg);
                throw new ValidationException(msg);
            }
            LocalDateTime now = LocalDateTime.now();
            String day = format(now, "yyyy-MM-dd");
            String remarks = name + "钱包提现";
            String appId = PyamentV3Configurer.supplier_app_id;
            TransferReqDTO transferReqDTO = new TransferReqDTO();
            transferReqDTO.setId(UUIDGenerator.getUUID());
            transferReqDTO.setAppId(appId);
            transferReqDTO.setName(day + "日" + remarks);
            transferReqDTO.setRemarks(remarks);
 
            //保存账单信息
            WalletBillRecordDO  walletBillRecordDO = new WalletBillRecordDO();
            walletBillRecordDO.setSupplierId(s.getId());
            walletBillRecordDO.setType(Constants.BILL_CHANGE_TYPE.withdraw.name());
            walletBillRecordDO.setMethod(Constants.BILL_CHANGE_METHOD.reduce.name());
            walletBillRecordDO.setTransferId(transferReqDTO.getAppId());
            //提现金额
            BigDecimal withdrawAmount  = withdrawRecordDO.getAmount();
            if(withdrawAmount.doubleValue() > 0) {//结算金额>0时才去转账
                TransferDetailReqDTO dr = new TransferDetailReqDTO();
                dr.setId(UUIDGenerator.getUUID());
                dr.setAmount(withdrawAmount.multiply(new BigDecimal(100)).longValue());
                dr.setOpenId(wechat.getOpenId());
                dr.setRemarks(remarks);
                if (withdrawAmount.doubleValue() >= 2000) {
                    dr.setUserName(wechat.getRealName());
                }
 
                List<TransferDetailReqDTO> details = new ArrayList<>();
                details.add(dr);
                walletBillRecordDO.setTransferDetailId(dr.getId());
                log.info("发起转账");
                transferReqDTO.setDetails(details);
                //发起转账
                paymentV3Service.doBatchTransfer(transferReqDTO, SecurityUtils.getUserId());
            }
            walletBillRecordMapper.insert(walletBillRecordDO);
            //不需要更新结算单了,此时提现的金额和计算单上的金额不一致
 
        }
    }
    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);
    }
}