StoreAdminCommentForm.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. namespace app\modules\alliance\models\store;
  8. use app\models\Goods;
  9. use app\models\OrderComment;
  10. use app\models\OrderDetail;
  11. use app\models\SaasUser;
  12. use app\models\User;
  13. use yii\base\Model;
  14. class StoreAdminCommentForm extends Model
  15. {
  16. //商品名称
  17. public $goods_name;
  18. //日期区间
  19. public $date_range;
  20. //商品ID
  21. public $goods_id;
  22. //评论状态 好中差评
  23. public $status = 0;
  24. //评论状态 已回复未回复
  25. public $type = 0;
  26. //评论ID
  27. public $id;
  28. //评论内容
  29. public $reply_content;
  30. public $store_id;
  31. public function rules()
  32. {
  33. return [
  34. [['goods_id', 'status', 'type', 'id', 'store_id'], 'integer'],
  35. [['goods_name', 'reply_content'], 'string'],
  36. [['date_range'], 'array']
  37. ];
  38. }
  39. public function commentGoodsList()
  40. {
  41. try {
  42. //商城ID
  43. $store_id = $this->store_id;
  44. //商品名称
  45. $goods_name = $this->goods_name;
  46. //日期区间
  47. $date_range = $this->date_range;
  48. //查询数据
  49. $query = OrderComment::find()->alias('oc')->where(['oc.store_id' => $store_id, 'oc.is_delete' => 0, 'oc.is_hide' => 0, 'g.is_delete' => 0])
  50. ->leftJoin(['g' => Goods::tableName()], 'oc.goods_id = g.id')
  51. ->select('oc.goods_id, oc.created_at, g.name, g.id, g.cover_pic, g.price, count(*) comment_num')
  52. ->groupBy("goods_id");
  53. if (!empty($goods_name)) {
  54. $query->andWhere(['LIKE', 'g.name', $goods_name]);
  55. }
  56. if (!empty($date_range)) {
  57. //日期转时间戳
  58. $begin_time = strtotime($date_range['begin_time']);
  59. //日期转时间戳 末尾时间再加一天时间
  60. $end_time = strtotime($date_range['end_time']) + 60 * 60 * 24;
  61. $query->andWhere(['AND', ['>', 'oc.created_at', $begin_time], ['<', 'oc.created_at', $end_time]]);
  62. }
  63. //分页
  64. $pagination = pagination_make($query);
  65. $list = $pagination['list'];
  66. foreach ($list as &$item) {
  67. $item['created_at'] = date('Y-m-d H:i:s', $item['created_at']);
  68. }
  69. return [
  70. 'code' => 0,
  71. 'msg' => "获取成功",
  72. 'data' => [
  73. 'list' => $list,
  74. 'pageNo' => $pagination['pageNo'],
  75. 'totalCount' => $pagination['totalCount'],
  76. ]
  77. ];
  78. } catch (\Exception $e) {
  79. return [
  80. 'code' => 1,
  81. 'msg' => $e->getMessage()
  82. ];
  83. }
  84. }
  85. public function commentList()
  86. {
  87. try {
  88. $status = (int)$this->status;
  89. $goods_id = $this->goods_id;
  90. $type = (int)$this->type;
  91. $query = OrderComment::find()->alias('oc')->leftJoin(['od' => OrderDetail::tableName()], 'oc.order_detail_id = od.id')
  92. ->leftJoin(['u' => User::tableName()], 'oc.user_id = u.id')
  93. ->leftJoin(['su' => SaasUser::tableName()], 'su.mobile = u.binding')
  94. ->where(['oc.is_delete' => 0, 'su.is_delete' => 0, 'od.is_delete' => 0, 'oc.goods_id' => $goods_id]);
  95. if ($status === 1) {
  96. $query->andWhere(['>', 'oc.score', 3]);
  97. }
  98. if ($status === 2) {
  99. $query->andWhere(['=', 'oc.score', 3]);
  100. }
  101. if ($status === 3) {
  102. $query->andWhere(['<', 'oc.score', 3]);
  103. }
  104. if ($type === 1) {
  105. $query->andWhere(['oc.reply_content' => null]);
  106. }
  107. if ($type === 2) {
  108. $query->andWhere(['is not', 'oc.reply_content', null]);
  109. }
  110. $query->select('oc.id, oc.created_at, oc.score, oc.content, oc.pic_list, oc.reply_content, od.attr, su.name, su.avatar');
  111. //分页
  112. $pagination = pagination_make($query);
  113. $list = $pagination['list'];
  114. foreach ($list as &$item) {
  115. $item['created_at'] = date('Y-m-d H:i:s', $item['created_at']);
  116. $item['pic_list'] = json_decode($item['pic_list'], true);
  117. }
  118. $query = OrderComment::find()->where(['goods_id' => $goods_id, 'is_delete' => 0]);
  119. //好评数量
  120. $Praise_query = clone $query;
  121. $Praise = $Praise_query->andWhere(['>', 'score', 3])->count();
  122. //中评数量
  123. $Medium_query = clone $query;
  124. $Medium_evaluation = $Medium_query->andWhere(['=', 'score', 3])->count();
  125. //差评数量
  126. $bad_query = clone $query;
  127. $bad_num = $bad_query->andWhere(['<', 'score', 3])->count();
  128. if ($status === 1) {
  129. $query->andWhere(['>', 'score', 3]);
  130. }
  131. if ($status === 2) {
  132. $query->andWhere(['=', 'score', 3]);
  133. }
  134. if ($status === 3) {
  135. $query->andWhere(['<', 'score', 3]);
  136. }
  137. //全部数量
  138. $all_query = clone $query;
  139. //未回复数量
  140. $not_reply = $query->andWhere(['reply_content' => null])->count();
  141. //全部数量
  142. $all_reply = $all_query->count();
  143. return [
  144. 'code' => 0,
  145. 'msg' => "获取成功",
  146. 'data' => [
  147. 'list' => $list,
  148. 'pageNo' => $pagination['pageNo'],
  149. 'totalCount' => $pagination['totalCount'],
  150. 'praise' => $Praise <= 99 ? $Praise : '99+',
  151. 'medium_evaluatio' => $Medium_evaluation <= 99 ? $Medium_evaluation : '99+',
  152. 'bad_num' => $bad_num <= 99 ? $bad_num : '99+',
  153. 'not_reply' => $not_reply <= 99 ? $not_reply : '99+',
  154. 'all_reply' => $all_reply <= 99 ? $all_reply : '99+'
  155. ]
  156. ];
  157. } catch (\Exception $e) {
  158. return [
  159. 'code' => 1,
  160. 'msg' => $e->getMessage()
  161. ];
  162. }
  163. }
  164. /**
  165. * 评论删除
  166. */
  167. public function commentDel()
  168. {
  169. try {
  170. $id = $this->id;
  171. $comment = OrderComment::find()->where(['id' => $id, 'is_delete' => 0])->one();
  172. if (!$comment) {
  173. throw new \Exception("评论未找到");
  174. }
  175. $comment->is_delete = 1;
  176. if (!$comment->save()) {
  177. throw new \Exception("操作失败");
  178. }
  179. return [
  180. 'code' => 0,
  181. 'msg' => "操作成功"
  182. ];
  183. } catch (\Exception $e) {
  184. return [
  185. 'code' => 1,
  186. 'msg' => $e->getMessage()
  187. ];
  188. }
  189. }
  190. /**
  191. * 回复评论
  192. */
  193. public function commentReply()
  194. {
  195. try {
  196. $id = $this->id;
  197. $reply_content = $this->reply_content;
  198. $comment = OrderComment::find()->where(['id' => $id, 'is_delete' => 0])->one();
  199. if (!$comment) {
  200. throw new \Exception("评论不存在");
  201. }
  202. if (!$reply_content) {
  203. throw new \Exception("评论不能为空");
  204. }
  205. if ($comment->reply_content) {
  206. throw new \Exception("已经评论,不可重复回复");
  207. }
  208. $comment->reply_content = $reply_content;
  209. if (!$comment->save()) {
  210. throw new \Exception("操作失败");
  211. }
  212. return [
  213. 'code' => 0,
  214. 'msg' => "操作成功"
  215. ];
  216. } catch (\Exception $e) {
  217. return [
  218. 'code' => 1,
  219. 'msg' => $e->getMessage()
  220. ];
  221. }
  222. }
  223. }