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_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. if (!SysDept::where('dept_id', $id)->update($data)) {
  112. throw new BusinessException('修改营销团队失败');
  113. }
  114. } catch (BusinessException $e) {
  115. return json_fail($e->getMessage());
  116. } catch (\Exception $e) {
  117. return json_fail('修改营销团队失败');
  118. }
  119. return json_success('修改营销团队成功');
  120. }
  121. /**
  122. * @Desc 修改营销团队状态
  123. * @Author Gorden
  124. * @Date 2024/2/21 10:41
  125. *
  126. * @param $id
  127. * @param $status
  128. * @return \support\Response
  129. */
  130. public static function updateStatus($id, $status)
  131. {
  132. try {
  133. if (!SysDept::where('dept_id', $id)->update(['dept_status' => $status])) {
  134. throw new \Exception('营销团队状态修改失败');
  135. }
  136. } catch (\Exception $e) {
  137. return json_fail('营销团队状态修改失败');
  138. }
  139. return json_success('营销团队状态修改成功');
  140. }
  141. /**
  142. * @Desc 删除营销团队
  143. * @Author Gorden
  144. * @Date 2024/2/21 10:48
  145. *
  146. * @param $id
  147. * @return \support\Response
  148. */
  149. public static function delDept(Request $request)
  150. {
  151. $ids = $request->post('dept_id');
  152. if (!$ids) {
  153. return json_fail("数据错误");
  154. }
  155. try {
  156. if (is_array($ids)) {
  157. SysDept::whereIn('dept_id', $ids)->delete();
  158. } else {
  159. SysDept::where('dept_id', $ids)->delete();
  160. }
  161. } catch (\Exception $e) {
  162. return json_fail('删除营销团队失败');
  163. }
  164. return json_success('删除营销团队成功');
  165. }
  166. /**
  167. * @Desc 检查营销团队是否存在
  168. * @Author Gorden
  169. * @Date 2024/2/21 13:37
  170. *
  171. * @param $deptId
  172. * @return bool
  173. */
  174. public static function checkDeptExist($deptId)
  175. {
  176. return SysDept::where('dept_id', $deptId)->exists();
  177. }
  178. }