1 changed files with 178 additions and 202 deletions
@ -1,202 +1,178 @@ |
|||||||
//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.dao.EmptyResultDataAccessException; |
||||||
//
|
import org.springframework.http.HttpStatus; |
||||||
//import org.springframework.util.CollectionUtils;
|
import org.springframework.http.ResponseEntity; |
||||||
//import org.springframework.web.bind.annotation.*;
|
import org.springframework.jdbc.core.JdbcTemplate; |
||||||
//
|
import org.springframework.transaction.annotation.Transactional; |
||||||
//import javax.transaction.Transactional;
|
import org.springframework.util.CollectionUtils; |
||||||
//import java.time.LocalDateTime;
|
import org.springframework.util.StringUtils; |
||||||
//import java.time.format.DateTimeFormatter;
|
import org.springframework.web.bind.annotation.*; |
||||||
//import java.util.HashMap;
|
|
||||||
//import java.util.List;
|
import java.time.LocalDateTime; |
||||||
//import java.util.Map;
|
import java.time.format.DateTimeFormatter; |
||||||
//
|
import java.util.ArrayList; |
||||||
//@RestController
|
import java.util.List; |
||||||
//@RequestMapping("/weiyichi")
|
import java.util.Map; |
||||||
//public class TransferAccount {
|
import java.math.BigDecimal; |
||||||
//
|
|
||||||
// // add 01
|
@RestController |
||||||
// @Autowired
|
@RequestMapping("/weiyichi") |
||||||
// private JdbcTemplate jdbcTemplate;
|
public class transferAccount { |
||||||
//
|
@Autowired |
||||||
// //转账
|
private JdbcTemplate jdbcTemplate; |
||||||
// @Transactional
|
private Boolean verifyAccountPassword(String cardNo, String password) { |
||||||
// @PostMapping("/transfer")
|
String sql = "SELECT * FROM account_tab WHERE cardNo = ? AND password = ?"; |
||||||
// public ApiResponse<?> transfer(@RequestBody TransferDTO transferDTO) {
|
return !jdbcTemplate.queryForList(sql, cardNo, password).isEmpty(); |
||||||
// try {
|
} |
||||||
// // 参数校验
|
|
||||||
// if (transferDTO.getOutCardNo() == null || transferDTO.getInCardNo() == null) {
|
// 查询账户信息
|
||||||
// return ApiResponse.error("INVALID_CARDNO", "卡号不能为空");
|
private List<Map<String, Object>> getAccount(String cardNo) { |
||||||
// }
|
String sql = "SELECT * FROM account_tab WHERE cardNo = ?"; |
||||||
// if (transferDTO.getAmount() == null || transferDTO.getAmount() <= 0) {
|
return jdbcTemplate.queryForList(sql, cardNo); |
||||||
// return ApiResponse.error("INVALID_AMOUNT", "转账金额必须大于0");
|
} |
||||||
// }
|
|
||||||
//
|
// 生成流水号
|
||||||
// // 验证转出账户
|
private String getRecordNo(String staffNo, String organiNm) { |
||||||
// String outSql = "SELECT * FROM account_tab WHERE cardNo = ?";
|
String formattedTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMdd")); |
||||||
// List<Map<String, Object>> outAccount = jdbcTemplate.queryForList(outSql, transferDTO.getOutCardNo());
|
String sql = "SELECT COUNT(*) FROM recordtable"; |
||||||
// if (CollectionUtils.isEmpty(outAccount)) {
|
int count = jdbcTemplate.queryForObject(sql, Integer.class) + 1; |
||||||
// return ApiResponse.error("ACCOUNT_NOT_FOUND", "转出账户不存在");
|
String sequence = String.format("%06d", count); |
||||||
// }
|
return formattedTime + organiNm + staffNo + sequence; |
||||||
//
|
} |
||||||
//// // 验证密码
|
|
||||||
//// if (!verifyAccountPassword(transferDTO.getOutCardNo(), transferDTO.getPassword())) {
|
// 保存流水记录
|
||||||
//// return ApiResponse.error("PASSWORD_MISMATCH", "密码错误");
|
private void saveRecordTable(RecordTable record) { |
||||||
//// }
|
String sql = "INSERT INTO recordtable (date, cardNo, money, serviceTy, balance, recordNo) VALUES (?, ?, ?, ?, ?, ?)"; |
||||||
//
|
String date = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")); |
||||||
// // 验证转入账户
|
jdbcTemplate.update(sql, date, record.getCardNo(), record.getMoney(), record.getServiceTy(), record.getBalance(), record.getRecordNo()); |
||||||
// String inSql = "SELECT * FROM account_tab WHERE cardNo = ?";
|
} |
||||||
// if (CollectionUtils.isEmpty(jdbcTemplate.queryForList(inSql, transferDTO.getInCardNo()))) {
|
// 转账接口
|
||||||
// return ApiResponse.error("ACCOUNT_NOT_FOUND", "转入账户不存在");
|
@PostMapping("/transfer") |
||||||
// }
|
@Transactional |
||||||
//
|
public ResponseEntity<String> transfer(@RequestBody TransferDTO transferDTO) { |
||||||
// // 检查余额
|
// 验证转出账户
|
||||||
// double outBalance = Double.parseDouble(outAccount.get(0).get("balance").toString());
|
if (!verifyAccountPassword(transferDTO.getFromCardNo(), transferDTO.getPassword())) { |
||||||
// if (outBalance < transferDTO.getAmount()) {
|
return ResponseEntity.badRequest().body("密码错误"); |
||||||
// return ApiResponse.error("INSUFFICIENT_BALANCE", "余额不足");
|
} |
||||||
// }
|
|
||||||
//
|
// 查询转出账户
|
||||||
// // 更新余额
|
List<Map<String, Object>> fromAccount = getAccount(transferDTO.getFromCardNo()); |
||||||
// jdbcTemplate.update("UPDATE account_tab SET balance = ? WHERE cardNo = ?",
|
if (CollectionUtils.isEmpty(fromAccount)) { |
||||||
// outBalance - transferDTO.getAmount(), transferDTO.getOutCardNo());
|
return ResponseEntity.badRequest().body("转出账户不存在"); |
||||||
// jdbcTemplate.update("UPDATE account_tab SET balance = balance + ? WHERE cardNo = ?",
|
} |
||||||
// transferDTO.getAmount(), transferDTO.getInCardNo());
|
Double fromBalance = Double.parseDouble(fromAccount.get(0).get("balance").toString()); |
||||||
//
|
|
||||||
// // 生成流水记录
|
// 验证余额
|
||||||
// generateTransferRecords(transferDTO, outBalance);
|
if (fromBalance < transferDTO.getMoney()) { |
||||||
// logger.info("转账成功:从 {} 到 {},金额 {}",
|
return ResponseEntity.badRequest().body("余额不足"); |
||||||
// transferDTO.getOutCardNo(), transferDTO.getInCardNo(), transferDTO.getAmount());
|
} |
||||||
// return ApiResponse.success(null);
|
|
||||||
// } catch (Exception e) {
|
// 查询转入账户
|
||||||
// logger.error("转账失败:{}", e.getMessage());
|
List<Map<String, Object>> toAccount = getAccount(transferDTO.getToCardNo()); |
||||||
// return ApiResponse.error("TRANSFER_FAILED", "转账失败");
|
if (CollectionUtils.isEmpty(toAccount)) { |
||||||
// }
|
return ResponseEntity.badRequest().body("转入账户不存在"); |
||||||
// }
|
} |
||||||
//
|
|
||||||
//
|
// 更新转出账户
|
||||||
//
|
String updateFromSql = "UPDATE account_tab SET balance = balance - ? WHERE cardNo = ?"; |
||||||
// // 账户查询
|
jdbcTemplate.update(updateFromSql, transferDTO.getMoney(), transferDTO.getFromCardNo()); |
||||||
// @GetMapping("/query")
|
|
||||||
// public ApiResponse<Map<String, Object>> query(
|
// 更新转入账户
|
||||||
// @RequestParam String cardNo,
|
String updateToSql = "UPDATE account_tab SET balance = balance + ? WHERE cardNo = ?"; |
||||||
// @RequestParam(required = false) String type,
|
jdbcTemplate.update(updateToSql, transferDTO.getMoney(), transferDTO.getToCardNo()); |
||||||
// @RequestParam(required = false) String filter,
|
|
||||||
// @RequestParam(required = false) String startDate,
|
// 生成流水记录(转出)
|
||||||
// @RequestParam(required = false) String endDate) {
|
createTransferRecord(transferDTO.getFromCardNo(), transferDTO.getMoney(), "2", |
||||||
//
|
Double.parseDouble(fromAccount.get(0).get("balance").toString()) - transferDTO.getMoney(), |
||||||
// // 参数校验
|
transferDTO.getStaffNo(), transferDTO.getOrganiNm()); |
||||||
// if (cardNo == null || !cardNo.matches("\\d{16}")) {
|
|
||||||
// return ApiResponse.error("INVALID_CARDNO", "卡号格式错误");
|
// 生成流水记录(转入)
|
||||||
// }
|
createTransferRecord(transferDTO.getToCardNo(), transferDTO.getMoney(), "1", |
||||||
//
|
Double.parseDouble(toAccount.get(0).get("balance").toString()) + transferDTO.getMoney(), |
||||||
// Map<String, Object> result = new HashMap<>();
|
transferDTO.getStaffNo(), transferDTO.getOrganiNm()); |
||||||
//
|
|
||||||
// // 余额查询
|
return ResponseEntity.ok("转账成功"); |
||||||
// if (type == null || "balance".equals(type)) {
|
} |
||||||
// try {
|
|
||||||
// String balanceSql = "SELECT balance FROM account_tab WHERE cardNo = ?";
|
private void createTransferRecord(String cardNo, Double amount, String serviceTy, |
||||||
// Double balance = jdbcTemplate.queryForObject(balanceSql, Double.class, cardNo);
|
Double balance, String staffNo, String organiNm) { |
||||||
// result.put("balance", balance);
|
RecordTable record = new RecordTable(); |
||||||
// } catch (Exception e) {
|
record.setCardNo(cardNo); |
||||||
// return ApiResponse.error("BALANCE_QUERY_FAILED", "余额查询失败");
|
record.setMoney(amount); |
||||||
// }
|
record.setServiceTy(serviceTy); |
||||||
// }
|
record.setBalance(balance); |
||||||
//
|
record.setRecordNo(getRecordNo(staffNo, organiNm)); |
||||||
// // 交易记录查询
|
saveRecordTable(record); |
||||||
// if (type == null || "transactions".equals(type)) {
|
} |
||||||
// try {
|
@GetMapping("/getTransactions") |
||||||
// String transSql = buildTransactionSql(filter, startDate, endDate);
|
public List<Map<String, Object>> getTransactions( |
||||||
// List<Map<String, Object>> transactions = jdbcTemplate.queryForList(transSql, cardNo);
|
@RequestParam String cardNo, |
||||||
// result.put("transactions", transactions);
|
@RequestParam(required = false) String startDate, |
||||||
// } catch (Exception e) {
|
@RequestParam(required = false) String endDate) { |
||||||
// return ApiResponse.error("TRANSACTION_QUERY_FAILED", "交易记录查询失败");
|
|
||||||
// }
|
StringBuilder sql = new StringBuilder( |
||||||
// }
|
"SELECT date, money, serviceTy, balance, recordNo FROM recordtable WHERE cardNo = ?"); |
||||||
//
|
|
||||||
// return ApiResponse.success(result);
|
List<Object> params = new ArrayList<>(); |
||||||
// }
|
params.add(cardNo); |
||||||
//
|
|
||||||
// //构建交易记录查询
|
if (StringUtils.hasText(startDate)) { |
||||||
// private String buildTransactionSql(String filter, String startDate, String endDate) {
|
sql.append(" AND date >= ?"); |
||||||
// String sql = "SELECT " +
|
params.add(startDate + " 00:00:00"); |
||||||
// "date, cardNo, money, " +
|
} |
||||||
// "CASE serviceTy " +
|
if (StringUtils.hasText(endDate)) { |
||||||
// "WHEN '1' THEN '存款' " +
|
sql.append(" AND date <= ?"); |
||||||
// "WHEN '2' THEN '取款' " +
|
params.add(endDate + " 23:59:59"); |
||||||
// "WHEN '3' THEN '转账' " +
|
} |
||||||
// "ELSE '未知' " +
|
sql.append(" ORDER BY date DESC"); |
||||||
// "END AS serviceDesc, " +
|
|
||||||
// "balance, recordNo " +
|
return jdbcTemplate.queryForList(sql.toString(), params.toArray()); |
||||||
// "FROM recordtable " +
|
} |
||||||
// "WHERE cardNo = ?";
|
|
||||||
//
|
@GetMapping("/getBalance") |
||||||
// if ("income".equals(filter)) {
|
public ResponseEntity<Double> getBalance(@RequestParam String cardNo) { |
||||||
// sql += " AND serviceTy = '1'";
|
String sql = "SELECT balance FROM account_tab WHERE cardNo = ?"; |
||||||
// } else if ("expense".equals(filter)) {
|
try { |
||||||
// sql += " AND serviceTy IN ('2', '3')";
|
Double balance = jdbcTemplate.queryForObject(sql, Double.class, cardNo); |
||||||
// }
|
return ResponseEntity.ok(balance); |
||||||
//
|
} catch (EmptyResultDataAccessException e) { |
||||||
// if (startDate != null && endDate != null) {
|
return ResponseEntity.notFound().build(); |
||||||
// sql += " AND date BETWEEN ? AND ?";
|
} |
||||||
// }
|
} |
||||||
//
|
@PostMapping("/closeAccount") |
||||||
// return sql;
|
public ResponseEntity<String> closeAccount( |
||||||
// }
|
@RequestParam String cardNo, |
||||||
//
|
@RequestParam String password) { |
||||||
// //销户接口
|
|
||||||
// @PostMapping("/closeAccount")
|
// 验证账户
|
||||||
// public ApiResponse<?> closeAccount(
|
if (!verifyAccountPassword(cardNo, password)) { |
||||||
// @RequestParam String cardNo,
|
return ResponseEntity.badRequest().body("密码错误"); |
||||||
// @RequestParam String password,
|
} |
||||||
// @RequestParam String staffNo,
|
|
||||||
// @RequestParam String organiNm) {
|
// 检查余额
|
||||||
//
|
Double balance = jdbcTemplate.queryForObject( |
||||||
// // 参数校验
|
"SELECT balance FROM account_tab WHERE cardNo = ?", Double.class, cardNo); |
||||||
// if (cardNo == null || cardNo.trim().isEmpty()) {
|
|
||||||
// return ApiResponse.error("INVALID_CARDNO", "卡号不能为空");
|
if (balance > 0) { |
||||||
// }
|
return ResponseEntity.badRequest().body("账户余额不为零,请先结清"); |
||||||
// if (password == null || password.trim().isEmpty()) {
|
} |
||||||
// return ApiResponse.error("INVALID_PASSWORD", "密码不能为空");
|
|
||||||
// }
|
// 更新账户状态
|
||||||
//
|
int updated = jdbcTemplate.update( |
||||||
// // 验证密码
|
"UPDATE account_tab SET status = '2' WHERE cardNo = ?", cardNo); |
||||||
// if (!verifyAccountPassword(cardNo, password)) {
|
|
||||||
// return ApiResponse.error("PASSWORD_MISMATCH", "密码错误");
|
if (updated > 0) { |
||||||
// }
|
return ResponseEntity.ok("销户成功"); |
||||||
//
|
} |
||||||
// // 检查余额
|
return ResponseEntity.badRequest().body("销户失败"); |
||||||
// 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