77 lines
3.5 KiB
Java
77 lines
3.5 KiB
Java
package com.example.demo.admin;
|
||
import org.springframework.http.ResponseEntity;
|
||
import org.springframework.jdbc.core.JdbcTemplate;
|
||
import org.springframework.web.bind.annotation.*;
|
||
|
||
import java.util.*;
|
||
|
||
@RestController
|
||
@RequestMapping("/api/admin/parts")
|
||
public class AdminPartController {
|
||
|
||
private final JdbcTemplate jdbcTemplate;
|
||
|
||
public AdminPartController(JdbcTemplate jdbcTemplate) {
|
||
this.jdbcTemplate = jdbcTemplate;
|
||
}
|
||
|
||
@GetMapping
|
||
public ResponseEntity<?> list(@RequestParam(name = "shopId", required = false) Long shopId,
|
||
@RequestParam(name = "kw", required = false) String kw,
|
||
@RequestParam(name = "status", required = false) String status,
|
||
@RequestParam(name = "page", defaultValue = "1") int page,
|
||
@RequestParam(name = "size", defaultValue = "20") int size) {
|
||
int offset = Math.max(0, page - 1) * Math.max(1, size);
|
||
StringBuilder sql = new StringBuilder(
|
||
"SELECT p.id,p.user_id AS userId,p.shop_id AS shopId,s.name AS shopName,p.name,p.brand,p.model,p.spec,p.is_blacklisted " +
|
||
"FROM products p JOIN shops s ON s.id=p.shop_id WHERE p.deleted_at IS NULL");
|
||
List<Object> ps = new ArrayList<>();
|
||
if (shopId != null) { sql.append(" AND p.shop_id=?"); ps.add(shopId); }
|
||
if (kw != null && !kw.isBlank()) {
|
||
sql.append(" AND (p.name LIKE ? OR p.brand LIKE ? OR p.model LIKE ? OR p.spec LIKE ?)");
|
||
String like = "%" + kw.trim() + "%";
|
||
ps.add(like); ps.add(like); ps.add(like); ps.add(like);
|
||
}
|
||
if (status != null && !status.isBlank()) {
|
||
// 支持两种入参:"1/0"(正常/黑名单)或历史字符串(忽略过滤避免报错)
|
||
try {
|
||
int s = Integer.parseInt(status);
|
||
int isBlack = (s == 1 ? 0 : 1);
|
||
sql.append(" AND p.is_blacklisted=?");
|
||
ps.add(isBlack);
|
||
} catch (NumberFormatException ignore) {
|
||
// 兼容历史:pending/approved/rejected → 不加过滤
|
||
}
|
||
}
|
||
sql.append(" ORDER BY p.id DESC LIMIT ").append(offset).append(", ").append(size);
|
||
List<Map<String,Object>> list = jdbcTemplate.query(sql.toString(), ps.toArray(), (rs, i) -> {
|
||
Map<String,Object> m = new LinkedHashMap<>();
|
||
m.put("id", rs.getLong("id"));
|
||
m.put("userId", rs.getLong("userId"));
|
||
m.put("shopId", rs.getLong("shopId"));
|
||
m.put("shopName", rs.getString("shopName"));
|
||
m.put("name", rs.getString("name"));
|
||
m.put("brand", rs.getString("brand"));
|
||
m.put("model", rs.getString("model"));
|
||
m.put("spec", rs.getString("spec"));
|
||
m.put("status", rs.getInt("is_blacklisted") == 1 ? 0 : 1);
|
||
return m;
|
||
});
|
||
Map<String,Object> body = new HashMap<>(); body.put("list", list); return ResponseEntity.ok(body);
|
||
}
|
||
|
||
@PutMapping("/{id}/blacklist")
|
||
public ResponseEntity<?> blacklist(@PathVariable("id") Long id) {
|
||
jdbcTemplate.update("UPDATE products SET is_blacklisted=1 WHERE id=?", id);
|
||
return ResponseEntity.ok().build();
|
||
}
|
||
|
||
@PutMapping("/{id}/restore")
|
||
public ResponseEntity<?> restore(@PathVariable("id") Long id) {
|
||
jdbcTemplate.update("UPDATE products SET is_blacklisted=0 WHERE id=?", id);
|
||
return ResponseEntity.ok().build();
|
||
}
|
||
}
|
||
|
||
|