package ssw.{app_code}.controller; import com.baomidou.dynamic.datasource.annotation.DS; import ssw.{app_code}.entity.XxxEntity; import ssw.{app_code}.service.XxxService; import jnpf.base.ActionResult; import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.tags.Tag; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.web.bind.annotation.*; import java.util.List; import java.util.Map; /** * {业务描述} Controller * * 路径规范: /api/ssw/{app_code}/{module}/... * 不要用 /api/ssw/{app_code}/public/..., 业务接口必须登录 * * ⭐ FIX-TEMPLATE-V0.12+ (jxcj 实战验证): * - @DS("{app_code}") 在 Controller 类上 (FIX-TEMPLATE-1) * - 配套: Entity extends SuperBaseEntity + Mapper 放 jnpf.mapper + Service 也加 @DS * * ⚠️ FIX-TEMPLATE-4: create() 返回 ActionResult 并返回 entity * ✅ ActionResult.success(entity) - 客户端拿到 f_id * ❌ ActionResult.success() - 客户端拿不到 f_id * * ⚠️ FIX-TEMPLATE-5: 业务错误码规范 * ✅ ActionResult.fail(400, "MISSING_FIELD") - code=400 * ✅ ActionResult.fail(401, "INVALID_SECRET") - 鉴权失败 * ✅ ActionResult.fail(403, "FORBIDDEN") - 权限不足 * ✅ ActionResult.fail(404, "NOT_FOUND") - 资源不存在 * ❌ ActionResult.fail("401 INVALID_SECRET") - code 实际是 400 * * ⚠️ FIX-TEMPLATE-V0.12+ (jxcj 实战踩坑): * SuperBaseEntity.id 是 public field, Lombok @Data 不生成 setId * PUT 更新时必须用反射设值, 避免编译错误: * entity.id = id; // ❌ 编译失败 (类型不匹配) * idField.set(entity, id); // ✅ 用反射 */ @Slf4j @RestController @RequiredArgsConstructor @DS("{app_code}") // ⭐ FIX-TEMPLATE-1: 数据源切换 (Controller 也需要!) @Tag(name = "Xxx", description = "{业务描述}") @RequestMapping("/api/ssw/{app_code}/{module}") public class XxxController { private final XxxService xxxService; /** 列表 (GET ?keyword=&status=) */ @GetMapping("/list") @Operation(summary = "{业务}列表") public ActionResult> list( @RequestParam(required = false) String keyword, @RequestParam(required = false) String status) { return ActionResult.success(xxxService.getList(keyword, status)); } /** 详情 (GET /{id}) */ @GetMapping("/{id}") @Operation(summary = "{业务}详情") public ActionResult info(@PathVariable("id") String id) { return ActionResult.success(xxxService.getInfo(id)); } /** ⭐ FIX-TEMPLATE-4: 新增返回实体 (客户端能拿 f_id) */ @PostMapping @Operation(summary = "新增{业务}") public ActionResult create(@RequestBody XxxEntity entity) { xxxService.create(entity); return ActionResult.success(entity); } /** 更新 (PUT /{id} body=JSON) */ @PutMapping("/{id}") @Operation(summary = "更新{业务}") public ActionResult update(@PathVariable("id") String id, @RequestBody XxxEntity entity) { // ⭐ FIX-TEMPLATE-V0.12+: SuperBaseEntity.id 是 public field, 用反射设值 try { java.lang.reflect.Field idField = entity.getClass().getSuperclass().getDeclaredField("id"); idField.setAccessible(true); idField.set(entity, id); } catch (Exception e) { return ActionResult.fail("设置 id 失败: " + e.getMessage()); } return xxxService.update(entity) ? ActionResult.success() : ActionResult.fail("操作失败"); } /** 删除 (DELETE /{id}) */ @DeleteMapping("/{id}") @Operation(summary = "删除{业务}") public ActionResult delete(@PathVariable("id") String id) { return xxxService.delete(id) ? ActionResult.success() : ActionResult.fail("操作失败"); } /** 统计 (如需) */ @GetMapping("/stats") @Operation(summary = "统计概览") public ActionResult> stats() { return ActionResult.success(xxxService.getStats()); } }