Department.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <?php
  2. namespace app\admin\controller\qingdong\department;
  3. use app\admin\controller\qingdong\Base;
  4. use addons\qingdong\model\StaffDepartment;
  5. use app\common\controller\Backend;
  6. use fast\Tree;
  7. /**
  8. * 部门列表
  9. */
  10. class Department extends Backend {
  11. public function _initialize() {
  12. parent::_initialize();
  13. $this->model = new StaffDepartment();
  14. // 必须将结果集转换为数组
  15. $ruleList = collection($this->model->where([])->order('id', 'desc')->select())->toArray();
  16. foreach ($ruleList as $k => &$v) {
  17. $v['name'] = __($v['name']);
  18. }
  19. unset($v);
  20. Tree::instance()->init($ruleList);
  21. $this->rulelist = Tree::instance()->getTreeList(Tree::instance()->getTreeArray(0), 'name');
  22. $ruledata = [0 => __('None')];
  23. foreach ($this->rulelist as $k => &$v) {
  24. $ruledata[$v['id']] = $v['name'];
  25. }
  26. $this->view->assign('department', $ruledata);
  27. }
  28. /**
  29. * 部门列表
  30. */
  31. public function index() {
  32. $this->request->filter(['strip_tags']);
  33. if ($this->request->isAjax()) {
  34. $list = $this->rulelist;
  35. $total = count($this->rulelist);
  36. $result = array("total" => $total, "rows" => $list);
  37. return json($result);
  38. }
  39. return $this->view->fetch();
  40. }
  41. /**
  42. * 添加部门
  43. */
  44. public function add() {
  45. if ($this->request->isAjax()) {
  46. $data = $this->request->post('row/a');
  47. $result = StaffDepartment::create($data);
  48. if (!$result) {
  49. $this->error('提交失败');
  50. }
  51. $this->success('提交成功');
  52. }
  53. return $this->view->fetch();
  54. }
  55. /**
  56. * 修改部门
  57. */
  58. public function edit($ids = null) {
  59. $map['id'] = $ids;
  60. if ($this->request->isAjax()) {
  61. $data = $this->request->post('row/a');
  62. $result = StaffDepartment::update($data, $map);
  63. if (!$result) {
  64. $this->error('修改失败');
  65. }
  66. $this->success('修改成功');
  67. }
  68. $data = StaffDepartment::where($map)->find();
  69. $this->view->assign("row", $data);
  70. return $this->view->fetch();
  71. }
  72. /**
  73. * 删除部门
  74. */
  75. public function del($ids = null) {
  76. if ($this->request->isAjax()) {
  77. //删除子部门
  78. $where=['pid' => array('in', $ids)];
  79. $result = StaffDepartment::destroy($where);
  80. $map['id'] = array('in', $ids);
  81. $result = StaffDepartment::destroy($map);
  82. if (!$result) {
  83. $this->error('删除失败');
  84. }
  85. $this->success('删除成功');
  86. }
  87. return $this->view->fetch();
  88. }
  89. }