StoreAdminCommentForm.php 8.3 KB

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