| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233 |
- 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<ZichanInfo>;
- // 验证必填字段
- 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<ZichanInfo>;
- // 验证必填字段
- 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;
- }
|