ProductReplyDao.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2024 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. * 根据条件搜索产品回复信息。
  38. *
  39. * 该方法通过接收一个包含搜索条件的数组,动态地构建查询语句,以检索产品回复数据。
  40. * 搜索条件可以包括商家ID、回复状态、虚拟状态、昵称、产品ID、产品类型和删除状态等。
  41. * 每个条件都是可选的,只有当条件存在且不为空时,才会被添加到查询语句中。
  42. *
  43. * @param array $where 包含搜索条件的数组。
  44. * @return BaseQuery|\think\db\Query
  45. */
  46. public function search(array $where)
  47. {
  48. // 获取数据库查询对象
  49. return ProductReply::getDB()->when(isset($where['mer_id']) && $where['mer_id'] !== '', function ($query) use ($where) {
  50. // 如果指定了商家ID,则添加到查询条件中
  51. $query->where('mer_id', $where['mer_id']);
  52. })->when(isset($where['is_reply']) && $where['is_reply'] !== '', function ($query) use ($where) {
  53. // 如果指定了回复状态,则添加到查询条件中
  54. $query->where('is_reply', $where['is_reply']);
  55. })->when(isset($where['is_virtual']) && $where['is_virtual'] !== '', function ($query) use ($where) {
  56. // 如果指定了虚拟状态,则添加到查询条件中
  57. $query->where('is_virtual', $where['is_virtual']);
  58. })->when(isset($where['nickname']) && $where['nickname'] !== '', function ($query) use ($where) {
  59. // 如果指定了昵称,则添加到查询条件中,使用LIKE进行模糊搜索
  60. $query->whereLike('nickname', "%{$where['nickname']}%");
  61. })->when(isset($where['product_id']) && $where['product_id'] !== '', function ($query) use ($where) {
  62. // 如果指定了产品ID,则添加到查询条件中
  63. $query->where('product_id', $where['product_id']);
  64. })->when(isset($where['product_type']) && $where['product_type'] !== '', function ($query) use ($where) {
  65. // 如果指定了产品类型,则添加到查询条件中
  66. $query->where('product_type', 'product_type');
  67. })->when(isset($where['is_del']) && $where['is_del'] !== '', function ($query) use ($where) {
  68. // 如果指定了删除状态,则添加到查询条件中
  69. $query->where('is_del', $where['is_del']);
  70. })->order('sort DESC'); // 按排序降序排列
  71. }
  72. /**
  73. * 根据条件搜索产品评价信息并进行关联查询。
  74. * 该方法通过构建一个复杂的SQL查询来实现,它根据传入的条件来过滤产品评价,并且联合查询产品及相关信息。
  75. *
  76. * @param array $where 查询条件数组,包含各种过滤参数如是否已回复、昵称、关键词等。
  77. * @return \think\db\Query 返回一个查询对象,该对象可用于进一步的查询操作或获取数据。
  78. */
  79. public function searchJoinQuery(array $where)
  80. {
  81. // 从产品评价表中获取数据,表别名为A
  82. return ProductReply::getDB()->alias('A')
  83. // 加入产品表(表别名为B),根据产品ID进行关联
  84. ->join('StoreProduct B', 'A.product_id = B.product_id')
  85. // 如果条件中指定了是否已回复,则在查询中加入该条件
  86. ->when(isset($where['is_reply']) && $where['is_reply'] !== '', function ($query) use ($where) {
  87. $query->where('A.is_reply', $where['is_reply']);
  88. })
  89. // 如果条件中指定了昵称,则在查询中加入昵称模糊搜索条件
  90. ->when(isset($where['nickname']) && $where['nickname'] !== '', function ($query) use ($where) {
  91. $query->whereLike('A.nickname', "%{$where['nickname']}%");
  92. })
  93. // 如果条件中指定了关键词,则在查询中加入对产品名称或产品ID的搜索条件
  94. ->when(isset($where['keyword']) && $where['keyword'] !== '', function ($query) use ($where) {
  95. $query->where(function ($query) use ($where) {
  96. $query->where('B.store_name', 'like', "%{$where['keyword']}%")
  97. ->whereOr('B.product_id', $where['keyword']);
  98. });
  99. })
  100. // 如果条件中指定了日期范围,则在查询中加入创建时间的条件
  101. ->when(isset($where['date']) && $where['date'] !== '', function ($query) use ($where) {
  102. getModelTime($query, $where['date'], 'A.create_time');
  103. })
  104. // 如果条件中指定了商家ID,则在查询中加入商家ID的条件
  105. ->when(isset($where['mer_id']) && $where['mer_id'] !== '', function ($query) use ($where) {
  106. $query->where('A.mer_id', $where['mer_id']);
  107. })
  108. // 如果条件中指定了产品ID,则在查询中加入产品ID的条件
  109. ->when(isset($where['product_id']) && $where['product_id'] !== '', function ($query) use ($where) {
  110. $query->where('A.product_id', $where['product_id']);
  111. })
  112. // 按照评价排序和创建时间倒序排列
  113. ->order('A.sort DESC, A.create_time DESC')
  114. // 仅查询未被删除的评价
  115. ->where('A.is_del', 0)
  116. // 指定返回的字段
  117. ->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');
  118. }
  119. /**
  120. * 批量插入数据到产品回复表中。
  121. *
  122. * 本方法用于一次性插入多条数据到产品回复表中,提高了数据插入的效率。
  123. * 它通过调用ProductReply类中的getDB方法来获取数据库连接对象,然后进一步调用insertAll方法进行数据插入。
  124. *
  125. * @param array $data 包含多条产品回复数据的数组,每条数据是一个关联数组,其中键是字段名,值是字段值。
  126. * @return bool 插入操作的执行结果,成功返回true,失败返回false。
  127. */
  128. public function insertAll(array $data)
  129. {
  130. // 调用ProductReply类的静态方法getDB来获取数据库连接对象,并进一步调用insertAll方法插入数据
  131. return ProductReply::getDB()->insertAll($data);
  132. }
  133. /**
  134. * 检查指定ID的产品回复是否存在且未被删除。
  135. *
  136. * 本函数通过查询数据库来确定给定ID的产品回复是否存在,同时确保该记录没有被标记为删除。
  137. * 存在并未被删除的条件是通过查询满足ID匹配和is_del字段为0(未删除)的记录数来判断的。
  138. * 如果存在至少一条满足条件的记录,则认为该产品回复存在。
  139. *
  140. * @param int $id 需要检查的产品回复的唯一标识ID。
  141. * @return bool 如果产品回复存在且未被删除,则返回true;否则返回false。
  142. * @throws DbException
  143. */
  144. public function exists(int $id)
  145. {
  146. // 构建查询条件,查询满足ID和未删除条件的记录数
  147. return ProductReply::getDB()->where($this->getPk(), $id)->where('is_del', 0)->count() > 0;
  148. }
  149. /**
  150. * 检查指定商户是否存在指定ID的商品回复
  151. *
  152. * 本函数通过查询数据库来确定是否存在某个特定商户(通过$merId指定)发布的商品回复(通过$id指定)
  153. * 并且该回复未被删除(is_del字段为0)。
  154. *
  155. * @param string $merId 商户ID,用于指定查询的商户范围
  156. * @param int $id 商品回复的ID,用于指定查询的具体回复
  157. * @return bool 如果存在符合条件的商品回复,则返回true,否则返回false
  158. * @throws DbException
  159. */
  160. public function merExists($merId, int $id)
  161. {
  162. // 使用数据库查询工具,根据指定条件查询商品回复记录的数量
  163. // 条件包括:主键ID为$id,未被删除(is_del为0),以及商户ID为$merId
  164. return ProductReply::getDB()->where($this->getPk(), $id)->where('is_del', 0)->where('mer_id', $merId)->count() > 0;
  165. }
  166. /**
  167. * 删除产品回复
  168. *
  169. * 本函数用于标记一个产品回复为已删除,并将该信息推送到队列中以更新相关产品的回复数量。
  170. * 删除操作是通过修改数据库中对应记录的is_del字段来实现的,而不是物理删除记录。
  171. * 使用队列来异步处理产品回复数量的更新,可以提高系统的响应速度和处理能力。
  172. *
  173. * @param int $id 回复的唯一标识符,用于在数据库中定位到具体的回复记录。
  174. * @throws DbException
  175. * @throws \think\db\exception\DataNotFoundException
  176. * @throws \think\db\exception\ModelNotFoundException
  177. */
  178. public function delete(int $id)
  179. {
  180. // 根据$id查询数据库中的回复记录
  181. $reply = ProductReply::getDB()->where('reply_id', $id)->find();
  182. // 将查询到的回复记录的is_del字段设置为1,标记为已删除
  183. $reply->is_del = 1;
  184. // 保存修改,更新数据库中的记录
  185. $reply->save();
  186. // 将回复所属产品的ID推送到队列中,用于后续的异步更新产品回复数量
  187. Queue::push(UpdateProductReplyJob::class, $reply['product_id']);
  188. }
  189. /**
  190. * 返回评论数
  191. * @Author:Qinii
  192. * @Date: 2020/6/2
  193. * @param int $productId
  194. * @param array $where
  195. * @return mixed
  196. */
  197. public function getProductReplay(int $productId, $where = [0, 5])
  198. {
  199. return $this->getModel()::getDB()->where('product_id', $productId)->whereBetween('rate', $where)->select();
  200. }
  201. /**
  202. * 计算产品的总评分率
  203. *
  204. * 本函数旨在查询指定产品的所有有效评价的评分总和以及评价总数,
  205. * 通过这些数据可以计算出产品的总评分率,用于统计和展示产品评分情况。
  206. *
  207. * @param int $productId 产品ID
  208. * @return array 返回包含总评分和总评价数的数组
  209. * @throws DbException
  210. * @throws \think\db\exception\DataNotFoundException
  211. * @throws \think\db\exception\ModelNotFoundException
  212. */
  213. public function productTotalRate($productId)
  214. {
  215. // 使用ProductReply模型的数据库查询方法,指定查询条件为产品ID和未被删除的评价
  216. // 然后计算评分总和并统计评价数量,最后返回查询结果
  217. return ProductReply::getDB()->where('product_id', $productId)->where('is_del', 0)->field('sum(rate) as total_rate,count(reply_id) as total_count')->find();
  218. }
  219. /**
  220. * 计算商铺平均分
  221. * @param $merId
  222. * @return mixed
  223. * @author Qinii
  224. * @day 2020-06-11
  225. */
  226. public function merchantTotalRate($merId)
  227. {
  228. 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();
  229. }
  230. /**
  231. * 清除特定字段值对应的数据记录
  232. *
  233. * 本函数用于根据指定的字段值和该值对应的ID,从数据库中删除相应的记录。
  234. * 这是个通用函数,可以通过传入不同的字段名和ID值来删除不同表中的数据。
  235. *
  236. * @param mixed $id 需要删除的数据记录的ID值,可以是数字、字符串等
  237. * @param string $field 指定的字段名,用于查询和删除数据
  238. */
  239. public function clear($id,$field)
  240. {
  241. // 使用模型获取数据库实例,并构造删除语句,根据字段和ID删除数据
  242. $this->getModel()::getDB()->where($field, $id)->delete();
  243. }
  244. }