|
|
@@ -1,14 +1,17 @@
|
|
|
package com.zhongshu.card.server.core.service.payment;
|
|
|
|
|
|
import com.github.microservice.components.data.base.util.PageEntityUtil;
|
|
|
+import com.zhongshu.card.client.model.org.UserCountModel;
|
|
|
import com.zhongshu.card.client.model.payment.WalletModel;
|
|
|
import com.zhongshu.card.client.model.payment.WalletRechargeModel;
|
|
|
+import com.zhongshu.card.client.model.payment.WalletRechargeParam;
|
|
|
import com.zhongshu.card.client.model.payment.WalletRechargeSearch;
|
|
|
import com.zhongshu.card.client.model.school.BookInfoModel;
|
|
|
import com.zhongshu.card.client.model.school.BookInfoSearch;
|
|
|
import com.zhongshu.card.client.ret.ResultContent;
|
|
|
import com.zhongshu.card.client.service.org.UserAccountService;
|
|
|
import com.zhongshu.card.client.utils.type.DataState;
|
|
|
+import com.zhongshu.card.client.utils.type.RechargeType;
|
|
|
import com.zhongshu.card.server.core.dao.org.UserCountDao;
|
|
|
import com.zhongshu.card.server.core.dao.payment.WalletDao;
|
|
|
import com.zhongshu.card.server.core.dao.payment.WalletRechargeDao;
|
|
|
@@ -19,6 +22,8 @@ import com.zhongshu.card.server.core.domain.school.BookInfo;
|
|
|
import com.zhongshu.card.server.core.service.base.SuperService;
|
|
|
import com.zhongshu.card.server.core.service.org.UserAccountServiceImpl;
|
|
|
import com.zhongshu.card.server.core.util.BeanUtils;
|
|
|
+import com.zhongshu.card.server.core.util.CommonUtil;
|
|
|
+import com.zhongshu.card.server.core.util.DateUtils;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.apache.commons.lang3.ObjectUtils;
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
@@ -26,8 +31,10 @@ 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
|
|
|
@@ -49,6 +56,8 @@ public class WalletService extends SuperService {
|
|
|
@Autowired
|
|
|
UserAccountServiceImpl userAccountService;
|
|
|
|
|
|
+ private final ReentrantLock lock = new ReentrantLock();
|
|
|
+
|
|
|
/**
|
|
|
* 初始当前用户钱包
|
|
|
*
|
|
|
@@ -76,6 +85,7 @@ public class WalletService extends SuperService {
|
|
|
Wallet wallet = walletDao.findByUserId(userId);
|
|
|
if (ObjectUtils.isEmpty(wallet)) {
|
|
|
wallet = new Wallet();
|
|
|
+ initEntityNoCheckOid(wallet);
|
|
|
wallet.setAmount(BigDecimal.ZERO);
|
|
|
wallet.setUserId(userId);
|
|
|
wallet.setUserAccount(userAccount);
|
|
|
@@ -131,12 +141,77 @@ public class WalletService extends SuperService {
|
|
|
* @return
|
|
|
*/
|
|
|
public ResultContent<Page<WalletRechargeModel>> page(WalletRechargeSearch param, Pageable pageable) {
|
|
|
- initOidSearchParamNoTip(param);
|
|
|
+ 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--------------------------
|
|
|
|
|
|
@@ -158,4 +233,18 @@ public class WalletService extends SuperService {
|
|
|
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;
|
|
|
+ }
|
|
|
}
|