Position.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2021 勾股工作室
  4. * @license https://opensource.org/licenses/GPL-3.0
  5. * @link https://www.gougucms.com
  6. */
  7. declare (strict_types = 1);
  8. namespace app\admin\controller;
  9. use app\admin\BaseController;
  10. use app\admin\validate\PositionCheck;
  11. use think\exception\ValidateException;
  12. use think\facade\Db;
  13. use think\facade\View;
  14. use app\admin\model\Department as DepartmentModel;
  15. use app\admin\model\Position as PositionModel;
  16. class Position extends BaseController
  17. {
  18. protected $Departmentmodel;
  19. protected $Positionmodel;
  20. public function initialize(){
  21. parent::initialize();
  22. $this->Departmentmodel = new DepartmentModel;
  23. $this->Positionmodel = new PositionModel;
  24. }
  25. public function index()
  26. {
  27. if (request()->isAjax()) {
  28. $list = $this->Departmentmodel->with('position')->where('pid', 0)->select();
  29. $data = array();
  30. $length = 0;
  31. for($i = 0;$i < count($list); $i++){
  32. $data[$length++] = [
  33. 'd_id' => $list[$i]['id'],
  34. 'id' => -1,
  35. 'title' => $list[$i]['title'],
  36. 'did' => 0,
  37. 'remark' => $list[$i]['remark'],
  38. 'status' => $list[$i]['status'],
  39. ];
  40. $position = $list[$i]['position'];
  41. for($j = 0;$j < count($position); $j++){
  42. $data[$length++] = [
  43. 'd_id' => -1,
  44. 'id' => $position[$j]['id'],
  45. 'title' => $position[$j]['title'],
  46. 'did' => $position[$j]['did'],
  47. 'remark' => $position[$j]['remark'],
  48. 'status' => $position[$j]['status'],
  49. ];
  50. }
  51. }
  52. // halt($data);
  53. return to_assign(0, '', $data);
  54. } else {
  55. return view();
  56. }
  57. }
  58. //添加&编辑
  59. public function add()
  60. {
  61. $param = get_params();
  62. if (request()->isAjax()) {
  63. if (!empty($param['id']) && $param['id'] > 0) {
  64. try {
  65. validate(PositionCheck::class)->scene('edit')->check($param);
  66. } catch (ValidateException $e) {
  67. // 验证失败 输出错误信息
  68. return to_assign(1, $e->getError());
  69. }
  70. $res = Db::name('Position')->where(['id' => $param['id']])->strict(false)->field(true)->update($param);
  71. if($res!==false){
  72. add_log('edit', $param['id'], $param);
  73. return to_assign();
  74. }
  75. else{
  76. return to_assign(1, '提交失败');
  77. }
  78. } else {
  79. try {
  80. validate(PositionCheck::class)->scene('add')->check($param);
  81. } catch (ValidateException $e) {
  82. // 验证失败 输出错误信息
  83. return to_assign(1, $e->getError());
  84. }
  85. $pid = Db::name('Position')->strict(false)->field(true)->insertGetId($param);
  86. if($pid>0){
  87. add_log('add', $pid, $param);
  88. return to_assign();
  89. }
  90. else{
  91. return to_assign(1, '提交失败');
  92. }
  93. }
  94. }
  95. else{
  96. $id = isset($param['id']) ? $param['id'] : 0;
  97. if ($id > 0) {
  98. $detail = Db::name('Position')->where(['id' => $id])->find();
  99. View::assign('detail', $detail);
  100. }
  101. View::assign('id', $id);
  102. return view();
  103. }
  104. }
  105. //删除
  106. public function delete()
  107. {
  108. $id = get_params("id");
  109. if ($id == 1) {
  110. return to_assign(0, "超级岗位,不能删除");
  111. }
  112. $data['status'] = '-1';
  113. $data['id'] = $id;
  114. $data['update_time'] = time();
  115. if (Db::name('Position')->update($data) !== false) {
  116. add_log('delete', $id);
  117. return to_assign(0, "删除岗位成功");
  118. } else {
  119. return to_assign(1, "删除失败");
  120. }
  121. }
  122. }