CommentService.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. <?php
  2. namespace addons\ReservationService\common\services;
  3. use addons\ReservationService\common\models\ReservationServiceUser;
  4. use common\enums\OrderCommentEnum;
  5. use common\enums\StatusEnum;
  6. use common\models\order\OrderComments;
  7. use common\models\order\OrderReserveComments;
  8. use yii\helpers\Json;
  9. class CommentService
  10. {
  11. /**
  12. * 评价列表
  13. * @param int $mall_id
  14. * @param int $user_id
  15. * @param array $params
  16. * @return array|false|mixed|string|null
  17. */
  18. public function page(int $mall_id, int $user_id, array $params)
  19. {
  20. $where = [
  21. ['reserve_type' => StatusEnum::ENABLED]
  22. ];
  23. if (!empty($user_id)) $where[] = ['user_id' => $user_id];
  24. if (isset($params['store_id'])) {
  25. $where[] = ['store_id' => $params['store_id']];
  26. }
  27. if (!empty($params['tech_id'])) $where[] = ['tech_id' => $params['tech_id']];
  28. if (!empty($params['tech_user_id'])) $where[] = ['tech_user_id' => $params['tech_user_id']];
  29. $total = OrderReserveComments::find()->where(array_merge(['and'], $where))->count();
  30. $data['comment_count'][] = ['name' => OrderCommentEnum::OC_ALL, 'count' => $total ?? 0];
  31. $good_num = OrderReserveComments::find()->where(array_merge(['and'], $where))->andWhere(['in', 'score', OrderCommentEnum::OC_PRAISE_SCORES])->count();
  32. $data['comment_count'][] = ['name' => OrderCommentEnum::OC_PRAISE, 'count' => $good_num ?? 0];
  33. $count = OrderReserveComments::find()->where(array_merge(['and'], $where))->andWhere(['in', 'score', OrderCommentEnum::OC_MIDDLE_SCORES])->count();
  34. $data['comment_count'][] = ['name' => OrderCommentEnum::OC_MIDDLE, 'count' => $count ?? 0];
  35. $count = OrderReserveComments::find()->where(array_merge(['and'], $where))->andWhere(['in', 'score', OrderCommentEnum::OC_NEGATIVE_SCORES])->count();
  36. $data['comment_count'][] = ['name' => OrderCommentEnum::OC_NEGATIVE, 'count' => $count ?? 0];
  37. if (!empty($params['type'])) {
  38. switch ($params['type']) {
  39. case OrderCommentEnum::TYPE1: //差评
  40. $where[] = ['in', 'score', [1,2]];
  41. break;
  42. case OrderCommentEnum::TYPE2: //中评
  43. $where[] = ['in', 'score',[3]];
  44. break;
  45. case OrderCommentEnum::TYPE4: //好评
  46. $where[] = ['in', 'score', [4,5]];
  47. break;
  48. }
  49. }
  50. $ret = OrderReserveComments::listPage([
  51. 'where' => $where,
  52. 'order' => 'id desc',
  53. 'with' => ['comment' => function($query) {
  54. $query->select('id, pic_url, content, score, created_at, is_virtual, virtual_user, virtual_avatar, virtual_time');
  55. }, 'user', 'orderDetail' => function($query) {
  56. $query->select('id, goods_id, goods_name, goods_attr_id, goods_attr_name, price, goods_price, num, pic_url');
  57. }],
  58. 'limit' => $params['limit'] ?? 10,
  59. 'select' => 'id, aging_score, major_score, attitude_score, tags, user_id, order_detail_id,staff_score,show_tech'
  60. ]);
  61. if (!empty(OrderReserveComments::getStaticError())) return OrderReserveComments::getStaticError();
  62. if (!empty($ret['list'])) {
  63. foreach ($ret['list'] as &$item) {
  64. $item['tags'] = !empty($item['tags']) ? Json::decode($item['tags']) : [];
  65. $item['comment']['pic_url'] = !empty($item['comment']['pic_url']) ? Json::decode($item['comment']['pic_url']) : [];
  66. if (!empty($item['comment']['is_virtual'])) {
  67. $item['user'] = [
  68. 'nickname' => $item['comment']['virtual_user'],
  69. 'avatar_url' => $item['comment']['virtual_avatar'],
  70. ];
  71. }
  72. $item['created_at'] = date('Y-m-d H:i:s', !empty($item['comment']['is_virtual']) ?
  73. $item['comment']['virtual_time'] : $item['comment']['created_at']);
  74. unset($item['comment']['is_virtual'], $item['comment']['virtual_user,'], $item['comment']['virtual_avatar'], $item['comment']['virtual_time']);
  75. }
  76. }
  77. $ret['comment_count'] = $data['comment_count'];
  78. return $ret;
  79. }
  80. /**
  81. * 获取指定技师的评分
  82. * @param int $mall_id
  83. * @param int $user_id
  84. * @param int $tech_id
  85. * @return int|string
  86. */
  87. public function rating(int $mall_id, int $user_id = 0, int $tech_id = 0)
  88. {
  89. $query = ReservationServiceUser::find()->where(['mall_id' => $mall_id])
  90. ->andWhere(['status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]]);
  91. if (!empty($user_id)) {
  92. $query = $query->andWhere(['user_id' => $user_id]);
  93. } else if (!empty($tech_id)) {
  94. $query = $query->andWhere(['id' => $tech_id]);
  95. } else {
  96. return 0;
  97. }
  98. $service_rating = $query->select('service_rating')->scalar();
  99. return !empty($service_rating) ? $service_rating : 0;
  100. }
  101. public function commentsList($params)
  102. {
  103. $mall_id = $params['mall_id'];
  104. $where = [
  105. ['mall_id' => $mall_id],
  106. [ 'status' => StatusEnum::ENABLED],
  107. ['goods_id' => $params['goods_id']],
  108. ['store_id' => $params['store_id']],
  109. ];
  110. if (!empty($params['type'])) {
  111. switch ($params['type']) {
  112. case OrderCommentEnum::TYPE1: //差评
  113. $where[] = ['in', 'score', OrderCommentEnum::OC_NEGATIVE_SCORES];
  114. break;
  115. case OrderCommentEnum::TYPE2: //中评
  116. $where[] = ['in', 'score', OrderCommentEnum::OC_MIDDLE_SCORES];
  117. break;
  118. case OrderCommentEnum::TYPE4: //好评
  119. $where[] = ['in', 'score', OrderCommentEnum::OC_PRAISE_SCORES];
  120. break;
  121. }
  122. }
  123. $params['where'] = $where;
  124. $params['select'] = 'id,user_id,order_detail_id,pic_url,score,content,created_at';
  125. $params['with'] = ['users'];
  126. $params['order'] = 'is_top desc,id desc';
  127. $order_comments_list = OrderComments::listPage($params);
  128. if (!empty($order_comments_list['list'])) {
  129. $order_detail_ids = array_column($order_comments_list['list'],'order_detail_id');
  130. $order_reserve_comments_list = OrderReserveComments::lists(['where'=>[['order_detail_id'=>$order_detail_ids]],'index'=>'order_detail_id']);
  131. $tech_user_ids = array_column($order_reserve_comments_list,'tech_user_id');
  132. $reservation_service_user_list = ReservationServiceUser::lists(['where'=>[['user_id'=>$tech_user_ids],['status'=>StatusEnum::ENABLED]],'index'=>'user_id','select'=>'user_id,avatar,name']);
  133. foreach ($order_comments_list['list'] as &$v) {
  134. $v['pic_url'] = !empty($v['pic_url']) ? json_decode($v['pic_url'], true) : [];
  135. $v['nickname'] = $v['avatar_url'] ='';
  136. if(isset($v['users'])){
  137. $v['nickname'] = $v['users']['nickname'] ?? "";
  138. $v['avatar_url'] = $v['users']['avatar_url'] ?? '';
  139. }
  140. unset($v['users']);
  141. $v['tags'] = [];
  142. $v['staff_score'] = $v['environment_score'] = $v['service_score'] = 0;
  143. $v['tech_user_name'] = $v['tech_user_avatar'] = '';
  144. if(isset($order_reserve_comments_list[$v['order_detail_id']])){
  145. $order_comment = $order_reserve_comments_list[$v['order_detail_id']];
  146. $v['tags'] = json_decode($order_comment['tags']);
  147. $v['staff_score'] = $order_comment['staff_score'];
  148. $v['environment_score'] = $order_comment['environment_score'];
  149. $v['service_score'] = $order_comment['service_score'];
  150. $v['score'] = $order_comment['score'];
  151. $tech_user_id = $order_comment['tech_user_id'];
  152. if (isset($order_comment) && $order_comment['show_tech'] == 1 && isset($reservation_service_user_list[$tech_user_id])) {
  153. $v['tech_user_name'] = $reservation_service_user_list[$tech_user_id]['name'];
  154. $v['tech_user_avatar'] = $reservation_service_user_list[$tech_user_id]['avatar'];
  155. }
  156. }
  157. }
  158. }
  159. return $order_comments_list;
  160. }
  161. }