rating.js 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. 'use strict';
  2. import mongoose from 'mongoose'
  3. import {ratingList, scores, tags} from '../../InitData/rate'
  4. const Schema = mongoose.Schema;
  5. const rateSchema = new Schema({
  6. restaurant_id: Number,
  7. ratings: [
  8. {
  9. avatar: {type: String, default: ''},
  10. highlights: [],
  11. item_ratings: [
  12. {
  13. food_id: Number,
  14. food_name: String,
  15. image_hash: {type: String, default: ''},
  16. is_valid: {type: Number, default: 1},
  17. },
  18. ],
  19. rated_at: String,
  20. rating_star: Number,
  21. rating_text: String,
  22. tags: {type: Array, default: []},
  23. time_spent_desc: String,
  24. username: {type: String, default: "匿名用户"},
  25. },
  26. ],
  27. scores: {
  28. compare_rating: {type: Number, default: 0},
  29. deliver_time: {type: Number, default: 0},
  30. food_score: {type: Number, default: 0},
  31. order_rating_amount: {type: Number, default: 0},
  32. overall_score: {type: Number, default: 0},
  33. service_score: {type: Number, default: 0},
  34. },
  35. tags: [{
  36. count: {type: Number, default: 0},
  37. name: String,
  38. unsatisfied: {type: Boolean, default: false},
  39. }]
  40. })
  41. rateSchema.index({restaurant_id: 1});
  42. rateSchema.statics.initData = async function (restaurant_id){
  43. try{
  44. const data = await this.findOne({restaurant_id});
  45. if (!data) {
  46. const newRating = {
  47. restaurant_id,
  48. ratings: ratingList,
  49. scores,
  50. tags,
  51. }
  52. await this.create(newRating);
  53. return true
  54. }else{
  55. return false
  56. }
  57. }catch(err){
  58. console.log('初始化评论数据失败');
  59. throw new Error(err)
  60. }
  61. }
  62. rateSchema.statics.getData = async function (restaurant_id, type){
  63. try{
  64. const data = await this.findOne({restaurant_id}, '-_id');
  65. if (!data) {
  66. throw new Error('未找到当前餐馆的评论数据');
  67. }else{
  68. return data[type]
  69. }
  70. }catch(err){
  71. console.log('初始化评论数据失败');
  72. throw new Error(err)
  73. }
  74. }
  75. const Rating = mongoose.model('Rating', rateSchema);
  76. export default Rating