TeamService.php 5.4 KB

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