123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158 |
- <?php
- namespace app\bug\controller;
- use app\bug\model\CompanyItem;
- use think\App;
- use think\facade\Validate;
- class Newfill extends Base
- {
- public function __construct(App $app)
- {
- parent::__construct($app);
- }
- public function list()
- {
- $param = $this->request->param(["pid"=>0,"name"=>'',"nickname"=>""],"post","trim");
- $condition=[];
- $depart []=['is_del',"=",0];
- $condition[] = ['is_del',"=",0];
- if($param['pid']!==""){
- $condition[]=["pid","=",$param['pid']];
- }
- if($param['name']!=""){
- $condition[]=["name","like","%{$param['name']}%"];
- }
- if($param['nickname']!=""){
- $is= \app\bug\model\DepartUser::where([['nickname','like',"%{$param['nickname']}%"]])->column('itemid');
- $condition[]=["id","in",$is];
- }
- $pidlist = CompanyItem::where($condition)->column("pid");
- if(!empty($pidlist)){
- $depart[]=[["itemid","in",$pidlist]];
- }else{
- if($param['pid']===""){
- $depart[]=['itemid','=',0];
- }else{
- $depart[]=['itemid','=',$param['pid']];
- }
- }
- $item = \app\bug\model\DepartUser::where($depart)->select();
- $list = CompanyItem::where($condition)->select();
- return success("获取成功",["depart"=>$list,"item"=>$item]);
- }
- /*状态*/
- public function stat(){
- $id = $this->request->post("id/d");
- $s = CompanyItem::where(["id"=>$id])->findOrEmpty();
- if( $s->isEmpty()){
- return error("未找到部门");
- }
- $s->status= $s['status']==0? '1':'0';
- $s->save();
- return success("更新成功");
- }
- /*职位*/
- public function userp(){
- $post = $this->request->post(['id'=>"","position"=>""],"post","trim");
- $valid=Validate::rule([
- 'id|主键id'=>'number|gt:0',
- 'position|职位'=>'require|number|in:1,2',
- ]);
- if($valid->check($post)==false) return error($valid->getError());
- $t = \app\bug\model\DepartUser::where(["id"=>$post['id'],"is_del"=>0])->findOrEmpty();
- if($t->isEmpty())return error("未找到用户");
-
- if($post['position']==2){
- $f = \app\bug\model\DepartUser::where([['itemid',"=",$t['itemid']],["is_del",'=',0],['id','<>',$post['id']],["position",'=',2]])
- ->findOrEmpty();
- if($f->isEmpty()==false){
- $f->position=1;
- $f->save();
- }
- }
- $t->position=$post['position'];
- $t->save();
- return success('更新成功');
- }
-
- public function add(){
- $param = $this->request->param(["name"=>"","pid"=>0,"weight"=>1],"post","trim");
- $valid = Validate::rule([
- "name|部门名称"=>"require|max:255|unique:company_item,name^is_del",
- "pid|父级id"=>"require|number|egt:0",
- "weight|排序权重"=>"require|number|egt:0"
- ]);
- if($valid->check($param)==false)return error($valid->getError());
-
- $depart_link="";
- if($param['pid']!=0){
- $parent = CompanyItem::where("id",$param['pid'])->findOrEmpty();
- if($parent->isEmpty()) return error("父级数据不存在");
- $depart_link=$parent->depart_link;
- }
- $level = count(array_filter(explode("-",$depart_link)))+1;
- CompanyItem::startTrans();
- try{
- $data=[
- "name"=>$param['name'],
- "pid"=>$param['pid'],
- "level"=>$level,
- "weight"=>$param['weight'],
- "depart_link"=>$depart_link. CompanyItem::getLastInsID()."-"
- ];
- CompanyItem::create($data);
- CompanyItem::commit();
- return success("创建成功");
- }catch (\Exception $e){
- CompanyItem::rollback();
- return error($e->getMessage());
- }
- }
-
- public function refresh(){
- $param= $this->request->param(["id"=>"","pid"=>0,"weight"=>"","name"=>""],"post","trim");
- $valid=Validate::rule([
- 'id|部门主键id'=>'require|number|gt:0',
- 'name|部门名称'=>"require|max:255|unique:company_item,name,{$param['id']}",
- 'weight|权重'=>'max:255',
- 'pid|父级id'=>'require|number',
- ]);
- if($valid->check($param)==false) return error($valid->getError());
- $items = CompanyItem::where(['id'=>$param['id'],'is_del'=>0])->findOrEmpty();
- if($items->isEmpty())return error("部门信息不存在");
- $spid=['depart_link'=>""];
- if($param['pid']!=0){
- $spid = CompanyItem::where(['id'=>$param['pid'],'is_del'=>0])->findOrEmpty();
- if($spid->isEmpty()) return error("父级数据不能为空");
- }
- $depart_link=$spid['depart_link']."{$items->id}-";
- $itemlevel =explode('-',$depart_link);
- $itemlevel = array_filter($itemlevel);
- $itemlevel= count($itemlevel);
- $items->name=$param['name'];
- $items->pid=$param['pid'];
- $items->weight=$param['weight'];
- $items->depart_link=$depart_link;
- $items->level=$itemlevel;
- $re= $items->save();
- return $re ? success("信息更新成功"): error("信息更新失败");
- }
- /*查询*/
- public function query(){
- return success("获取成功",(new CompanyItem())->GetTree());
- }
- public function itemdel(){
- $id = $this->request->post("id/d");
- $items =CompanyItem::where(['is_del'=>0,'id'=>$id])->findOrEmpty();
- if($items->isEmpty())return error("部门信息不存在");
- $items->is_del =1;
- $result= $items->save();
- return $result ? success("删除成功"): error("删除失败");
- }
- }
|