StoreProductReply.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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\store;
  12. use app\wap\model\user\User;
  13. use basic\ModelBasic;
  14. use service\UtilService;
  15. use traits\ModelTrait;
  16. /**商品评论
  17. * Class StoreProductReply
  18. * @package app\wap\model\store
  19. */
  20. class StoreProductReply extends ModelBasic
  21. {
  22. use ModelTrait;
  23. protected $insert = ['add_time'];
  24. protected function setAddTimeAttr()
  25. {
  26. return time();
  27. }
  28. protected function setPicsAttr($value)
  29. {
  30. return is_array($value) ? json_encode($value) : $value;
  31. }
  32. protected function getPicsAttr($value)
  33. {
  34. return json_decode($value, true);
  35. }
  36. public static function reply($group, $type = 'product')
  37. {
  38. $group['reply_type'] = $type;
  39. return self::set($group);
  40. }
  41. public static function productValidWhere($alias = '')
  42. {
  43. $model = new self;
  44. if ($alias) {
  45. $model->alias($alias);
  46. $alias .= '.';
  47. }
  48. return $model->where("{$alias}is_del", 0)->where("{$alias}reply_type", 'product');
  49. }
  50. public static function getProductReplyList($productId, $page = 0, $limit = 8, $score = 4, $order = 'All')
  51. {
  52. $model = self::productValidWhere('A')->where('A.product_id', $productId)
  53. ->field('A.product_score,A.service_score,A.delivery_score,A.comment,A.pics,A.add_time,A.merchant_reply_content,A.is_selected,A.uid,A.id');
  54. $baseOrder = 'A.is_selected DESC,A.add_time DESC,A.product_score DESC,A.service_score DESC,A.delivery_score DESC';
  55. $model = $model->order($baseOrder);
  56. switch ($score) {
  57. case 3:
  58. $model = $model->where('A.product_score', 5);
  59. break;
  60. case 2:
  61. $model = $model->where('A.product_score', '>', 1)->where('A.product_score', '<', 5);
  62. break;
  63. case 1:
  64. $model = $model->where('A.product_score', 1);
  65. break;
  66. }
  67. $list = $model->page($page, $limit)->select()->toArray() ?: [];
  68. foreach ($list as $k => $reply) {
  69. $list[$k] = self::tidyProductReply($reply);
  70. }
  71. return $list;
  72. }
  73. public static function userData($uid, $reply_id)
  74. {
  75. if ($uid) {
  76. $user = User::where('uid', $uid)->field('nickname,avatar')->find();
  77. } else {
  78. $user = self::getDb('reply_false')->where('reply_id', $reply_id)->field('nickname,avatar')->find();
  79. }
  80. return $user;
  81. }
  82. public static function tidyProductReply($res)
  83. {
  84. $user = self::userData($res['uid'], $res['id']);
  85. $res['nickname'] = UtilService::anonymity($user['nickname']);
  86. $res['avatar'] = $user['avatar'];
  87. $res['add_time'] = date('Y-m-d H:i', $res['add_time']);
  88. $res['star'] = round(($res['product_score'] + $res['service_score'] + $res['delivery_score']) / 3);
  89. $res['comment'] = $res['comment'] ?: '此用户没有填写评价';
  90. return $res;
  91. }
  92. /**评论数据
  93. * @param $productId
  94. * @return mixed
  95. * @throws \think\Exception
  96. */
  97. public static function getProductReplyData($productId)
  98. {
  99. $data['whole'] = self::productValidWhere()->where('product_id', $productId)->count();//全部评论
  100. $data['praise'] = self::productValidWhere()->where('product_id', $productId)->where('product_score', '>=', 5)->count();//好评评论
  101. $data['review'] = self::productValidWhere()->where('product_id', $productId)->where('product_score', '>', 1)->where('product_score', '<', 5)->count();//中评评论
  102. $data['difference'] = self::productValidWhere()->where('product_id', $productId)->where('product_score', '=', 1)->count();//差评评论
  103. if ($data['whole'] > 0) {
  104. $data['positive_rate'] = bcmul(bcdiv($data['praise'], $data['whole'], 2), 100, 0);//好评率
  105. } else {
  106. $data['positive_rate'] = 0;
  107. }
  108. $product_score = self::productValidWhere()->where('product_id', $productId)->avg('product_score');
  109. $service_score = self::productValidWhere()->where('product_id', $productId)->avg('service_score');
  110. $delivery_score = self::productValidWhere()->where('product_id', $productId)->avg('delivery_score');
  111. $data['star'] = round(($product_score + $service_score + $delivery_score) / 3);//评分
  112. return $data;
  113. }
  114. public static function isReply($unique, $reply_type = 'product')
  115. {
  116. return self::be(['unique' => $unique, 'reply_type' => $reply_type]);
  117. }
  118. public static function getRecProductReply($productId)
  119. {
  120. $res = self::productValidWhere('A')->where('A.product_id', $productId)
  121. ->field('A.product_score,A.service_score,A.delivery_score,A.comment,A.pics,A.add_time,B.nickname,B.avatar,C.cart_info')
  122. ->join('__USER__ B', 'A.uid = B.uid')
  123. ->join('__STORE_ORDER_CART_INFO__ C', 'A.unique = C.unique')
  124. ->order('A.product_score DESC,A.service_score DESC,A.delivery_score DESC, A.add_time DESC')->find();
  125. if (!$res) return null;
  126. return self::tidyProductReply($res->toArray());
  127. }
  128. }