import { Hono } from "hono"; import debug from "debug"; import type { ZichanCategory } from "../client/share/monitorTypes.ts"; import type { Variables, WithAuth } from "./middlewares.ts"; const log = { api: debug("d8d:zichan:api"), }; // 创建资产分类管理路由 export function createZichanCategoryRoutes(withAuth: WithAuth) { const zichanCategoryRoutes = new Hono<{ Variables: Variables }>(); // 获取资产分类列表 zichanCategoryRoutes.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 name = c.req.query("name"); const code = c.req.query("code"); let query = apiClient.database .table("zichan_category") .where("is_deleted", 0) .orderBy("id", "desc") .limit(limit) .offset(offset); if (name) { query = query.where("name", "like", `%${name}%`); } if (code) { query = query.where("code", "like", `%${code}%`); } // 克隆查询以获取总数 const countQuery = query.clone(); // 执行分页查询 const categories = await query.limit(limit).offset(offset); // 获取总数 const count = await countQuery.count(); return c.json({ data: categories, pagination: { total: Number(count), current: page, pageSize: limit, totalPages: Math.ceil(Number(count) / limit), }, }); } catch (error) { log.api("获取资产分类列表失败:", error); return c.json({ error: "获取资产分类列表失败" }, 500); } }); // 创建资产分类 zichanCategoryRoutes.post("/", withAuth, async (c) => { try { const apiClient = c.get('apiClient'); const categoryData = (await c.req.json()) as Partial; // 验证必填字段 if (!categoryData.name || !categoryData.code) { return c.json({ error: "名称和编码不能为空" }, 400); } // 检查是否已存在相同编码的分类 const existingCategory = await apiClient.database .table("zichan_category") .where("code", categoryData.code) .where("is_deleted", 0) .first(); if (existingCategory) { return c.json({ error: "相同编码的分类已存在" }, 400); } // 插入新分类 const [id] = await apiClient.database .table("zichan_category") .insert(categoryData); return c.json({ message: "资产分类创建成功", data: { id, ...categoryData }, }); } catch (error) { log.api("创建资产分类失败:", error); return c.json({ error: "创建资产分类失败" }, 500); } }); // 更新资产分类 zichanCategoryRoutes.put("/:id", withAuth, async (c) => { try { const apiClient = c.get('apiClient'); const id = Number(c.req.param("id")); const categoryData = (await c.req.json()) as Partial; // 验证必填字段 if (!categoryData.name || !categoryData.code) { return c.json({ error: "名称和编码不能为空" }, 400); } // 检查是否已存在相同编码的分类 const existingCategory = await apiClient.database .table("zichan_category") .where("code", categoryData.code) .where("is_deleted", 0) .where("id", "!=", id) .first(); if (existingCategory) { return c.json({ error: "相同编码的分类已存在" }, 400); } // 更新分类 await apiClient.database .table("zichan_category") .where("id", id) .update({ ...categoryData, updated_at: apiClient.database.fn.now(), }); return c.json({ message: "资产分类更新成功", data: { id, ...categoryData }, }); } catch (error) { log.api("更新资产分类失败:", error); return c.json({ error: "更新资产分类失败" }, 500); } }); // 删除资产分类 zichanCategoryRoutes.delete("/:id", withAuth, async (c) => { try { const apiClient = c.get('apiClient'); const id = Number(c.req.param("id")); if (!id || isNaN(id)) { return c.json({ error: "无效的资产分类ID" }, 400); } // 检查是否有关联的资产 const assetCount = await apiClient.database .table("zichan_info") .where("device_category", id) .where("is_deleted", 0) .count(); if (assetCount > 0) { return c.json({ error: "该分类下有关联的资产,无法删除" }, 400); } // 软删除分类 await apiClient.database.table("zichan_category").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 zichanCategoryRoutes; }