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