routes_device_types.ts 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. import { Hono } from "hono";
  2. import debug from "debug";
  3. import type {
  4. DeviceType,
  5. } from "../client/share/monitorTypes.ts";
  6. import {
  7. EnableStatus,
  8. DeleteStatus,
  9. } from "../client/share/types.ts";
  10. import type { Variables, WithAuth } from "./middlewares.ts";
  11. const log = {
  12. api: debug("d8d:device:api"),
  13. };
  14. // 创建设备类型路由
  15. export function createDeviceTypesRoutes(withAuth: WithAuth) {
  16. const deviceTypesRoutes = new Hono<{ Variables: Variables }>();
  17. // 获取设备类型列表
  18. deviceTypesRoutes.get("/", withAuth, async (c) => {
  19. try {
  20. const apiClient = c.get('apiClient');
  21. // 获取分页参数
  22. const page = Number(c.req.query("page")) || 1;
  23. const limit = Number(c.req.query("limit")) || 10;
  24. const offset = (page - 1) * limit;
  25. // 获取筛选参数
  26. const name = c.req.query("name");
  27. const code = c.req.query("code");
  28. // 构建查询
  29. let query = apiClient.database
  30. .table("device_types")
  31. .where("is_deleted", DeleteStatus.NOT_DELETED)
  32. .orderBy("id", "desc");
  33. // 应用筛选条件
  34. if (name) {
  35. query = query.where("name", "like", `%${name}%`);
  36. }
  37. if (code) {
  38. query = query.where("code", "like", `%${code}%`);
  39. }
  40. // 克隆查询以获取总数
  41. const countQuery = query.clone();
  42. // 执行分页查询
  43. const deviceTypes = await query.limit(limit).offset(offset);
  44. // 获取总数
  45. const count = await countQuery.count();
  46. return c.json({
  47. data: deviceTypes,
  48. pagination: {
  49. total: Number(count),
  50. current: page,
  51. pageSize: limit,
  52. totalPages: Math.ceil(Number(count) / limit),
  53. },
  54. });
  55. } catch (error) {
  56. log.api("获取设备类型列表失败:", error);
  57. return c.json({ error: "获取设备类型列表失败" }, 500);
  58. }
  59. });
  60. // 获取单个设备类型
  61. deviceTypesRoutes.get("/:id", withAuth, async (c) => {
  62. try {
  63. const id = Number(c.req.param("id"));
  64. if (!id || isNaN(id)) {
  65. return c.json({ error: "无效的设备类型ID" }, 400);
  66. }
  67. const apiClient = c.get('apiClient');
  68. const [deviceType] = await apiClient.database
  69. .table("device_types")
  70. .where({ id, is_deleted: DeleteStatus.NOT_DELETED });
  71. if (!deviceType) {
  72. return c.json({ error: "设备类型不存在" }, 404);
  73. }
  74. return c.json(deviceType);
  75. } catch (error) {
  76. log.api("获取设备类型详情失败:", error);
  77. return c.json({ error: "获取设备类型详情失败" }, 500);
  78. }
  79. });
  80. // 创建设备类型
  81. deviceTypesRoutes.post("/", withAuth, async (c) => {
  82. try {
  83. const data = (await c.req.json()) as Partial<DeviceType>;
  84. // 验证必填字段
  85. if (!data.name || !data.code) {
  86. return c.json({ error: "类型名称和编码不能为空" }, 400);
  87. }
  88. const apiClient = c.get('apiClient');
  89. // 检查编码是否已存在
  90. const existingCode = await apiClient.database
  91. .table("device_types")
  92. .where({ code: data.code, is_deleted: DeleteStatus.NOT_DELETED })
  93. .first();
  94. if (existingCode) {
  95. return c.json({ error: "类型编码已存在" }, 400);
  96. }
  97. // 插入新记录
  98. const [id] = await apiClient.database.table("device_types").insert({
  99. ...data,
  100. is_enabled: data.is_enabled ?? EnableStatus.ENABLED,
  101. is_deleted: DeleteStatus.NOT_DELETED,
  102. });
  103. // 获取创建的记录
  104. const [createdType] = await apiClient.database
  105. .table("device_types")
  106. .where("id", id);
  107. return c.json({
  108. message: "设备类型创建成功",
  109. data: createdType,
  110. });
  111. } catch (error) {
  112. log.api("创建设备类型失败:", error);
  113. return c.json({ error: "创建设备类型失败" }, 500);
  114. }
  115. });
  116. // 更新设备类型
  117. deviceTypesRoutes.put("/:id", withAuth, async (c) => {
  118. try {
  119. const id = Number(c.req.param("id"));
  120. if (!id || isNaN(id)) {
  121. return c.json({ error: "无效的设备类型ID" }, 400);
  122. }
  123. const data = (await c.req.json()) as Partial<DeviceType>;
  124. // 验证必填字段
  125. if (!data.name || !data.code) {
  126. return c.json({ error: "类型名称和编码不能为空" }, 400);
  127. }
  128. const apiClient = c.get('apiClient');
  129. // 检查编码是否已存在(且不是当前记录)
  130. if (data.code) {
  131. const existingCode = await apiClient.database
  132. .table("device_types")
  133. .where({ code: data.code, is_deleted: DeleteStatus.NOT_DELETED })
  134. .whereNot("id", id.toString())
  135. .first();
  136. if (existingCode) {
  137. return c.json({ error: "类型编码已存在" }, 400);
  138. }
  139. }
  140. // 检查记录是否存在
  141. const existingType = await apiClient.database
  142. .table("device_types")
  143. .where({ id, is_deleted: DeleteStatus.NOT_DELETED })
  144. .first();
  145. if (!existingType) {
  146. return c.json({ error: "设备类型不存在" }, 404);
  147. }
  148. // 更新记录
  149. await apiClient.database
  150. .table("device_types")
  151. .where("id", id)
  152. .update({
  153. ...data,
  154. updated_at: apiClient.database.fn.now(),
  155. });
  156. // 获取更新后的记录
  157. const [updatedType] = await apiClient.database
  158. .table("device_types")
  159. .where("id", id);
  160. return c.json({
  161. message: "设备类型更新成功",
  162. data: updatedType,
  163. });
  164. } catch (error) {
  165. log.api("更新设备类型失败:", error);
  166. return c.json({ error: "更新设备类型失败" }, 500);
  167. }
  168. });
  169. // 删除设备类型(软删除)
  170. deviceTypesRoutes.delete("/:id", withAuth, async (c) => {
  171. try {
  172. const id = Number(c.req.param("id"));
  173. if (!id || isNaN(id)) {
  174. return c.json({ error: "无效的设备类型ID" }, 400);
  175. }
  176. const apiClient = c.get('apiClient');
  177. // 检查是否有关联的设备实例
  178. const relatedInstances = await apiClient.database
  179. .table("device_instances")
  180. .where({ type_id: id, is_deleted: DeleteStatus.NOT_DELETED })
  181. .first();
  182. if (relatedInstances) {
  183. return c.json({ error: "该类型有关联的设备实例,无法删除" }, 400);
  184. }
  185. // 检查记录是否存在
  186. const existingType = await apiClient.database
  187. .table("device_types")
  188. .where({ id, is_deleted: DeleteStatus.NOT_DELETED })
  189. .first();
  190. if (!existingType) {
  191. return c.json({ error: "设备类型不存在" }, 404);
  192. }
  193. // 软删除
  194. await apiClient.database.table("device_types").where("id", id).update({
  195. is_deleted: DeleteStatus.DELETED,
  196. updated_at: apiClient.database.fn.now(),
  197. });
  198. return c.json({
  199. message: "设备类型删除成功",
  200. id,
  201. });
  202. } catch (error) {
  203. log.api("删除设备类型失败:", error);
  204. return c.json({ error: "删除设备类型失败" }, 500);
  205. }
  206. });
  207. return deviceTypesRoutes;
  208. }