| | |
| | | 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; |
| | |
| | | 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.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 java.math.RoundingMode; |
| | | import java.time.LocalDateTime; |
| | | import java.util.*; |
| | | import java.util.concurrent.TimeUnit; |
| | | |
| | | @Slf4j |
| | | @Service |
| | |
| | | |
| | | @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)); |
| | |
| | | //旧逻辑转账的时候才会标记状态为结算中 |
| | | //新逻辑不是立即转账。不走结算后转账逻辑。此时没有转账时间、openID、转账ID、转账明细ID,直接标记结算状态完成,存金额到钱包中 |
| | | settlement.setStatus(Constants.SETTLEMENT_STATUS.COMPLETED.name()); |
| | | |
| | | settlementMapper.insert(settlement); |
| | | |
| | | WalletDO walletDO = walletService.getCurrentSupplier(); |
| | | if (null == walletDO) { |
| | | throw new ValidationException("钱包不能为空"); |
| | | } |
| | | RLock lock = redissonClient.getLock(String.format(LockConstants.WALLET_ID_KEY, walletDO.getId())); |
| | | try { |
| | | if (lock.tryLock(10, 30, TimeUnit.SECONDS)) { |
| | | try { |
| | | WalletDO walletDOInfo = walletService.getCurrentSupplier(); |
| | | //增加供应商结算金额保存到钱包 |
| | | 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); |
| | | } finally { |
| | | lock.unlock(); |
| | | } |
| | | } |
| | | } catch (InterruptedException e) { |
| | | throw new RuntimeException(e); |
| | | } |
| | | |
| | | } |
| | | |