| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202 |
- <?php
- // +----------------------------------------------------------------------
- // | likeadmin快速开发前后端分离管理后台(PHP版)
- // +----------------------------------------------------------------------
- // | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
- // | 开源版本可自由商用,可去除界面版权logo
- // | gitee下载:https://gitee.com/likeshop_gitee/likeadmin
- // | github下载:https://github.com/likeshop-github/likeadmin
- // | 访问官网:https://www.likeadmin.cn
- // | likeadmin团队 版权所有 拥有最终解释权
- // +----------------------------------------------------------------------
- // | author: likeadminTeam
- // +----------------------------------------------------------------------
- namespace app\adminapi\logic\dept;
- use app\common\enum\YesNoEnum;
- use app\common\logic\BaseLogic;
- use app\common\model\dept\Dept;
- /**
- * 部门管理逻辑
- * Class DeptLogic
- * @package app\adminapi\logic\dept
- */
- class DeptLogic extends BaseLogic
- {
- /**
- * @notes 部门列表
- * @param $params
- * @return array
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- * @author 段誉
- * @date 2022/5/30 15:44
- */
- public static function lists($params)
- {
- $where = [];
- if (!empty($params['name'])) {
- $where[] = ['name', 'like', '%' . $params['name'] . '%'];
- }
- if (isset($params['status']) && $params['status'] != '') {
- $where[] = ['status', '=', $params['status']];
- }
- $lists = Dept::where($where)
- ->append(['status_desc'])
- ->order(['sort' => 'desc', 'id' => 'desc'])
- ->select()
- ->toArray();
- $pid = 0;
- if (!empty($lists)) {
- $pid = min(array_column($lists, 'pid'));
- }
- return self::getTree($lists, $pid);
- }
- /**
- * @notes 列表树状结构
- * @param $array
- * @param int $pid
- * @param int $level
- * @return array
- * @author 段誉
- * @date 2022/5/30 15:44
- */
- public static function getTree($array, $pid = 0, $level = 0)
- {
- $list = [];
- foreach ($array as $key => $item) {
- if ($item['pid'] == $pid) {
- $item['level'] = $level;
- $item['children'] = self::getTree($array, $item['id'], $level + 1);
- $list[] = $item;
- }
- }
- return $list;
- }
- /**
- * @notes 上级部门
- * @return array
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- * @author 段誉
- * @date 2022/5/26 18:36
- */
- public static function leaderDept()
- {
- $lists = Dept::field(['id', 'name'])->where(['status' => 1])
- ->order(['sort' => 'desc', 'id' => 'desc'])
- ->select()
- ->toArray();
- return $lists;
- }
- /**
- * @notes 添加部门
- * @param array $params
- * @author 段誉
- * @date 2022/5/25 18:20
- */
- public static function add(array $params)
- {
- Dept::create([
- 'pid' => $params['pid'],
- 'name' => $params['name'],
- 'leader' => $params['leader'] ?? '',
- 'mobile' => $params['mobile'] ?? '',
- 'status' => $params['status'],
- 'sort' => $params['sort'] ?? 0
- ]);
- }
- /**
- * @notes 编辑部门
- * @param array $params
- * @return bool
- * @author 段誉
- * @date 2022/5/25 18:39
- */
- public static function edit(array $params): bool
- {
- try {
- $pid = $params['pid'];
- $oldDeptData = Dept::findOrEmpty($params['id']);
- if ($oldDeptData['pid'] == 0) {
- $pid = 0;
- }
- Dept::update([
- 'id' => $params['id'],
- 'pid' => $pid,
- 'name' => $params['name'],
- 'leader' => $params['leader'] ?? '',
- 'mobile' => $params['mobile'] ?? '',
- 'status' => $params['status'],
- 'sort' => $params['sort'] ?? 0
- ]);
- return true;
- } catch (\Exception $e) {
- self::setError($e->getMessage());
- return false;
- }
- }
- /**
- * @notes 删除部门
- * @param array $params
- * @author 段誉
- * @date 2022/5/25 18:40
- */
- public static function delete(array $params)
- {
- Dept::destroy($params['id']);
- }
- /**
- * @notes 获取部门详情
- * @param $params
- * @return array
- * @author 段誉
- * @date 2022/5/25 18:40
- */
- public static function detail($params): array
- {
- return Dept::findOrEmpty($params['id'])->toArray();
- }
- /**
- * @notes 部门数据
- * @return array
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- * @author 段誉
- * @date 2022/10/13 10:19
- */
- public static function getAllData()
- {
- $data = Dept::where(['status' => YesNoEnum::YES])
- ->order(['sort' => 'desc', 'id' => 'desc'])
- ->select()
- ->toArray();
- $pid = min(array_column($data, 'pid'));
- return self::getTree($data, $pid);
- }
- }
|