DeptLogic.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | likeadmin快速开发前后端分离管理后台(PHP版)
  4. // +----------------------------------------------------------------------
  5. // | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
  6. // | 开源版本可自由商用,可去除界面版权logo
  7. // | gitee下载:https://gitee.com/likeshop_gitee/likeadmin
  8. // | github下载:https://github.com/likeshop-github/likeadmin
  9. // | 访问官网:https://www.likeadmin.cn
  10. // | likeadmin团队 版权所有 拥有最终解释权
  11. // +----------------------------------------------------------------------
  12. // | author: likeadminTeam
  13. // +----------------------------------------------------------------------
  14. namespace app\adminapi\logic\dept;
  15. use app\common\enum\YesNoEnum;
  16. use app\common\logic\BaseLogic;
  17. use app\common\model\dept\Dept;
  18. /**
  19. * 部门管理逻辑
  20. * Class DeptLogic
  21. * @package app\adminapi\logic\dept
  22. */
  23. class DeptLogic extends BaseLogic
  24. {
  25. /**
  26. * @notes 部门列表
  27. * @param $params
  28. * @return array
  29. * @throws \think\db\exception\DataNotFoundException
  30. * @throws \think\db\exception\DbException
  31. * @throws \think\db\exception\ModelNotFoundException
  32. * @author 段誉
  33. * @date 2022/5/30 15:44
  34. */
  35. public static function lists($params)
  36. {
  37. $where = [];
  38. if (!empty($params['name'])) {
  39. $where[] = ['name', 'like', '%' . $params['name'] . '%'];
  40. }
  41. if (isset($params['status']) && $params['status'] != '') {
  42. $where[] = ['status', '=', $params['status']];
  43. }
  44. $lists = Dept::where($where)
  45. ->append(['status_desc'])
  46. ->order(['sort' => 'desc', 'id' => 'desc'])
  47. ->select()
  48. ->toArray();
  49. $pid = 0;
  50. if (!empty($lists)) {
  51. $pid = min(array_column($lists, 'pid'));
  52. }
  53. return self::getTree($lists, $pid);
  54. }
  55. /**
  56. * @notes 列表树状结构
  57. * @param $array
  58. * @param int $pid
  59. * @param int $level
  60. * @return array
  61. * @author 段誉
  62. * @date 2022/5/30 15:44
  63. */
  64. public static function getTree($array, $pid = 0, $level = 0)
  65. {
  66. $list = [];
  67. foreach ($array as $key => $item) {
  68. if ($item['pid'] == $pid) {
  69. $item['level'] = $level;
  70. $item['children'] = self::getTree($array, $item['id'], $level + 1);
  71. $list[] = $item;
  72. }
  73. }
  74. return $list;
  75. }
  76. /**
  77. * @notes 上级部门
  78. * @return array
  79. * @throws \think\db\exception\DataNotFoundException
  80. * @throws \think\db\exception\DbException
  81. * @throws \think\db\exception\ModelNotFoundException
  82. * @author 段誉
  83. * @date 2022/5/26 18:36
  84. */
  85. public static function leaderDept()
  86. {
  87. $lists = Dept::field(['id', 'name'])->where(['status' => 1])
  88. ->order(['sort' => 'desc', 'id' => 'desc'])
  89. ->select()
  90. ->toArray();
  91. return $lists;
  92. }
  93. /**
  94. * @notes 添加部门
  95. * @param array $params
  96. * @author 段誉
  97. * @date 2022/5/25 18:20
  98. */
  99. public static function add(array $params)
  100. {
  101. Dept::create([
  102. 'pid' => $params['pid'],
  103. 'name' => $params['name'],
  104. 'leader' => $params['leader'] ?? '',
  105. 'mobile' => $params['mobile'] ?? '',
  106. 'status' => $params['status'],
  107. 'sort' => $params['sort'] ?? 0
  108. ]);
  109. }
  110. /**
  111. * @notes 编辑部门
  112. * @param array $params
  113. * @return bool
  114. * @author 段誉
  115. * @date 2022/5/25 18:39
  116. */
  117. public static function edit(array $params): bool
  118. {
  119. try {
  120. $pid = $params['pid'];
  121. $oldDeptData = Dept::findOrEmpty($params['id']);
  122. if ($oldDeptData['pid'] == 0) {
  123. $pid = 0;
  124. }
  125. Dept::update([
  126. 'id' => $params['id'],
  127. 'pid' => $pid,
  128. 'name' => $params['name'],
  129. 'leader' => $params['leader'] ?? '',
  130. 'mobile' => $params['mobile'] ?? '',
  131. 'status' => $params['status'],
  132. 'sort' => $params['sort'] ?? 0
  133. ]);
  134. return true;
  135. } catch (\Exception $e) {
  136. self::setError($e->getMessage());
  137. return false;
  138. }
  139. }
  140. /**
  141. * @notes 删除部门
  142. * @param array $params
  143. * @author 段誉
  144. * @date 2022/5/25 18:40
  145. */
  146. public static function delete(array $params)
  147. {
  148. Dept::destroy($params['id']);
  149. }
  150. /**
  151. * @notes 获取部门详情
  152. * @param $params
  153. * @return array
  154. * @author 段誉
  155. * @date 2022/5/25 18:40
  156. */
  157. public static function detail($params): array
  158. {
  159. return Dept::findOrEmpty($params['id'])->toArray();
  160. }
  161. /**
  162. * @notes 部门数据
  163. * @return array
  164. * @throws \think\db\exception\DataNotFoundException
  165. * @throws \think\db\exception\DbException
  166. * @throws \think\db\exception\ModelNotFoundException
  167. * @author 段誉
  168. * @date 2022/10/13 10:19
  169. */
  170. public static function getAllData()
  171. {
  172. $data = Dept::where(['status' => YesNoEnum::YES])
  173. ->order(['sort' => 'desc', 'id' => 'desc'])
  174. ->select()
  175. ->toArray();
  176. $pid = min(array_column($data, 'pid'));
  177. return self::getTree($data, $pid);
  178. }
  179. }