Ver Fonte

账单管理

john há 8 meses atrás
pai
commit
a18364e969

+ 2 - 0
node_expores/db/index.js

@@ -1,2 +1,4 @@
 export * from './files.js';
 export * from './auth.js';
+export * from './record.js';
+export * from './type.js';

+ 111 - 0
node_expores/db/record.js

@@ -0,0 +1,111 @@
+import connection from './base.js';
+
+
+export async function record_insert({
+  book_id = '',
+  type_id = '',
+  author_id = '',
+  total_fee = '',
+  remark = '',
+  create_time = "",
+  update_time = "",
+}) {
+  return new Promise(async (resolve, reject) => {
+    try {
+      const sql = `
+        INSERT INTO record (book_id, type_id, author_id, total_fee, remark, create_time, update_time)
+        VALUES (?, ?, ?, ?, ?, ?, ?)
+      `;
+      const values = [book_id, type_id, author_id, total_fee, remark, create_time, update_time];
+      // 直接接收 execute 返回的内容
+      connection.execute(sql, values, (result, fields) => {
+        return resolve(fields.insertId);
+      });
+
+    } catch (err) {
+      // console.error("Error inserting data:", err);
+      // throw err;
+      return resolve(false);
+    }
+  })
+}
+
+// 关联 账单 跟 附件的数据
+/*
+`file_id` varchar(300) NOT NULL,
+`record_id` varchar(300) NOT NULL,
+`book_id` varchar(300) NOT NULL,
+`user_id` varchar(300) NOT NULL,
+*/
+export function record_and_files({
+  file_id = "",
+  record_id = "",
+  book_id = "",
+  user_id = "",
+  create_time = "",
+  update_time = "",
+}) {
+  return new Promise(async (resolve, reject) => {
+    try {
+      const sql = `
+        INSERT INTO record_files (file_id, record_id, book_id, user_id, create_time, update_time)
+        VALUES (?, ?, ?, ?, ?, ?)
+      `;
+      const values = [file_id, record_id, book_id, user_id, create_time, update_time];
+      // 直接接收 execute 返回的内容
+      const result = await connection.execute(sql, values);
+      // console.log("Record inserted successfully:", result);
+      return resolve(result);
+    } catch (err) {
+      // console.error("Error inserting data:", err);
+      // throw err;
+      return resolve(false);
+    }
+  })
+}
+
+export function recordRelation({
+  name = "",
+  record_id = "",
+  book_id = "",
+  user_id = "",
+  create_time = "",
+  update_time = "",
+}) {
+  return new Promise(async (resolve, reject) => {
+    try {
+      const sql = `
+        INSERT INTO record_files (file_id, record_id, book_id, user_id, create_time, update_time)
+        VALUES (?, ?, ?, ?, ?, ?)
+      `;
+      const values = [file_id, record_id, book_id, user_id, create_time, update_time];
+      // 直接接收 execute 返回的内容
+      const result = await connection.execute(sql, values);
+      // console.log("Record inserted successfully:", result);
+      return resolve(result);
+    } catch (err) {
+      // console.error("Error inserting data:", err);
+      // throw err;
+      return resolve(false);
+    }
+  })
+}
+
+
+// 获取用户详情
+export function getRecordInfoById(record_id) {
+  return new Promise((resolve, reject) => {
+    connection.query(
+      `SELECT * FROM record WHERE id = ?`,
+      [record_id],
+      (err, rows) => {
+        if (err) {
+          // reject(err);
+          resolve(false); // 如果存在记录,则返回 true,否则返回 false
+        } else {
+          resolve(rows.length > 0 ? rows[0] : false); // 如果存在记录,则返回 true,否则返回 false
+        }
+      },
+    );
+  });
+}

+ 46 - 0
node_expores/db/type.js

@@ -0,0 +1,46 @@
+import connection from "./base.js";
+
+// 用户是否已经注册
+export function isType({ book_id = "", user_id = "", type = "" }) {
+  return new Promise((resolve, reject) => {
+    connection.query(
+      `SELECT * FROM types WHERE book_id = ? AND user_id = ? AND name = ?`,
+      [book_id, user_id, type],
+      (err, rows) => {
+        if (err) {
+          // reject(err);
+          resolve(false); // 如果存在记录,则返回 true,否则返回 false
+        } else {
+          resolve(rows.length > 0 ? rows[0] : false); // 如果存在记录,则返回 true,否则返回 false
+        }
+      },
+    );
+  });
+}
+
+export async function type_insert({
+  book_id = "",
+  user_id = "",
+  type = "",
+  create_time = "",
+  update_time = "",
+}) {
+  return new Promise(async (resolve, reject) => {
+    try {
+      const sql = `
+        INSERT INTO types (name, book_id, user_id, create_time, update_time)
+        VALUES (?, ?, ?, ?, ?)
+      `;
+      const values = [
+        type, book_id, user_id, create_time, update_time
+      ];
+      // 直接接收 execute 返回的内容
+      const result = await connection.execute(sql, values);
+      return resolve(result);
+    } catch (err) {
+      console.error("Error inserting data:", err);
+      // throw err;
+      return resolve(false);
+    }
+  });
+}

+ 17 - 0
node_expores/db/update.sql

@@ -6,3 +6,20 @@ ALTER TABLE cashbook.authors ADD user_id varchar(300) NOT NULL;
 ALTER TABLE cashbook.authors ADD CONSTRAINT authors_unique_2 UNIQUE KEY (user_id);
 ALTER TABLE cashbook.authors ADD create_time TIMESTAMP NOT NULL;
 ALTER TABLE cashbook.authors ADD update_time TIMESTAMP NOT NULL;
+
+-- cashbook.record_files definition
+CREATE TABLE `record_files` (
+  `id` int NOT NULL AUTO_INCREMENT,
+  `file_id` varchar(300) NOT NULL,
+  `record_id` varchar(300) NOT NULL,
+  `book_id` varchar(300) NOT NULL,
+  `user_id` varchar(300) NOT NULL,
+  `create_time` timestamp NOT NULL,
+  `update_time` timestamp NOT NULL,
+  PRIMARY KEY (`id`)
+) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
+
+ALTER TABLE cashbook.types ADD user_id varchar(300) NOT NULL;
+
+ALTER TABLE cashbook.types MODIFY COLUMN record_id int NULL;
+ALTER TABLE cashbook.record MODIFY COLUMN author_id VARCHAR(300) NOT NULL;

+ 1 - 1
node_expores/router/authors/index.js

@@ -50,7 +50,7 @@ router.post("/", async function (req, res) {
     email: account,
     mobile: account,
   });
-  console.log(48, islogin);
+
   if (!islogin) {
     res.status(404).json({
       code: 404,

+ 6 - 6
node_expores/router/authors/login.js

@@ -16,12 +16,12 @@ router.delete("/", function (req, res) {
 // 获取用户详情
 router.post("/user_info", function (req, res) {
   let userInfo = {
-    name: req.body.decoded.name,
-    user_id: req.body.decoded.user_id,
-    create_time: req.body.decoded.create_time,
-    update_time: req.body.decoded.update_time,
-    mobile: req.body.decoded.mobile,
-    email: req.body.decoded.email
+    name: req.body.userInfo.name,
+    user_id: req.body.userInfo.user_id,
+    create_time: req.body.userInfo.create_time,
+    update_time: req.body.userInfo.update_time,
+    mobile: req.body.userInfo.mobile,
+    email: req.body.userInfo.email
   }
   res.json({
     code: 200,

+ 78 - 3
node_expores/router/record/index.js

@@ -1,17 +1,92 @@
 // 添加账本
 import express from "express";
-
 const router = express.Router();
+import {
+  record_insert,
+  record_and_files,
+  recordRelation,
+  isType,
+  type_insert,
+  getRecordInfoById
+} from "#db";
+import dayjs from "dayjs";
 
 // middleware that is specific to this router
 router.use(function timeLog(req, res, next) {
   console.log("Time: ", Date.now());
   next();
 });
+
+// 添加单个账单记录
+router.post("/", async function (req, res) {
+  const {
+    book_id = "",
+    total_fee = 0,
+    type = "",
+    remark = "",
+    files = [],
+    userInfo = {},
+  } = req.body;
+  // type 是否存在重复项,有就返回id,没有就新增 再返回id
+  const isAddType = await isType({
+    book_id,
+    user_id: userInfo.user_id,
+    type,
+  });
+  let typeInfo = {};
+  if (!isAddType) {
+    await type_insert({
+      book_id,
+      user_id: userInfo.user_id,
+      type,
+      create_time: dayjs().format("YYYY-MM-DD HH:mm:ss"),
+      update_time: dayjs().format("YYYY-MM-DD HH:mm:ss"),
+    });
+    typeInfo = await isType({
+      book_id,
+      user_id: userInfo.user_id,
+      type,
+    });
+  } else {
+    typeInfo = { ...isAddType };
+  }
+  console.log(51, {
+      book_id,
+      type_id: typeInfo.id,
+      author_id: userInfo.user_id,
+      total_fee,
+      remark,
+      create_time: dayjs().format("YYYY-MM-DD HH:mm:ss"),
+      update_time: dayjs().format("YYYY-MM-DD HH:mm:ss"),
+  })
+  const insertId = await record_insert({
+    book_id,
+    type_id: typeInfo.id,
+    author_id: userInfo.user_id,
+    total_fee,
+    remark,
+    create_time: dayjs().format("YYYY-MM-DD HH:mm:ss"),
+    update_time: dayjs().format("YYYY-MM-DD HH:mm:ss"),
+  });
+
+  res.json({
+    code: 200,
+    data: {
+      record_id: insertId
+    }
+  });
+});
+
 // define the home page route
-router.get("/", function (req, res) {
-  res.send("Record home page");
+router.get("/:record_id", async function (req, res) {
+  const record_id = req.params.record_id; // 获取 fileId 参数
+  const recordInfo = await getRecordInfoById(record_id)
+  res.json({
+    code: 200,
+    data: recordInfo
+  });
 });
+
 // define the about route
 router.get("/about", function (req, res) {
   res.send("About record");

+ 1 - 1
node_expores/utils/authorization.js

@@ -25,7 +25,7 @@ export function verifyToken(req, res, next) {
       console.log("verify error", err);
       return res.json({ code: "404", msg: "token无效" });
     }
-    req.body.decoded = decoded
+    req.body.userInfo = decoded
     next();
   });
 }