TeamService.php 5.2 KB

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