123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164 |
- <?php
- /**
- * @copyright Copyright (c) 2021 勾股工作室
- * @license https://opensource.org/licenses/GPL-3.0
- * @link https://www.gougucms.com
- */
- declare (strict_types = 1);
- namespace app\admin\controller;
- use app\admin\BaseController;
- use app\admin\validate\DepartmentCheck;
- use think\exception\ValidateException;
- use think\facade\Db;
- use think\facade\View;
- use think\facade\Session;
- class Department extends BaseController
- {
- public function index()
- {
- if (request()->isAjax()) {
- $unit_name = get_login_admin('unit_name');
- $admin_permission = get_login_admin('permission');
- $where = array();
- $where[] = ["d.status","=",1];
- $where[] = ["d.delete_time","=",0];
- if($admin_permission == 0){
- $where[] = ['d.unit_name', '=',$unit_name];
- }
- $list = Db::name('Department')
- ->where($where)
- // ->where('d.unit_name', $unit_name)
- ->field('d.*,a.nickname as leader')
- ->alias('d')
- ->join('Admin a', 'a.id = d.leader_id', 'LEFT')
- ->order('d.id asc')
- ->select();
- // halt($list);
- return to_assign(0, '', $list);
- } else {
- return view();
- }
- }
- //添加部门
- public function add()
- {
- $param = get_params();
-
- if (request()->isAjax()) {
- // halt($param);
- if ($param['id'] > 0) { //编辑已有部门
- try {
- validate(DepartmentCheck::class)->scene('edit')->check($param);
- } catch (ValidateException $e) {
- // 验证失败 输出错误信息
- return to_assign(1, $e->getError());
- }
- $param['update_time'] = time();
- $department_array = get_department_son($param['id']);
- if (in_array($param['pid'], $department_array)) {
- return to_assign(1, '上级部门不能是该部门本身或其下属部门');
- } else {
- Db::name('Department')->strict(false)->field(true)->update($param);
- add_log('edit', $param['id'], $param);
- return to_assign(0, "编辑成功!");
- }
-
- } else { //新增部门
- try {
- validate(DepartmentCheck::class)->scene('add')->check($param);
- } catch (ValidateException $e) {
- // 验证失败 输出错误信息
- return to_assign(1, $e->getError());
- }
- if(get_login_admin('permission') != 1){
- if($param['pid'] == 0){
- return to_assign(1, '不可创建顶级部门');
- }
- }
- $did = Db::name('Department')->strict(false)->field(true)->insertGetId($param);
- $pid = $did;
- while($pid != 0){
- $value = Db::name('department')->where('id', $pid)->column('id,pid,title')[0];
- $unit_name = $value['id'];
- $pid = $value['pid'];
- $title = $value['title'];
- }
- $unit_name = [
- 'unit_name' => $unit_name,
- ];
- Db::name('Department')->where('id', $did)->update($unit_name);
- add_log('add', $did, $param);
- return to_assign(0,'操作成功');
- }
- } else {
- $id = isset($param['id']) ? $param['id'] : 0;
- $pid = isset($param['pid']) ? $param['pid'] : 0;
-
- $permission = get_login_admin('permission');
- if ($id > 0) { //编辑
- $detail = Db::name('Department')->where(['id' => $id])->find();
- $users = Db::name('Admin')->where(['did' => $id, 'status' => 1])->select();
- // $d_type = Db::name('Department')->where('id', $id)->value('type');
-
- // View::assign('d_type', $d_type);
- View::assign('users', $users);
- View::assign('detail', $detail);
- }
- // else{
- // $admin = get_login_admin();
- // View::assign('admin', $admin);
- // }
- if($permission == 1){
- $d_type = Db::name('Department')->where('id', $id)->value('type');
- $department = set_recursion(get_department());
- View::assign('d_type', $d_type);
- }else{
- $department = Db::name('Department')->where(['status' => 1])->where('unit_name', '=', get_login_admin('unit_name'))->select()->toArray();
- $department = set_recursion($department);
- }
- View::assign('permission', $permission);
- View::assign('department', $department);
- View::assign('pid', $pid);
- View::assign('id', $id);
- return view();
- }
- }
- //删除
- public function delete()
- {
- $id = get_params("id");
- $count = Db::name('Department')->where([['pid', '=', $id], ['status', '>=', 0]])->count();
- if ($count > 0) {
- return to_assign(1, "该部门下还有子部门,无法删除");
- }
- $users = Db::name('Admin')->where([['did', '=', $id], ['status', '>=', 0]])->count();
- if ($users > 0) {
- return to_assign(1, "该部门下还有员工,无法删除");
- }
- if (Db::name('Department')->delete($id) !== false) {
- add_log('delete', $id);
- return to_assign(0, "删除部门成功");
- } else {
- return to_assign(1, "删除失败");
- }
- }
- }
|