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.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mzl.flower.constant.Constants;
import com.mzl.flower.constant.LockConstants;
import com.mzl.flower.dto.request.wallet.CreateWalletBillRecordDTO;
import com.mzl.flower.dto.request.wallet.QueryWalletBillRecordDTO;
import com.mzl.flower.dto.response.wallet.WalletBillRecordVO;
import com.mzl.flower.dto.request.wallet.QueryWalletBillDTO;
import com.mzl.flower.dto.response.wallet.WalletBillRecordVO;
import com.mzl.flower.entity.payment.Transfer;
import com.mzl.flower.entity.payment.TransferDetail;
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.payment.TransferDetailMapper;
import com.mzl.flower.mapper.payment.TransferMapper;
import com.mzl.flower.mapper.wallet.WalletBillRecordMapper;
import com.mzl.flower.mapper.wallet.WalletMapper;
import com.mzl.flower.mapper.wallet.WalletWithdrawRecordMapper;
import com.mzl.flower.service.wallet.WalletBillRecordService;
import com.mzl.flower.service.wallet.WalletService;
import com.mzl.flower.utils.ExcelExportUtil;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
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.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.List;
/**
*
* 服务实现类
*
*
* @author @TaoJie
* @since 2024-10-22
*/
@Service
@Transactional
public class WalletBillRecordServiceImpl extends ServiceImpl implements WalletBillRecordService {
@Resource
private TransferMapper transferMapper;
@Resource
private TransferDetailMapper transferDetailMapper;
@Resource
private WalletBillRecordMapper walletBillRecordMapper;
@Resource
private WalletService walletService;
@Resource
private WalletMapper walletMapper;
@Resource
private WalletWithdrawRecordMapper walletWithdrawRecordMapper;
@Resource
RedissonClient redissonClient;
@Override
public void create(CreateWalletBillRecordDTO dto) {
}
public void updateTransferStatus(String transferId) {
Transfer t = transferMapper.selectById(transferId);
if ("FINISHED".equals(t.getStatus())) {
WalletBillRecordDO walletBillRecordDO = walletBillRecordMapper.selectOne(new QueryWrapper().eq("transfer_id", transferId));
if (walletBillRecordDO == null) {
return;
}
String transferDetailId = walletBillRecordDO.getTransferDetailId();
TransferDetail td = transferDetailMapper.selectById(transferDetailId);
if (td == null) {
log.warn("未找到对应明细");
return;
}
String dStatus = td.getStatus();
if ("SUCCESS".equals(dStatus)) {
walletBillRecordDO.setTransferState(Constants.SETTLEMENT_STATUS.COMPLETED.name());
walletBillRecordDO.update("sys");
walletBillRecordMapper.updateById(walletBillRecordDO);
//更新钱包
WalletDO walletDO = walletService.getOrCreateBySupplierId(walletBillRecordDO.getWalletId());
RLock lock = redissonClient.getLock(String.format(LockConstants.WALLET_ID_KEY, walletDO.getId()));
try {
if (lock.tryLock(10, 30, TimeUnit.SECONDS)) {
try {
if (!ObjectUtils.isEmpty(walletDO)) {
//提现中金额:减少提现中金额
walletDO.setWithdrawingAmount(walletDO.getWithdrawingAmount().subtract(walletBillRecordDO.getChangeAmount()));
//已提现金额:增加已提现金额
walletDO.setWithdrawnAmount(walletDO.getWithdrawnAmount().add(walletBillRecordDO.getChangeAmount()));
walletMapper.updateById(walletDO);
}
} finally {
lock.unlock();
}
}
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
//回写提现状态
if (StringUtils.isEmpty(walletBillRecordDO.getWithdrawRecordId())) {
WalletWithdrawRecordDO withdrawRecordDO = walletWithdrawRecordMapper.selectById(walletBillRecordDO.getWithdrawRecordId());
withdrawRecordDO.setWithdrawState(Constants.WALLET_WITHDRAW_STATE.SUCCESS.name());
walletWithdrawRecordMapper.updateById(withdrawRecordDO);
}
} else if ("FAIL".equals(dStatus)) {
walletBillRecordDO.setTransferState(Constants.SETTLEMENT_STATUS.FAILED.name());
walletBillRecordDO.update("sys");
walletBillRecordMapper.updateById(walletBillRecordDO);
WalletDO walletDO = walletService.getOrCreateBySupplierId(walletBillRecordDO.getWalletId());
RLock lock = redissonClient.getLock(String.format(LockConstants.WALLET_ID_KEY, walletDO.getId()));
try {
if (lock.tryLock(10, 30, TimeUnit.SECONDS)) {
try {
if (!ObjectUtils.isEmpty(walletDO)) {
//提现中金额:减少提现中金额
walletDO.setWithdrawingAmount(walletDO.getWithdrawingAmount().subtract(walletBillRecordDO.getChangeAmount()));
//可提现金额:增加可提现金额
walletDO.setWithdrawableAmount(walletDO.getWithdrawableAmount().add(walletBillRecordDO.getChangeAmount()));
walletMapper.updateById(walletDO);
}
} finally {
lock.unlock();
}
}
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
//回写提现状态
if (StringUtils.isEmpty(walletBillRecordDO.getWithdrawRecordId())) {
WalletWithdrawRecordDO withdrawRecordDO = walletWithdrawRecordMapper.selectById(walletBillRecordDO.getWithdrawRecordId());
withdrawRecordDO.setWithdrawState(Constants.WALLET_WITHDRAW_STATE.FAILURE.name());
walletWithdrawRecordMapper.updateById(withdrawRecordDO);
}
}
}
}
@Override
public Page queryPage(QueryWalletBillRecordDTO queryWalletBillRecordDTO, Page page) {
List list = walletBillRecordMapper.queryPage(queryWalletBillRecordDTO, page);
page.setRecords(list);
return page;
}
@Override
public void exportSupplierFinanceList(HttpServletResponse response, QueryWalletBillRecordDTO queryWalletBillRecordDTO) {
List list = walletBillRecordMapper.queryWalletBillRecordList(queryWalletBillRecordDTO);
String[] rowsName = new String[]{"序号","供应商信息", "变动类型", "原金额", "变动金额", "现余额", "提交时间", "审核时间", "备注"};
List