cloudroam
2024-11-12 4537f8e46508fbe3627fe916ada90ebc7d281a7e
src/main/java/com/mzl/flower/service/payment/OrderSettlementService.java
@@ -6,6 +6,7 @@
import com.mzl.flower.config.exception.ValidationException;
import com.mzl.flower.config.security.SecurityUtils;
import com.mzl.flower.constant.Constants;
import com.mzl.flower.constant.LockConstants;
import com.mzl.flower.dto.request.payment.*;
import com.mzl.flower.dto.response.payment.OrderSettlementDTO;
import com.mzl.flower.dto.response.payment.OrderSettlementDetailDTO;
@@ -17,7 +18,6 @@
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.WalletBillRecordDetail;
import com.mzl.flower.entity.wallet.WalletDO;
import com.mzl.flower.mapper.flower.FlowerSupplierSaleNumMapper;
import com.mzl.flower.mapper.payment.*;
@@ -27,19 +27,25 @@
import com.mzl.flower.mapper.wallet.WalletBillRecordMapper;
import com.mzl.flower.mapper.wallet.WalletMapper;
import com.mzl.flower.service.BaseService;
import com.mzl.flower.service.wallet.WalletService;
import com.mzl.flower.utils.ExcelExportUtil;
import com.mzl.flower.utils.UUIDGenerator;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.net.URLEncoder;
import java.time.LocalDateTime;
import java.util.*;
import java.util.concurrent.TimeUnit;
@Slf4j
@Service
@@ -96,6 +102,12 @@
    @Autowired
    private WalletBillRecordDetailMapper walletBillRecordDetailMapper;
    @Autowired
    private WalletService walletService;
    @Autowired
    RedissonClient redissonClient;
    public Page<OrderSettlementListDTO> selectSettlementList(Page page, OrderSettlementQueryDTO dto){
        dto.setStartDate(parseLocalDateTime(dto.getStartDateStr(), true));
@@ -665,11 +677,6 @@
            settlement.setUserId(s.getUserId());
            settlement.setStatus(Constants.SETTLEMENT_STATUS.PENDING.name());
            //2024-10-23
            //新增WalletBillRecordDO
            WalletBillRecordDO walletBillRecord =  new WalletBillRecordDO();
            walletBillRecord.setId(UUIDGenerator.getUUID());
            Integer flowerNum = 0;//商品数量
            BigDecimal totalAmount = new BigDecimal(0);//交易合计
            BigDecimal checkFee = new BigDecimal(0);//降级扣款
@@ -712,10 +719,10 @@
                //2024-10-23
                //copy信息到t_wallet_bill_record_detail
                WalletBillRecordDetail walletBillRecordDetail = new WalletBillRecordDetail();
                BeanUtils.copyProperties(detail, walletBillRecordDetail, "id");
                walletBillRecordDetail.setBillRecordId(walletBillRecord.getId());
                walletBillRecordDetailMapper.insert(walletBillRecordDetail);
//                WalletBillRecordDetail walletBillRecordDetail = new WalletBillRecordDetail();
//                BeanUtils.copyProperties(detail, walletBillRecordDetail, "id");
//                walletBillRecordDetail.setBillRecordId(walletBillRecord.getId());
//                walletBillRecordDetailMapper.insert(walletBillRecordDetail);
                flowerNum += oi.getNum();
                totalAmount = totalAmount.add(detail.getTotalAmount());
@@ -727,6 +734,51 @@
                serviceFee = serviceFee.add(ois.getServiceFee());
                serviceFeeRate = ois.getServiceFeeRate();
                settlementAmount = settlementAmount.add(ois.getIncomeSupplier());
                WalletDO walletDO = walletService.getOrCreateBySupplierId(supplierId);
                RLock lock = redissonClient.getLock(String.format(LockConstants.WALLET_ID_KEY, walletDO.getId()));
                try {
                    if (lock.tryLock(10, 30, TimeUnit.SECONDS)) {
                        try {
                            //2024-10-28 直接保存到walletBillRecord
                            //新增WalletBillRecordDO
                            if(ois.getIncomeSupplier().compareTo(BigDecimal.ZERO) > 0) {
                                WalletBillRecordDO walletBillRecord = new WalletBillRecordDO();
                                walletBillRecord.setId(UUIDGenerator.getUUID());
                                WalletDO walletDOInfo = walletService.getBySupplierId(supplierId);
                                //增加供应商结算金额保存到钱包
                                walletBillRecord.setSupplierId(supplierId);
                                walletBillRecord.setWalletId(walletDOInfo.getId());
                                walletBillRecord.setSettlementId(settlement.getId());
                                walletBillRecord.setOrderItemId(detail.getOrderItemId());
                                //变动金额等于供应商收入
                                walletBillRecord.setTotalAmount(ois.getIncomeSupplier());
                                walletBillRecord.setType(Constants.BILL_CHANGE_TYPE.settlement.name());
                                walletBillRecord.setMethod(Constants.BILL_CHANGE_METHOD.add.name());
                                walletBillRecord.setOriginalAmount(walletDOInfo.getTotalAmount());
                                walletBillRecord.setChangeAmount(ois.getIncomeSupplier());
                                walletBillRecord.setBalance(walletDOInfo.getWithdrawableAmount().add(ois.getIncomeSupplier()));
                                Order order = orderMapper.selectById(detail.getOrderId());
                                if (!ObjectUtils.isEmpty(order)) {
                                    walletBillRecord.setRemark("订单完成(订单号" + order.getOrderNo() + ")" + ",获得收入");
                                    walletBillRecord.setOrderNo(order.getOrderNo());
                                }
                                //更新钱包
                                //可提现金额=钱包余额=结算金额
                                walletDOInfo.setWithdrawableAmount(walletDOInfo.getWithdrawableAmount().add(ois.getIncomeSupplier()));
                                walletDOInfo.setTotalAmount(walletDOInfo.getWithdrawableAmount());
                                //已结算金额
                                walletDOInfo.setSettledAmount(walletDOInfo.getSettledAmount().add(ois.getIncomeSupplier()));
                                walletMapper.updateById(walletDOInfo);
                                walletBillRecord.create();
                                walletBillRecordMapper.insert(walletBillRecord);
                            }
                        } finally {
                            lock.unlock();
                        }
                    }
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
            settlement.setFlowerNum(flowerNum);
@@ -759,34 +811,7 @@
            //旧逻辑转账的时候才会标记状态为结算中
            //新逻辑不是立即转账。不走结算后转账逻辑。此时没有转账时间、openID、转账ID、转账明细ID,直接标记结算状态完成,存金额到钱包中
            settlement.setStatus(Constants.SETTLEMENT_STATUS.COMPLETED.name());
            settlementMapper.insert(settlement);
            //增加供应商结算金额保存到钱包
            walletBillRecord.setSupplierId(s.getId());
            walletBillRecord.setSettlementId(settlement.getId());
            walletBillRecord.setType(Constants.BILL_CHANGE_TYPE.settlement.name());
            walletBillRecord.setMethod(Constants.BILL_CHANGE_METHOD.add.name());
            WalletDO walletDOInfo = walletMapper.getTotalAmount(s.getUserId());
            if (ObjectUtils.isEmpty(walletDOInfo)) {
                // 新增钱包
                WalletDO walletDO = new WalletDO();
                walletDO.setUserId(s.getUserId());
                walletDO.setSupplierId(s.getId());
                walletDO.setTotalAmount(BigDecimal.ZERO);
                walletMapper.insert(walletDO);
                walletBillRecord.setOriginalAmount(BigDecimal.ZERO);
                walletBillRecord.setChangeAmount(settlementAmount);
                walletBillRecord.setBalance(settlementAmount);
            }else{
                walletBillRecord.setOriginalAmount(walletDOInfo.getTotalAmount());
                walletBillRecord.setChangeAmount(settlementAmount);
                walletBillRecord.setBalance(walletDOInfo.getTotalAmount().add(settlementAmount));
                //更新钱包
                walletDOInfo.setTotalAmount(walletBillRecord.getBalance());
                walletMapper.updateById(walletDOInfo);
            }
            walletBillRecordMapper.insert(walletBillRecord);
        }
@@ -881,4 +906,48 @@
            orderMapper.updateById(o);
        }
    }
    public void exportSettlementList(HttpServletResponse response, OrderSettlementQueryDTO dto) {
        dto.setStartDate(parseLocalDateTime(dto.getStartDateStr(), true));
        dto.setEndDate(parseLocalDateTime(dto.getEndDateStr(), false));
        List<OrderSettlementListDTO> ls = settlementMapper.selectSettlementListInfo(null, dto);
        String[] rowsName = new String[]{"序号","结算人", "结算金额(元)", "订单数量", "买家数量", "商品数量", "结算合计(元)", "结算均价(元)", "降级扣款(元)",
                "缺货扣款(元)", "补货扣款(元)", "售后理赔(元)", "服务费(元)", "集货站运费(元)", "结算类型", "结算状态", "结算时间"};
        List<Object[]> dataList = new ArrayList<>();
        int sn = 1;
        for (OrderSettlementListDTO o : ls) {
            Object[] objs = new Object[rowsName.length];
            int a = 0;
            objs[a++] = sn;
            objs[a++] = o.getUserName();
            objs[a++] = o.getSettlementAmount();
            objs[a++] = o.getOrderNum();
            objs[a++] = o.getCustomerNum();
            objs[a++] = o.getFlowerNum();
            objs[a++] = o.getTotalAmount();
            objs[a++] = o.getPrice();
            objs[a++] = o.getCheckFee();
            objs[a++] = o.getLackFee();
            objs[a++] = o.getReplaceFee();
            objs[a++] = o.getSalesFee();
            objs[a++] = o.getServiceFee();
            objs[a++] = o.getStationFee();
            objs[a++] = o.getTypeStr();
            objs[a++] = o.getStatusStr();
            objs[a++] = o.getTransferTime();
            dataList.add(objs);
            sn++;
        }
        ExcelExportUtil excelExportUtil = new ExcelExportUtil("结算列表", rowsName, dataList, response);
        try {
            response.addHeader("filename", URLEncoder.encode("结算列表.xls", "UTF-8"));
            response.addHeader("Access-Control-Expose-Headers", "filename");
            excelExportUtil.export();
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
    }
}