From 047697ecd9a02a3d1a10350664312f80cc56a45b Mon Sep 17 00:00:00 2001 From: ronger Date: Mon, 30 Nov 2020 20:44:38 +0800 Subject: [PATCH] =?UTF-8?q?:sparkles:=20=E7=A7=AF=E5=88=86=E7=B3=BB?= =?UTF-8?q?=E7=BB=9F=E5=9F=BA=E7=A1=80=E5=8A=9F=E8=83=BD=E5=AE=9E=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 2 +- .../com/rymcu/forest/dto/BankAccountDTO.java | 35 +++++++ .../forest/dto/BankAccountSearchDTO.java | 17 ++++ .../java/com/rymcu/forest/dto/BankDTO.java | 33 +++++++ .../forest/dto/TransactionRecordDTO.java | 32 +++++++ .../java/com/rymcu/forest/entity/Bank.java | 37 ++++++++ .../com/rymcu/forest/entity/BankAccount.java | 38 ++++++++ .../rymcu/forest/entity/CurrencyIssue.java | 29 ++++++ .../forest/entity/TransactionRecord.java | 38 ++++++++ .../forest/mapper/BankAccountMapper.java | 35 +++++++ .../com/rymcu/forest/mapper/BankMapper.java | 18 ++++ .../mapper/TransactionRecordMapper.java | 30 ++++++ .../forest/service/BankAccountService.java | 35 +++++++ .../com/rymcu/forest/service/BankService.java | 14 +++ .../service/TransactionRecordService.java | 27 ++++++ .../service/impl/BankAccountServiceImpl.java | 79 ++++++++++++++++ .../forest/service/impl/BankServiceImpl.java | 27 ++++++ .../impl/TransactionRecordServiceImpl.java | 91 +++++++++++++++++++ .../java/com/rymcu/forest/util/DateUtil.java | 38 ++++++++ .../web/api/bank/BankAccountController.java | 52 +++++++++++ .../forest/web/api/bank/BankController.java | 47 ++++++++++ .../api/bank/TransactionRecordController.java | 31 +++++++ src/main/java/mapper/BankAccountMapper.xml | 35 +++++++ src/main/java/mapper/BankMapper.xml | 19 ++++ .../java/mapper/TransactionRecordMapper.xml | 21 +++++ 25 files changed, 859 insertions(+), 1 deletion(-) create mode 100644 src/main/java/com/rymcu/forest/dto/BankAccountDTO.java create mode 100644 src/main/java/com/rymcu/forest/dto/BankAccountSearchDTO.java create mode 100644 src/main/java/com/rymcu/forest/dto/BankDTO.java create mode 100644 src/main/java/com/rymcu/forest/dto/TransactionRecordDTO.java create mode 100644 src/main/java/com/rymcu/forest/entity/Bank.java create mode 100644 src/main/java/com/rymcu/forest/entity/BankAccount.java create mode 100644 src/main/java/com/rymcu/forest/entity/CurrencyIssue.java create mode 100644 src/main/java/com/rymcu/forest/entity/TransactionRecord.java create mode 100644 src/main/java/com/rymcu/forest/mapper/BankAccountMapper.java create mode 100644 src/main/java/com/rymcu/forest/mapper/BankMapper.java create mode 100644 src/main/java/com/rymcu/forest/mapper/TransactionRecordMapper.java create mode 100644 src/main/java/com/rymcu/forest/service/BankAccountService.java create mode 100644 src/main/java/com/rymcu/forest/service/BankService.java create mode 100644 src/main/java/com/rymcu/forest/service/TransactionRecordService.java create mode 100644 src/main/java/com/rymcu/forest/service/impl/BankAccountServiceImpl.java create mode 100644 src/main/java/com/rymcu/forest/service/impl/BankServiceImpl.java create mode 100644 src/main/java/com/rymcu/forest/service/impl/TransactionRecordServiceImpl.java create mode 100644 src/main/java/com/rymcu/forest/util/DateUtil.java create mode 100644 src/main/java/com/rymcu/forest/web/api/bank/BankAccountController.java create mode 100644 src/main/java/com/rymcu/forest/web/api/bank/BankController.java create mode 100644 src/main/java/com/rymcu/forest/web/api/bank/TransactionRecordController.java create mode 100644 src/main/java/mapper/BankAccountMapper.xml create mode 100644 src/main/java/mapper/BankMapper.xml create mode 100644 src/main/java/mapper/TransactionRecordMapper.xml diff --git a/.gitignore b/.gitignore index 7768ab4..a67cc18 100644 --- a/.gitignore +++ b/.gitignore @@ -18,6 +18,7 @@ target/ *.iws *.iml *.ipr +/.mvn/ ### NetBeans ### /nbproject/private/ @@ -29,4 +30,3 @@ build/ ### VS Code ### .vscode/ -/.mvn/ diff --git a/src/main/java/com/rymcu/forest/dto/BankAccountDTO.java b/src/main/java/com/rymcu/forest/dto/BankAccountDTO.java new file mode 100644 index 0000000..6707d5e --- /dev/null +++ b/src/main/java/com/rymcu/forest/dto/BankAccountDTO.java @@ -0,0 +1,35 @@ +package com.rymcu.forest.dto; + +import com.alibaba.fastjson.annotation.JSONField; +import lombok.Data; + +import java.math.BigDecimal; +import java.util.Date; +import java.util.List; + +/** + * @author ronger + */ +@Data +public class BankAccountDTO { + + private Integer idBankAccount; + /** 所属银行 */ + private Integer idBank; + /** 所属银行名称 */ + private String bankName; + /** 银行账户 */ + private String bankAccount; + /** 账户余额 */ + private BigDecimal accountBalance; + /** 账户所有者 */ + private Integer accountOwner; + /** 账户所有者姓名 */ + private String accountOwnerName; + /** 创建时间 */ + @JSONField(format = "yyyy-MM-dd HH:mm:ss") + private Date createdTime; + + private List transactionRecords; + +} diff --git a/src/main/java/com/rymcu/forest/dto/BankAccountSearchDTO.java b/src/main/java/com/rymcu/forest/dto/BankAccountSearchDTO.java new file mode 100644 index 0000000..7cbd91d --- /dev/null +++ b/src/main/java/com/rymcu/forest/dto/BankAccountSearchDTO.java @@ -0,0 +1,17 @@ +package com.rymcu.forest.dto; + +import lombok.Data; + +/** + * @author ronger + */ +@Data +public class BankAccountSearchDTO { + /** 所属银行名称 */ + private String bankName; + /** 银行账户 */ + private String bankAccount; + /** 账户所有者姓名 */ + private String accountOwnerName; + +} diff --git a/src/main/java/com/rymcu/forest/dto/BankDTO.java b/src/main/java/com/rymcu/forest/dto/BankDTO.java new file mode 100644 index 0000000..166288c --- /dev/null +++ b/src/main/java/com/rymcu/forest/dto/BankDTO.java @@ -0,0 +1,33 @@ +package com.rymcu.forest.dto; + +import com.alibaba.fastjson.annotation.JSONField; +import lombok.Data; + +import java.math.BigDecimal; +import java.util.Date; + +/** + * @author ronger + */ +@Data +public class BankDTO { + + private Integer idBank; + /** 银行名称 */ + private String bankName; + /** 银行负责人 */ + private Integer bankOwner; + /** 银行负责人 */ + private String bankOwnerName; + /** 银行账户 */ + private String bankAccount; + /** 账户余额 */ + private BigDecimal accountBalance; + /** 银行描述 */ + private String bankDescription; + /** 创建人 */ + private Integer createdBy; + /** 创建时间 */ + @JSONField(format = "yyyy-MM-dd HH:mm:ss") + private Date createdTime; +} diff --git a/src/main/java/com/rymcu/forest/dto/TransactionRecordDTO.java b/src/main/java/com/rymcu/forest/dto/TransactionRecordDTO.java new file mode 100644 index 0000000..14148d8 --- /dev/null +++ b/src/main/java/com/rymcu/forest/dto/TransactionRecordDTO.java @@ -0,0 +1,32 @@ +package com.rymcu.forest.dto; + +import com.alibaba.fastjson.annotation.JSONField; +import lombok.Data; + +import java.math.BigDecimal; +import java.util.Date; + +/** + * @author ronger + */ +@Data +public class TransactionRecordDTO { + + private Integer idTransactionRecord; + /** 交易流水号 */ + private String transactionNo; + /** 款项 */ + private String funds; + /** 交易发起方 */ + private String formBankAccount; + /** 交易收款方 */ + private String toBankAccount; + /** 交易金额 */ + private BigDecimal money; + /** 交易类型 */ + private String transactionType; + /** 交易时间 */ + @JSONField(format = "yyyy-MM-dd HH:mm:ss") + private Date transactionTime; + +} diff --git a/src/main/java/com/rymcu/forest/entity/Bank.java b/src/main/java/com/rymcu/forest/entity/Bank.java new file mode 100644 index 0000000..259be13 --- /dev/null +++ b/src/main/java/com/rymcu/forest/entity/Bank.java @@ -0,0 +1,37 @@ +package com.rymcu.forest.entity; + +import com.alibaba.fastjson.annotation.JSONField; +import lombok.Data; + +import javax.persistence.Column; +import javax.persistence.GeneratedValue; +import javax.persistence.Id; +import javax.persistence.Table; +import java.util.Date; + +/** + * 银行 + * @author ronger + */ +@Table(name = "vertical_bank") +@Data +public class Bank { + + /** 主键 */ + @Id + @Column(name = "id") + @GeneratedValue(generator = "JDBC") + private Integer idBank; + /** 银行名称 */ + private String bankName; + /** 银行负责人 */ + private Integer bankOwner; + /** 银行描述 */ + private String bankDescription; + /** 创建人 */ + private Integer createdBy; + /** 创建时间 */ + @JSONField(format = "yyyy-MM-dd HH:mm:ss") + private Date createdTime; + +} diff --git a/src/main/java/com/rymcu/forest/entity/BankAccount.java b/src/main/java/com/rymcu/forest/entity/BankAccount.java new file mode 100644 index 0000000..e871baa --- /dev/null +++ b/src/main/java/com/rymcu/forest/entity/BankAccount.java @@ -0,0 +1,38 @@ +package com.rymcu.forest.entity; + +import com.alibaba.fastjson.annotation.JSONField; +import lombok.Data; + +import javax.persistence.Column; +import javax.persistence.GeneratedValue; +import javax.persistence.Id; +import javax.persistence.Table; +import java.math.BigDecimal; +import java.util.Date; + +/** + * 银行账户 + * @author ronger + */ +@Table(name = "vertical_bank_account") +@Data +public class BankAccount { + /** 主键 */ + @Id + @Column(name = "id") + @GeneratedValue(generator = "JDBC") + private Integer idBankAccount; + /** 所属银行 */ + private Integer idBank; + /** 银行账户 */ + private String bankAccount; + /** 账户余额 */ + private BigDecimal accountBalance; + /** 账户所有者 */ + private Integer accountOwner; + /** 创建时间 */ + @JSONField(format = "yyyy-MM-dd HH:mm:ss") + private Date createdTime; + /** 账户类型 */ + private String accountType; +} diff --git a/src/main/java/com/rymcu/forest/entity/CurrencyIssue.java b/src/main/java/com/rymcu/forest/entity/CurrencyIssue.java new file mode 100644 index 0000000..bb2c9d4 --- /dev/null +++ b/src/main/java/com/rymcu/forest/entity/CurrencyIssue.java @@ -0,0 +1,29 @@ +package com.rymcu.forest.entity; + +import lombok.Data; + +import javax.persistence.GeneratedValue; +import javax.persistence.Id; +import javax.persistence.Table; +import java.math.BigDecimal; +import java.util.Date; + +/** + * 货币发行记录 + * @author ronger + */ +@Table(name = "vertical_currency_issue") +@Data +public class CurrencyIssue { + /** 主键 */ + @Id + @GeneratedValue(generator = "JDBC") + private Integer id; + /** 发行数额 */ + private BigDecimal issueValue; + /** 发行人 */ + private Integer createdBy; + /** 发行时间 */ + private Date createdTime; + +} diff --git a/src/main/java/com/rymcu/forest/entity/TransactionRecord.java b/src/main/java/com/rymcu/forest/entity/TransactionRecord.java new file mode 100644 index 0000000..4084a92 --- /dev/null +++ b/src/main/java/com/rymcu/forest/entity/TransactionRecord.java @@ -0,0 +1,38 @@ +package com.rymcu.forest.entity; + +import lombok.Data; + +import javax.persistence.Column; +import javax.persistence.GeneratedValue; +import javax.persistence.Id; +import javax.persistence.Table; +import java.math.BigDecimal; +import java.util.Date; + +/** + * 交易记录 + * @author ronger + */ +@Table(name = "vertical_transaction_record") +@Data +public class TransactionRecord { + /** 主键 */ + @Id + @Column(name = "id") + @GeneratedValue(generator = "JDBC") + private Integer idTransactionRecord; + /** 交易流水号 */ + private String transactionNo; + /** 款项 */ + private String funds; + /** 交易发起方 */ + private String formBankAccount; + /** 交易收款方 */ + private String toBankAccount; + /** 交易金额 */ + private BigDecimal money; + /** 交易类型 */ + private String transactionType; + /** 交易时间 */ + private Date transactionTime; +} diff --git a/src/main/java/com/rymcu/forest/mapper/BankAccountMapper.java b/src/main/java/com/rymcu/forest/mapper/BankAccountMapper.java new file mode 100644 index 0000000..a71f580 --- /dev/null +++ b/src/main/java/com/rymcu/forest/mapper/BankAccountMapper.java @@ -0,0 +1,35 @@ +package com.rymcu.forest.mapper; + +import com.rymcu.forest.core.mapper.Mapper; +import com.rymcu.forest.dto.BankAccountDTO; +import com.rymcu.forest.entity.BankAccount; +import org.apache.ibatis.annotations.Param; + +import java.util.List; + +/** + * @author ronger + */ +public interface BankAccountMapper extends Mapper { + /** + * 查询银行账户 + * @param bankName + * @param accountOwnerName + * @param bankAccount + * @return + */ + List selectBankAccounts(@Param("bankName") String bankName, @Param("accountOwnerName") String accountOwnerName, @Param("bankAccount") String bankAccount); + + /** + * 获取银行账户信息 + * @param idBank + * @return + */ + BankAccountDTO selectBankAccount(@Param("idBank") Integer idBank); + + /** + * 获取当前最大卡号 + * @return + */ + String selectMaxBankAccount(); +} diff --git a/src/main/java/com/rymcu/forest/mapper/BankMapper.java b/src/main/java/com/rymcu/forest/mapper/BankMapper.java new file mode 100644 index 0000000..d330d8c --- /dev/null +++ b/src/main/java/com/rymcu/forest/mapper/BankMapper.java @@ -0,0 +1,18 @@ +package com.rymcu.forest.mapper; + +import com.rymcu.forest.core.mapper.Mapper; +import com.rymcu.forest.dto.BankDTO; +import com.rymcu.forest.entity.Bank; + +import java.util.List; + +/** + * @author ronger + */ +public interface BankMapper extends Mapper { + /** + * 查询银行列表数据 + * @return + */ + List selectBanks(); +} diff --git a/src/main/java/com/rymcu/forest/mapper/TransactionRecordMapper.java b/src/main/java/com/rymcu/forest/mapper/TransactionRecordMapper.java new file mode 100644 index 0000000..e181c85 --- /dev/null +++ b/src/main/java/com/rymcu/forest/mapper/TransactionRecordMapper.java @@ -0,0 +1,30 @@ +package com.rymcu.forest.mapper; + +import com.rymcu.forest.core.mapper.Mapper; +import com.rymcu.forest.dto.TransactionRecordDTO; +import com.rymcu.forest.entity.TransactionRecord; +import org.apache.ibatis.annotations.Param; + +import java.math.BigDecimal; +import java.util.List; + +/** + * @author ronger + */ +public interface TransactionRecordMapper extends Mapper { + /** + * 交易 + * @param formBankAccount + * @param toBankAccount + * @param money + * @return + */ + Integer transfer(@Param("formBankAccount") String formBankAccount, @Param("toBankAccount") String toBankAccount, @Param("money") BigDecimal money); + + /** + * 查询指定账户的交易记录 + * @param bankAccount + * @return + */ + List selectTransactionRecords(@Param("bankAccount") String bankAccount); +} diff --git a/src/main/java/com/rymcu/forest/service/BankAccountService.java b/src/main/java/com/rymcu/forest/service/BankAccountService.java new file mode 100644 index 0000000..a764f7b --- /dev/null +++ b/src/main/java/com/rymcu/forest/service/BankAccountService.java @@ -0,0 +1,35 @@ +package com.rymcu.forest.service; + +import com.rymcu.forest.core.service.Service; +import com.rymcu.forest.dto.BankAccountDTO; +import com.rymcu.forest.dto.BankAccountSearchDTO; +import com.rymcu.forest.entity.BankAccount; +import org.apache.ibatis.annotations.Param; + +import java.util.List; + +/** + * @author ronger + */ +public interface BankAccountService extends Service { + /** + * 查询银行账户列表 + * @param bankAccountSearchDTO + * @return + */ + List findBankAccounts(BankAccountSearchDTO bankAccountSearchDTO); + + /** + * 查询用户银行账户 + * @param idUser + * @return + */ + BankAccountDTO findBankAccountByIdUser(Integer idUser); + + /** + * 根据账户查询银行账户信息 + * @param bankAccount + * @return + */ + BankAccount findByBankAccount(String bankAccount); +} diff --git a/src/main/java/com/rymcu/forest/service/BankService.java b/src/main/java/com/rymcu/forest/service/BankService.java new file mode 100644 index 0000000..59fd8bb --- /dev/null +++ b/src/main/java/com/rymcu/forest/service/BankService.java @@ -0,0 +1,14 @@ +package com.rymcu.forest.service; + +import com.rymcu.forest.core.service.Service; +import com.rymcu.forest.dto.BankDTO; +import com.rymcu.forest.entity.Bank; + +import java.util.List; + +/** + * @author ronger + */ +public interface BankService extends Service { + List findBanks(); +} diff --git a/src/main/java/com/rymcu/forest/service/TransactionRecordService.java b/src/main/java/com/rymcu/forest/service/TransactionRecordService.java new file mode 100644 index 0000000..50ab0a3 --- /dev/null +++ b/src/main/java/com/rymcu/forest/service/TransactionRecordService.java @@ -0,0 +1,27 @@ +package com.rymcu.forest.service; + +import com.rymcu.forest.core.exception.ServiceException; +import com.rymcu.forest.core.service.Service; +import com.rymcu.forest.dto.TransactionRecordDTO; +import com.rymcu.forest.entity.TransactionRecord; + +import java.util.List; + +/** + * @author ronger + */ +public interface TransactionRecordService extends Service { + /** + * 交易 + * @param transactionRecord + * @return + */ + TransactionRecord transfer(TransactionRecord transactionRecord); + + /** + * 查询指定账户的交易记录 + * @param bankAccount + * @return + */ + List findTransactionRecords(String bankAccount); +} diff --git a/src/main/java/com/rymcu/forest/service/impl/BankAccountServiceImpl.java b/src/main/java/com/rymcu/forest/service/impl/BankAccountServiceImpl.java new file mode 100644 index 0000000..714f372 --- /dev/null +++ b/src/main/java/com/rymcu/forest/service/impl/BankAccountServiceImpl.java @@ -0,0 +1,79 @@ +package com.rymcu.forest.service.impl; + +import com.rymcu.forest.core.service.AbstractService; +import com.rymcu.forest.dto.BankAccountDTO; +import com.rymcu.forest.dto.BankAccountSearchDTO; +import com.rymcu.forest.dto.TransactionRecordDTO; +import com.rymcu.forest.entity.BankAccount; +import com.rymcu.forest.mapper.BankAccountMapper; +import com.rymcu.forest.service.BankAccountService; +import com.rymcu.forest.service.TransactionRecordService; +import org.apache.commons.lang.StringUtils; +import org.springframework.stereotype.Service; + +import javax.annotation.Resource; +import java.math.BigDecimal; +import java.util.Date; +import java.util.List; +import java.util.Objects; + +/** + * @author ronger + */ +@Service +public class BankAccountServiceImpl extends AbstractService implements BankAccountService { + + private static String DEFAULT_ACCOUNT_TYPE = "0"; + + @Resource + BankAccountMapper bankAccountMapper; + @Resource + TransactionRecordService transactionRecordService; + + @Override + public List findBankAccounts(BankAccountSearchDTO bankAccountSearchDTO) { + List bankAccounts = bankAccountMapper.selectBankAccounts(bankAccountSearchDTO.getBankName(), bankAccountSearchDTO.getAccountOwnerName(), bankAccountSearchDTO.getBankAccount()); + return bankAccounts; + } + + @Override + public BankAccountDTO findBankAccountByIdUser(Integer idUser) { + BankAccount bankAccount = new BankAccount(); + bankAccount.setAccountOwner(idUser); + bankAccount.setAccountType(DEFAULT_ACCOUNT_TYPE); + 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(2); + bankAccount.setBankAccount(nextBankAccount()); + bankAccount.setCreatedTime(new Date()); + bankAccountMapper.insertSelective(bankAccount); + bankAccountDTO = bankAccountMapper.selectBankAccount(bankAccount.getIdBankAccount()); + } + // 查询交易记录 + List records = transactionRecordService.findTransactionRecords(bankAccountDTO.getBankAccount()); + bankAccountDTO.setTransactionRecords(records); + return bankAccountDTO; + } + + @Override + public BankAccount findByBankAccount(String bankAccount) { + BankAccount searchBankAccount = new BankAccount(); + searchBankAccount.setBankAccount(bankAccount); + return bankAccountMapper.selectOne(searchBankAccount); + } + + private String nextBankAccount() { + String bankAccount = "600000001"; + String maxBankAccount = bankAccountMapper.selectMaxBankAccount(); + if (StringUtils.isNotBlank(maxBankAccount)) { + BigDecimal bigDecimal = new BigDecimal(maxBankAccount).add(new BigDecimal("1")); + return bigDecimal.toString(); + } + return bankAccount; + } +} diff --git a/src/main/java/com/rymcu/forest/service/impl/BankServiceImpl.java b/src/main/java/com/rymcu/forest/service/impl/BankServiceImpl.java new file mode 100644 index 0000000..431a506 --- /dev/null +++ b/src/main/java/com/rymcu/forest/service/impl/BankServiceImpl.java @@ -0,0 +1,27 @@ +package com.rymcu.forest.service.impl; + +import com.rymcu.forest.core.service.AbstractService; +import com.rymcu.forest.dto.BankDTO; +import com.rymcu.forest.entity.Bank; +import com.rymcu.forest.mapper.BankMapper; +import com.rymcu.forest.service.BankService; +import org.springframework.stereotype.Service; + +import javax.annotation.Resource; +import java.util.List; + +/** + * @author ronger + */ +@Service +public class BankServiceImpl extends AbstractService implements BankService { + + @Resource + private BankMapper bankMapper; + + @Override + public List findBanks() { + List banks = bankMapper.selectBanks(); + return banks; + } +} diff --git a/src/main/java/com/rymcu/forest/service/impl/TransactionRecordServiceImpl.java b/src/main/java/com/rymcu/forest/service/impl/TransactionRecordServiceImpl.java new file mode 100644 index 0000000..385f22d --- /dev/null +++ b/src/main/java/com/rymcu/forest/service/impl/TransactionRecordServiceImpl.java @@ -0,0 +1,91 @@ +package com.rymcu.forest.service.impl; + +import com.rymcu.forest.core.exception.ServiceException; +import com.rymcu.forest.core.service.AbstractService; +import com.rymcu.forest.core.service.redis.RedisService; +import com.rymcu.forest.dto.TransactionRecordDTO; +import com.rymcu.forest.entity.BankAccount; +import com.rymcu.forest.entity.TransactionRecord; +import com.rymcu.forest.mapper.TransactionRecordMapper; +import com.rymcu.forest.service.BankAccountService; +import com.rymcu.forest.service.TransactionRecordService; +import com.rymcu.forest.util.DateUtil; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +import javax.annotation.Resource; +import java.math.BigDecimal; +import java.util.Calendar; +import java.util.Date; +import java.util.List; +import java.util.Objects; + +/** + * @author ronger + */ +@Service +public class TransactionRecordServiceImpl extends AbstractService implements TransactionRecordService { + + @Resource + private TransactionRecordMapper transactionRecordMapper; + @Resource + private BankAccountService bankAccountService; + @Resource + private RedisService redisService; + + @Override + @Transactional(rollbackFor = Exception.class) + public TransactionRecord transfer(TransactionRecord transactionRecord) { + // 判断发起者账户状态 + boolean formAccountStatus = checkFormAccountStatus(transactionRecord.getFormBankAccount(), transactionRecord.getMoney()); + if (formAccountStatus) { + Integer result = transactionRecordMapper.transfer(transactionRecord.getFormBankAccount(), transactionRecord.getToBankAccount(), transactionRecord.getMoney()); + if (result > 0) { + transactionRecord.setTransactionNo(nextTransactionNo()); + transactionRecord.setTransactionTime(new Date()); + transactionRecordMapper.insertSelective(transactionRecord); + } + } + return transactionRecord; + } + + @Override + public List findTransactionRecords(String bankAccount) { + return transactionRecordMapper.selectTransactionRecords(bankAccount); + } + + private String nextTransactionNo() { + String orderNo = "E"; + String key = "orderId"; + int timeout = 60; + //根据时间获取前缀 + String prefix = getPrefix(new Date()); + //使用redis获取自增ID + long id = redisService.incrBy(key, timeout); + return orderNo + prefix + DateUtil.getNowDateNum() + String.format("%1$05d", id); + } + + private static String getPrefix(Date date) { + Calendar c = Calendar.getInstance(); + c.setTime(date); + int year = c.get(Calendar.YEAR); + int day = c.get(Calendar.DAY_OF_YEAR); + int hour = c.get(Calendar.HOUR_OF_DAY); + //天数转为3位字符串,不满3位用0补齐 + String dayFmt = String.format("%1$03d", day); + //小时转为2位字符串,不满2位用0补齐 + String hourFmt = String.format("%1$02d", hour); + //2位年份+3位天数+2位小时 + return (year - 2000) + dayFmt + hourFmt; + } + + private boolean checkFormAccountStatus(String formBankAccount, BigDecimal money) { + BankAccount bankAccount = bankAccountService.findByBankAccount(formBankAccount); + if (Objects.nonNull(bankAccount)) { + if (bankAccount.getAccountBalance().compareTo(money) > 0) { + return true; + } + } + return false; + } +} diff --git a/src/main/java/com/rymcu/forest/util/DateUtil.java b/src/main/java/com/rymcu/forest/util/DateUtil.java new file mode 100644 index 0000000..3da5633 --- /dev/null +++ b/src/main/java/com/rymcu/forest/util/DateUtil.java @@ -0,0 +1,38 @@ +package com.rymcu.forest.util; + +import org.apache.commons.lang.StringUtils; + +import java.util.Calendar; + +/** + * @author ronger + */ +public class DateUtil { + + public static String strLen(String s, int len) { + if (StringUtils.isBlank(s)) { + s = ""; + } + for (int i = 0; i < len - s.length(); ++i) { + s = "0" + s; + } + return s; + } + + public static String getYear(Calendar cal) { + return String.valueOf(cal.get(1)); + } + + public static String getMonth(Calendar cal) { + return strLen(String.valueOf(cal.get(2) + 1), 2); + } + + public static String getDay(Calendar cal) { + return strLen(String.valueOf(cal.get(5)), 2); + } + + public static String getNowDateNum() { + Calendar cal = Calendar.getInstance(); + return getYear(cal) + getMonth(cal) + getDay(cal); + } +} diff --git a/src/main/java/com/rymcu/forest/web/api/bank/BankAccountController.java b/src/main/java/com/rymcu/forest/web/api/bank/BankAccountController.java new file mode 100644 index 0000000..acfacbf --- /dev/null +++ b/src/main/java/com/rymcu/forest/web/api/bank/BankAccountController.java @@ -0,0 +1,52 @@ +package com.rymcu.forest.web.api.bank; + +import com.github.pagehelper.PageHelper; +import com.github.pagehelper.PageInfo; +import com.rymcu.forest.core.result.GlobalResult; +import com.rymcu.forest.core.result.GlobalResultGenerator; +import com.rymcu.forest.dto.ArticleDTO; +import com.rymcu.forest.dto.BankAccountDTO; +import com.rymcu.forest.dto.BankAccountSearchDTO; +import com.rymcu.forest.entity.Bank; +import com.rymcu.forest.entity.BankAccount; +import com.rymcu.forest.service.BankAccountService; +import com.rymcu.forest.service.BankService; +import org.springframework.web.bind.annotation.*; + +import javax.annotation.Resource; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +/** + * @author ronger + */ +@RestController +@RequestMapping("/api/v1/admin/bank-account") +public class BankAccountController { + + @Resource + private BankAccountService bankAccountService; + + @GetMapping("/list") + public GlobalResult banks(@RequestParam(defaultValue = "0") Integer page, @RequestParam(defaultValue = "10") Integer rows, BankAccountSearchDTO bankAccountSearchDTO) { + PageHelper.startPage(page, rows); + List list = bankAccountService.findBankAccounts(bankAccountSearchDTO); + PageInfo pageInfo = new PageInfo(list); + Map map = new HashMap(2); + map.put("bankAccounts", pageInfo.getList()); + Map pagination = new HashMap(4); + pagination.put("pageSize", pageInfo.getPageSize()); + pagination.put("total", pageInfo.getTotal()); + pagination.put("currentPage", pageInfo.getPageNum()); + map.put("pagination", pagination); + return GlobalResultGenerator.genSuccessResult(map); + } + + @GetMapping("/{idUser}") + public GlobalResult detail(@PathVariable Integer idUser) { + BankAccountDTO bankAccount = bankAccountService.findBankAccountByIdUser(idUser); + return GlobalResultGenerator.genSuccessResult(bankAccount); + } + +} diff --git a/src/main/java/com/rymcu/forest/web/api/bank/BankController.java b/src/main/java/com/rymcu/forest/web/api/bank/BankController.java new file mode 100644 index 0000000..6dda2de --- /dev/null +++ b/src/main/java/com/rymcu/forest/web/api/bank/BankController.java @@ -0,0 +1,47 @@ +package com.rymcu.forest.web.api.bank; + +import com.github.pagehelper.PageHelper; +import com.github.pagehelper.PageInfo; +import com.rymcu.forest.core.result.GlobalResult; +import com.rymcu.forest.core.result.GlobalResultGenerator; +import com.rymcu.forest.dto.ArticleDTO; +import com.rymcu.forest.dto.BankDTO; +import com.rymcu.forest.entity.Bank; +import com.rymcu.forest.service.BankService; +import com.rymcu.forest.util.Utils; +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 javax.annotation.Resource; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +/** + * @author ronger + */ +@RestController +@RequestMapping("/api/v1/admin/bank") +public class BankController { + + @Resource + private BankService bankService; + + @GetMapping("/list") + public GlobalResult banks(@RequestParam(defaultValue = "0") Integer page, @RequestParam(defaultValue = "10") Integer rows) { + PageHelper.startPage(page, rows); + List list = bankService.findBanks(); + PageInfo pageInfo = new PageInfo(list); + Map map = new HashMap(2); + map.put("banks", pageInfo.getList()); + Map pagination = new HashMap(4); + pagination.put("pageSize", pageInfo.getPageSize()); + pagination.put("total", pageInfo.getTotal()); + pagination.put("currentPage", pageInfo.getPageNum()); + map.put("pagination", pagination); + return GlobalResultGenerator.genSuccessResult(map); + } + +} diff --git a/src/main/java/com/rymcu/forest/web/api/bank/TransactionRecordController.java b/src/main/java/com/rymcu/forest/web/api/bank/TransactionRecordController.java new file mode 100644 index 0000000..39172bb --- /dev/null +++ b/src/main/java/com/rymcu/forest/web/api/bank/TransactionRecordController.java @@ -0,0 +1,31 @@ +package com.rymcu.forest.web.api.bank; + +import com.rymcu.forest.core.exception.ServiceException; +import com.rymcu.forest.core.result.GlobalResult; +import com.rymcu.forest.core.result.GlobalResultGenerator; +import com.rymcu.forest.entity.TransactionRecord; +import com.rymcu.forest.service.TransactionRecordService; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +import javax.annotation.Resource; + +/** + * @author ronger + */ +@RestController +@RequestMapping("/api/v1/transaction") +public class TransactionRecordController { + + @Resource + private TransactionRecordService transactionRecordService; + + @PostMapping("/transfer") + public GlobalResult transfer(@RequestBody TransactionRecord transactionRecord) { + transactionRecord = transactionRecordService.transfer(transactionRecord); + return GlobalResultGenerator.genSuccessResult(transactionRecord); + } + +} diff --git a/src/main/java/mapper/BankAccountMapper.xml b/src/main/java/mapper/BankAccountMapper.xml new file mode 100644 index 0000000..cbc5ec3 --- /dev/null +++ b/src/main/java/mapper/BankAccountMapper.xml @@ -0,0 +1,35 @@ + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/main/java/mapper/BankMapper.xml b/src/main/java/mapper/BankMapper.xml new file mode 100644 index 0000000..14faced --- /dev/null +++ b/src/main/java/mapper/BankMapper.xml @@ -0,0 +1,19 @@ + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/main/java/mapper/TransactionRecordMapper.xml b/src/main/java/mapper/TransactionRecordMapper.xml new file mode 100644 index 0000000..0457ced --- /dev/null +++ b/src/main/java/mapper/TransactionRecordMapper.xml @@ -0,0 +1,21 @@ + + + + + + + + + + + + + + + update vertical_bank_account set account_balance = account_balance - #{money} where bank_account = #{formBankAccount}; + update vertical_bank_account set account_balance = account_balance + #{money} where bank_account = #{toBankAccount}; + + + \ No newline at end of file