Browse Source

周期账单修改、删除、添加

John 8 months ago
parent
commit
f724ac6bf6

+ 4 - 1
node_expores/db/more_record.js

@@ -41,6 +41,7 @@ export async function addMoreRecord({
 }
 
 export async function updataMoreRecord(params) {
+  console.log(44, params)
   const {
     more_id = "",
     type_id = "",
@@ -54,7 +55,7 @@ export async function updataMoreRecord(params) {
   return new Promise(async (resolve, reject) => {
     try {
       const sql = `
-        UPDATE more_record SET type_id = ?, name = ?, total_fee = ?, remark = ?, update_time = ?, start_time= ?, end_time ?, WHERE more_id = ?
+        UPDATE more_record SET type_id = ?, name = ?, total_fee = ?, remark = ?, update_time = ?, start_time= ?, end_time = ? WHERE more_id = ?;
       `;
       const values = [
         type_id,
@@ -68,6 +69,7 @@ export async function updataMoreRecord(params) {
       ];
       // 执行更新语句
       connection.execute(sql, values, (err, result) => {
+        console.log(72727272, err, result)
         if (err) {
           // 错误处理
           return resolve(false);
@@ -81,6 +83,7 @@ export async function updataMoreRecord(params) {
 }
 
 export function delMoreRecord(more_id, author_id) {
+  console.log(8686868, more_id, author_id);
   return new Promise((resolve, reject) => {
     try {
       const sql = `DELETE FROM more_record WHERE more_id = ? AND author_id = ?`;

+ 13 - 36
node_expores/router/record/index.js

@@ -17,7 +17,7 @@ import {
 } from "#db";
 import { shanghaiTime, shanghaiTimeFormat } from "#utils";
 import dayjs from "dayjs";
-import {getTypeInfoFn, setFilesByRecord} from './utils.js'
+import { getTypeInfoFn,setFilesById,setFilesByRecord } from './utils.js'
 
 // middleware that is specific to this router
 router.use(function timeLog(req, res, next) {
@@ -118,40 +118,18 @@ router.put("/:record_id", async function (req, res) {
     userInfo = {},
   } = req.body;
 
-  const getAllfiles = await getFileByRecordId(record_id);
-  const getAllfilesIds = getAllfiles.map((elm) => elm.file_id);
-
-  const dellFilesInRecordFiles = getAllfiles.filter(
-    (elm) => files.indexOf(elm.file_id) < 0
-  );
-  const needAddFiles = files.filter(
-    (file_id) => getAllfilesIds.indexOf(file_id) < 0
-  );
-
-  if (dellFilesInRecordFiles.length) {
-    await Promise.all(
-      dellFilesInRecordFiles.map((elm) =>
-        delFileByRecordId(record_id, elm.file_id)
-      )
-    );
-  }
-  if (needAddFiles.length) {
-    // 更新附件信息
-    await Promise.all(
-      needAddFiles.map((file_id) =>
-        addFileByRecordId({
-          file_id,
-          record_id,
-          book_id,
-          author_id: userInfo.user_id,
-          create_time: shanghaiTime().format("YYYY-MM-DD HH:mm:ss"),
-          update_time: shanghaiTime().format("YYYY-MM-DD HH:mm:ss"),
-        })
-      )
-    );
-  }
+  
+  // 更新附件信息
+  await setFilesById({
+    record_id,
+    userInfo,
+    book_id, 
+    files
+  })
 
-  const typeId = await getTypeInfo({
+  
+  // 更新类型
+  const typeId = await getTypeInfoFn({
     userInfo,
     book_id,
     type,
@@ -211,11 +189,10 @@ router.get("/:book_id/m/:time", async function (req, res) {
   });
 });
 
-// 根据日期获取数据
+// 删除指定账单
 router.delete("/:record_id", async function (req, res) {
   const record_id = req.params.record_id; // 获取 fileId 参数
   const { userInfo = {} } = req.body;
-  console.log(251, userInfo, record_id);
   const recordInfo = await getRecordInfoById(record_id);
 
   // 删除附件映射关系

+ 69 - 20
node_expores/router/record/more.js

@@ -1,12 +1,13 @@
 import express from "express";
+
 const router = express.Router();
 import { v4 as uuidv4 } from "uuid";
-import { shanghaiTime, shanghaiTimeFormat } from "#utils";
-import { addMoreRecord, updataMoreRecord, delMoreRecord } from "#db";
-import { getTypeInfoFn, setFilesByRecord } from "./utils.js";
+import { shanghaiTime,shanghaiTimeFormat } from "#utils";
+import { addMoreRecord,updataMoreRecord,delMoreRecord,record_update,getRecordInfoById,getFileByRecordId,delFileByRecordId,delByRecordId } from "#db";
+import { getTypeInfoFn,setFilesById,setFilesByRecord } from "./utils.js";
 
 // 添加多个账单记录
-router.post("/", async function (req, res) {
+router.post("/",async function ( req,res ) {
   const {
     name = "",
     remark = "",
@@ -19,9 +20,9 @@ router.post("/", async function (req, res) {
     userInfo = {},
   } = req.body;
 
-  const more_id = `M_${uuidv4()}`;
+  const more_id = `M_${ uuidv4() }`;
 
-  const type_id = await getTypeInfoFn({ userInfo, book_id, type });
+  const type_id = await getTypeInfoFn({userInfo,book_id,type});
 
   await addMoreRecord({
     more_id,
@@ -53,31 +54,79 @@ router.post("/", async function (req, res) {
     },
   });
 });
-
+  
 // 编辑多个账单记录
-router.put("/", async function (req, res) {
+router.put("/:more_id",async function ( req,res ) {
+  const more_id = req.params.more_id; // 获取 fileId 参数
   const {
+    name = "",
+    remark = "",
     book_id = "",
+    start_time = "",
+    end_time = "",
     total_fee = 0,
     type = "",
-    time = "",
-    remark = "",
     files = [],
     userInfo = {},
   } = req.body;
+
+  // 更新附件信息
+  await setFilesById({
+    record_id: more_id,
+    userInfo,
+    book_id,
+    files
+  })
+
+  // 更新类型
+  const typeId = await getTypeInfoFn({
+    userInfo,
+    book_id,
+    type,
+  });
+   
+  
+  // 更新数据内容
+  const recordInfo = await updataMoreRecord({
+    name,
+    more_id,
+    type_id: typeId,
+    author_id: userInfo.user_id,
+    total_fee: total_fee,
+    remark: remark,
+    start_time: start_time,
+    end_time: end_time,
+    update_time: shanghaiTime().format("YYYY-MM-DD HH:mm:ss"),
+  });
+  
+  res.json({
+    code: 200,
+    data: recordInfo ? "" : "更新失败",
+  });
 });
 
 // 删除单个账单记录
-router.delete("/", async function (req, res) {
-  const {
-    book_id = "",
-    total_fee = 0,
-    type = "",
-    time = "",
-    remark = "",
-    files = [],
-    userInfo = {},
-  } = req.body;
+router.delete("/:more_id",async function ( req,res ) {
+  const more_id = req.params.more_id; // 获取 fileId 参数
+  const { userInfo = {} } = req.body;
+  // const recordInfo = await getRecordInfoById(more_id);
+
+  // 删除附件映射关系
+  const getAllFiles = await getFileByRecordId(more_id);
+  if (getAllFiles.length) {
+    await Promise.all(
+      getAllFiles.map((elm) =>
+        delFileByRecordId(more_id, elm.file_id)
+      )
+    );
+  }
+  // 删除record数据
+  await delMoreRecord(more_id, userInfo.user_id);
+
+  res.json({
+    code: 200,
+    data: '',
+  });
 });
 
 export default router;

+ 38 - 0
node_expores/router/record/utils.js

@@ -16,6 +16,7 @@ import {
 import { shanghaiTime, shanghaiTimeFormat } from "#utils";
 
 export async function getTypeInfoFn({ userInfo, book_id, type }) {
+  console.log(1919191919, userInfo, book_id, type )
   if (!type) {
     return "";
   }
@@ -63,4 +64,41 @@ export async function setFilesByRecord(params) {
       })
     )
   );
+}
+
+
+export async function setFilesById(params) {
+  const {record_id, userInfo, book_id, files = []} = params
+  const getAllFiles = await getFileByRecordId(record_id);
+  const getAllFilesIds = getAllFiles.map((elm) => elm.file_id);
+
+  const dellFilesInRecordFiles = getAllFiles.filter(
+    (elm) => files.indexOf(elm.file_id) < 0
+  );
+  const needAddFiles = files.filter(
+    (file_id) => getAllFilesIds.indexOf(file_id) < 0
+  );
+
+  if (dellFilesInRecordFiles.length) {
+    await Promise.all(
+      dellFilesInRecordFiles.map((elm) =>
+        delFileByRecordId(record_id, elm.file_id)
+      )
+    );
+  }
+  if (needAddFiles.length) {
+    // 更新附件信息
+    await Promise.all(
+      needAddFiles.map((file_id) =>
+        addFileByRecordId({
+          file_id,
+          record_id,
+          book_id,
+          author_id: userInfo.user_id,
+          create_time: shanghaiTime().format("YYYY-MM-DD HH:mm:ss"),
+          update_time: shanghaiTime().format("YYYY-MM-DD HH:mm:ss"),
+        })
+      )
+    );
+  }
 }