123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284 |
- <?php
- namespace app\admin\controller\csmadmin;
- use app\admin\controller\auth\Admin;
- use addons\csmadmin\library\CsmUtils;
- use addons\csmadmin\library\CsmContants;
- /**
- * 部门管理
- *
- * @icon fa fa-circle-o
- */
- class Csmadmindepart extends Admin
- {
- /**
- * Depart模型对象
- *
- * @var \app\admin\model\csmadmin\Depart
- */
- protected $model = null;
- public function _initialize()
- {
- parent::_initialize();
- $this->model = new \app\admin\model\csmadmin\Depart();
- $this->view->assign("fromsysList", $this->model->getFromsysList());
- $this->view->assign("statusList", $this->model->getStatusList());
- }
- /**
- * 部门和部门人员维护
- * http://127.0.0.1/fastadmin_plugin_csmmeet/public/q3HJDu2RgE.php/csmadmin/csmadmindepart/admindepart
- *
- * @return string
- */
- public function admindepart()
- {
- $config = get_addon_config(CsmContants::$ADDONS);
- if ($config['canadmindepratmng'] == 'N') {
- CsmUtils::error('您未开启【按组织管理】设置,本功能无法使用', null, null, 100);
- }
- $this->request->filter([
- 'strip_tags'
- ]);
- // 显示树
- // $this->assignconfig('treedata', $this->_getdeparttree());
- return $this->view->fetch();
- }
- public function getdeparttreeAjax()
- {
- $dao = new \app\admin\model\csmadmin\Depart();
- $list = $dao->where('status', '=', 'normal')->order("weigh", 'asc')->order("id", 'asc')->select();
- $treedata = [];
- foreach ($list as $v) {
- $treedata[] = [
- 'id' => $v->id,
- 'parent' => $v->parent_id ? $v->parent_id : '#',
- 'text' => $v->name
- ];
- }
- return json($treedata);
- }
- /**
- * 根据部门id获取人员ids,用于点击部门查询人员
- */
- public function getUsersByDepartsAjax()
- {
- $departids = $this->request->request('departids');
- if (empty($departids)) {
- $this->success('', null, array('userids' => ''));
- }
- $dao = new \app\admin\model\Admin();
- $sql = $dao->alias('t1')
- ->field('t1.id faadmin_id')
- ->where("t1.status", '=', 'normal')
- ->where("t2.status", '=', 'normal')
- ->join('csmadmin_depart2user t2', 't1.id=t2.faadmin_id', 'left');
- if ($departids != null && $departids != '') {
- $sql->where("t2.csmadmin_depart_id", 'in', $departids);
- }
- $list = $sql->select();
- $userids = '-1';
- foreach ($list as $item) {
- $userids .= ',' . $item->faadmin_id;
- }
- $this->success('', null, array(
- 'userids' => $userids
- ));
- }
- /**
- * 将人员从部门中移除
- */
- public function removeUserFromDepartAjax()
- {
- $csmadmin_depart_id = $this->request->request('csmadmin_depart_id');
- $faadmin_id = $this->request->request('faadmin_id');
- $dao = new \app\admin\model\csmadmin\Depart2user();
- $param = [
- 'status' => 'hidden',
- 'updatetime' => time()
- ];
- $dao->where('csmadmin_depart_id', '=', $csmadmin_depart_id)
- ->where('faadmin_id', '=', $faadmin_id)
- ->where('status', '=', 'normal')
- ->update($param);
- $this->success();
- }
- /**
- * 将人员加入到部门
- */
- public function selectuser()
- {
- if ($this->request->isPost()) {
- $csmadmin_depart_id = $this->request->request('csmadmin_depart_id');
- $faadmin_id = $this->request->request('faadmin_id');
- $dao = new \app\admin\model\csmadmin\Depart2user();
- $row = $dao->where('csmadmin_depart_id', '=', $csmadmin_depart_id)
- ->where('faadmin_id', '=', $faadmin_id)
- ->where('status', '=', 'normal')
- ->find();
- if ($row != null) {
- $this->error("用户已经在该组织中,添加失败");
- } else {
- $param = [
- "csmadmin_depart_id" => $csmadmin_depart_id,
- "faadmin_id" => $faadmin_id,
- "createtime" => time()
- ];
- $dao->create($param);
- $this->success();
- }
- }
- return $this->view->fetch();
- }
- /**
- * 部门维护
- */
- public function insertdepart()
- {
- if ($this->request->isPost()) {
- $parent_id = $this->request->request('parent_id');
- $departname = $this->request->request('departname');
- $dao = new \app\admin\model\csmadmin\Depart();
- $param = [
- "name" => $departname,
- "createtime" => time(),
- "weigh" => '100',
- ];
- if ($parent_id == null || $parent_id == '' || $parent_id == '0') {
- //根节点
- $param['parent_id'] = 0;
- } else {
- //非根节点
- //获取父节点的根节点
- $parentrow = $dao->where('id', '=', $parent_id)->find();
- $root_id = 0;
- if ($parentrow && $parentrow->parent_id == 0) {
- $root_id = $parentrow->id;
- } else if ($parentrow) {
- $root_id = $parentrow->root_id;
- }
- $param['parent_id'] = $parent_id;
- $param['root_id'] = $root_id;
- }
- $row = $dao->create($param);
- $id = $row->id;
- if ($parent_id == null || $parent_id == '' || $parent_id == '0') {
- //根节点
- $dao->where('id', '=', $id)->update(['root_id' => $id]);
- }
- $this->success();
- }
- $csmadmin_depart_id = $this->request->request('csmadmin_depart_id');
- $this->assign("csmadmin_depart_id", $csmadmin_depart_id);
- return $this->view->fetch();
- }
- public function deldepartajax()
- {
- $csmadmin_depart_id = $this->request->request('csmadmin_depart_id');
- $dao = new \app\admin\model\csmadmin\Depart();
- $row = $dao->where('parent_id', '=', $csmadmin_depart_id)
- ->where('status', '=', 'normal')
- ->find();
- if ($row) {
- $this->error("该组织有子组织,无法删除");
- }
- $dao2 = new \app\admin\model\csmadmin\Depart2user();
- $row = $dao2->alias('t')->where('csmadmin_depart_id', '=', $csmadmin_depart_id)
- ->join('admin a', "t.faadmin_id=a.id and a.status='normal'")
- ->where('t.status', '=', 'normal')
- ->find();
- //echo $dao2->getLastSql();
- if ($row) {
- $this->error("该组织下有员工,无法删除");
- }
- $dao->where('id', '=', $csmadmin_depart_id)->delete();
- $dao2 = new \app\admin\model\csmadmin\Group2depart();
- $dao2->where("csmadmin_depart_id", "=", $csmadmin_depart_id)->delete();
- $this->success();
- }
- public function updatedepart()
- {
- $csmadmin_depart_id = $this->request->request('csmadmin_depart_id');
- $dao = new \app\admin\model\csmadmin\Depart();
- $row = $dao->where('id', '=', $csmadmin_depart_id)
- ->where('status', '=', 'normal')
- ->find();
- if ($this->request->isPost()) {
- $departname = $this->request->request('departname');
- $weigh = $this->request->request('weigh');
- $parent_id = $this->request->request('parent_id');
- $root_id = $row->root_id;
- if ($parent_id != $row->parent_id) {
- //如果移动节点,则需要更新当前节点和所有子节点的root_id
- $parentrow = $dao->where('id', '=', $parent_id)->find();
- if ($parentrow) {
- $root_id = $parentrow->root_id;
- }
- $this->updateSubDepartRootId($dao, array($row->id), $root_id);
- }
- $param = [
- 'parent_id' => $parent_id,
- "root_id" => $root_id,
- 'weigh' => $weigh,
- 'name' => $departname,
- 'updatetime' => time()
- ];
- $row->where('id', '=', $row->id)->update($param);
- $this->success();
- }
- $rowjson = json_decode($row, true);
- $this->assign("row", $rowjson ? $rowjson : []);
- $this->assign("csmadmin_depart_id", $csmadmin_depart_id);
- return $this->view->fetch();
- }
- /**
- * 更新子节点的root_id
- */
- private function updateSubDepartRootId($dao, $depart_ids, $root_id)
- {
- $param = [
- "root_id" => $root_id,
- 'updatetime' => time()
- ];
- $dao->where('parent_id', 'in', $depart_ids)->update($param);
- $list = $dao->where('parent_id', 'in', $depart_ids)->select();
- $subdepart_ids = [];
- foreach ($list as $item) {
- $subdepart_ids[] = $item->id;
- }
- if (count($subdepart_ids) > 0) {
- $this->updateSubDepartRootId($dao, $subdepart_ids, $root_id);
- }
- }
- }
|