1 changed files with 0 additions and 178 deletions
@ -1,178 +0,0 @@ |
|||||||
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.dao.EmptyResultDataAccessException; |
|
||||||
import org.springframework.http.HttpStatus; |
|
||||||
import org.springframework.http.ResponseEntity; |
|
||||||
import org.springframework.jdbc.core.JdbcTemplate; |
|
||||||
import org.springframework.transaction.annotation.Transactional; |
|
||||||
import org.springframework.util.CollectionUtils; |
|
||||||
import org.springframework.util.StringUtils; |
|
||||||
import org.springframework.web.bind.annotation.*; |
|
||||||
|
|
||||||
import java.time.LocalDateTime; |
|
||||||
import java.time.format.DateTimeFormatter; |
|
||||||
import java.util.ArrayList; |
|
||||||
import java.util.List; |
|
||||||
import java.util.Map; |
|
||||||
import java.math.BigDecimal; |
|
||||||
|
|
||||||
@RestController |
|
||||||
@RequestMapping("/weiyichi") |
|
||||||
public class transferAccount { |
|
||||||
@Autowired |
|
||||||
private JdbcTemplate jdbcTemplate; |
|
||||||
private Boolean verifyAccountPassword(String cardNo, String password) { |
|
||||||
String sql = "SELECT * FROM account_tab WHERE cardNo = ? AND password = ?"; |
|
||||||
return !jdbcTemplate.queryForList(sql, cardNo, password).isEmpty(); |
|
||||||
} |
|
||||||
|
|
||||||
// 查询账户信息
|
|
||||||
private List<Map<String, Object>> getAccount(String cardNo) { |
|
||||||
String sql = "SELECT * FROM account_tab WHERE cardNo = ?"; |
|
||||||
return jdbcTemplate.queryForList(sql, cardNo); |
|
||||||
} |
|
||||||
|
|
||||||
// 生成流水号
|
|
||||||
private String getRecordNo(String staffNo, String organiNm) { |
|
||||||
String formattedTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMdd")); |
|
||||||
String sql = "SELECT COUNT(*) FROM recordtable"; |
|
||||||
int count = jdbcTemplate.queryForObject(sql, Integer.class) + 1; |
|
||||||
String sequence = String.format("%06d", count); |
|
||||||
return formattedTime + organiNm + staffNo + sequence; |
|
||||||
} |
|
||||||
|
|
||||||
// 保存流水记录
|
|
||||||
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()); |
|
||||||
} |
|
||||||
// 转账接口
|
|
||||||
@PostMapping("/transfer") |
|
||||||
@Transactional |
|
||||||
public ResponseEntity<String> transfer(@RequestBody TransferDTO transferDTO) { |
|
||||||
// 验证转出账户
|
|
||||||
if (!verifyAccountPassword(transferDTO.getFromCardNo(), transferDTO.getPassword())) { |
|
||||||
return ResponseEntity.badRequest().body("密码错误"); |
|
||||||
} |
|
||||||
|
|
||||||
// 查询转出账户
|
|
||||||
List<Map<String, Object>> fromAccount = getAccount(transferDTO.getFromCardNo()); |
|
||||||
if (CollectionUtils.isEmpty(fromAccount)) { |
|
||||||
return ResponseEntity.badRequest().body("转出账户不存在"); |
|
||||||
} |
|
||||||
Double fromBalance = Double.parseDouble(fromAccount.get(0).get("balance").toString()); |
|
||||||
|
|
||||||
// 验证余额
|
|
||||||
if (fromBalance < transferDTO.getMoney()) { |
|
||||||
return ResponseEntity.badRequest().body("余额不足"); |
|
||||||
} |
|
||||||
|
|
||||||
// 查询转入账户
|
|
||||||
List<Map<String, Object>> toAccount = getAccount(transferDTO.getToCardNo()); |
|
||||||
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()); |
|
||||||
|
|
||||||
// 更新转入账户
|
|
||||||
String updateToSql = "UPDATE account_tab SET balance = balance + ? WHERE cardNo = ?"; |
|
||||||
jdbcTemplate.update(updateToSql, transferDTO.getMoney(), transferDTO.getToCardNo()); |
|
||||||
|
|
||||||
// 生成流水记录(转出)
|
|
||||||
createTransferRecord(transferDTO.getFromCardNo(), transferDTO.getMoney(), "2", |
|
||||||
Double.parseDouble(fromAccount.get(0).get("balance").toString()) - transferDTO.getMoney(), |
|
||||||
transferDTO.getStaffNo(), transferDTO.getOrganiNm()); |
|
||||||
|
|
||||||
// 生成流水记录(转入)
|
|
||||||
createTransferRecord(transferDTO.getToCardNo(), transferDTO.getMoney(), "1", |
|
||||||
Double.parseDouble(toAccount.get(0).get("balance").toString()) + transferDTO.getMoney(), |
|
||||||
transferDTO.getStaffNo(), transferDTO.getOrganiNm()); |
|
||||||
|
|
||||||
return ResponseEntity.ok("转账成功"); |
|
||||||
} |
|
||||||
|
|
||||||
private void createTransferRecord(String cardNo, Double amount, String serviceTy, |
|
||||||
Double balance, String staffNo, String organiNm) { |
|
||||||
RecordTable record = new RecordTable(); |
|
||||||
record.setCardNo(cardNo); |
|
||||||
record.setMoney(amount); |
|
||||||
record.setServiceTy(serviceTy); |
|
||||||
record.setBalance(balance); |
|
||||||
record.setRecordNo(getRecordNo(staffNo, organiNm)); |
|
||||||
saveRecordTable(record); |
|
||||||
} |
|
||||||
@GetMapping("/getTransactions") |
|
||||||
public List<Map<String, Object>> getTransactions( |
|
||||||
@RequestParam String cardNo, |
|
||||||
@RequestParam(required = false) String startDate, |
|
||||||
@RequestParam(required = false) String endDate) { |
|
||||||
|
|
||||||
StringBuilder sql = new StringBuilder( |
|
||||||
"SELECT date, money, serviceTy, balance, recordNo FROM recordtable WHERE cardNo = ?"); |
|
||||||
|
|
||||||
List<Object> params = new ArrayList<>(); |
|
||||||
params.add(cardNo); |
|
||||||
|
|
||||||
if (StringUtils.hasText(startDate)) { |
|
||||||
sql.append(" AND date >= ?"); |
|
||||||
params.add(startDate + " 00:00:00"); |
|
||||||
} |
|
||||||
if (StringUtils.hasText(endDate)) { |
|
||||||
sql.append(" AND date <= ?"); |
|
||||||
params.add(endDate + " 23:59:59"); |
|
||||||
} |
|
||||||
sql.append(" ORDER BY date DESC"); |
|
||||||
|
|
||||||
return jdbcTemplate.queryForList(sql.toString(), params.toArray()); |
|
||||||
} |
|
||||||
|
|
||||||
@GetMapping("/getBalance") |
|
||||||
public ResponseEntity<Double> getBalance(@RequestParam String cardNo) { |
|
||||||
String sql = "SELECT balance FROM account_tab WHERE cardNo = ?"; |
|
||||||
try { |
|
||||||
Double balance = jdbcTemplate.queryForObject(sql, Double.class, cardNo); |
|
||||||
return ResponseEntity.ok(balance); |
|
||||||
} catch (EmptyResultDataAccessException e) { |
|
||||||
return ResponseEntity.notFound().build(); |
|
||||||
} |
|
||||||
} |
|
||||||
@PostMapping("/closeAccount") |
|
||||||
public ResponseEntity<String> closeAccount( |
|
||||||
@RequestParam String cardNo, |
|
||||||
@RequestParam String password) { |
|
||||||
|
|
||||||
// 验证账户
|
|
||||||
if (!verifyAccountPassword(cardNo, password)) { |
|
||||||
return ResponseEntity.badRequest().body("密码错误"); |
|
||||||
} |
|
||||||
|
|
||||||
// 检查余额
|
|
||||||
Double balance = jdbcTemplate.queryForObject( |
|
||||||
"SELECT balance FROM account_tab WHERE cardNo = ?", Double.class, cardNo); |
|
||||||
|
|
||||||
if (balance > 0) { |
|
||||||
return ResponseEntity.badRequest().body("账户余额不为零,请先结清"); |
|
||||||
} |
|
||||||
|
|
||||||
// 更新账户状态
|
|
||||||
int updated = jdbcTemplate.update( |
|
||||||
"UPDATE account_tab SET status = '2' WHERE cardNo = ?", cardNo); |
|
||||||
|
|
||||||
if (updated > 0) { |
|
||||||
return ResponseEntity.ok("销户成功"); |
|
||||||
} |
|
||||||
return ResponseEntity.badRequest().body("销户失败"); |
|
||||||
} |
|
||||||
} |
|
Loading…
Reference in new issue