CommentListForm.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. /*
  8. * @Author: your name
  9. * @Date: 2021-03-02 09:50:20
  10. * @LastEditTime: 2021-05-07 16:37:47
  11. * @LastEditors: Please set LastEditors
  12. * @Description: In User Settings Edit
  13. * @FilePath: \admin_php\modules\client\models\v1\CommentListForm.php
  14. */
  15. namespace app\modules\alliance\models;
  16. use app\models\OrderComment;
  17. use app\models\OrderDetail;
  18. use app\models\SaasUser;
  19. use app\models\User;
  20. use yii\base\Model;
  21. use yii\data\Pagination;
  22. use yii\helpers\Json;
  23. class CommentListForm extends Model
  24. {
  25. public $goods_id;
  26. public $score = -1;
  27. public $page = 1;
  28. public $limit = 20;
  29. public function rules()
  30. {
  31. return [
  32. [['goods_id'], 'required'],
  33. [['page', 'score'], 'integer'],
  34. ];
  35. }
  36. public function search()
  37. {
  38. if (!$this->validate()) {
  39. return [
  40. 'code' => 1,
  41. 'msg' => $this->getErrorSummary(false)[0],
  42. ];
  43. }
  44. $query = OrderComment::find()->where(['goods_id' => $this->goods_id, 'is_delete' => 0, 'is_hide' => 0]);
  45. if ($this->score > 0) {
  46. if ($this->score == 1) {
  47. $score = [1, 2];
  48. } elseif($this->score == 2) {
  49. $score = [3, 4];
  50. } else {
  51. $score = [5];
  52. }
  53. $query->andWhere([
  54. 'score' => $score
  55. ]);
  56. }
  57. $count = $query->count();
  58. $pagination = new Pagination(['totalCount' => $count, 'pageSize' => $this->limit, 'page' => $this->page - 1]);
  59. $list = $query->limit($pagination->limit)->offset($pagination->offset)->orderBy('created_at DESC')->asArray()
  60. ->select('order_detail_id, is_virtual, virtual_user, virtual_avatar, score, content, pic_list,
  61. created_at, reply_content, user_id, saas_id , user_type, user_name, user_avatar')->all();
  62. foreach ($list as $i => $item) {
  63. if (intval($item['user_type']) === OrderComment::USER_TYPE_CUSTOM) {
  64. $list[$i]['nickname'] = $item['user_name'];
  65. $list[$i]['avatar_url'] = $item['user_avatar'];
  66. } else {
  67. if ($item['saas_id']) {
  68. $saasUser = SaasUser::findOne($item['saas_id']);
  69. $list[$i]['nickname'] = $saasUser->name;
  70. $list[$i]['avatar_url'] = $saasUser->avatar;
  71. } else {
  72. $user = User::findOne($item['user_id']);
  73. $list[$i]['nickname'] = $user->nickname;
  74. $list[$i]['avatar_url'] = $user->avatar_url;
  75. }
  76. }
  77. $list[$i]['attr'] = Json::decode(OrderDetail::findOne($item['order_detail_id'])->attr);
  78. $list[$i]['addtime'] = date('Y-m-d', $item['created_at']);
  79. $list[$i]['pic_list'] = json_decode($item['pic_list']);
  80. $list[$i]['nickname'] = $this->substr_cut($list[$i]['nickname']);
  81. if ($item['is_virtual'] == 1) {
  82. $list[$i]['nickname'] = $this->substr_cut($item['virtual_user']);
  83. $list[$i]['avatar_url'] = $item['virtual_avatar'];
  84. }
  85. unset($list[$i]['virtual_avatar']);
  86. unset($list[$i]['is_virtual']);
  87. unset($list[$i]['virtual_user']);
  88. };
  89. $data = [
  90. 'row_count' => $count,
  91. 'page_count' => $pagination->pageCount,
  92. 'list' => $list,
  93. 'comment_count' => $this->countData(),
  94. ];
  95. return [
  96. 'code' =>0,
  97. 'data' => $data
  98. ];
  99. }
  100. public function countData()
  101. {
  102. if (!$this->validate()) {
  103. return $this->errorResponse;
  104. }
  105. $score_all = OrderComment::find()->alias('oc')
  106. ->where(['oc.goods_id' => $this->goods_id, 'oc.is_delete' => 0, 'oc.is_hide' => 0,])->count();
  107. $score_3 = OrderComment::find()->alias('oc')
  108. ->where(['oc.goods_id' => $this->goods_id, 'oc.is_delete' => 0, 'oc.is_hide' => 0, 'oc.score' => [5,4]])->count();
  109. $score_2 = OrderComment::find()->alias('oc')
  110. ->where(['oc.goods_id' => $this->goods_id, 'oc.is_delete' => 0, 'oc.is_hide' => 0, 'oc.score' => [3,2]])->count();
  111. $score_1 = OrderComment::find()->alias('oc')
  112. ->where(['oc.goods_id' => $this->goods_id, 'oc.is_delete' => 0, 'oc.is_hide' => 0, 'oc.score' => [1]])->count();
  113. return (object)[
  114. 'score_all' => $score_all ? $score_all : 0,
  115. 'score_3' => $score_3 ? $score_3 : 0,
  116. 'score_2' => $score_2 ? $score_2 : 0,
  117. 'score_1' => $score_1 ? $score_1 : 0,
  118. ];
  119. }
  120. // 将用户名 做隐藏
  121. private function substr_cut($user_name)
  122. {
  123. $strlen = mb_strlen($user_name, 'utf-8');
  124. $firstStr = mb_substr($user_name, 0, 1, 'utf-8');
  125. $lastStr = mb_substr($user_name, -1, 1, 'utf-8');
  126. return $strlen <= 2 ? $firstStr . '*' : $firstStr . str_repeat("*", 3) . $lastStr;
  127. }
  128. }