ComplaintService.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. <?php
  2. namespace App\Services\Common;
  3. use App\Models\ActionLogModel;
  4. use App\Models\ComplaintModel;
  5. use App\Services\BaseService;
  6. /**
  7. * 投诉建议-服务类
  8. */
  9. class ComplaintService extends BaseService
  10. {
  11. public function __construct()
  12. {
  13. $this->model = new ComplaintModel();
  14. }
  15. public static function make()
  16. {
  17. if (!self::$instance) {
  18. self::$instance = (new static());
  19. }
  20. return self::$instance;
  21. }
  22. /**
  23. * 获取数据列表
  24. * @param array $params 请求参数
  25. * @param int $pageSize 分页大小
  26. */
  27. public function getDataList($params, $pageSize = 15)
  28. {
  29. $query = $this->model->where('mark', 1);
  30. // 状态筛选
  31. if (isset($params['status']) && $params['status'] !== '') {
  32. $query->where('status', $params['status']);
  33. }
  34. // 关键词搜索
  35. if (isset($params['keyword']) && $params['keyword']) {
  36. $keyword = $params['keyword'];
  37. $query->where(function ($q) use ($keyword) {
  38. $q->where('realname', 'like', '%' . $keyword . '%')
  39. ->orWhere('mobile', 'like', '%' . $keyword . '%')
  40. ->orWhere('content', 'like', '%' . $keyword . '%');
  41. });
  42. }
  43. $list = $query->with(['user'])
  44. ->orderBy('id', 'desc')
  45. ->paginate($pageSize);
  46. $list = $list ? $list->toArray() : [];
  47. if ($list && isset($list['data'])) {
  48. foreach ($list['data'] as &$item) {
  49. $item['create_time'] = $item['create_time'] ? date('Y-m-d H:i:s', $item['create_time']) : '';
  50. $item['update_time'] = $item['update_time'] ? date('Y-m-d H:i:s', $item['update_time']) : '';
  51. // 处理图片
  52. if (!empty($item['albums'])) {
  53. $albums = json_decode($item['albums'], true);
  54. if (is_array($albums)) {
  55. $item['albums_array'] = array_map(function($url) {
  56. return get_image_url($url);
  57. }, $albums);
  58. } else {
  59. $item['albums_array'] = [];
  60. }
  61. } else {
  62. $item['albums_array'] = [];
  63. }
  64. // 状态文本
  65. $item['status_text'] = $this->getStatusText($item['status']);
  66. // 用户昵称
  67. $item['user_name'] = $item['user']['nickname'] ?? '-';
  68. }
  69. }
  70. return [
  71. 'msg' => '操作成功',
  72. 'code' => 0,
  73. 'data' => $list['data'] ?? [],
  74. 'count' => $list['total'] ?? 0,
  75. ];
  76. }
  77. /**
  78. * 获取状态文本
  79. */
  80. private function getStatusText($status)
  81. {
  82. $statusMap = [
  83. 1 => '待处理',
  84. 2 => '已处理'
  85. ];
  86. return isset($statusMap[$status]) ? $statusMap[$status] : '未知';
  87. }
  88. /**
  89. * 编辑(处理投诉)
  90. */
  91. public function edit($data = [])
  92. {
  93. if (empty($data)) {
  94. $data = request()->all();
  95. }
  96. if (empty($data['id'])) {
  97. return ['code' => 1, 'msg' => '参数错误'];
  98. }
  99. // 只允许修改备注和状态
  100. $updateData = [
  101. 'update_time' => time()
  102. ];
  103. if (isset($data['remark'])) {
  104. $updateData['remark'] = $data['remark'];
  105. }
  106. if (isset($data['status'])) {
  107. $updateData['status'] = $data['status'];
  108. }
  109. $result = $this->model->where('id', $data['id'])->update($updateData);
  110. if ($result !== false) {
  111. ActionLogModel::setTitle("处理投诉建议");
  112. ActionLogModel::record();
  113. return ['code' => 0, 'msg' => '操作成功'];
  114. }
  115. return ['code' => 1, 'msg' => '操作失败'];
  116. }
  117. /**
  118. * 删除
  119. */
  120. public function delete()
  121. {
  122. $id = request()->post('id');
  123. if (!$id) {
  124. return ['code' => 1, 'msg' => '参数错误'];
  125. }
  126. // 支持批量删除
  127. if (is_array($id)) {
  128. $result = $this->model->whereIn('id', $id)->update(['mark' => 0]);
  129. } else {
  130. $result = $this->model->where('id', $id)->update(['mark' => 0]);
  131. }
  132. if ($result) {
  133. ActionLogModel::setTitle("删除投诉建议");
  134. ActionLogModel::record();
  135. return ['code' => 0, 'msg' => '删除成功'];
  136. }
  137. return ['code' => 1, 'msg' => '删除失败'];
  138. }
  139. /**
  140. * 修改状态
  141. */
  142. public function status()
  143. {
  144. $params = request()->all();
  145. $id = isset($params['id']) ? intval($params['id']) : 0;
  146. $status = isset($params['status']) ? intval($params['status']) : 0;
  147. if (!$id) {
  148. return ['code' => 1, 'msg' => 'ID不能为空'];
  149. }
  150. if (!$status || ($status != 1 && $status != 2)) {
  151. return ['code' => 1, 'msg' => '状态参数错误'];
  152. }
  153. $result = $this->model->where('id', $id)->update([
  154. 'status' => $status,
  155. 'update_time' => time()
  156. ]);
  157. if ($result !== false) {
  158. ActionLogModel::setTitle("修改投诉状态");
  159. ActionLogModel::record();
  160. return ['code' => 0, 'msg' => '状态修改成功'];
  161. }
  162. return ['code' => 1, 'msg' => '状态修改失败'];
  163. }
  164. /**
  165. * 获取详情
  166. */
  167. public function getInfo($id)
  168. {
  169. $info = $this->model->where('id', $id)->where('mark', 1)->first();
  170. if (!$info) {
  171. return ['code' => 1, 'msg' => '数据不存在'];
  172. }
  173. $info = $info->toArray();
  174. // 处理图片
  175. if (!empty($info['albums'])) {
  176. $albums = json_decode($info['albums'], true);
  177. if (is_array($albums)) {
  178. $info['albums_array'] = array_map(function($url) {
  179. return get_image_url($url);
  180. }, $albums);
  181. } else {
  182. $info['albums_array'] = [];
  183. }
  184. } else {
  185. $info['albums_array'] = [];
  186. }
  187. return ['code' => 0, 'data' => $info];
  188. }
  189. }