ServiceTeamService.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. <?php
  2. namespace app\admin\service;
  3. use app\model\SysDept;
  4. use support\Db;
  5. use support\exception\BusinessException;
  6. use support\Request;
  7. class ServiceTeamService
  8. {
  9. const MEDICAL_DEPT_CATEGORY = '服务团队';
  10. /**
  11. * @Desc 服务团队列表
  12. * @Author Gorden
  13. * @Date 2024/2/21 10:28
  14. *
  15. * @param $page
  16. * @param $limit
  17. * @param $keywords
  18. * @return \support\Response
  19. */
  20. public static function deptList(Request $request)
  21. {
  22. $page = $request->get('page', 1);
  23. $pageSize = $request->get('pageSize', 10);
  24. $keywords = format_string($request->get('keywords', ''));
  25. $rows = SysDept::select('dept_id', 'dept_name','dept_addtimes')
  26. ->where('dept_category', self::MEDICAL_DEPT_CATEGORY)
  27. ->when($keywords != '', function ($query) use ($keywords) {
  28. $query->where('dept_name', 'like', '%' . $keywords . '%');
  29. })
  30. ->orderBy('dept_addtimes', 'DESC')
  31. ->forPage($page, $pageSize)
  32. ->get()
  33. ->toArray();
  34. $total = SysDept::where('dept_category', self::MEDICAL_DEPT_CATEGORY)
  35. ->when($keywords != '', function ($query) use ($keywords) {
  36. $query->where('dept_name', 'like', '%' . $keywords . '%');
  37. })->count();
  38. return json_success('', compact('rows', 'page', 'pageSize', 'total'));
  39. }
  40. /**
  41. * @Desc 服务团队详情
  42. * @Author Gorden
  43. * @Date 2024/2/21 10:35
  44. *
  45. * @param $id
  46. * @return \support\Response
  47. */
  48. public static function deptInfo($id)
  49. {
  50. $dept = SysDept::find($id);
  51. if (!$dept) {
  52. return json_fail('服务团队不存在');
  53. }
  54. $dept = $dept->toArray();
  55. return json_success('', $dept);
  56. }
  57. /**
  58. * @Desc 创建服务团队
  59. * @Author Gorden
  60. * @Date 2024/2/21 9:22
  61. *
  62. * @param $params
  63. * @return \support\Response
  64. */
  65. public static function insertDept($params)
  66. {
  67. try {
  68. $data = [
  69. 'dept_category' => self::MEDICAL_DEPT_CATEGORY,
  70. 'dept_name' => $params['dept_name'],
  71. 'dept_addtimes' => time()
  72. ];
  73. $deptId = SysDept::insertGetId($data);
  74. if (!$deptId) {
  75. throw new BusinessException('创建服务团队失败');
  76. }
  77. } catch (BusinessException $e) {
  78. return json_fail($e->getMessage());
  79. } catch (\Exception $e) {
  80. dump($e->getMessage());
  81. return json_fail('创建服务团队失败');
  82. }
  83. return json_success('服务团队创建成功');
  84. }
  85. /**
  86. * @Desc 修改服务团队
  87. * @Author Gorden
  88. * @Date 2024/2/21 10:15
  89. *
  90. * @param $id
  91. * @param $params
  92. * @return \support\Response
  93. */
  94. public static function updateDept($id, $params)
  95. {
  96. $dept = SysDept::where('dept_id', $id)->first();
  97. if (!$dept) {
  98. return json_fail('服务团队不存在');
  99. }
  100. try {
  101. $data = [
  102. 'dept_name' => $params['dept_name'],
  103. ];
  104. // 修改失败,异常
  105. if (!SysDept::where('dept_id', $id)->update($data)) {
  106. throw new BusinessException('修改服务团队失败');
  107. }
  108. } catch (BusinessException $e) {
  109. return json_fail($e->getMessage());
  110. } catch (\Exception $e) {
  111. return json_fail('修改服务团队失败');
  112. }
  113. return json_success('修改服务团队成功');
  114. }
  115. /**
  116. * @Desc 修改服务团队状态
  117. * @Author Gorden
  118. * @Date 2024/2/21 10:41
  119. *
  120. * @param $id
  121. * @param $status
  122. * @return \support\Response
  123. */
  124. public static function updateStatus($id, $status)
  125. {
  126. try {
  127. if (!SysDept::where('dept_id', $id)->update(['dept_status' => $status])) {
  128. throw new \Exception('服务团队状态修改失败');
  129. }
  130. } catch (\Exception $e) {
  131. return json_fail('服务团队状态修改失败');
  132. }
  133. return json_success('服务团队状态修改成功');
  134. }
  135. /**
  136. * @Desc 删除服务团队
  137. * @Author Gorden
  138. * @Date 2024/2/21 10:48
  139. *
  140. * @param $id
  141. * @return \support\Response
  142. */
  143. public static function delDept(Request $request)
  144. {
  145. $ids = $request->post('dept_id');
  146. if (!$ids) {
  147. return json_fail("数据错误");
  148. }
  149. try {
  150. if (is_array($ids)) {
  151. SysDept::whereIn('dept_id', $ids)->delete();
  152. } else {
  153. SysDept::where('dept_id', $ids)->delete();
  154. }
  155. } catch (\Exception $e) {
  156. return json_fail('删除服务团队失败');
  157. }
  158. return json_success('删除服务团队成功');
  159. }
  160. /**
  161. * @Desc 检查服务团队是否存在
  162. * @Author Gorden
  163. * @Date 2024/2/21 13:37
  164. *
  165. * @param $deptId
  166. * @return bool
  167. */
  168. public static function checkDeptExist($deptId)
  169. {
  170. return SysDept::where('dept_id', $deptId)->exists();
  171. }
  172. }