TeamService.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. <?php
  2. namespace app\admin\service\medical;
  3. use app\model\SysDept;
  4. use support\Db;
  5. use support\exception\BusinessException;
  6. use support\Request;
  7. class TeamService
  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_status' => $params['dept_status'] ?? 'ACTIVED',
  72. 'dept_addtimes' => time()
  73. ];
  74. $deptId = SysDept::insertGetId($data);
  75. if (!$deptId) {
  76. throw new BusinessException('创建医疗团队失败');
  77. }
  78. } catch (BusinessException $e) {
  79. return json_fail($e->getMessage());
  80. } catch (\Exception $e) {
  81. dump($e->getMessage());
  82. return json_fail('创建医疗团队失败');
  83. }
  84. return json_success('医疗团队创建成功');
  85. }
  86. /**
  87. * @Desc 修改医疗团队
  88. * @Author Gorden
  89. * @Date 2024/2/21 10:15
  90. *
  91. * @param $id
  92. * @param $params
  93. * @return \support\Response
  94. */
  95. public static function updateDept($id, $params)
  96. {
  97. $dept = SysDept::where('dept_id', $id)->first();
  98. if (!$dept) {
  99. return json_fail('医疗团队不存在');
  100. }
  101. try {
  102. $data = [
  103. 'dept_name' => $params['dept_name'],
  104. ];
  105. // 修改失败,异常
  106. if (!SysDept::where('dept_id', $id)->update($data)) {
  107. throw new BusinessException('修改医疗团队失败');
  108. }
  109. } catch (BusinessException $e) {
  110. return json_fail($e->getMessage());
  111. } catch (\Exception $e) {
  112. return json_fail('修改医疗团队失败');
  113. }
  114. return json_success('修改医疗团队成功');
  115. }
  116. /**
  117. * @Desc 修改医疗团队状态
  118. * @Author Gorden
  119. * @Date 2024/2/21 10:41
  120. *
  121. * @param $id
  122. * @param $status
  123. * @return \support\Response
  124. */
  125. public static function updateStatus($id, $status)
  126. {
  127. try {
  128. if (!SysDept::where('dept_id', $id)->update(['dept_status' => $status])) {
  129. throw new \Exception('医疗团队状态修改失败');
  130. }
  131. } catch (\Exception $e) {
  132. return json_fail('医疗团队状态修改失败');
  133. }
  134. return json_success('医疗团队状态修改成功');
  135. }
  136. /**
  137. * @Desc 删除医疗团队
  138. * @Author Gorden
  139. * @Date 2024/2/21 10:48
  140. *
  141. * @param $id
  142. * @return \support\Response
  143. */
  144. public static function delDept(Request $request)
  145. {
  146. $ids = $request->post('dept_id');
  147. if (!$ids) {
  148. return json_fail("数据错误");
  149. }
  150. try {
  151. if (is_array($ids)) {
  152. SysDept::whereIn('dept_id', $ids)->delete();
  153. } else {
  154. SysDept::where('dept_id', $ids)->delete();
  155. }
  156. } catch (\Exception $e) {
  157. return json_fail('删除医疗团队失败');
  158. }
  159. return json_success('删除医疗团队成功');
  160. }
  161. /**
  162. * @Desc 检查医疗团队是否存在
  163. * @Author Gorden
  164. * @Date 2024/2/21 13:37
  165. *
  166. * @param $deptId
  167. * @return bool
  168. */
  169. public static function checkDeptExist($deptId)
  170. {
  171. return SysDept::where('dept_id', $deptId)->exists();
  172. }
  173. }