|
|
@@ -1,259 +0,0 @@
|
|
|
-package com.zhongshu.card.server.core.service.wallet;
|
|
|
-
|
|
|
-import com.github.microservice.components.data.base.util.PageEntityUtil;
|
|
|
-import com.zhongshu.card.client.model.org.UserCountModel;
|
|
|
-import com.zhongshu.card.client.model.wallet.WalletModel;
|
|
|
-import com.zhongshu.card.client.model.wallet.WalletRechargeModel;
|
|
|
-import com.zhongshu.card.client.model.wallet.WalletRechargeParam;
|
|
|
-import com.zhongshu.card.client.model.wallet.WalletRechargeSearch;
|
|
|
-import com.github.microservice.net.ResultContent;
|
|
|
-import com.zhongshu.card.client.type.DataState;
|
|
|
-import com.zhongshu.card.client.type.RechargeType;
|
|
|
-import com.zhongshu.card.server.core.dao.org.UserCountDao;
|
|
|
-import com.zhongshu.card.server.core.dao.wallet.WalletDao;
|
|
|
-import com.zhongshu.card.server.core.dao.wallet.WalletRechargeDao;
|
|
|
-import com.zhongshu.card.server.core.domain.org.UserAccount;
|
|
|
-import com.zhongshu.card.server.core.domain.wallet.Wallet;
|
|
|
-import com.zhongshu.card.server.core.domain.wallet.WalletRecharge;
|
|
|
-import com.zhongshu.card.server.core.service.base.SuperService;
|
|
|
-import com.zhongshu.card.server.core.service.user.UserAccountServiceImpl;
|
|
|
-import com.zhongshu.card.server.core.util.BeanUtils;
|
|
|
-import com.zhongshu.card.server.core.util.CommonUtil;
|
|
|
-import com.zhongshu.card.client.utils.DateUtils;
|
|
|
-import lombok.extern.slf4j.Slf4j;
|
|
|
-import org.apache.commons.lang3.ObjectUtils;
|
|
|
-import org.apache.commons.lang3.StringUtils;
|
|
|
-import org.springframework.beans.factory.annotation.Autowired;
|
|
|
-import org.springframework.data.domain.Page;
|
|
|
-import org.springframework.data.domain.Pageable;
|
|
|
-import org.springframework.stereotype.Service;
|
|
|
-import org.springframework.transaction.annotation.Transactional;
|
|
|
-
|
|
|
-import java.math.BigDecimal;
|
|
|
-import java.util.concurrent.locks.ReentrantLock;
|
|
|
-
|
|
|
-/**
|
|
|
- * @author TRX
|
|
|
- * @date 2024/6/29
|
|
|
- */
|
|
|
-@Slf4j
|
|
|
-@Service
|
|
|
-public class WalletService extends SuperService {
|
|
|
-
|
|
|
- @Autowired
|
|
|
- WalletDao walletDao;
|
|
|
-
|
|
|
- @Autowired
|
|
|
- WalletRechargeDao walletRechargeDao;
|
|
|
-
|
|
|
- @Autowired
|
|
|
- UserCountDao userCountDao;
|
|
|
-
|
|
|
- @Autowired
|
|
|
- UserAccountServiceImpl userAccountService;
|
|
|
-
|
|
|
- private final ReentrantLock lock = new ReentrantLock();
|
|
|
-
|
|
|
-
|
|
|
- public ResultContent checkUserWallet(String userId, BigDecimal amount) {
|
|
|
- Wallet wallet = getUserWalletByUserId(userId);
|
|
|
- if (wallet.getDataState() != DataState.Enable) {
|
|
|
- return ResultContent.buildFail(String.format("支付失败钱包", wallet.getDataState().getRemark()));
|
|
|
- }
|
|
|
- if (wallet.getAmount().compareTo(amount) < 0) {
|
|
|
- return ResultContent.buildFail("余额不足");
|
|
|
- }
|
|
|
- return ResultContent.buildSuccess();
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 初始当前用户钱包
|
|
|
- *
|
|
|
- * @return
|
|
|
- */
|
|
|
- public ResultContent<Wallet> initCurrentUserWallet() {
|
|
|
- String userId = getCurrentUserId();
|
|
|
- return initUserWallet(userId);
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 初始用户钱包
|
|
|
- *
|
|
|
- * @param userId
|
|
|
- * @return
|
|
|
- */
|
|
|
- public ResultContent<Wallet> initUserWallet(String userId) {
|
|
|
- if (StringUtils.isEmpty(userId)) {
|
|
|
- return ResultContent.buildFail("userId不能为空");
|
|
|
- }
|
|
|
- UserAccount userAccount = userCountDao.findTopByUserId(userId);
|
|
|
- if (ObjectUtils.isEmpty(userAccount)) {
|
|
|
- return ResultContent.buildFail(String.format("用户不存在:%s", userId));
|
|
|
- }
|
|
|
- Wallet wallet = walletDao.findByUserId(userId);
|
|
|
- if (ObjectUtils.isEmpty(wallet)) {
|
|
|
- wallet = new Wallet();
|
|
|
- initEntityNoCheckOid(wallet);
|
|
|
- wallet.setAmount(BigDecimal.ZERO);
|
|
|
- wallet.setUserId(userId);
|
|
|
- wallet.setUserAccount(userAccount);
|
|
|
- wallet.setDataState(DataState.Enable);
|
|
|
- walletDao.save(wallet);
|
|
|
- }
|
|
|
- return ResultContent.buildSuccess(wallet);
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 通过用户userId得到钱包
|
|
|
- *
|
|
|
- * @param userId
|
|
|
- * @return
|
|
|
- */
|
|
|
- public Wallet getUserWalletByUserId(String userId) {
|
|
|
- Wallet wallet = walletDao.findByUserId(userId);
|
|
|
- if (ObjectUtils.isEmpty(wallet)) {
|
|
|
- ResultContent<Wallet> resultContent = initUserWallet(userId);
|
|
|
- if (resultContent.isSuccess()) {
|
|
|
- wallet = resultContent.getContent();
|
|
|
- }
|
|
|
- }
|
|
|
- return wallet;
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- /**
|
|
|
- * 得到用户钱包信息
|
|
|
- *
|
|
|
- * @param userId
|
|
|
- * @return
|
|
|
- */
|
|
|
- public ResultContent<WalletModel> getUserWallet(String userId) {
|
|
|
- Wallet wallet = walletDao.findByUserId(userId);
|
|
|
- if (ObjectUtils.isEmpty(wallet)) {
|
|
|
- ResultContent<Wallet> resultContent = initUserWallet(userId);
|
|
|
- if (resultContent.isFailed()) {
|
|
|
- return ResultContent.buildFail(resultContent.getMsg());
|
|
|
- }
|
|
|
- wallet = resultContent.getContent();
|
|
|
- }
|
|
|
- return ResultContent.buildSuccess(toModel(wallet));
|
|
|
- }
|
|
|
-
|
|
|
- //---------------------------充值 start------------------------
|
|
|
-
|
|
|
- /**
|
|
|
- * 充值列表
|
|
|
- *
|
|
|
- * @param param
|
|
|
- * @param pageable
|
|
|
- * @return
|
|
|
- */
|
|
|
- public ResultContent<Page<WalletRechargeModel>> page(WalletRechargeSearch param, Pageable pageable) {
|
|
|
- initOidSearchParamNoCheckOid(param);
|
|
|
- Page<WalletRecharge> page = walletRechargeDao.page(pageable, param);
|
|
|
- return ResultContent.buildSuccess(PageEntityUtil.concurrent2PageModel(page, this::toModel));
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 充值
|
|
|
- *
|
|
|
- * @param param
|
|
|
- * @return
|
|
|
- */
|
|
|
- @Transactional
|
|
|
- public ResultContent addWalletRecharge(WalletRechargeParam param) {
|
|
|
- String userId = param.getUserId();
|
|
|
- UserAccount userAccount = userCountDao.findTopByUserId(userId);
|
|
|
- if (ObjectUtils.isEmpty(userAccount)) {
|
|
|
- return ResultContent.buildFail(String.format("用户不存在:%s", userId));
|
|
|
- }
|
|
|
- Wallet wallet = getUserWalletByUserId(userId);
|
|
|
- // 充值金额
|
|
|
- BigDecimal amount = param.getAmount();
|
|
|
- if (CommonUtil.bigDecimalIsEmpty(amount)) {
|
|
|
- return ResultContent.buildFail(String.format("充值金额不能小于等于0"));
|
|
|
- }
|
|
|
-
|
|
|
- try {
|
|
|
- getLock().lock();
|
|
|
- WalletRecharge walletRecharge = new WalletRecharge();
|
|
|
- initEntityNoCheckOid(walletRecharge);
|
|
|
- BigDecimal preAmount = wallet.getAmount();
|
|
|
- if (CommonUtil.bigDecimalIsEmpty(preAmount)) {
|
|
|
- preAmount = BigDecimal.ZERO;
|
|
|
- }
|
|
|
- BigDecimal afterAmount = preAmount.add(amount);
|
|
|
- wallet.setAmount(afterAmount);
|
|
|
-
|
|
|
- walletRecharge.setPreAmount(preAmount);
|
|
|
- walletRecharge.setAmount(amount);
|
|
|
- walletRecharge.setRechargeType(RechargeType.Active);
|
|
|
- walletRecharge.setAfterAmount(afterAmount);
|
|
|
- walletRecharge.setUserId(userId);
|
|
|
- walletRecharge.setUserAccount(userAccount);
|
|
|
- walletRecharge.setWallet(wallet);
|
|
|
- walletRecharge.setUserName(userAccount.getName());
|
|
|
- walletRecharge.setPhone(userAccount.getPhone());
|
|
|
- walletRecharge.setYear(DateUtils.paresTime(System.currentTimeMillis(), DateUtils.patternyyyy));
|
|
|
- walletRecharge.setMonth(DateUtils.paresTime(System.currentTimeMillis(), DateUtils.patternMM));
|
|
|
- walletRecharge.setTimeStr(DateUtils.paresTime(System.currentTimeMillis(), DateUtils.patternyyyySSS));
|
|
|
-
|
|
|
- walletDao.save(wallet);
|
|
|
- walletRechargeDao.save(walletRecharge);
|
|
|
- } catch (Exception e) {
|
|
|
- e.printStackTrace();
|
|
|
- getLock().unlock();
|
|
|
- }
|
|
|
- return ResultContent.buildSuccess();
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- /**
|
|
|
- * 得到充值详情
|
|
|
- *
|
|
|
- * @param id
|
|
|
- * @return
|
|
|
- */
|
|
|
- public ResultContent<WalletRechargeModel> getUserWalletRecharge(String id) {
|
|
|
- WalletRecharge walletRecharge = walletRechargeDao.findTopById(id);
|
|
|
- if (ObjectUtils.isEmpty(walletRecharge)) {
|
|
|
- return ResultContent.buildFail(String.format("数据ID不存在:%s", id));
|
|
|
- }
|
|
|
- return ResultContent.buildSuccess(toModelIncludeUser(walletRecharge));
|
|
|
- }
|
|
|
-
|
|
|
- //---------------------------充值 end--------------------------
|
|
|
-
|
|
|
- public WalletModel toModel(Wallet entity) {
|
|
|
- WalletModel model = null;
|
|
|
- if (ObjectUtils.isNotEmpty(entity)) {
|
|
|
- model = new WalletModel();
|
|
|
- BeanUtils.copyProperties(entity, model);
|
|
|
- }
|
|
|
- return model;
|
|
|
- }
|
|
|
-
|
|
|
- public WalletRechargeModel toModel(WalletRecharge entity) {
|
|
|
- WalletRechargeModel model = null;
|
|
|
- if (ObjectUtils.isNotEmpty(entity)) {
|
|
|
- model = new WalletRechargeModel();
|
|
|
- BeanUtils.copyProperties(entity, model);
|
|
|
- }
|
|
|
- return model;
|
|
|
- }
|
|
|
-
|
|
|
- public WalletRechargeModel toModelIncludeUser(WalletRecharge entity) {
|
|
|
- WalletRechargeModel model = null;
|
|
|
- if (ObjectUtils.isNotEmpty(entity)) {
|
|
|
- model = new WalletRechargeModel();
|
|
|
- BeanUtils.copyProperties(entity, model);
|
|
|
- UserCountModel userAccount = userAccountService.toModel(entity.getUserAccount());
|
|
|
- model.setUserCount(userAccount);
|
|
|
- }
|
|
|
- return model;
|
|
|
- }
|
|
|
-
|
|
|
- public ReentrantLock getLock() {
|
|
|
- return lock;
|
|
|
- }
|
|
|
-}
|