index.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. // 添加账本
  2. import express from "express";
  3. import dayjs from "dayjs";
  4. const router = express.Router();
  5. import {
  6. record_insert,
  7. record_update,
  8. isType,
  9. type_insert,
  10. getRecordInfoById,
  11. addFileByRecordId,
  12. getFileByRecordId,
  13. getRecordsInfoByTime,
  14. getRecordsInfoByMonth,
  15. getTypesById,
  16. delFileByRecordId,
  17. delByRecordId,
  18. getMoreRecordsInfoByMonth,
  19. getMoreRecordsInfoByTime,
  20. } from "#db";
  21. import { shanghaiTime, shanghaiTimeFormat } from "#utils";
  22. import {
  23. getTypeInfoFn,
  24. setFilesById,
  25. setFilesByRecord,
  26. getFileUrl,
  27. } from "./utils.js";
  28. // middleware that is specific to this router
  29. router.use(function timeLog(req, res, next) {
  30. console.log("Time: ", Date.now());
  31. next();
  32. });
  33. // 添加单个账单记录
  34. router.post("/", async function (req, res) {
  35. const {
  36. book_id = "",
  37. total_fee = 0,
  38. type = "",
  39. time = "",
  40. remark = "",
  41. files = [],
  42. userInfo = {},
  43. } = req.body;
  44. const type_id = await getTypeInfoFn({ userInfo, book_id, type });
  45. const insertId = await record_insert({
  46. book_id,
  47. type_id: type_id,
  48. author_id: userInfo.user_id,
  49. total_fee,
  50. remark,
  51. time: shanghaiTimeFormat(time),
  52. create_time: shanghaiTime().format("YYYY-MM-DD HH:mm:ss"),
  53. update_time: shanghaiTime().format("YYYY-MM-DD HH:mm:ss"),
  54. });
  55. await setFilesByRecord({
  56. files,
  57. insertId,
  58. book_id,
  59. userInfo,
  60. });
  61. res.json({
  62. code: 200,
  63. data: {
  64. record_id: insertId,
  65. },
  66. });
  67. });
  68. // define the home page route
  69. router.get("/:record_id", async function (req, res) {
  70. const record_id = req.params.record_id; // 获取 fileId 参数
  71. const recordInfo = await getRecordInfoById(record_id);
  72. const files = await getFileByRecordId(record_id);
  73. const typesRes = await getTypesById({
  74. typeId: recordInfo.type_id,
  75. book_id: recordInfo.book_id,
  76. author_id: recordInfo.author_id,
  77. });
  78. if (typesRes) {
  79. recordInfo.type = typesRes.name;
  80. } else {
  81. recordInfo.type = "";
  82. }
  83. res.json({
  84. code: 200,
  85. data: {
  86. headers: req.headers,
  87. ...recordInfo,
  88. time: shanghaiTimeFormat(recordInfo.time, "YYYY-MM-DD"),
  89. create_time: shanghaiTimeFormat(recordInfo.create_time),
  90. update_time: shanghaiTimeFormat(recordInfo.update_time),
  91. files: files.map((elm) => getFileUrl(req, elm)),
  92. },
  93. });
  94. });
  95. // 更新账本数据
  96. router.put("/:record_id", async function (req, res) {
  97. const record_id = req.params.record_id; // 获取 fileId 参数
  98. const {
  99. book_id = "",
  100. total_fee = 0,
  101. type = "",
  102. time = "",
  103. remark = "",
  104. files = [],
  105. userInfo = {},
  106. } = req.body;
  107. // 更新附件信息
  108. await setFilesById({
  109. record_id,
  110. userInfo,
  111. book_id,
  112. files,
  113. });
  114. // 更新类型
  115. const typeId = await getTypeInfoFn({
  116. userInfo,
  117. book_id,
  118. type,
  119. });
  120. const recordInfo = await record_update({
  121. id: record_id, // 要更新的记录的唯一标识符
  122. type_id: typeId,
  123. author_id: userInfo.user_id,
  124. total_fee: total_fee,
  125. remark: remark,
  126. time: time,
  127. update_time: shanghaiTime().format("YYYY-MM-DD HH:mm:ss"),
  128. });
  129. res.json({
  130. code: 200,
  131. data: recordInfo ? "" : "更新失败",
  132. });
  133. });
  134. // define the about route
  135. router.get("/about", function (req, res) {
  136. res.send("About record");
  137. });
  138. // 根据日期获取数据
  139. router.get("/:book_id/:time", async function (req, res) {
  140. const time = req.params.time; // 获取 fileId 参数
  141. const book_id = req.params.book_id; // 获取 fileId 参数
  142. const recordsInfoRes = await getRecordsInfoByTime(time, book_id);
  143. const moreRecordsInfoRes = await getMoreRecordsInfoByTime(
  144. time,
  145. dayjs(time).date(),
  146. book_id
  147. );
  148. res.json({
  149. code: 200,
  150. data: recordsInfoRes
  151. .map((elm) => ({
  152. ...elm,
  153. time: shanghaiTimeFormat(elm.time, "YYYY-MM-DD"),
  154. create_time: shanghaiTimeFormat(elm.create_time),
  155. update_time: shanghaiTimeFormat(elm.update_time),
  156. }))
  157. .concat(
  158. moreRecordsInfoRes.map((elm) => {
  159. return {
  160. ...elm,
  161. time: shanghaiTimeFormat(`${time}-${elm.log_day}`, "YYYY-MM-DD"),
  162. create_time: shanghaiTimeFormat(elm.create_time),
  163. update_time: shanghaiTimeFormat(elm.update_time),
  164. };
  165. })
  166. )
  167. .sort(
  168. (a, b) => dayjs(b.update_time).unix() - dayjs(a.update_time).unix()
  169. ),
  170. });
  171. });
  172. // 根据日期获取数据
  173. router.get("/:book_id/m/:time", async function (req, res) {
  174. const time = req.params.time; // 获取 fileId 参数
  175. const book_id = req.params.book_id; // 获取 fileId 参数
  176. const recordsInfoRes = await getRecordsInfoByMonth(time, book_id);
  177. const moreRecordsInfoRes = await getMoreRecordsInfoByMonth(time, book_id);
  178. res.json({
  179. code: 200,
  180. data: recordsInfoRes
  181. .map((elm) => ({
  182. ...elm,
  183. time: shanghaiTimeFormat(elm.time, "YYYY-MM-DD"),
  184. create_time: shanghaiTimeFormat(elm.create_time),
  185. update_time: shanghaiTimeFormat(elm.update_time),
  186. }))
  187. .concat(
  188. moreRecordsInfoRes.map((elm) => {
  189. return {
  190. ...elm,
  191. time: shanghaiTimeFormat(`${time}-${elm.log_day}`, "YYYY-MM-DD"),
  192. create_time: shanghaiTimeFormat(elm.create_time),
  193. update_time: shanghaiTimeFormat(elm.update_time),
  194. };
  195. })
  196. )
  197. .sort((a, b) => dayjs(a.time).unix() - dayjs(b.time).unix()),
  198. });
  199. });
  200. // 删除指定账单
  201. router.delete("/:record_id", async function (req, res) {
  202. const record_id = req.params.record_id; // 获取 fileId 参数
  203. const { userInfo = {} } = req.body;
  204. const recordInfo = await getRecordInfoById(record_id);
  205. // 删除附件映射关系
  206. const getAllfiles = await getFileByRecordId(record_id);
  207. if (getAllfiles.length) {
  208. await Promise.all(
  209. getAllfiles.map((elm) => delFileByRecordId(record_id, elm.file_id))
  210. );
  211. }
  212. // 删除record数据
  213. await delByRecordId(record_id, userInfo.user_id);
  214. res.json({
  215. code: 200,
  216. data: "",
  217. });
  218. });
  219. export default router;