| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136 |
- import { model } from '../_utils/model';
- import { DATA_MODEL_KEY } from '../../config/model';
- import { cloudbaseTemplateConfig } from '../../config/index';
- import { COMMENTS } from '../cloudbaseMock/index';
- const COMMENT_MODEL_KEY = DATA_MODEL_KEY.COMMENT;
- export async function getGoodsDetailCommentInfo(spuId) {
- if (cloudbaseTemplateConfig.useMock) {
- const all = COMMENTS.filter((x) => x.spu._id === spuId);
- const good = all.filter((x) => x.rating > 3);
- const firstAndTotal = (x) => ({
- data: {
- records: x.length === 0 ? [] : [x[0]],
- total: x.length,
- },
- });
- return Promise.resolve([firstAndTotal(all), firstAndTotal(good)]);
- }
- const firstAndCount = () =>
- model()[COMMENT_MODEL_KEY].list({
- filter: {
- relateWhere: {
- spu: {
- where: {
- _id: {
- $eq: spuId,
- },
- },
- },
- },
- },
- select: {
- $master: true,
- spu: {
- _id: true,
- },
- },
- orderBy: [{ rating: 'desc' }],
- pageNumber: 1,
- pageSize: 1,
- getCount: true,
- });
- const goodCount = () =>
- model()[COMMENT_MODEL_KEY].list({
- filter: {
- relateWhere: {
- spu: {
- where: {
- _id: {
- $eq: spuId,
- },
- },
- },
- },
- where: {
- rating: {
- $gt: 3,
- },
- },
- },
- select: {
- $master: true,
- },
- pageNumber: 1,
- pageSize: 1,
- getCount: true,
- });
- return await Promise.all([firstAndCount(), goodCount()]);
- }
- export async function getCommentsOfSpu({ spuId, pageNumber, pageSize }) {
- if (cloudbaseTemplateConfig.useMock) {
- const all = COMMENTS.filter((x) => x.spu._id === spuId);
- const startIndex = (pageNumber - 1) * pageSize;
- const endIndex = startIndex + pageSize;
- const records = all.slice(startIndex, endIndex);
- return {
- records,
- total: all.length,
- };
- }
- return (
- await model()[COMMENT_MODEL_KEY].list({
- select: {
- $master: true,
- order_item: {
- _id: true,
- },
- spu: {
- _id: true,
- },
- },
- filter: {
- relateWhere: {
- spu: {
- where: {
- _id: {
- $eq: spuId,
- },
- },
- },
- },
- },
- pageSize,
- pageNumber,
- getCount: true,
- })
- ).data;
- }
- /**
- *
- * @param {{
- * orderItemId: string,
- * content: string,
- * rating: number,
- * spuId: string
- * }} param0
- */
- export function createComment({ orderItemId, content, rating, spuId }) {
- return model()[COMMENT_MODEL_KEY].create({
- data: {
- content,
- rating,
- order_item: {
- _id: orderItemId,
- },
- spu: {
- _id: spuId,
- },
- },
- });
- }
|