diff --git a/src/main/java/com/rymcu/forest/dto/ArticleDTO.java b/src/main/java/com/rymcu/forest/dto/ArticleDTO.java index 4b7e370..04b4293 100644 --- a/src/main/java/com/rymcu/forest/dto/ArticleDTO.java +++ b/src/main/java/com/rymcu/forest/dto/ArticleDTO.java @@ -70,4 +70,6 @@ public class ArticleDTO { private Integer articleThumbsUpCount; /** 赞赏总数 */ private Integer articleSponsorCount; + + private Boolean canSponsor; } diff --git a/src/main/java/com/rymcu/forest/dto/TokenUser.java b/src/main/java/com/rymcu/forest/dto/TokenUser.java index 37ace1f..f5569e1 100644 --- a/src/main/java/com/rymcu/forest/dto/TokenUser.java +++ b/src/main/java/com/rymcu/forest/dto/TokenUser.java @@ -26,4 +26,6 @@ public class TokenUser { private Set scope; + private String bankAccount; + } diff --git a/src/main/java/com/rymcu/forest/enumerate/TransactionCode.java b/src/main/java/com/rymcu/forest/enumerate/TransactionCode.java index a0e6bd2..48edd92 100644 --- a/src/main/java/com/rymcu/forest/enumerate/TransactionCode.java +++ b/src/main/java/com/rymcu/forest/enumerate/TransactionCode.java @@ -5,7 +5,8 @@ package com.rymcu.forest.enumerate; */ public enum TransactionCode { - InsufficientBalance(901, "余额不足"); + INSUFFICIENT_BALANCE(901, "余额不足"), + UNKNOWN_ACCOUNT(902, "账号不存在"); private int code; diff --git a/src/main/java/com/rymcu/forest/service/BankAccountService.java b/src/main/java/com/rymcu/forest/service/BankAccountService.java index d90323e..0eea673 100644 --- a/src/main/java/com/rymcu/forest/service/BankAccountService.java +++ b/src/main/java/com/rymcu/forest/service/BankAccountService.java @@ -46,5 +46,19 @@ public interface BankAccountService extends Service { */ BankAccount findInfoByBankAccount(String formBankAccount); + /** + * 根据时间查询账号交易记录 + * @param bankAccount + * @param startDate + * @param endDate + * @return + */ List findUserTransactionRecords(String bankAccount, String startDate, String endDate); + + /** + * 创建钱包账号 + * @param idUser + * @return + */ + BankAccount createBankAccount(Long idUser); } diff --git a/src/main/java/com/rymcu/forest/service/impl/ArticleServiceImpl.java b/src/main/java/com/rymcu/forest/service/impl/ArticleServiceImpl.java index e69376f..2e62f0f 100644 --- a/src/main/java/com/rymcu/forest/service/impl/ArticleServiceImpl.java +++ b/src/main/java/com/rymcu/forest/service/impl/ArticleServiceImpl.java @@ -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
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
implements Arti List 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 { diff --git a/src/main/java/com/rymcu/forest/service/impl/BankAccountServiceImpl.java b/src/main/java/com/rymcu/forest/service/impl/BankAccountServiceImpl.java index 960c592..72bc07e 100644 --- a/src/main/java/com/rymcu/forest/service/impl/BankAccountServiceImpl.java +++ b/src/main/java/com/rymcu/forest/service/impl/BankAccountServiceImpl.java @@ -43,19 +43,10 @@ public class BankAccountServiceImpl extends AbstractService impleme String defaultAccountType = "0"; bankAccount.setAccountType(defaultAccountType); List 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 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); diff --git a/src/main/java/com/rymcu/forest/service/impl/SponsorServiceImpl.java b/src/main/java/com/rymcu/forest/service/impl/SponsorServiceImpl.java index 380666b..77bf427 100644 --- a/src/main/java/com/rymcu/forest/service/impl/SponsorServiceImpl.java +++ b/src/main/java/com/rymcu/forest/service/impl/SponsorServiceImpl.java @@ -47,7 +47,7 @@ public class SponsorServiceImpl extends AbstractService 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()); diff --git a/src/main/java/com/rymcu/forest/service/impl/TransactionRecordServiceImpl.java b/src/main/java/com/rymcu/forest/service/impl/TransactionRecordServiceImpl.java index 9992e41..f087bae 100644 --- a/src/main/java/com/rymcu/forest/service/impl/TransactionRecordServiceImpl.java +++ b/src/main/java/com/rymcu/forest/service/impl/TransactionRecordServiceImpl.java @@ -49,7 +49,7 @@ public class TransactionRecordServiceImpl extends AbstractService 0) { - return true; - } + return bankAccount.getAccountBalance().compareTo(money) > 0; } return false; } diff --git a/src/main/java/com/rymcu/forest/web/api/auth/AuthController.java b/src/main/java/com/rymcu/forest/web/api/auth/AuthController.java index 40bc1ad..66c4ab6 100644 --- a/src/main/java/com/rymcu/forest/web/api/auth/AuthController.java +++ b/src/main/java/com/rymcu/forest/web/api/auth/AuthController.java @@ -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 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); diff --git a/src/main/java/com/rymcu/forest/web/api/bank/WalletController.java b/src/main/java/com/rymcu/forest/web/api/bank/WalletController.java index 22ba086..3d05a26 100644 --- a/src/main/java/com/rymcu/forest/web/api/bank/WalletController.java +++ b/src/main/java/com/rymcu/forest/web/api/bank/WalletController.java @@ -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 pageInfo = new PageInfo(list); return GlobalResultGenerator.genSuccessResult(pageInfo); } + + @PostMapping("/create") + public GlobalResult create() { + User user = UserUtils.getCurrentUserByToken(); + return GlobalResultGenerator.genSuccessResult(bankAccountService.createBankAccount(user.getIdUser())); + } } diff --git a/src/main/resources/static/forest.sql b/src/main/resources/static/forest.sql index 0c1cd23..aa44180 100644 --- a/src/main/resources/static/forest.sql +++ b/src/main/resources/static/forest.sql @@ -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 '主键' @@ -577,4 +580,11 @@ Nebula-Pi 开发板平台

表 1-1 主板元器件说明

-', '2022-06-13 22:35:34', '2022-06-13 22:35:34'); \ No newline at end of file +', '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');