Merge pull request #111 from ronger-x/master

🎨 钱包账号激活接口
This commit is contained in:
ronger 2022-11-01 22:55:31 +08:00 committed by GitHub
commit 6791d758f6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 76 additions and 26 deletions

View File

@ -70,4 +70,6 @@ public class ArticleDTO {
private Integer articleThumbsUpCount;
/** 赞赏总数 */
private Integer articleSponsorCount;
private Boolean canSponsor;
}

View File

@ -26,4 +26,6 @@ public class TokenUser {
private Set<String> scope;
private String bankAccount;
}

View File

@ -5,7 +5,8 @@ package com.rymcu.forest.enumerate;
*/
public enum TransactionCode {
InsufficientBalance(901, "余额不足");
INSUFFICIENT_BALANCE(901, "余额不足"),
UNKNOWN_ACCOUNT(902, "账号不存在");
private int code;

View File

@ -46,5 +46,19 @@ public interface BankAccountService extends Service<BankAccount> {
*/
BankAccount findInfoByBankAccount(String formBankAccount);
/**
* 根据时间查询账号交易记录
* @param bankAccount
* @param startDate
* @param endDate
* @return
*/
List<TransactionRecordDTO> findUserTransactionRecords(String bankAccount, String startDate, String endDate);
/**
* 创建钱包账号
* @param idUser
* @return
*/
BankAccount createBankAccount(Long idUser);
}

View File

@ -13,10 +13,7 @@ import com.rymcu.forest.entity.User;
import com.rymcu.forest.handler.event.ArticleDeleteEvent;
import com.rymcu.forest.handler.event.ArticleEvent;
import com.rymcu.forest.mapper.ArticleMapper;
import com.rymcu.forest.service.ArticleService;
import com.rymcu.forest.service.NotificationService;
import com.rymcu.forest.service.TagService;
import com.rymcu.forest.service.UserService;
import com.rymcu.forest.service.*;
import com.rymcu.forest.util.Html2TextUtil;
import com.rymcu.forest.util.Utils;
import com.rymcu.forest.util.XssUtils;
@ -52,6 +49,8 @@ public class ArticleServiceImpl extends AbstractService<Article> implements Arti
private NotificationService notificationService;
@Resource
private ApplicationEventPublisher publisher;
@Resource
private BankAccountService bankAccountService;
@Value("${resource.domain}")
private String domain;
@ -282,6 +281,8 @@ public class ArticleServiceImpl extends AbstractService<Article> implements Arti
List<PortfolioArticleDTO> portfolioArticleDTOList = articleMapper.selectPortfolioArticles(article.getIdArticle());
portfolioArticleDTOList.forEach(this::genPortfolioArticles);
article.setPortfolios(portfolioArticleDTOList);
// 查询作者是否开通钱包账号
article.setCanSponsor(Objects.nonNull(bankAccountService.findBankAccountByIdUser(article.getArticleAuthorId())));
} else if (type.equals(articleEdit)) {
article.setArticleContent(articleContent.getArticleContent());
} else {

View File

@ -43,19 +43,10 @@ public class BankAccountServiceImpl extends AbstractService<BankAccount> impleme
String defaultAccountType = "0";
bankAccount.setAccountType(defaultAccountType);
List<BankAccount> bankAccounts = bankAccountMapper.select(bankAccount);
BankAccountDTO bankAccountDTO;
if (Objects.nonNull(bankAccounts) && bankAccounts.size() > 0) {
bankAccountDTO = bankAccountMapper.selectBankAccount(bankAccounts.get(0).getIdBankAccount());
} else {
bankAccount.setAccountBalance(new BigDecimal("0"));
// 默认为社区发展与改革银行
bankAccount.setIdBank(2L);
bankAccount.setBankAccount(nextBankAccount());
bankAccount.setCreatedTime(new Date());
bankAccountMapper.insertSelective(bankAccount);
bankAccountDTO = bankAccountMapper.selectBankAccount(bankAccount.getIdBankAccount());
return bankAccountMapper.selectBankAccount(bankAccounts.get(0).getIdBankAccount());
}
return bankAccountDTO;
return null;
}
@Override
@ -69,6 +60,19 @@ public class BankAccountServiceImpl extends AbstractService<BankAccount> impleme
return transactionRecordService.findTransactionRecords(bankAccount, startDate, endDate);
}
@Override
public BankAccount createBankAccount(Long idUser) {
BankAccount bankAccount = new BankAccount();
bankAccount.setAccountBalance(new BigDecimal("0"));
// 默认为社区发展与改革银行
bankAccount.setIdBank(2L);
bankAccount.setAccountOwner(idUser);
bankAccount.setBankAccount(nextBankAccount());
bankAccount.setCreatedTime(new Date());
bankAccountMapper.insertSelective(bankAccount);
return bankAccount;
}
@Override
public BankAccountDTO findByBankAccount(String bankAccount) {
return bankAccountMapper.selectByBankAccount(bankAccount);

View File

@ -47,7 +47,7 @@ public class SponsorServiceImpl extends AbstractService<Sponsor> implements Spon
ArticleDTO articleDTO = articleService.findArticleDTOById(sponsor.getDataId(), 1);
TransactionRecord transactionRecord = transactionRecordService.userTransfer(articleDTO.getArticleAuthorId(), sponsor.getSponsor(), transactionEnum);
if (Objects.isNull(transactionRecord.getIdTransactionRecord())) {
throw new TransactionException(TransactionCode.InsufficientBalance);
throw new TransactionException(TransactionCode.INSUFFICIENT_BALANCE);
}
// 更新文章赞赏数
int result = sponsorMapper.updateArticleSponsorCount(articleDTO.getIdArticle());

View File

@ -49,7 +49,7 @@ public class TransactionRecordServiceImpl extends AbstractService<TransactionRec
transactionRecordMapper.insertSelective(transactionRecord);
}
} else {
throw new TransactionException(TransactionCode.InsufficientBalance);
throw new TransactionException(TransactionCode.INSUFFICIENT_BALANCE);
}
return transactionRecord;
}
@ -73,6 +73,9 @@ public class TransactionRecordServiceImpl extends AbstractService<TransactionRec
public TransactionRecord userTransfer(Long toUserId, Long formUserId, TransactionEnum transactionType) {
BankAccountDTO toBankAccount = bankAccountMapper.findPersonBankAccountByIdUser(toUserId);
BankAccountDTO formBankAccount = bankAccountMapper.findPersonBankAccountByIdUser(formUserId);
if (Objects.isNull(toBankAccount) || Objects.isNull(formBankAccount)) {
throw new TransactionException(TransactionCode.UNKNOWN_ACCOUNT);
}
TransactionRecord transactionRecord = new TransactionRecord();
transactionRecord.setToBankAccount(toBankAccount.getBankAccount());
transactionRecord.setFormBankAccount(formBankAccount.getBankAccount());
@ -84,6 +87,9 @@ public class TransactionRecordServiceImpl extends AbstractService<TransactionRec
@Override
public TransactionRecord bankTransfer(Long idUser, TransactionEnum transactionType) {
BankAccountDTO toBankAccount = bankAccountMapper.findPersonBankAccountByIdUser(idUser);
if (Objects.isNull(toBankAccount)) {
throw new TransactionException(TransactionCode.UNKNOWN_ACCOUNT);
}
Boolean isTrue;
// 校验货币规则
switch (transactionType) {
@ -156,9 +162,7 @@ public class TransactionRecordServiceImpl extends AbstractService<TransactionRec
private boolean checkFormAccountStatus(String formBankAccount, BigDecimal money) {
BankAccount bankAccount = findInfoByBankAccount(formBankAccount);
if (Objects.nonNull(bankAccount)) {
if (bankAccount.getAccountBalance().compareTo(money) > 0) {
return true;
}
return bankAccount.getAccountBalance().compareTo(money) > 0;
}
return false;
}

View File

@ -4,8 +4,10 @@ import com.alibaba.fastjson2.JSONObject;
import com.rymcu.forest.auth.TokenManager;
import com.rymcu.forest.core.result.GlobalResult;
import com.rymcu.forest.core.result.GlobalResultGenerator;
import com.rymcu.forest.dto.BankAccountDTO;
import com.rymcu.forest.dto.TokenUser;
import com.rymcu.forest.entity.User;
import com.rymcu.forest.service.BankAccountService;
import com.rymcu.forest.service.UserService;
import com.rymcu.forest.util.BeanCopierUtil;
import com.rymcu.forest.util.UserUtils;
@ -25,6 +27,8 @@ public class AuthController {
private UserService userService;
@Resource
TokenManager tokenManager;
@Resource
private BankAccountService bankAccountService;
@PostMapping("/login")
public GlobalResult<TokenUser> login(@RequestBody User user) {
@ -53,6 +57,10 @@ public class AuthController {
TokenUser tokenUser = new TokenUser();
BeanCopierUtil.copy(user, tokenUser);
tokenUser.setScope(userService.findUserPermissions(user));
BankAccountDTO bankAccountDTO = bankAccountService.findBankAccountByIdUser(user.getIdUser());
if (Objects.nonNull(bankAccountDTO)) {
tokenUser.setBankAccount(bankAccountDTO.getBankAccount());
}
JSONObject object = new JSONObject();
object.put("user", tokenUser);
return GlobalResultGenerator.genSuccessResult(object);

View File

@ -6,13 +6,11 @@ import com.rymcu.forest.core.result.GlobalResult;
import com.rymcu.forest.core.result.GlobalResultGenerator;
import com.rymcu.forest.dto.BankAccountDTO;
import com.rymcu.forest.dto.TransactionRecordDTO;
import com.rymcu.forest.entity.BankAccount;
import com.rymcu.forest.entity.User;
import com.rymcu.forest.service.BankAccountService;
import com.rymcu.forest.util.UserUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
@ -54,4 +52,10 @@ public class WalletController {
PageInfo<TransactionRecordDTO> pageInfo = new PageInfo(list);
return GlobalResultGenerator.genSuccessResult(pageInfo);
}
@PostMapping("/create")
public GlobalResult<BankAccount> create() {
User user = UserUtils.getCurrentUserByToken();
return GlobalResultGenerator.genSuccessResult(bankAccountService.createBankAccount(user.getIdUser()));
}
}

View File

@ -68,6 +68,9 @@ create table forest_bank_account
account_type char default '0' null comment '0: 普通账户 1: 银行账户'
) comment '银行账户表 ' collate = utf8mb4_unicode_ci;
create unique index forest_bank_account_pk
on forest_bank_account (account_owner, bank_account);
create table forest_comment
(
id bigint auto_increment comment '主键'
@ -578,3 +581,10 @@ Nebula-Pi 开发板平台
</table>
<p> 1-1 </p>
', '2022-06-13 22:35:34', '2022-06-13 22:35:34');
INSERT INTO forest.forest_bank (id, bank_name, bank_owner, bank_description, created_by, created_time) VALUES (1, '社区中央银行', 1, '社区中央银行', 1, '2020-11-26 21:24:19');
INSERT INTO forest.forest_bank (id, bank_name, bank_owner, bank_description, created_by, created_time) VALUES (2, '社区发展与改革银行', 1, '社区发展与改革银行', 1, '2020-11-26 21:31:27');
INSERT INTO forest.forest_bank_account (id, id_bank, bank_account, account_balance, account_owner, created_time, account_type) VALUES (2, 1, '100000002', 1207980.00000000, 2, '2020-11-26 21:37:18', '1');
INSERT INTO forest.forest_bank_account (id, id_bank, bank_account, account_balance, account_owner, created_time, account_type) VALUES (1, 1, '100000001', 997500000.00000000, 1, '2020-11-26 21:36:21', '1');