TeamService.php 5.3 KB

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