8 changed files with 277 additions and 227 deletions
@ -1,202 +1,202 @@
|
||||
package demo.weiyichi.controller; |
||||
|
||||
|
||||
import demo.weiyichi.model.AccountTab; |
||||
import demo.weiyichi.model.ApiResponse; |
||||
import demo.weiyichi.model.RecordTable; |
||||
import demo.weiyichi.model.WithdrawMoneyDTO; |
||||
import demo.weiyichi.model.TransferDTO; |
||||
import demo.weiyichi.model.QueryDTO; |
||||
import org.springframework.beans.factory.annotation.Autowired; |
||||
import org.springframework.jdbc.core.JdbcTemplate; |
||||
|
||||
import org.springframework.util.CollectionUtils; |
||||
import org.springframework.web.bind.annotation.*; |
||||
|
||||
import javax.transaction.Transactional; |
||||
import java.time.LocalDateTime; |
||||
import java.time.format.DateTimeFormatter; |
||||
import java.util.HashMap; |
||||
import java.util.List; |
||||
import java.util.Map; |
||||
|
||||
@RestController |
||||
@RequestMapping("/weiyichi") |
||||
public class TransferAccount { |
||||
|
||||
// add 01
|
||||
@Autowired |
||||
private JdbcTemplate jdbcTemplate; |
||||
|
||||
//转账
|
||||
@Transactional |
||||
@PostMapping("/transfer") |
||||
public ApiResponse<?> transfer(@RequestBody TransferDTO transferDTO) { |
||||
try { |
||||
// 参数校验
|
||||
if (transferDTO.getOutCardNo() == null || transferDTO.getInCardNo() == null) { |
||||
return ApiResponse.error("INVALID_CARDNO", "卡号不能为空"); |
||||
} |
||||
if (transferDTO.getAmount() == null || transferDTO.getAmount() <= 0) { |
||||
return ApiResponse.error("INVALID_AMOUNT", "转账金额必须大于0"); |
||||
} |
||||
|
||||
// 验证转出账户
|
||||
String outSql = "SELECT * FROM account_tab WHERE cardNo = ?"; |
||||
List<Map<String, Object>> outAccount = jdbcTemplate.queryForList(outSql, transferDTO.getOutCardNo()); |
||||
if (CollectionUtils.isEmpty(outAccount)) { |
||||
return ApiResponse.error("ACCOUNT_NOT_FOUND", "转出账户不存在"); |
||||
} |
||||
|
||||
// 验证密码
|
||||
if (!verifyAccountPassword(transferDTO.getOutCardNo(), transferDTO.getPassword())) { |
||||
return ApiResponse.error("PASSWORD_MISMATCH", "密码错误"); |
||||
} |
||||
|
||||
// 验证转入账户
|
||||
String inSql = "SELECT * FROM account_tab WHERE cardNo = ?"; |
||||
if (CollectionUtils.isEmpty(jdbcTemplate.queryForList(inSql, transferDTO.getInCardNo()))) { |
||||
return ApiResponse.error("ACCOUNT_NOT_FOUND", "转入账户不存在"); |
||||
} |
||||
|
||||
// 检查余额
|
||||
double outBalance = Double.parseDouble(outAccount.get(0).get("balance").toString()); |
||||
if (outBalance < transferDTO.getAmount()) { |
||||
return ApiResponse.error("INSUFFICIENT_BALANCE", "余额不足"); |
||||
} |
||||
|
||||
// 更新余额
|
||||
jdbcTemplate.update("UPDATE account_tab SET balance = ? WHERE cardNo = ?", |
||||
outBalance - transferDTO.getAmount(), transferDTO.getOutCardNo()); |
||||
jdbcTemplate.update("UPDATE account_tab SET balance = balance + ? WHERE cardNo = ?", |
||||
transferDTO.getAmount(), transferDTO.getInCardNo()); |
||||
|
||||
// 生成流水记录
|
||||
generateTransferRecords(transferDTO, outBalance); |
||||
logger.info("转账成功:从 {} 到 {},金额 {}", |
||||
transferDTO.getOutCardNo(), transferDTO.getInCardNo(), transferDTO.getAmount()); |
||||
return ApiResponse.success(null); |
||||
} catch (Exception e) { |
||||
logger.error("转账失败:{}", e.getMessage()); |
||||
return ApiResponse.error("TRANSFER_FAILED", "转账失败"); |
||||
} |
||||
} |
||||
|
||||
|
||||
|
||||
// 账户查询
|
||||
@GetMapping("/query") |
||||
public ApiResponse<Map<String, Object>> query( |
||||
@RequestParam String cardNo, |
||||
@RequestParam(required = false) String type, |
||||
@RequestParam(required = false) String filter, |
||||
@RequestParam(required = false) String startDate, |
||||
@RequestParam(required = false) String endDate) { |
||||
|
||||
// 参数校验
|
||||
if (cardNo == null || !cardNo.matches("\\d{16}")) { |
||||
return ApiResponse.error("INVALID_CARDNO", "卡号格式错误"); |
||||
} |
||||
|
||||
Map<String, Object> result = new HashMap<>(); |
||||
|
||||
// 余额查询
|
||||
if (type == null || "balance".equals(type)) { |
||||
try { |
||||
String balanceSql = "SELECT balance FROM account_tab WHERE cardNo = ?"; |
||||
Double balance = jdbcTemplate.queryForObject(balanceSql, Double.class, cardNo); |
||||
result.put("balance", balance); |
||||
} catch (Exception e) { |
||||
return ApiResponse.error("BALANCE_QUERY_FAILED", "余额查询失败"); |
||||
} |
||||
} |
||||
|
||||
// 交易记录查询
|
||||
if (type == null || "transactions".equals(type)) { |
||||
try { |
||||
String transSql = buildTransactionSql(filter, startDate, endDate); |
||||
List<Map<String, Object>> transactions = jdbcTemplate.queryForList(transSql, cardNo); |
||||
result.put("transactions", transactions); |
||||
} catch (Exception e) { |
||||
return ApiResponse.error("TRANSACTION_QUERY_FAILED", "交易记录查询失败"); |
||||
} |
||||
} |
||||
|
||||
return ApiResponse.success(result); |
||||
} |
||||
|
||||
//构建交易记录查询
|
||||
private String buildTransactionSql(String filter, String startDate, String endDate) { |
||||
String sql = "SELECT " + |
||||
"date, cardNo, money, " + |
||||
"CASE serviceTy " + |
||||
"WHEN '1' THEN '存款' " + |
||||
"WHEN '2' THEN '取款' " + |
||||
"WHEN '3' THEN '转账' " + |
||||
"ELSE '未知' " + |
||||
"END AS serviceDesc, " + |
||||
"balance, recordNo " + |
||||
"FROM recordtable " + |
||||
"WHERE cardNo = ?"; |
||||
|
||||
if ("income".equals(filter)) { |
||||
sql += " AND serviceTy = '1'"; |
||||
} else if ("expense".equals(filter)) { |
||||
sql += " AND serviceTy IN ('2', '3')"; |
||||
} |
||||
|
||||
if (startDate != null && endDate != null) { |
||||
sql += " AND date BETWEEN ? AND ?"; |
||||
} |
||||
|
||||
return sql; |
||||
} |
||||
|
||||
//销户接口
|
||||
@PostMapping("/closeAccount") |
||||
public ApiResponse<?> closeAccount( |
||||
@RequestParam String cardNo, |
||||
@RequestParam String password, |
||||
@RequestParam String staffNo, |
||||
@RequestParam String organiNm) { |
||||
|
||||
// 参数校验
|
||||
if (cardNo == null || cardNo.trim().isEmpty()) { |
||||
return ApiResponse.error("INVALID_CARDNO", "卡号不能为空"); |
||||
} |
||||
if (password == null || password.trim().isEmpty()) { |
||||
return ApiResponse.error("INVALID_PASSWORD", "密码不能为空"); |
||||
} |
||||
|
||||
// 验证密码
|
||||
if (!verifyAccountPassword(cardNo, password)) { |
||||
return ApiResponse.error("PASSWORD_MISMATCH", "密码错误"); |
||||
} |
||||
|
||||
// 检查余额
|
||||
Double balance = getBalance(cardNo).getData(); |
||||
if (balance == null || balance != 0) { |
||||
return ApiResponse.error("BALANCE_NOT_ZERO", "账户余额不为0,无法销户"); |
||||
} |
||||
|
||||
try { |
||||
// 更新账户状态
|
||||
String updateSql = "UPDATE account_tab SET status = '2' WHERE cardNo = ?"; |
||||
jdbcTemplate.update(updateSql, cardNo); |
||||
|
||||
// 生成销户流水记录
|
||||
RecordTable record = new RecordTable(); |
||||
record.setCardNo(cardNo); |
||||
record.setServiceTy("4"); // 4-销户
|
||||
record.setRecordNo(getRecordNo(staffNo, organiNm)); |
||||
saveRecordTable(record); |
||||
|
||||
logger.info("账户 {} 已销户,操作员工:{}", cardNo, staffNo); |
||||
return ApiResponse.success(null); |
||||
} catch (Exception e) { |
||||
logger.error("销户失败:{}", e.getMessage()); |
||||
return ApiResponse.error("CLOSE_FAILED", "销户失败"); |
||||
} |
||||
} |
||||
|
||||
} |
||||
//package demo.weiyichi.controller;
|
||||
//
|
||||
//
|
||||
//import demo.weiyichi.model.AccountTab;
|
||||
//import demo.weiyichi.model.ApiResponse;
|
||||
//import demo.weiyichi.model.RecordTable;
|
||||
//import demo.weiyichi.model.WithdrawMoneyDTO;
|
||||
//import demo.weiyichi.model.TransferDTO;
|
||||
//import demo.weiyichi.model.QueryDTO;
|
||||
//import org.springframework.beans.factory.annotation.Autowired;
|
||||
//import org.springframework.jdbc.core.JdbcTemplate;
|
||||
//
|
||||
//import org.springframework.util.CollectionUtils;
|
||||
//import org.springframework.web.bind.annotation.*;
|
||||
//
|
||||
//import javax.transaction.Transactional;
|
||||
//import java.time.LocalDateTime;
|
||||
//import java.time.format.DateTimeFormatter;
|
||||
//import java.util.HashMap;
|
||||
//import java.util.List;
|
||||
//import java.util.Map;
|
||||
//
|
||||
//@RestController
|
||||
//@RequestMapping("/weiyichi")
|
||||
//public class TransferAccount {
|
||||
//
|
||||
// // add 01
|
||||
// @Autowired
|
||||
// private JdbcTemplate jdbcTemplate;
|
||||
//
|
||||
// //转账
|
||||
// @Transactional
|
||||
// @PostMapping("/transfer")
|
||||
// public ApiResponse<?> transfer(@RequestBody TransferDTO transferDTO) {
|
||||
// try {
|
||||
// // 参数校验
|
||||
// if (transferDTO.getOutCardNo() == null || transferDTO.getInCardNo() == null) {
|
||||
// return ApiResponse.error("INVALID_CARDNO", "卡号不能为空");
|
||||
// }
|
||||
// if (transferDTO.getAmount() == null || transferDTO.getAmount() <= 0) {
|
||||
// return ApiResponse.error("INVALID_AMOUNT", "转账金额必须大于0");
|
||||
// }
|
||||
//
|
||||
// // 验证转出账户
|
||||
// String outSql = "SELECT * FROM account_tab WHERE cardNo = ?";
|
||||
// List<Map<String, Object>> outAccount = jdbcTemplate.queryForList(outSql, transferDTO.getOutCardNo());
|
||||
// if (CollectionUtils.isEmpty(outAccount)) {
|
||||
// return ApiResponse.error("ACCOUNT_NOT_FOUND", "转出账户不存在");
|
||||
// }
|
||||
//
|
||||
//// // 验证密码
|
||||
//// if (!verifyAccountPassword(transferDTO.getOutCardNo(), transferDTO.getPassword())) {
|
||||
//// return ApiResponse.error("PASSWORD_MISMATCH", "密码错误");
|
||||
//// }
|
||||
//
|
||||
// // 验证转入账户
|
||||
// String inSql = "SELECT * FROM account_tab WHERE cardNo = ?";
|
||||
// if (CollectionUtils.isEmpty(jdbcTemplate.queryForList(inSql, transferDTO.getInCardNo()))) {
|
||||
// return ApiResponse.error("ACCOUNT_NOT_FOUND", "转入账户不存在");
|
||||
// }
|
||||
//
|
||||
// // 检查余额
|
||||
// double outBalance = Double.parseDouble(outAccount.get(0).get("balance").toString());
|
||||
// if (outBalance < transferDTO.getAmount()) {
|
||||
// return ApiResponse.error("INSUFFICIENT_BALANCE", "余额不足");
|
||||
// }
|
||||
//
|
||||
// // 更新余额
|
||||
// jdbcTemplate.update("UPDATE account_tab SET balance = ? WHERE cardNo = ?",
|
||||
// outBalance - transferDTO.getAmount(), transferDTO.getOutCardNo());
|
||||
// jdbcTemplate.update("UPDATE account_tab SET balance = balance + ? WHERE cardNo = ?",
|
||||
// transferDTO.getAmount(), transferDTO.getInCardNo());
|
||||
//
|
||||
// // 生成流水记录
|
||||
// generateTransferRecords(transferDTO, outBalance);
|
||||
// logger.info("转账成功:从 {} 到 {},金额 {}",
|
||||
// transferDTO.getOutCardNo(), transferDTO.getInCardNo(), transferDTO.getAmount());
|
||||
// return ApiResponse.success(null);
|
||||
// } catch (Exception e) {
|
||||
// logger.error("转账失败:{}", e.getMessage());
|
||||
// return ApiResponse.error("TRANSFER_FAILED", "转账失败");
|
||||
// }
|
||||
// }
|
||||
//
|
||||
//
|
||||
//
|
||||
// // 账户查询
|
||||
// @GetMapping("/query")
|
||||
// public ApiResponse<Map<String, Object>> query(
|
||||
// @RequestParam String cardNo,
|
||||
// @RequestParam(required = false) String type,
|
||||
// @RequestParam(required = false) String filter,
|
||||
// @RequestParam(required = false) String startDate,
|
||||
// @RequestParam(required = false) String endDate) {
|
||||
//
|
||||
// // 参数校验
|
||||
// if (cardNo == null || !cardNo.matches("\\d{16}")) {
|
||||
// return ApiResponse.error("INVALID_CARDNO", "卡号格式错误");
|
||||
// }
|
||||
//
|
||||
// Map<String, Object> result = new HashMap<>();
|
||||
//
|
||||
// // 余额查询
|
||||
// if (type == null || "balance".equals(type)) {
|
||||
// try {
|
||||
// String balanceSql = "SELECT balance FROM account_tab WHERE cardNo = ?";
|
||||
// Double balance = jdbcTemplate.queryForObject(balanceSql, Double.class, cardNo);
|
||||
// result.put("balance", balance);
|
||||
// } catch (Exception e) {
|
||||
// return ApiResponse.error("BALANCE_QUERY_FAILED", "余额查询失败");
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// // 交易记录查询
|
||||
// if (type == null || "transactions".equals(type)) {
|
||||
// try {
|
||||
// String transSql = buildTransactionSql(filter, startDate, endDate);
|
||||
// List<Map<String, Object>> transactions = jdbcTemplate.queryForList(transSql, cardNo);
|
||||
// result.put("transactions", transactions);
|
||||
// } catch (Exception e) {
|
||||
// return ApiResponse.error("TRANSACTION_QUERY_FAILED", "交易记录查询失败");
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// return ApiResponse.success(result);
|
||||
// }
|
||||
//
|
||||
// //构建交易记录查询
|
||||
// private String buildTransactionSql(String filter, String startDate, String endDate) {
|
||||
// String sql = "SELECT " +
|
||||
// "date, cardNo, money, " +
|
||||
// "CASE serviceTy " +
|
||||
// "WHEN '1' THEN '存款' " +
|
||||
// "WHEN '2' THEN '取款' " +
|
||||
// "WHEN '3' THEN '转账' " +
|
||||
// "ELSE '未知' " +
|
||||
// "END AS serviceDesc, " +
|
||||
// "balance, recordNo " +
|
||||
// "FROM recordtable " +
|
||||
// "WHERE cardNo = ?";
|
||||
//
|
||||
// if ("income".equals(filter)) {
|
||||
// sql += " AND serviceTy = '1'";
|
||||
// } else if ("expense".equals(filter)) {
|
||||
// sql += " AND serviceTy IN ('2', '3')";
|
||||
// }
|
||||
//
|
||||
// if (startDate != null && endDate != null) {
|
||||
// sql += " AND date BETWEEN ? AND ?";
|
||||
// }
|
||||
//
|
||||
// return sql;
|
||||
// }
|
||||
//
|
||||
// //销户接口
|
||||
// @PostMapping("/closeAccount")
|
||||
// public ApiResponse<?> closeAccount(
|
||||
// @RequestParam String cardNo,
|
||||
// @RequestParam String password,
|
||||
// @RequestParam String staffNo,
|
||||
// @RequestParam String organiNm) {
|
||||
//
|
||||
// // 参数校验
|
||||
// if (cardNo == null || cardNo.trim().isEmpty()) {
|
||||
// return ApiResponse.error("INVALID_CARDNO", "卡号不能为空");
|
||||
// }
|
||||
// if (password == null || password.trim().isEmpty()) {
|
||||
// return ApiResponse.error("INVALID_PASSWORD", "密码不能为空");
|
||||
// }
|
||||
//
|
||||
// // 验证密码
|
||||
// if (!verifyAccountPassword(cardNo, password)) {
|
||||
// return ApiResponse.error("PASSWORD_MISMATCH", "密码错误");
|
||||
// }
|
||||
//
|
||||
// // 检查余额
|
||||
// Double balance = getBalance(cardNo).getData();
|
||||
// if (balance == null || balance != 0) {
|
||||
// return ApiResponse.error("BALANCE_NOT_ZERO", "账户余额不为0,无法销户");
|
||||
// }
|
||||
//
|
||||
// try {
|
||||
// // 更新账户状态
|
||||
// String updateSql = "UPDATE account_tab SET status = '2' WHERE cardNo = ?";
|
||||
// jdbcTemplate.update(updateSql, cardNo);
|
||||
//
|
||||
// // 生成销户流水记录
|
||||
// RecordTable record = new RecordTable();
|
||||
// record.setCardNo(cardNo);
|
||||
// record.setServiceTy("4"); // 4-销户
|
||||
// record.setRecordNo(getRecordNo(staffNo, organiNm));
|
||||
// saveRecordTable(record);
|
||||
//
|
||||
// logger.info("账户 {} 已销户,操作员工:{}", cardNo, staffNo);
|
||||
// return ApiResponse.success(null);
|
||||
// } catch (Exception e) {
|
||||
// logger.error("销户失败:{}", e.getMessage());
|
||||
// return ApiResponse.error("CLOSE_FAILED", "销户失败");
|
||||
// }
|
||||
// }
|
||||
//
|
||||
//}
|
Loading…
Reference in new issue