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