ProductReplyDao.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. <?php
  2. namespace app\common\dao\store\product;
  3. use app\common\dao\BaseDao;
  4. use app\common\model\store\product\ProductReply;
  5. use ln\jobs\UpdateProductReplyJob;
  6. use think\db\BaseQuery;
  7. use think\db\exception\DbException;
  8. use think\facade\Db;
  9. use think\facade\Queue;
  10. /**
  11. * Class ProductReplyDao
  12. * @package app\common\dao\store\product
  13. * @author zfy
  14. * @day 2020/5/30
  15. */
  16. class ProductReplyDao extends BaseDao
  17. {
  18. /**
  19. * @return string
  20. * @author zfy
  21. * @day 2020/5/30
  22. */
  23. protected function getModel(): string
  24. {
  25. return ProductReply::class;
  26. }
  27. /**
  28. * @param array $where
  29. * @return BaseQuery
  30. * @author zfy
  31. * @day 2020/6/1
  32. */
  33. public function search(array $where)
  34. {
  35. return ProductReply::getDB()->when(isset($where['mer_id']) && $where['mer_id'] !== '', function ($query) use ($where) {
  36. $query->where('mer_id', $where['mer_id']);
  37. })->when(isset($where['is_reply']) && $where['is_reply'] !== '', function ($query) use ($where) {
  38. $query->where('is_reply', $where['is_reply']);
  39. })->when(isset($where['is_virtual']) && $where['is_virtual'] !== '', function ($query) use ($where) {
  40. $query->where('is_virtual', $where['is_virtual']);
  41. })->when(isset($where['nickname']) && $where['nickname'] !== '', function ($query) use ($where) {
  42. $query->whereLike('nickname', "%{$where['nickname']}%");
  43. })->when(isset($where['product_id']) && $where['product_id'] !== '', function ($query) use ($where) {
  44. $query->where('product_id', $where['product_id']);
  45. })->when(isset($where['product_type']) && $where['product_type'] !== '', function ($query) use ($where) {
  46. $query->where('product_type', 'product_type');
  47. })->when(isset($where['is_del']) && $where['is_del'] !== '', function ($query) use ($where) {
  48. $query->where('is_del', $where['is_del']);
  49. })->order('sort DESC');
  50. }
  51. public function searchJoinQuery(array $where)
  52. {
  53. return ProductReply::getDB()->alias('A')
  54. ->join('StoreProduct B', 'A.product_id = B.product_id')
  55. ->when(isset($where['is_reply']) && $where['is_reply'] !== '', function ($query) use ($where) {
  56. $query->where('A.is_reply', $where['is_reply']);
  57. })
  58. ->when(isset($where['nickname']) && $where['nickname'] !== '', function ($query) use ($where) {
  59. $query->whereLike('A.nickname', "%{$where['nickname']}%");
  60. })
  61. ->when(isset($where['keyword']) && $where['keyword'] !== '', function ($query) use ($where) {
  62. $query->where(function ($query) use ($where) {
  63. $query->where('B.store_name', 'like', "%{$where['keyword']}%")
  64. ->whereOr('B.product_id', $where['keyword']);
  65. });
  66. })
  67. ->when(isset($where['date']) && $where['date'] !== '', function ($query) use ($where) {
  68. getModelTime($query, $where['date'], 'A.create_time');
  69. })
  70. ->when(isset($where['mer_id']) && $where['mer_id'] !== '', function ($query) use ($where) {
  71. $query->where('A.mer_id', $where['mer_id']);
  72. })->order('A.sort DESC, A.create_time DESC')
  73. ->where('A.is_del', 0)
  74. ->field('A.reply_id,A.is_reply,A.uid,A.product_score,A.service_score,A.postage_score,A.comment,A.pics,A.create_time,A.merchant_reply_content,A.nickname,A.avatar,B.store_name,B.image,B.product_id,A.sort');
  75. }
  76. /**
  77. * @param array $data
  78. * @return int
  79. * @author zfy
  80. * @day 2020/5/30
  81. */
  82. public function insertAll(array $data)
  83. {
  84. return ProductReply::getDB()->insertAll($data);
  85. }
  86. /**
  87. * @param int $id
  88. * @return bool
  89. * @author zfy
  90. * @day 2020/5/30
  91. */
  92. public function exists(int $id)
  93. {
  94. return ProductReply::getDB()->where($this->getPk(), $id)->where('is_del', 0)->count() > 0;
  95. }
  96. /**
  97. * @param $merId
  98. * @param int $id
  99. * @return bool
  100. * @author zfy
  101. * @day 2020/6/28
  102. */
  103. public function merExists($merId, int $id)
  104. {
  105. return ProductReply::getDB()->where($this->getPk(), $id)->where('is_del', 0)->where('mer_id', $merId)->count() > 0;
  106. }
  107. /**
  108. * @param int $id
  109. * @return int
  110. * @throws DbException
  111. * @author zfy
  112. * @day 2020/5/30
  113. */
  114. public function delete(int $id)
  115. {
  116. $reply = ProductReply::getDB()->where('reply_id', $id)->find();
  117. $reply->is_del = 1;
  118. $reply->save();
  119. Queue::push(UpdateProductReplyJob::class, $reply['product_id']);
  120. }
  121. /**
  122. * 返回评论数
  123. * @Author:Qinii
  124. * @Date: 2020/6/2
  125. * @param int $productId
  126. * @param array $where
  127. * @return mixed
  128. */
  129. public function getProductReplay(int $productId, $where = [0, 5])
  130. {
  131. return $this->getModel()::getDB()->where('product_id', $productId)->whereBetween('rate', $where)->select();
  132. }
  133. public function productTotalRate($productId)
  134. {
  135. return ProductReply::getDB()->where('product_id', $productId)->where('is_del', 0)->field('sum(rate) as total_rate,count(reply_id) as total_count')->find();
  136. }
  137. /**
  138. * 计算商铺平均分
  139. * @param $merId
  140. * @return mixed
  141. * @author Qinii
  142. * @day 2020-06-11
  143. */
  144. public function merchantTotalRate($merId)
  145. {
  146. return ($this->getModel()::getDB())->where('mer_id', $merId)->field('avg(product_score) product_score ,avg(service_score) service_score,avg(postage_score) postage_score')->find()->toArray();
  147. }
  148. }