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-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-1: 必须加 @DS("{app_code}") 数据源切换 */ @Slf4j @RestController @RequiredArgsConstructor @DS("{app_code}") // ⭐ FIX-TEMPLATE-1: 数据源切换 @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) Integer 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) { entity.setFId(id); 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()); } }