ProductReplyDao.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 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. });
  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. })->order('A.create_time DESC')
  82. ->where('A.is_del', 0)
  83. ->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');
  84. }
  85. /**
  86. * @param array $data
  87. * @return int
  88. * @author xaboy
  89. * @day 2020/5/30
  90. */
  91. public function insertAll(array $data)
  92. {
  93. return ProductReply::getDB()->insertAll($data);
  94. }
  95. /**
  96. * @param int $id
  97. * @return bool
  98. * @author xaboy
  99. * @day 2020/5/30
  100. */
  101. public function exists(int $id)
  102. {
  103. return ProductReply::getDB()->where($this->getPk(), $id)->where('is_del', 0)->count() > 0;
  104. }
  105. /**
  106. * @param $merId
  107. * @param int $id
  108. * @return bool
  109. * @author xaboy
  110. * @day 2020/6/28
  111. */
  112. public function merExists($merId, int $id)
  113. {
  114. return ProductReply::getDB()->where($this->getPk(), $id)->where('is_del', 0)->where('mer_id', $merId)->count() > 0;
  115. }
  116. /**
  117. * @param int $id
  118. * @return int
  119. * @throws DbException
  120. * @author xaboy
  121. * @day 2020/5/30
  122. */
  123. public function delete(int $id)
  124. {
  125. $reply = ProductReply::getDB()->where('reply_id', $id)->find();
  126. $reply->is_del = 1;
  127. $reply->save();
  128. Queue::push(UpdateProductReplyJob::class, $reply['product_id']);
  129. }
  130. /**
  131. * 返回评论数
  132. * @Author:Qinii
  133. * @Date: 2020/6/2
  134. * @param int $productId
  135. * @param array $where
  136. * @return mixed
  137. */
  138. public function getProductReplay(int $productId, $where = [0, 5])
  139. {
  140. return $this->getModel()::getDB()->where('product_id', $productId)->whereBetween('rate', $where)->select();
  141. }
  142. public function productTotalRate($productId)
  143. {
  144. return ProductReply::getDB()->where('product_id', $productId)->where('is_del', 0)->field('sum(rate) as total_rate,count(reply_id) as total_count')->find();
  145. }
  146. /**
  147. * 计算商铺平均分
  148. * @param $merId
  149. * @return mixed
  150. * @author Qinii
  151. * @day 2020-06-11
  152. */
  153. public function merchantTotalRate($merId)
  154. {
  155. 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();
  156. }
  157. }