SpecialReply.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2022 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\wap\model\special;
  12. use basic\ModelBasic;
  13. use service\UtilService;
  14. use traits\ModelTrait;
  15. use app\wap\model\special\Special;
  16. use app\wap\model\user\User;
  17. /**专题评论
  18. * Class SpecialReply
  19. * @package app\wap\model\special
  20. */
  21. class SpecialReply extends ModelBasic
  22. {
  23. use ModelTrait;
  24. protected $insert = ['add_time'];
  25. protected function setAddTimeAttr()
  26. {
  27. return time();
  28. }
  29. protected function setPicsAttr($value)
  30. {
  31. return is_array($value) ? json_encode($value) : $value;
  32. }
  33. protected function getPicsAttr($value)
  34. {
  35. return json_decode($value, true);
  36. }
  37. public static function reply($group)
  38. {
  39. return self::set($group);
  40. }
  41. /**字段过滤
  42. * @param string $alias
  43. * @return SpecialReply
  44. */
  45. public static function specialValidWhere($alias = '')
  46. {
  47. $model = new self;
  48. if ($alias) {
  49. $model->alias($alias);
  50. $alias .= '.';
  51. }
  52. return $model->where("{$alias}is_del", 0);
  53. }
  54. /**修改专题评论
  55. * @param $special_id
  56. * @return bool
  57. */
  58. public static function uodateScore($special_id)
  59. {
  60. $score = round(self::specialValidWhere()->where('special_id', $special_id)->avg('satisfied_score'), 1);
  61. $data['score'] = $score;
  62. return Special::edit($data, $special_id, 'id');
  63. }
  64. /**获取专题的评论列表
  65. * @param $special_id
  66. * @param string $order
  67. * @param int $page
  68. * @param int $limit
  69. * @return array
  70. * @throws \think\db\exception\DataNotFoundException
  71. * @throws \think\db\exception\ModelNotFoundException
  72. * @throws \think\exception\DbException
  73. */
  74. public static function getSpecialReplyList($special_id, $page = 0, $limit = 8, $order = 'All', $uid = 0)
  75. {
  76. $where['A.special_id'] = $special_id;
  77. if ($uid) {
  78. $where['A.uid'] = $uid;
  79. }
  80. $model = self::specialValidWhere('A')->where($where)
  81. ->field('A.satisfied_score,A.comment,A.pics,A.add_time,A.merchant_reply_content,S.title,A.is_selected,A.uid,A.id')
  82. ->join('__SPECIAL__ S', 'A.special_id = S.id');
  83. $baseOrder = 'A.is_selected DESC,A.add_time DESC,A.satisfied_score DESC';
  84. $model = $model->order($baseOrder);
  85. $list = $model->page($page, $limit)->select()->toArray() ?: [];
  86. foreach ($list as $k => $reply) {
  87. $list[$k] = self::tidySpecialReply($reply);
  88. }
  89. return $list;
  90. }
  91. /**用户信息
  92. * @param $uid
  93. * @param $reply_id
  94. * @return array|false|\PDOStatement|string|\think\Model
  95. * @throws \think\db\exception\DataNotFoundException
  96. * @throws \think\db\exception\ModelNotFoundException
  97. * @throws \think\exception\DbException
  98. */
  99. public static function userData($uid, $reply_id)
  100. {
  101. if ($uid) {
  102. $user = User::where('uid', $uid)->field('nickname,avatar')->find();
  103. } else {
  104. $user = self::getDb('reply_false')->where('reply_id', $reply_id)->field('nickname,avatar')->find();
  105. }
  106. return $user;
  107. }
  108. /**评论处理
  109. * @param $res
  110. * @return mixed
  111. * @throws \think\db\exception\DataNotFoundException
  112. * @throws \think\db\exception\ModelNotFoundException
  113. * @throws \think\exception\DbException
  114. */
  115. public static function tidySpecialReply($res)
  116. {
  117. $user = self::userData($res['uid'], $res['id']);
  118. $res['nickname'] = UtilService::anonymity($user['nickname']);
  119. $res['avatar'] = $user['avatar'];
  120. $res['add_time'] = date('Y-m-d H:i', $res['add_time']);
  121. $res['star'] = $res['satisfied_score'];
  122. $res['comment'] = $res['comment'] ?: '此用户没有填写评价';
  123. return $res;
  124. }
  125. /**评论数据
  126. * @param $special_id
  127. * @return mixed
  128. * @throws \think\Exception
  129. */
  130. public static function getSpecialReplyData($special_id)
  131. {
  132. $data['whole'] = self::specialValidWhere()->where('special_id', $special_id)->count();//全部评论
  133. $score = Special::where('id', $special_id)->value('score');
  134. if ($data['whole'] > 0 && $score > 0) {
  135. $data['score'] = $score;
  136. } else if ($data['whole'] > 0 && $score == 0) {
  137. $data['score'] = round(self::specialValidWhere()->where('special_id', $special_id)->avg('satisfied_score'), 1);
  138. } else {
  139. $data['score'] = 0;
  140. }
  141. return $data;
  142. }
  143. /**获取单个评论
  144. * @param $special_id
  145. * @return mixed|null
  146. * @throws \think\Exception
  147. * @throws \think\db\exception\DataNotFoundException
  148. * @throws \think\db\exception\ModelNotFoundException
  149. * @throws \think\exception\DbException
  150. */
  151. public static function getRecSpecialReply($special_id)
  152. {
  153. $res = self::specialValidWhere('A')->where('A.special_id', $special_id)
  154. ->field('A.is_selected,A.satisfied_score,A.comment,A.pics,A.add_time,B.nickname,B.avatar,A.merchant_reply_content,S.title')
  155. ->join('__USER__ B', 'A.uid = B.uid')
  156. ->join('__SPECIAL__ S', 'A.special_id = S.id')->find();
  157. if (!$res) return null;
  158. return self::tidySpecialReply($res->toArray());
  159. }
  160. }