Comment.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. <?php
  2. namespace app\api\model\service;
  3. use think\Model;
  4. use think\Exception;
  5. class Comment extends Model
  6. {
  7. // 表名
  8. protected $name = 'service_comment';
  9. // 自动写入时间戳字段
  10. protected $autoWriteTimestamp = 'int';
  11. // 定义时间戳字段名
  12. protected $createTime = 'createtime';
  13. protected $updateTime = 'updatetime';
  14. /**
  15. * 查询评论列表
  16. * @param $get
  17. * @return bool|\PDOStatement|string|\think\Collection
  18. * @throws \think\db\exception\DataNotFoundException
  19. * @throws \think\db\exception\ModelNotFoundException
  20. * @throws \think\exception\DbException
  21. */
  22. public static function searchComment($get)
  23. {
  24. extract($get);
  25. $orderBy = 'id desc';
  26. if(isset($goods_id) && $goods_id != '')
  27. {
  28. $where['goods_id'] = $goods_id;
  29. }
  30. if(isset($shop_id) && $shop_id != '')
  31. {
  32. $where['shop_id'] = $shop_id;
  33. }
  34. if(isset($score) && $score != '')
  35. {
  36. $where['score'] = $score == 2?['in',[2,3,4]]:$score;
  37. }
  38. if(isset($images))
  39. {
  40. $where['is_img'] = 1;
  41. }
  42. if(isset($skill_id) && $skill_id != '')
  43. {
  44. $where['skill_id'] = $skill_id;
  45. }
  46. if(isset($user_id) && $user_id != '')
  47. {
  48. $where['user_id'] = $user_id;
  49. }
  50. $where['state'] = 1;
  51. $list = self::where($where)
  52. ->field("id,user_id,score,comment_label_ids,comment_label,content,images,createtime")
  53. ->order($orderBy)
  54. ->page($get['page'])
  55. ->limit(10)
  56. ->select();
  57. foreach ($list as &$value)
  58. {
  59. $value['user'] = User::where(['id'=>$value['user_id']])->field('id,avatar,nickname')->find();
  60. $value['images'] = $value['images']?explode(',',$value['images']):'';
  61. }
  62. return $list;
  63. }
  64. public static function commentAdd($params)
  65. {
  66. $order = Order::where(['id'=>$params['order_id'],'status'=>6])->field('id,user_id,skill_id,shop_id,goods_id')->find();
  67. if(!$order)
  68. {
  69. throw new Exception('当前订单无法训练');
  70. }
  71. $params['skill_id'] = $order['skill_id'];
  72. $params['shop_id'] = $order['shop_id'];
  73. $params['goods_id'] = $order['goods_id'];
  74. $params['comment_label'] = isset($params['comment_label_ids'])?CommentLabel::getLabel($params['comment_label_ids']):'';
  75. $params['is_img'] = $params['images']?1:0;
  76. $comment = new Comment($params);
  77. $comment->allowField(true)->save();
  78. Order::where(['id'=>$order['id']])->update(['status'=>7,'updatetime'=>time()]);
  79. return true;
  80. }
  81. /**
  82. * 统计评论
  83. * @param $param
  84. * @return array
  85. * @throws Exception
  86. */
  87. public static function totalComment($params)
  88. {
  89. extract($params);
  90. switch ($params['type'])
  91. {
  92. case 'goods':
  93. $where['goods_id'] = $goods_id;
  94. break;
  95. case 'shop':
  96. $where['shop_id'] = $shop_id;
  97. break;
  98. case 'skill':
  99. $where['skill_id'] = $skill_id;
  100. break;
  101. case 'user':
  102. $where['user_id'] = $user_id;
  103. break;
  104. }
  105. $where['state'] = 1;
  106. $data['totalNum'] = self::where($where)->count();
  107. $where['score'] = 5;
  108. $data['goodNum'] = self::where($where)->count();
  109. $data['goodPercent'] = $data['totalNum']>0?sprintf("%.2f",$data['goodNum']/$data['totalNum']):1.00;
  110. $where['score'] = ['in',[2,3,4]];
  111. $data['middleNum'] = self::where($where)->count();
  112. $where['score'] = 1;
  113. $data['badNum'] = self::where($where)->count();
  114. unset($where['score']);
  115. $where['is_img'] = 1;
  116. $data['imageNum'] = self::where($where)->count();
  117. return $data;
  118. }
  119. public static function skillGoodPercent($params)
  120. {
  121. $where['skill_id'] = $params['skill_id'];
  122. $where['state'] = 1;
  123. $data['totalNum'] = self::where($where)->count();
  124. $where['score'] = 5;
  125. $data['goodNum'] = self::where($where)->count();
  126. return $data['totalNum']>0?bcdiv($data['goodNum'],$data['totalNum'],2):1.00;;
  127. }
  128. public static function getCommentScore($params)
  129. {
  130. $params['state'] = 1;
  131. $total = self::where($params)->count();
  132. $score = $total>0?sprintf("%.2f",self::where($params)->avg('score')):5.0;
  133. return $score;
  134. }
  135. public static function getCount($where)
  136. {
  137. return self::where($where)->count('id');
  138. }
  139. }