StoreProductReply.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. <?php
  2. namespace app\models\store;
  3. use crmeb\basic\BaseModel;
  4. use crmeb\services\UtilService;
  5. use crmeb\traits\ModelTrait;
  6. /**
  7. * TODO 产品评价Model
  8. * Class StoreProductReply
  9. * @package app\models\store
  10. */
  11. class StoreProductReply extends BaseModel
  12. {
  13. /**
  14. * 数据表主键
  15. * @var string
  16. */
  17. protected $pk = 'id';
  18. /**
  19. * 模型名称
  20. * @var string
  21. */
  22. protected $name = 'store_product_reply';
  23. use ModelTrait;
  24. protected $insert = ['add_time'];
  25. protected function setAddTimeAttr()
  26. {
  27. return time();
  28. }
  29. protected function getPicsAttr($value)
  30. {
  31. return json_decode($value, true);
  32. }
  33. public static function reply($group, $type = 'product')
  34. {
  35. $group['reply_type'] = $type;
  36. return self::create($group);
  37. }
  38. public static function productValidWhere($alias = '')
  39. {
  40. if ($alias) {
  41. $model = self::alias($alias);
  42. $alias .= '.';
  43. } else {
  44. $model = new self;
  45. }
  46. return $model->where("{$alias}is_del", 0)->where("{$alias}reply_type", 'product');
  47. }
  48. /*
  49. * 设置查询产品评论条件
  50. * @param int $productId 产品id
  51. * @param string $order 排序方式
  52. * @return object
  53. * */
  54. public static function setProductReplyWhere($productId, $type = 0, $alias = 'A')
  55. {
  56. $model = self::productValidWhere($alias)->where('A.product_id', $productId)
  57. ->field('A.product_score,A.service_score,A.comment,A.merchant_reply_content,A.merchant_reply_time,A.pics,A.add_time,B.nickname,B.avatar,C.cart_info,A.merchant_reply_content,A.nickname as _nickname,A.avatar as _avatar')
  58. ->join('user B', 'A.uid = B.uid', 'left')
  59. ->join('store_order_cart_info C', 'A.unique = C.unique', 'left');
  60. switch ($type) {
  61. case 1:
  62. $model = $model->where('A.product_score', 5); //好评
  63. break;
  64. case 2:
  65. $model = $model->where('A.product_score', '<', 5)->where('A.product_score', '>', 2); //中评
  66. break;
  67. case 3:
  68. $model = $model->where('A.product_score', '<', 2); //差评
  69. break;
  70. }
  71. return $model;
  72. }
  73. public static function getProductReplyList($productId, $order = 0, $page = 0, $limit = 8)
  74. {
  75. $model = self::setProductReplyWhere($productId, $order);
  76. if ($page) $model = $model->page((int)$page, (int)$limit);
  77. $list = $model->order('add_time desc')->select()->toArray() ?: [];
  78. foreach ($list as $k => $reply) {
  79. if (!$reply['nickname']) $list[$k]['nickname'] = $reply['_nickname'];
  80. if (!$reply['avatar']) $list[$k]['avatar'] = $reply['_avatar'];
  81. unset($list[$k]['_nickname'], $list[$k]['_avatar']);
  82. $list[$k] = self::tidyProductReply($list[$k]);
  83. }
  84. return $list;
  85. }
  86. public static function tidyProductReply($res)
  87. {
  88. $res['cart_info'] = json_decode($res['cart_info'], true) ?: [];
  89. $res['suk'] = isset($res['cart_info']['productInfo']['attrInfo']) ? $res['cart_info']['productInfo']['attrInfo']['suk'] : '';
  90. $res['nickname'] = anonymity($res['nickname']);
  91. $res['merchant_reply_time'] = date('Y-m-d H:i', $res['merchant_reply_time']);
  92. $res['add_time'] = date('Y-m-d H:i', $res['add_time']);
  93. $res['star'] = bcadd($res['product_score'], $res['service_score'], 2);
  94. $res['star'] = bcdiv($res['star'], 2, 0);
  95. $res['comment'] = $res['comment'] ?: '此用户没有填写评价';
  96. $res['pics'] = is_string($res['pics']) ? json_decode($res['pics'], true) : $res['pics'];
  97. unset($res['cart_info']);
  98. return $res;
  99. }
  100. public static function isReply($unique, $reply_type = 'product')
  101. {
  102. return self::be(['unique' => $unique, 'reply_type' => $reply_type]);
  103. }
  104. public static function getRecProductReply($productId)
  105. {
  106. $res = self::productValidWhere('A')->where('A.product_id', $productId)
  107. ->field('A.product_score,A.service_score,A.comment,A.merchant_reply_content,A.merchant_reply_time,A.pics,A.add_time,B.nickname,B.avatar,C.cart_info,A.nickname as _nickname,A.avatar as _avatar')
  108. ->join('user B', 'A.uid = B.uid', 'left')
  109. ->join('store_order_cart_info C', 'A.unique = C.unique', 'left')
  110. ->order('A.add_time DESC,A.product_score DESC, A.service_score DESC, A.add_time DESC')->find();
  111. if (!$res) return null;
  112. if (!$res['nickname']) $res['nickname'] = $res['_nickname'];
  113. if (!$res['avatar']) $res['avatar'] = $res['_avatar'];
  114. unset($res['_nickname'], $res['_avatar']);
  115. return self::tidyProductReply($res->toArray());
  116. }
  117. public static function productReplyCount($productId)
  118. {
  119. // \think\Db::listen(function($sql, $time, $explain){
  120. // // 记录SQL
  121. // echo $sql. ' ['.$time.'s]';
  122. // });
  123. $data['sum_start'] = self::setProductReplyWhere($productId)->sum('product_score');
  124. $data['sum_count'] = self::setProductReplyWhere($productId)->count();
  125. $data['good_count'] = self::setProductReplyWhere($productId, 1)->count();
  126. $data['in_count'] = self::setProductReplyWhere($productId, 2)->count();
  127. $data['poor_count'] = self::setProductReplyWhere($productId, 3)->count();
  128. if ($data['sum_count'] != 0) {
  129. $data['reply_chance'] = bcdiv($data['good_count'], $data['sum_count'], 2);
  130. } else {
  131. $data['reply_chance'] = 0;
  132. }
  133. $data['reply_star'] = $data['sum_count'] > 0 ? bcdiv($data['sum_start'], $data['sum_count'], 0) : 0;
  134. $data['reply_chance'] = bcmul($data['reply_chance'], 100, 2);
  135. return $data;
  136. }
  137. }