FeedbackRepository.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <?php
  2. /**
  3. * @package merchant
  4. *
  5. * @author xaboy
  6. * @day 2020/5/28
  7. *
  8. *
  9. */
  10. namespace app\common\repositories\user;
  11. use app\common\dao\user\FeedbackDao;
  12. use app\common\dao\user\FeedbackReplyDao;
  13. use app\common\model\user\Feedback;
  14. use app\common\model\user\FeedbackReply;
  15. use app\common\repositories\BaseRepository;
  16. use think\facade\Db;
  17. /**
  18. * Class FeedbackRepository
  19. * @package app\common\repositories\user
  20. * @author xaboy
  21. * @day 2020/5/28
  22. * @mixin FeedbackDao
  23. */
  24. class FeedbackRepository extends BaseRepository
  25. {
  26. /**
  27. * FeedbackRepository constructor.
  28. * @param FeedbackDao $dao
  29. */
  30. public function __construct(FeedbackDao $dao)
  31. {
  32. $this->dao = $dao;
  33. }
  34. public function getList(array $where, $page, $limit)
  35. {
  36. $query = $this->dao->search($where)->with(['type' => function ($query) {
  37. $query->field('feedback_category_id,cate_name');
  38. }]);
  39. $count = $query->count();
  40. $list = $query->page($page, $limit)->order("feedback_id desc")->withAttr('images', function ($val) {
  41. return $val ? json_decode($val, true) : [];
  42. })->select();
  43. foreach ($list as &$item) {
  44. $isReply = Db::name('feedback_reply')
  45. ->where(['feedback_id' => $item['feedback_id']])
  46. ->find();
  47. if (!$isReply) {
  48. $item['userReply'] = 1;
  49. }else{
  50. $userReply = Db::name('feedback_reply')
  51. ->where(['feedback_id' => $item['feedback_id']])
  52. ->where(['admin_read' => 0])
  53. ->find();
  54. $item['userReply'] = $userReply ? 1 : 0;
  55. }
  56. }
  57. return compact('count', 'list');
  58. }
  59. public function getReplyList($userId, $page, $limit)
  60. {
  61. $feedbackIds = Db::name('feedback')
  62. ->alias('feed')
  63. ->leftJoin('feedback_reply reply', 'feed.feedback_id = reply.feedback_id')
  64. ->where(['feed.uid' => $userId,'reply.user_read' => 0])
  65. ->group('feed.feedback_id')
  66. ->field('feed.feedback_id')
  67. ->select()
  68. ->toArray();
  69. $feedbackIds = array_column($feedbackIds,'feedback_id');
  70. return $this->getList(['feedback_id'=>$feedbackIds], $page, $limit);
  71. }
  72. public function get($id, $read = 'user_read')
  73. {
  74. // 获取反馈信息
  75. $data = $this->dao->getWhere([$this->dao->getPk() => $id], '*', ['getReply']);
  76. $type = app()->make(FeedBackCategoryRepository::class)->getWhere(['feedback_category_id' => $data['type']]);
  77. $parent = app()->make(FeedBackCategoryRepository::class)->getWhere(['feedback_category_id' => $type['pid']]);
  78. $data['type'] = $type['cate_name'];
  79. $data['category'] = $parent['cate_name'];
  80. // 更改已读状态
  81. $this->read($id, $read);
  82. return $data;
  83. }
  84. /**
  85. * @param $feedbackId
  86. * @param $userId
  87. * @param $content
  88. * @return int|string
  89. * @author 史晨
  90. * @date 2026/3/17 14:15
  91. * 回复反馈
  92. */
  93. public function reply($feedbackId, $userId, $content)
  94. {
  95. $insert = [
  96. 'feedback_id' => $feedbackId,
  97. 'uid' => $userId,
  98. 'content' => $content,
  99. ];
  100. if ($userId == 0) $insert['admin_read'] = 1;
  101. return FeedbackReply::insert($insert);
  102. }
  103. /**
  104. * @param $feedbackId
  105. * @param $type
  106. * @return void
  107. * @author 史晨
  108. * @date 2026/3/17 14:37
  109. * 更改已读状态
  110. */
  111. public function read($feedbackId, $type)
  112. {
  113. FeedbackReply::update([$type => 1], ['feedback_id' => $feedbackId]);
  114. }
  115. }