123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238 |
- // 添加账本
- import express from "express";
- import dayjs from "dayjs";
- const router = express.Router();
- import {
- record_insert,
- record_update,
- isType,
- type_insert,
- getRecordInfoById,
- addFileByRecordId,
- getFileByRecordId,
- getRecordsInfoByTime,
- getRecordsInfoByMonth,
- getTypesById,
- delFileByRecordId,
- delByRecordId,
- getMoreRecordsInfoByMonth,
- getMoreRecordsInfoByTime,
- } from "#db";
- import { shanghaiTime, shanghaiTimeFormat } from "#utils";
- import {
- getTypeInfoFn,
- setFilesById,
- setFilesByRecord,
- getFileUrl,
- } from "./utils.js";
- // 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 = "",
- time = "",
- remark = "",
- files = [],
- userInfo = {},
- } = req.body;
- const type_id = await getTypeInfoFn({ userInfo, book_id, type });
- const insertId = await record_insert({
- book_id,
- type_id: type_id,
- author_id: userInfo.user_id,
- total_fee,
- remark,
- time: shanghaiTimeFormat(time),
- create_time: shanghaiTime().format("YYYY-MM-DD HH:mm:ss"),
- update_time: shanghaiTime().format("YYYY-MM-DD HH:mm:ss"),
- });
- await setFilesByRecord({
- files,
- insertId,
- book_id,
- userInfo,
- });
- res.json({
- code: 200,
- data: {
- record_id: insertId,
- },
- });
- });
- // define the home page route
- router.get("/:record_id", async function (req, res) {
- const record_id = req.params.record_id; // 获取 fileId 参数
- const recordInfo = await getRecordInfoById(record_id);
- const files = await getFileByRecordId(record_id);
- const typesRes = await getTypesById({
- typeId: recordInfo.type_id,
- book_id: recordInfo.book_id,
- author_id: recordInfo.author_id,
- });
- if (typesRes) {
- recordInfo.type = typesRes.name;
- } else {
- recordInfo.type = "";
- }
- res.json({
- code: 200,
- data: {
- headers: req.headers,
- ...recordInfo,
- time: shanghaiTimeFormat(recordInfo.time, "YYYY-MM-DD"),
- create_time: shanghaiTimeFormat(recordInfo.create_time),
- update_time: shanghaiTimeFormat(recordInfo.update_time),
- files: files.map((elm) => getFileUrl(req, elm)),
- },
- });
- });
- // 更新账本数据
- router.put("/:record_id", async function (req, res) {
- const record_id = req.params.record_id; // 获取 fileId 参数
- const {
- book_id = "",
- total_fee = 0,
- type = "",
- time = "",
- remark = "",
- files = [],
- userInfo = {},
- } = req.body;
- // 更新附件信息
- await setFilesById({
- record_id,
- userInfo,
- book_id,
- files,
- });
- // 更新类型
- const typeId = await getTypeInfoFn({
- userInfo,
- book_id,
- type,
- });
- const recordInfo = await record_update({
- id: record_id, // 要更新的记录的唯一标识符
- type_id: typeId,
- author_id: userInfo.user_id,
- total_fee: total_fee,
- remark: remark,
- time: time,
- update_time: shanghaiTime().format("YYYY-MM-DD HH:mm:ss"),
- });
- res.json({
- code: 200,
- data: recordInfo ? "" : "更新失败",
- });
- });
- // define the about route
- router.get("/about", function (req, res) {
- res.send("About record");
- });
- // 根据日期获取数据
- router.get("/:book_id/:time", async function (req, res) {
- const time = req.params.time; // 获取 fileId 参数
- const book_id = req.params.book_id; // 获取 fileId 参数
- const recordsInfoRes = await getRecordsInfoByTime(time, book_id);
- const moreRecordsInfoRes = await getMoreRecordsInfoByTime(
- time,
- dayjs(time).date(),
- book_id
- );
- res.json({
- code: 200,
- data: recordsInfoRes
- .map((elm) => ({
- ...elm,
- time: shanghaiTimeFormat(elm.time, "YYYY-MM-DD"),
- create_time: shanghaiTimeFormat(elm.create_time),
- update_time: shanghaiTimeFormat(elm.update_time),
- }))
- .concat(
- moreRecordsInfoRes.map((elm) => {
- return {
- ...elm,
- time: shanghaiTimeFormat(`${time}-${elm.log_day}`, "YYYY-MM-DD"),
- create_time: shanghaiTimeFormat(elm.create_time),
- update_time: shanghaiTimeFormat(elm.update_time),
- };
- })
- )
- .sort(
- (a, b) => dayjs(b.update_time).unix() - dayjs(a.update_time).unix()
- ),
- });
- });
- // 根据日期获取数据
- router.get("/:book_id/m/:time", async function (req, res) {
- const time = req.params.time; // 获取 fileId 参数
- const book_id = req.params.book_id; // 获取 fileId 参数
- const recordsInfoRes = await getRecordsInfoByMonth(time, book_id);
- const moreRecordsInfoRes = await getMoreRecordsInfoByMonth(time, book_id);
- res.json({
- code: 200,
- data: recordsInfoRes
- .map((elm) => ({
- ...elm,
- time: shanghaiTimeFormat(elm.time, "YYYY-MM-DD"),
- create_time: shanghaiTimeFormat(elm.create_time),
- update_time: shanghaiTimeFormat(elm.update_time),
- }))
- .concat(
- moreRecordsInfoRes.map((elm) => {
- return {
- ...elm,
- time: shanghaiTimeFormat(`${time}-${elm.log_day}`, "YYYY-MM-DD"),
- create_time: shanghaiTimeFormat(elm.create_time),
- update_time: shanghaiTimeFormat(elm.update_time),
- };
- })
- )
- .sort((a, b) => dayjs(a.time).unix() - dayjs(b.time).unix()),
- });
- });
- // 删除指定账单
- router.delete("/:record_id", async function (req, res) {
- const record_id = req.params.record_id; // 获取 fileId 参数
- const { userInfo = {} } = req.body;
- const recordInfo = await getRecordInfoById(record_id);
- // 删除附件映射关系
- const getAllfiles = await getFileByRecordId(record_id);
- if (getAllfiles.length) {
- await Promise.all(
- getAllfiles.map((elm) => delFileByRecordId(record_id, elm.file_id))
- );
- }
- // 删除record数据
- await delByRecordId(record_id, userInfo.user_id);
- res.json({
- code: 200,
- data: "",
- });
- });
- export default router;
|