| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187 |
- import { Hono } from "hono";
- import debug from "debug";
- import type {
- ZichanArea,
- } from "../client/share/monitorTypes.ts";
- import type { Variables, WithAuth } from "./middlewares.ts";
- const log = {
- api: debug("d8d:zichan:api"),
- };
- // 创建资产归属区域管理路由
- export function createZichanAreaRoutes(withAuth: WithAuth) {
- const zichanAreaRoutes = new Hono<{ Variables: Variables }>();
- // 获取资产归属区域列表
- zichanAreaRoutes.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_area")
- .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 areas = await query.limit(limit).offset(offset);
- // 获取总数
- const count = await countQuery.count();
- return c.json({
- data: areas,
- pagination: {
- total: Number(count),
- current: page,
- pageSize: limit,
- totalPages: Math.ceil(Number(count) / limit),
- },
- });
- } catch (error) {
- log.api("获取资产归属区域列表失败:", error);
- return c.json({ error: "获取资产归属区域列表失败" }, 500);
- }
- });
- // 创建资产归属区域
- zichanAreaRoutes.post("/", withAuth, async (c) => {
- try {
- const apiClient = c.get('apiClient');
- const areaData = (await c.req.json()) as Partial<ZichanArea>;
- // 验证必填字段
- if (!areaData.name || !areaData.code) {
- return c.json({ error: "名称和编码不能为空" }, 400);
- }
- // 检查是否已存在相同编码的区域
- const existingArea = await apiClient.database
- .table("zichan_area")
- .where("code", areaData.code)
- .where("is_deleted", 0)
- .first();
- if (existingArea) {
- return c.json({ error: "相同编码的区域已存在" }, 400);
- }
- // 插入新区域
- const [id] = await apiClient.database.table("zichan_area").insert(areaData);
- return c.json({
- message: "资产归属区域创建成功",
- data: { id, ...areaData },
- });
- } catch (error) {
- log.api("创建资产归属区域失败:", error);
- return c.json({ error: "创建资产归属区域失败" }, 500);
- }
- });
- // 更新资产归属区域
- zichanAreaRoutes.put("/:id", withAuth, async (c) => {
- try {
- const apiClient = c.get('apiClient');
- const id = Number(c.req.param("id"));
- const areaData = (await c.req.json()) as Partial<ZichanArea>;
- // 验证必填字段
- if (!areaData.name || !areaData.code) {
- return c.json({ error: "名称和编码不能为空" }, 400);
- }
- // 检查是否已存在相同编码的区域
- const existingArea = await apiClient.database
- .table("zichan_area")
- .where("code", areaData.code)
- .where("is_deleted", 0)
- .where("id", "!=", id)
- .first();
- if (existingArea) {
- return c.json({ error: "相同编码的区域已存在" }, 400);
- }
- // 更新区域
- await apiClient.database
- .table("zichan_area")
- .where("id", id)
- .update({
- ...areaData,
- updated_at: apiClient.database.fn.now(),
- });
- return c.json({
- message: "资产归属区域更新成功",
- data: { id, ...areaData },
- });
- } catch (error) {
- log.api("更新资产归属区域失败:", error);
- return c.json({ error: "更新资产归属区域失败" }, 500);
- }
- });
- // 删除资产归属区域
- zichanAreaRoutes.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("area", id)
- .where("is_deleted", 0)
- .count();
- if (assetCount > 0) {
- return c.json({ error: "该区域下有关联的资产,无法删除" }, 400);
- }
- // 软删除区域
- await apiClient.database.table("zichan_area").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 zichanAreaRoutes;
- }
|