Csmadmindepart.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. <?php
  2. namespace app\admin\controller\csmadmin;
  3. use app\admin\controller\auth\Admin;
  4. use addons\csmadmin\library\CsmUtils;
  5. use addons\csmadmin\library\CsmContants;
  6. /**
  7. * 部门管理
  8. *
  9. * @icon fa fa-circle-o
  10. */
  11. class Csmadmindepart extends Admin
  12. {
  13. /**
  14. * Depart模型对象
  15. *
  16. * @var \app\admin\model\csmadmin\Depart
  17. */
  18. protected $model = null;
  19. public function _initialize()
  20. {
  21. parent::_initialize();
  22. $this->model = new \app\admin\model\csmadmin\Depart();
  23. $this->view->assign("fromsysList", $this->model->getFromsysList());
  24. $this->view->assign("statusList", $this->model->getStatusList());
  25. }
  26. /**
  27. * 部门和部门人员维护
  28. * http://127.0.0.1/fastadmin_plugin_csmmeet/public/q3HJDu2RgE.php/csmadmin/csmadmindepart/admindepart
  29. *
  30. * @return string
  31. */
  32. public function admindepart()
  33. {
  34. $config = get_addon_config(CsmContants::$ADDONS);
  35. if ($config['canadmindepratmng'] == 'N') {
  36. CsmUtils::error('您未开启【按组织管理】设置,本功能无法使用', null, null, 100);
  37. }
  38. $this->request->filter([
  39. 'strip_tags'
  40. ]);
  41. // 显示树
  42. // $this->assignconfig('treedata', $this->_getdeparttree());
  43. return $this->view->fetch();
  44. }
  45. public function getdeparttreeAjax()
  46. {
  47. $dao = new \app\admin\model\csmadmin\Depart();
  48. $list = $dao->where('status', '=', 'normal')->order("weigh", 'asc')->order("id", 'asc')->select();
  49. $treedata = [];
  50. foreach ($list as $v) {
  51. $treedata[] = [
  52. 'id' => $v->id,
  53. 'parent' => $v->parent_id ? $v->parent_id : '#',
  54. 'text' => $v->name
  55. ];
  56. }
  57. return json($treedata);
  58. }
  59. /**
  60. * 根据部门id获取人员ids,用于点击部门查询人员
  61. */
  62. public function getUsersByDepartsAjax()
  63. {
  64. $departids = $this->request->request('departids');
  65. if (empty($departids)) {
  66. $this->success('', null, array('userids' => ''));
  67. }
  68. $dao = new \app\admin\model\Admin();
  69. $sql = $dao->alias('t1')
  70. ->field('t1.id faadmin_id')
  71. ->where("t1.status", '=', 'normal')
  72. ->where("t2.status", '=', 'normal')
  73. ->join('csmadmin_depart2user t2', 't1.id=t2.faadmin_id', 'left');
  74. if ($departids != null && $departids != '') {
  75. $sql->where("t2.csmadmin_depart_id", 'in', $departids);
  76. }
  77. $list = $sql->select();
  78. $userids = '-1';
  79. foreach ($list as $item) {
  80. $userids .= ',' . $item->faadmin_id;
  81. }
  82. $this->success('', null, array(
  83. 'userids' => $userids
  84. ));
  85. }
  86. /**
  87. * 将人员从部门中移除
  88. */
  89. public function removeUserFromDepartAjax()
  90. {
  91. $csmadmin_depart_id = $this->request->request('csmadmin_depart_id');
  92. $faadmin_id = $this->request->request('faadmin_id');
  93. $dao = new \app\admin\model\csmadmin\Depart2user();
  94. $param = [
  95. 'status' => 'hidden',
  96. 'updatetime' => time()
  97. ];
  98. $dao->where('csmadmin_depart_id', '=', $csmadmin_depart_id)
  99. ->where('faadmin_id', '=', $faadmin_id)
  100. ->where('status', '=', 'normal')
  101. ->update($param);
  102. $this->success();
  103. }
  104. /**
  105. * 将人员加入到部门
  106. */
  107. public function selectuser()
  108. {
  109. if ($this->request->isPost()) {
  110. $csmadmin_depart_id = $this->request->request('csmadmin_depart_id');
  111. $faadmin_id = $this->request->request('faadmin_id');
  112. $dao = new \app\admin\model\csmadmin\Depart2user();
  113. $row = $dao->where('csmadmin_depart_id', '=', $csmadmin_depart_id)
  114. ->where('faadmin_id', '=', $faadmin_id)
  115. ->where('status', '=', 'normal')
  116. ->find();
  117. if ($row != null) {
  118. $this->error("用户已经在该组织中,添加失败");
  119. } else {
  120. $param = [
  121. "csmadmin_depart_id" => $csmadmin_depart_id,
  122. "faadmin_id" => $faadmin_id,
  123. "createtime" => time()
  124. ];
  125. $dao->create($param);
  126. $this->success();
  127. }
  128. }
  129. return $this->view->fetch();
  130. }
  131. /**
  132. * 部门维护
  133. */
  134. public function insertdepart()
  135. {
  136. if ($this->request->isPost()) {
  137. $parent_id = $this->request->request('parent_id');
  138. $departname = $this->request->request('departname');
  139. $dao = new \app\admin\model\csmadmin\Depart();
  140. $param = [
  141. "name" => $departname,
  142. "createtime" => time(),
  143. "weigh" => '100',
  144. ];
  145. if ($parent_id == null || $parent_id == '' || $parent_id == '0') {
  146. //根节点
  147. $param['parent_id'] = 0;
  148. } else {
  149. //非根节点
  150. //获取父节点的根节点
  151. $parentrow = $dao->where('id', '=', $parent_id)->find();
  152. $root_id = 0;
  153. if ($parentrow && $parentrow->parent_id == 0) {
  154. $root_id = $parentrow->id;
  155. } else if ($parentrow) {
  156. $root_id = $parentrow->root_id;
  157. }
  158. $param['parent_id'] = $parent_id;
  159. $param['root_id'] = $root_id;
  160. }
  161. $row = $dao->create($param);
  162. $id = $row->id;
  163. if ($parent_id == null || $parent_id == '' || $parent_id == '0') {
  164. //根节点
  165. $dao->where('id', '=', $id)->update(['root_id' => $id]);
  166. }
  167. $this->success();
  168. }
  169. $csmadmin_depart_id = $this->request->request('csmadmin_depart_id');
  170. $this->assign("csmadmin_depart_id", $csmadmin_depart_id);
  171. return $this->view->fetch();
  172. }
  173. public function deldepartajax()
  174. {
  175. $csmadmin_depart_id = $this->request->request('csmadmin_depart_id');
  176. $dao = new \app\admin\model\csmadmin\Depart();
  177. $row = $dao->where('parent_id', '=', $csmadmin_depart_id)
  178. ->where('status', '=', 'normal')
  179. ->find();
  180. if ($row) {
  181. $this->error("该组织有子组织,无法删除");
  182. }
  183. $dao2 = new \app\admin\model\csmadmin\Depart2user();
  184. $row = $dao2->alias('t')->where('csmadmin_depart_id', '=', $csmadmin_depart_id)
  185. ->join('admin a', "t.faadmin_id=a.id and a.status='normal'")
  186. ->where('t.status', '=', 'normal')
  187. ->find();
  188. //echo $dao2->getLastSql();
  189. if ($row) {
  190. $this->error("该组织下有员工,无法删除");
  191. }
  192. $dao->where('id', '=', $csmadmin_depart_id)->delete();
  193. $dao2 = new \app\admin\model\csmadmin\Group2depart();
  194. $dao2->where("csmadmin_depart_id", "=", $csmadmin_depart_id)->delete();
  195. $this->success();
  196. }
  197. public function updatedepart()
  198. {
  199. $csmadmin_depart_id = $this->request->request('csmadmin_depart_id');
  200. $dao = new \app\admin\model\csmadmin\Depart();
  201. $row = $dao->where('id', '=', $csmadmin_depart_id)
  202. ->where('status', '=', 'normal')
  203. ->find();
  204. if ($this->request->isPost()) {
  205. $departname = $this->request->request('departname');
  206. $weigh = $this->request->request('weigh');
  207. $parent_id = $this->request->request('parent_id');
  208. $root_id = $row->root_id;
  209. if ($parent_id != $row->parent_id) {
  210. //如果移动节点,则需要更新当前节点和所有子节点的root_id
  211. $parentrow = $dao->where('id', '=', $parent_id)->find();
  212. if ($parentrow) {
  213. $root_id = $parentrow->root_id;
  214. }
  215. $this->updateSubDepartRootId($dao, array($row->id), $root_id);
  216. }
  217. $param = [
  218. 'parent_id' => $parent_id,
  219. "root_id" => $root_id,
  220. 'weigh' => $weigh,
  221. 'name' => $departname,
  222. 'updatetime' => time()
  223. ];
  224. $row->where('id', '=', $row->id)->update($param);
  225. $this->success();
  226. }
  227. $rowjson = json_decode($row, true);
  228. $this->assign("row", $rowjson ? $rowjson : []);
  229. $this->assign("csmadmin_depart_id", $csmadmin_depart_id);
  230. return $this->view->fetch();
  231. }
  232. /**
  233. * 更新子节点的root_id
  234. */
  235. private function updateSubDepartRootId($dao, $depart_ids, $root_id)
  236. {
  237. $param = [
  238. "root_id" => $root_id,
  239. 'updatetime' => time()
  240. ];
  241. $dao->where('parent_id', 'in', $depart_ids)->update($param);
  242. $list = $dao->where('parent_id', 'in', $depart_ids)->select();
  243. $subdepart_ids = [];
  244. foreach ($list as $item) {
  245. $subdepart_ids[] = $item->id;
  246. }
  247. if (count($subdepart_ids) > 0) {
  248. $this->updateSubDepartRootId($dao, $subdepart_ids, $root_id);
  249. }
  250. }
  251. }