StoreProductReply.php 5.8 KB

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