FeedbackRepository.php 3.3 KB

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