Newfill.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. <?php
  2. namespace app\bug\controller;
  3. use app\bug\model\CompanyItem;
  4. use think\App;
  5. use think\facade\Validate;
  6. class Newfill extends Base
  7. {
  8. public function __construct(App $app)
  9. {
  10. parent::__construct($app);
  11. }
  12. public function list()
  13. {
  14. $param = $this->request->param(["pid"=>0,"name"=>'',"nickname"=>""],"post","trim");
  15. $condition=[];
  16. $depart []=['is_del',"=",0];
  17. $condition[] = ['is_del',"=",0];
  18. if($param['pid']!==""){
  19. $condition[]=["pid","=",$param['pid']];
  20. }
  21. if($param['name']!=""){
  22. $condition[]=["name","like","%{$param['name']}%"];
  23. }
  24. if($param['nickname']!=""){
  25. $is= \app\bug\model\DepartUser::where([['nickname','like',"%{$param['nickname']}%"]])->column('itemid');
  26. $condition[]=["id","in",$is];
  27. }
  28. $pidlist = CompanyItem::where($condition)->column("pid");
  29. if(!empty($pidlist)){
  30. $depart[]=[["itemid","in",$pidlist]];
  31. }else{
  32. if($param['pid']===""){
  33. $depart[]=['itemid','=',0];
  34. }else{
  35. $depart[]=['itemid','=',$param['pid']];
  36. }
  37. }
  38. $item = \app\bug\model\DepartUser::where($depart)->select();
  39. $list = CompanyItem::where($condition)->select();
  40. return success("获取成功",["depart"=>$list,"item"=>$item]);
  41. }
  42. /*状态*/
  43. public function stat(){
  44. $id = $this->request->post("id/d");
  45. $s = CompanyItem::where(["id"=>$id])->findOrEmpty();
  46. if( $s->isEmpty()){
  47. return error("未找到部门");
  48. }
  49. $s->status= $s['status']==0? '1':'0';
  50. $s->save();
  51. return success("更新成功");
  52. }
  53. /*职位*/
  54. public function userp(){
  55. $post = $this->request->post(['id'=>"","position"=>""],"post","trim");
  56. $valid=Validate::rule([
  57. 'id|主键id'=>'number|gt:0',
  58. 'position|职位'=>'require|number|in:1,2',
  59. ]);
  60. if($valid->check($post)==false) return error($valid->getError());
  61. $t = \app\bug\model\DepartUser::where(["id"=>$post['id'],"is_del"=>0])->findOrEmpty();
  62. if($t->isEmpty())return error("未找到用户");
  63. if($post['position']==2){
  64. $f = \app\bug\model\DepartUser::where([['itemid',"=",$t['itemid']],["is_del",'=',0],['id','<>',$post['id']],["position",'=',2]])
  65. ->findOrEmpty();
  66. if($f->isEmpty()==false){
  67. $f->position=1;
  68. $f->save();
  69. }
  70. }
  71. $t->position=$post['position'];
  72. $t->save();
  73. return success('更新成功');
  74. }
  75. public function add(){
  76. $param = $this->request->param(["name"=>"","pid"=>0,"weight"=>1],"post","trim");
  77. $valid = Validate::rule([
  78. "name|部门名称"=>"require|max:255|unique:company_item,name^is_del",
  79. "pid|父级id"=>"require|number|egt:0",
  80. "weight|排序权重"=>"require|number|egt:0"
  81. ]);
  82. if($valid->check($param)==false)return error($valid->getError());
  83. $depart_link="";
  84. if($param['pid']!=0){
  85. $parent = CompanyItem::where("id",$param['pid'])->findOrEmpty();
  86. if($parent->isEmpty()) return error("父级数据不存在");
  87. $depart_link=$parent->depart_link;
  88. }
  89. $level = count(array_filter(explode("-",$depart_link)))+1;
  90. CompanyItem::startTrans();
  91. try{
  92. $data=[
  93. "name"=>$param['name'],
  94. "pid"=>$param['pid'],
  95. "level"=>$level,
  96. "weight"=>$param['weight'],
  97. "depart_link"=>$depart_link. CompanyItem::getLastInsID()."-"
  98. ];
  99. CompanyItem::create($data);
  100. CompanyItem::commit();
  101. return success("创建成功");
  102. }catch (\Exception $e){
  103. CompanyItem::rollback();
  104. return error($e->getMessage());
  105. }
  106. }
  107. public function refresh(){
  108. $param= $this->request->param(["id"=>"","pid"=>0,"weight"=>"","name"=>""],"post","trim");
  109. $valid=Validate::rule([
  110. 'id|部门主键id'=>'require|number|gt:0',
  111. 'name|部门名称'=>"require|max:255|unique:company_item,name,{$param['id']}",
  112. 'weight|权重'=>'max:255',
  113. 'pid|父级id'=>'require|number',
  114. ]);
  115. if($valid->check($param)==false) return error($valid->getError());
  116. $items = CompanyItem::where(['id'=>$param['id'],'is_del'=>0])->findOrEmpty();
  117. if($items->isEmpty())return error("部门信息不存在");
  118. $spid=['depart_link'=>""];
  119. if($param['pid']!=0){
  120. $spid = CompanyItem::where(['id'=>$param['pid'],'is_del'=>0])->findOrEmpty();
  121. if($spid->isEmpty()) return error("父级数据不能为空");
  122. }
  123. $depart_link=$spid['depart_link']."{$items->id}-";
  124. $itemlevel =explode('-',$depart_link);
  125. $itemlevel = array_filter($itemlevel);
  126. $itemlevel= count($itemlevel);
  127. $items->name=$param['name'];
  128. $items->pid=$param['pid'];
  129. $items->weight=$param['weight'];
  130. $items->depart_link=$depart_link;
  131. $items->level=$itemlevel;
  132. $re= $items->save();
  133. return $re ? success("信息更新成功"): error("信息更新失败");
  134. }
  135. /*查询*/
  136. public function query(){
  137. return success("获取成功",(new CompanyItem())->GetTree());
  138. }
  139. public function itemdel(){
  140. $id = $this->request->post("id/d");
  141. $items =CompanyItem::where(['is_del'=>0,'id'=>$id])->findOrEmpty();
  142. if($items->isEmpty())return error("部门信息不存在");
  143. $items->is_del =1;
  144. $result= $items->save();
  145. return $result ? success("删除成功"): error("删除失败");
  146. }
  147. }