ServiceTeamService.php 5.0 KB

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