FeedbackRepository.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. $userReply = Db::name('feedback_reply')
  45. ->where(['feedback_id' => $item['feedback_id']])
  46. ->where(['admin_read' => 0])
  47. ->field('content,reply_time')
  48. ->find();
  49. $item['userReply'] = $userReply ? 1 : 0;
  50. }
  51. return compact('count', 'list');
  52. }
  53. public function getReplyList($userId, $page, $limit)
  54. {
  55. $feedbackIds = Db::name('feedback')
  56. ->alias('feed')
  57. ->leftJoin('feedback_reply reply', 'feed.feedback_id = reply.feedback_id')
  58. ->where(['feed.uid' => $userId,'reply.user_read' => 0])
  59. ->group('feed.feedback_id')
  60. ->field('feed.feedback_id')
  61. ->select()
  62. ->toArray();
  63. $feedbackIds = array_column($feedbackIds,'feedback_id');
  64. return $this->getList(['feedback_id'=>$feedbackIds], $page, $limit);
  65. }
  66. public function get( $id)
  67. {
  68. // 获取反馈信息
  69. $data = $this->dao->getWhere([$this->dao->getPk() => $id],'*',['getReply']);
  70. $type = app()->make(FeedBackCategoryRepository::class)->getWhere(['feedback_category_id' => $data['type']]);
  71. $parent = app()->make(FeedBackCategoryRepository::class)->getWhere(['feedback_category_id' => $type['pid']]);
  72. $data['type'] = $type['cate_name'];
  73. $data['category'] = $parent['cate_name'];
  74. // 更改已读状态
  75. $this->read($id, 'user_read');
  76. return $data;
  77. }
  78. /**
  79. * @param $feedbackId
  80. * @param $userId
  81. * @param $content
  82. * @return int|string
  83. * @author 史晨
  84. * @date 2026/3/17 14:15
  85. * 回复反馈
  86. */
  87. public function reply($feedbackId, $userId, $content)
  88. {
  89. $insert = [
  90. 'feedback_id' => $feedbackId,
  91. 'uid' => $userId,
  92. 'content' => $content,
  93. ];
  94. return FeedbackReply::insert($insert);
  95. }
  96. /**
  97. * @param $feedbackId
  98. * @param $type
  99. * @return void
  100. * @author 史晨
  101. * @date 2026/3/17 14:37
  102. * 更改已读状态
  103. */
  104. public function read($feedbackId, $type)
  105. {
  106. FeedbackReply::update([$type => 1], ['feedback_id' => $feedbackId]);
  107. }
  108. }