utils.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. import {
  2. record_insert,
  3. record_update,
  4. isType,
  5. type_insert,
  6. getRecordInfoById,
  7. addFileByRecordId,
  8. getFileByRecordId,
  9. getRecordsInfoByTime,
  10. getRecordsInfoByMonth,
  11. getTypesById,
  12. delFileByRecordId,
  13. delByRecordId,
  14. } from "#db";
  15. import { shanghaiTime, shanghaiTimeFormat } from "#utils";
  16. export async function getTypeInfoFn({ userInfo, book_id, type }) {
  17. if (!type) {
  18. return "";
  19. }
  20. // type 是否存在重复项,有就返回id,没有就新增 再返回id
  21. const isAddType = await isType({
  22. book_id,
  23. author_id: userInfo.user_id,
  24. type,
  25. });
  26. let typeInfo = {};
  27. if (!isAddType) {
  28. await type_insert({
  29. book_id,
  30. author_id: userInfo.user_id,
  31. type,
  32. create_time: shanghaiTime().format("YYYY-MM-DD HH:mm:ss"),
  33. update_time: shanghaiTime().format("YYYY-MM-DD HH:mm:ss"),
  34. });
  35. typeInfo = await isType({
  36. book_id,
  37. author_id: userInfo.user_id,
  38. type,
  39. });
  40. } else {
  41. typeInfo = { ...isAddType };
  42. }
  43. return Promise.resolve(typeInfo.id);
  44. }
  45. export async function setFilesByRecord(params) {
  46. const {files = [], more_id, userInfo, book_id} = params
  47. if(!files.length) return
  48. await Promise.all(
  49. files.map((file_id) =>
  50. addFileByRecordId({
  51. file_id,
  52. record_id: more_id,
  53. book_id,
  54. author_id: userInfo.user_id,
  55. create_time: shanghaiTime().format("YYYY-MM-DD HH:mm:ss"),
  56. update_time: shanghaiTime().format("YYYY-MM-DD HH:mm:ss"),
  57. })
  58. )
  59. );
  60. }