comments.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. import { model } from '../_utils/model';
  2. import { DATA_MODEL_KEY } from '../../config/model';
  3. import { cloudbaseTemplateConfig } from '../../config/index';
  4. import { COMMENTS } from '../cloudbaseMock/index';
  5. const COMMENT_MODEL_KEY = DATA_MODEL_KEY.COMMENT;
  6. export async function getGoodsDetailCommentInfo(spuId) {
  7. if (cloudbaseTemplateConfig.useMock) {
  8. const all = COMMENTS.filter((x) => x.spu._id === spuId);
  9. const good = all.filter((x) => x.rating > 3);
  10. const firstAndTotal = (x) => ({
  11. data: {
  12. records: x.length === 0 ? [] : [x[0]],
  13. total: x.length,
  14. },
  15. });
  16. return Promise.resolve([firstAndTotal(all), firstAndTotal(good)]);
  17. }
  18. const firstAndCount = () =>
  19. model()[COMMENT_MODEL_KEY].list({
  20. filter: {
  21. relateWhere: {
  22. spu: {
  23. where: {
  24. _id: {
  25. $eq: spuId,
  26. },
  27. },
  28. },
  29. },
  30. },
  31. select: {
  32. $master: true,
  33. spu: {
  34. _id: true,
  35. },
  36. },
  37. orderBy: [{ rating: 'desc' }],
  38. pageNumber: 1,
  39. pageSize: 1,
  40. getCount: true,
  41. });
  42. const goodCount = () =>
  43. model()[COMMENT_MODEL_KEY].list({
  44. filter: {
  45. relateWhere: {
  46. spu: {
  47. where: {
  48. _id: {
  49. $eq: spuId,
  50. },
  51. },
  52. },
  53. },
  54. where: {
  55. rating: {
  56. $gt: 3,
  57. },
  58. },
  59. },
  60. select: {
  61. $master: true,
  62. },
  63. pageNumber: 1,
  64. pageSize: 1,
  65. getCount: true,
  66. });
  67. return await Promise.all([firstAndCount(), goodCount()]);
  68. }
  69. export async function getCommentsOfSpu({ spuId, pageNumber, pageSize }) {
  70. if (cloudbaseTemplateConfig.useMock) {
  71. const all = COMMENTS.filter((x) => x.spu._id === spuId);
  72. const startIndex = (pageNumber - 1) * pageSize;
  73. const endIndex = startIndex + pageSize;
  74. const records = all.slice(startIndex, endIndex);
  75. return {
  76. records,
  77. total: all.length,
  78. };
  79. }
  80. return (
  81. await model()[COMMENT_MODEL_KEY].list({
  82. select: {
  83. $master: true,
  84. order_item: {
  85. _id: true,
  86. },
  87. spu: {
  88. _id: true,
  89. },
  90. },
  91. filter: {
  92. relateWhere: {
  93. spu: {
  94. where: {
  95. _id: {
  96. $eq: spuId,
  97. },
  98. },
  99. },
  100. },
  101. },
  102. pageSize,
  103. pageNumber,
  104. getCount: true,
  105. })
  106. ).data;
  107. }
  108. /**
  109. *
  110. * @param {{
  111. * orderItemId: string,
  112. * content: string,
  113. * rating: number,
  114. * spuId: string
  115. * }} param0
  116. */
  117. export function createComment({ orderItemId, content, rating, spuId }) {
  118. return model()[COMMENT_MODEL_KEY].create({
  119. data: {
  120. content,
  121. rating,
  122. order_item: {
  123. _id: orderItemId,
  124. },
  125. spu: {
  126. _id: spuId,
  127. },
  128. },
  129. });
  130. }