routes_zichan_area.ts 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. import { Hono } from "hono";
  2. import debug from "debug";
  3. import type {
  4. ZichanArea,
  5. } from "../client/share/monitorTypes.ts";
  6. import type { Variables, WithAuth } from "./middlewares.ts";
  7. const log = {
  8. api: debug("d8d:zichan:api"),
  9. };
  10. // 创建资产归属区域管理路由
  11. export function createZichanAreaRoutes(withAuth: WithAuth) {
  12. const zichanAreaRoutes = new Hono<{ Variables: Variables }>();
  13. // 获取资产归属区域列表
  14. zichanAreaRoutes.get("/", withAuth, async (c) => {
  15. try {
  16. const apiClient = c.get('apiClient');
  17. // 获取分页参数
  18. const page = Number(c.req.query("page")) || 1;
  19. const limit = Number(c.req.query("limit")) || 10;
  20. const offset = (page - 1) * limit;
  21. // 获取筛选参数
  22. const name = c.req.query("name");
  23. const code = c.req.query("code");
  24. let query = apiClient.database
  25. .table("zichan_area")
  26. .where("is_deleted", 0)
  27. .orderBy("id", "desc")
  28. .limit(limit)
  29. .offset(offset);
  30. if (name) {
  31. query = query.where("name", "like", `%${name}%`);
  32. }
  33. if (code) {
  34. query = query.where("code", "like", `%${code}%`);
  35. }
  36. // 克隆查询以获取总数
  37. const countQuery = query.clone();
  38. // 执行分页查询
  39. const areas = await query.limit(limit).offset(offset);
  40. // 获取总数
  41. const count = await countQuery.count();
  42. return c.json({
  43. data: areas,
  44. pagination: {
  45. total: Number(count),
  46. current: page,
  47. pageSize: limit,
  48. totalPages: Math.ceil(Number(count) / limit),
  49. },
  50. });
  51. } catch (error) {
  52. log.api("获取资产归属区域列表失败:", error);
  53. return c.json({ error: "获取资产归属区域列表失败" }, 500);
  54. }
  55. });
  56. // 创建资产归属区域
  57. zichanAreaRoutes.post("/", withAuth, async (c) => {
  58. try {
  59. const apiClient = c.get('apiClient');
  60. const areaData = (await c.req.json()) as Partial<ZichanArea>;
  61. // 验证必填字段
  62. if (!areaData.name || !areaData.code) {
  63. return c.json({ error: "名称和编码不能为空" }, 400);
  64. }
  65. // 检查是否已存在相同编码的区域
  66. const existingArea = await apiClient.database
  67. .table("zichan_area")
  68. .where("code", areaData.code)
  69. .where("is_deleted", 0)
  70. .first();
  71. if (existingArea) {
  72. return c.json({ error: "相同编码的区域已存在" }, 400);
  73. }
  74. // 插入新区域
  75. const [id] = await apiClient.database.table("zichan_area").insert(areaData);
  76. return c.json({
  77. message: "资产归属区域创建成功",
  78. data: { id, ...areaData },
  79. });
  80. } catch (error) {
  81. log.api("创建资产归属区域失败:", error);
  82. return c.json({ error: "创建资产归属区域失败" }, 500);
  83. }
  84. });
  85. // 更新资产归属区域
  86. zichanAreaRoutes.put("/:id", withAuth, async (c) => {
  87. try {
  88. const apiClient = c.get('apiClient');
  89. const id = Number(c.req.param("id"));
  90. const areaData = (await c.req.json()) as Partial<ZichanArea>;
  91. // 验证必填字段
  92. if (!areaData.name || !areaData.code) {
  93. return c.json({ error: "名称和编码不能为空" }, 400);
  94. }
  95. // 检查是否已存在相同编码的区域
  96. const existingArea = await apiClient.database
  97. .table("zichan_area")
  98. .where("code", areaData.code)
  99. .where("is_deleted", 0)
  100. .where("id", "!=", id)
  101. .first();
  102. if (existingArea) {
  103. return c.json({ error: "相同编码的区域已存在" }, 400);
  104. }
  105. // 更新区域
  106. await apiClient.database
  107. .table("zichan_area")
  108. .where("id", id)
  109. .update({
  110. ...areaData,
  111. updated_at: apiClient.database.fn.now(),
  112. });
  113. return c.json({
  114. message: "资产归属区域更新成功",
  115. data: { id, ...areaData },
  116. });
  117. } catch (error) {
  118. log.api("更新资产归属区域失败:", error);
  119. return c.json({ error: "更新资产归属区域失败" }, 500);
  120. }
  121. });
  122. // 删除资产归属区域
  123. zichanAreaRoutes.delete("/:id", withAuth, async (c) => {
  124. try {
  125. const apiClient = c.get('apiClient');
  126. const id = Number(c.req.param("id"));
  127. if (!id || isNaN(id)) {
  128. return c.json({ error: "无效的资产归属区域ID" }, 400);
  129. }
  130. // 检查是否有关联的资产
  131. const assetCount = await apiClient.database
  132. .table("zichan_info")
  133. .where("area", id)
  134. .where("is_deleted", 0)
  135. .count();
  136. if (assetCount > 0) {
  137. return c.json({ error: "该区域下有关联的资产,无法删除" }, 400);
  138. }
  139. // 软删除区域
  140. await apiClient.database.table("zichan_area").where("id", id).update({
  141. is_deleted: 1,
  142. updated_at: apiClient.database.fn.now(),
  143. });
  144. return c.json({
  145. message: "资产归属区域删除成功",
  146. id,
  147. });
  148. } catch (error) {
  149. log.api("删除资产归属区域失败:", error);
  150. return c.json({ error: "删除资产归属区域失败" }, 500);
  151. }
  152. });
  153. return zichanAreaRoutes;
  154. }