import { Hono } from "hono"; import debug from "debug"; import type { ZichanInfo, } from "../client/share/monitorTypes.ts"; import { DeviceCategory, DeviceStatus, } from "../client/share/monitorTypes.ts"; import type { Variables, WithAuth } from "./middlewares.ts"; const log = { api: debug("d8d:zichan:api"), }; // 创建资产管理路由 export function createZichanRoutes(withAuth: WithAuth) { const zichanRoutes = new Hono<{ Variables: Variables }>(); // 获取资产列表 zichanRoutes.get("/", withAuth, async (c) => { try { const apiClient = c.get('apiClient'); // 获取分页参数 const page = Number(c.req.query("page")) || 1; const limit = Number(c.req.query("limit")) || 10; const offset = (page - 1) * limit; // 获取筛选参数 const assetName = c.req.query("asset_name"); const categoryParam = c.req.query("device_category"); const statusParam = c.req.query("device_status"); // 构建查询 let query = apiClient.database .table("zichan_info") .where("is_deleted", 0) .orderBy("id", "desc"); // 应用筛选条件 if (assetName) { query = query.where("asset_name", "like", `%${assetName}%`); } if (categoryParam) { const category = Number(categoryParam); if ( !isNaN(category) && Object.values(DeviceCategory).includes(category) ) { query = query.where("device_category", category); } } if (statusParam) { const status = Number(statusParam); if (!isNaN(status) && Object.values(DeviceStatus).includes(status)) { query = query.where("device_status", status); } } // 克隆查询以获取总数 const countQuery = query.clone(); // 执行分页查询 const assets = await query.limit(limit).offset(offset); // 获取总数 const count = await countQuery.count(); return c.json({ data: assets, pagination: { total: Number(count), current: page, pageSize: limit, totalPages: Math.ceil(Number(count) / limit), }, }); } catch (error) { log.api("获取资产列表失败:", error); return c.json({ error: "获取资产列表失败" }, 500); } }); // 获取单个资产 zichanRoutes.get("/:id", withAuth, async (c) => { try { const id = Number(c.req.param("id")); if (!id || isNaN(id)) { return c.json({ error: "无效的资产ID" }, 400); } const apiClient = c.get('apiClient'); const [asset] = await apiClient.database .table("zichan_info") .where({ id, is_deleted: 0 }); if (!asset) { return c.json({ error: "资产不存在" }, 404); } return c.json(asset); } catch (error) { log.api("获取资产详情失败:", error); return c.json({ error: "获取资产详情失败" }, 500); } }); // 创建资产 zichanRoutes.post("/", withAuth, async (c) => { try { const assetData = (await c.req.json()) as Partial; // 验证必填字段 if (!assetData.asset_name) { return c.json({ error: "资产名称不能为空" }, 400); } const apiClient = c.get('apiClient'); const [id] = await apiClient.database .table("zichan_info") .insert(assetData); // 获取创建的资产 const [createdAsset] = await apiClient.database .table("zichan_info") .where("id", id); return c.json({ message: "资产创建成功", data: createdAsset, }); } catch (error) { log.api("创建资产失败:", error); return c.json({ error: "创建资产失败" }, 500); } }); // 更新资产 zichanRoutes.put("/:id", withAuth, async (c) => { try { const id = Number(c.req.param("id")); if (!id || isNaN(id)) { return c.json({ error: "无效的资产ID" }, 400); } const assetData = (await c.req.json()) as Partial; // 验证必填字段 if (!assetData.asset_name) { return c.json({ error: "资产名称不能为空" }, 400); } const apiClient = c.get('apiClient'); // 检查资产是否存在 const [existingAsset] = await apiClient.database .table("zichan_info") .where({ id, is_deleted: 0 }); if (!existingAsset) { return c.json({ error: "资产不存在" }, 404); } // 更新资产 await apiClient.database .table("zichan_info") .where("id", id) .update({ ...assetData, updated_at: apiClient.database.fn.now(), }); // 获取更新后的资产 const [updatedAsset] = await apiClient.database .table("zichan_info") .where("id", id); return c.json({ message: "资产更新成功", data: updatedAsset, }); } catch (error) { log.api("更新资产失败:", error); return c.json({ error: "更新资产失败" }, 500); } }); // 删除资产(软删除) zichanRoutes.delete("/:id", withAuth, async (c) => { try { const id = Number(c.req.param("id")); if (!id || isNaN(id)) { return c.json({ error: "无效的资产ID" }, 400); } const apiClient = c.get('apiClient'); // 检查资产是否存在 const [existingAsset] = await apiClient.database .table("zichan_info") .where({ id, is_deleted: 0 }); if (!existingAsset) { return c.json({ error: "资产不存在" }, 404); } // 软删除资产 await apiClient.database.table("zichan_info").where("id", id).update({ is_deleted: 1, updated_at: apiClient.database.fn.now(), }); return c.json({ message: "资产删除成功", id, }); } catch (error) { log.api("删除资产失败:", error); return c.json({ error: "删除资产失败" }, 500); } }); return zichanRoutes; }