123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285 |
- <?php
- namespace app\admin\controller;
- use app\BaseController;
- use think\App;
- use think\facade\Db;
- class Newfill extends BaseController
- {
- public $post = "";
- public function __construct(App $app)
- {
- parent::__construct($app);
- $this->post = $this->request->post();
- }
- public function list()
- {
- // $page = isset($this->post['page']) && $this->post['page'] != "" ? intval($this->post['page']) : 1;
- // $size = isset($this->post['size']) && $this->post['size'] != "" ? intval($this->post['size']) : 10;
- $condition=[];
- $depart []=['is_del',"=",0];
- $condition[] = ['is_del',"=",0];
- $pid = isset($this->post['pid']) &&$this->post['pid']!=="" ?intval($this->post['pid']): "";
- if($pid!==""){
- $condition[]=["pid","=",$pid];
- }
- $name= isset($this->post['name'])&&$this->post['name']!=="" ? trim($this->post['name']):"";
- if($name!=""){
- $condition[]=["name","like","%{$this->post['name']}%"];
- }
- $nickname = isset($this->post['nickname']) && $this->post['nickname'] !==""? trim($this->post['nickname']) :"";
- if($nickname!=""){
- $is= Db::name("depart_user")->where([['nickname','like',"%{$nickname}%"]])->column('itemid');
- if(empty($is)){
- return error_show(1004,"未找到数据");
- }
- $condition[]=["id","in",$is];
- }
- if($pid==="" &&$name=="" &&$nickname==""){
- $condition[]=["pid","=",0];
- }
- $pidlist = Db::name("company_item")->where($condition)->column("pid");
- if(!empty($pidlist)){
- $depart[]=[["itemid","in",$pidlist]];
- }else{
- if($pid===""){
- $depart[]=['itemid','=',0];
- }else{
- $depart[]=['itemid','=',$pid];
- }
- }
- $item = Db::name("depart_user")->where($depart)->select();
- $list = Db::name("company_item")->where($condition)->select();
- return app_show(0,"获取成功",["depart"=>$list,"item"=>$item]);
- }
- /*状态*/
- public function stat(){
- $id = isset($this->post['id'])?intval($this->post['id']):"";
- if($id==""){
- return error_show(1004,"参数id 不能为空");
- }
- $s = Db::name('company_item')->where(["id"=>$id])->find();
- if(empty($s)){
- return error_show(1005,"未找到部门");
- }
- $var= $s['status'];
- $di = $s['status']==0? "1":"0";
- $s['status']=$di;
- $s['updatetime'] = date("Y-m-d H:i:s");
- // var_dump($s);
- $ti = Db::name('company_item')->save($s);
- $orde = ["order_code"=>$s['name'],"status"=>$var,"action_remark"=>'',"action_type"=>"edit"];
- ActionLog::logAdd($this->post['token'],$orde,"company_item",$s['status'],$orde);
- return app_show(0,"更新成功");
- }
- /*职位*/
- public function userp(){
- $id = isset($this->post['id'])?intval($this->post['id']):"";
- if($id==""){
- return error_show(1004,"参数id不能为空");
- }
- $t = Db::name('depart_user')->where(["id"=>$id,"is_del"=>0])->find();
- if(empty($t)){
- return error_show(1005,"未找到用户");
- }
- $position = isset($this->post['position'])?intval($this->post['position']):"";
- if($position==""){
- return error_show(1004,"职位不能为空");
- }
- if($position==2){
- $f = Db::name('depart_user')->where(['itemid'=>$t['itemid'],"is_del"=>0,"position"=>2])->find();
- if(!empty($f)&& $f['id']!=$id){
- $f['position']=1;
- $f['updatetime']= date("Y-m-d H:i:s");
- $m= Db::name('depart_user')->save($f);
- if($m==false){
- Db::rollback();
- return error_show(1004,"负责人修改失败");
- }
- }
- }
- $t['position']=$position;
- $t['updatetime'] = date("Y-m-d H:i:s");
- //var_dump($t);
- $ti = Db::name('depart_user')->save($t);
- if($ti==true){
- Db::commit();
- return app_show(0,"修改成功");
- }else{
- Db::rollback();
- return error_show(1004,"修改失败");
- }
- }
- public function add(){
- $token = isset($this->post['token']) ? trim($this->post['token']) : "";
- if($token==""){
- return error_show(101,'token不能为空');
- }
- $effetc = VerifyTokens($token);
- if(!empty($effetc) && $effetc['code']!=0){
- return error_show($effetc['code'],$effetc['message']);
- }
- $item_name=isset($this->post['name']) && $this->post['name'] !==""? trim($this->post['name']) :"";
- if($item_name==""){
- return error_show(0,"部门名称不能为空");
- }
- $repeat_name = Db::name("company_item")->where(["is_del"=>0,"name"=>$item_name])->find();
- if(!empty($repeat_name)){
- return error_show(1004,"部门名称已存在");
- }
- $pid = isset($this->post['pid']) && $this->post['pid'] !==""? trim($this->post['pid']):"";
- if($pid===""){
- return error_show(1002,"父级id不能为空");
- }
- //$spid = Db::name('company_item')->where(['id'=>$pid,'is_del'=>0])->find();
- $spid=['depart_link'=>""];
- if($pid!=0){
- $spid = Db::name('company_item')->where(['id'=>$pid,'is_del'=>0])->find();
- if(empty($spid)){
- return error_show(1004,"父级数据不能为空");
- }
- }
- $level = isset($this->post['level']) && $this->post['level'] !==""? trim($this->post['level']):"1";
- $weight = isset($this->post['weight']) && $this->post['weight'] !==""? trim($this->post['weight']):"1";
- Db::startTrans();
- try{
- $data =[
- "name"=>$item_name, "pid"=>$pid, "level"=>$level, "weight"=>$weight,
- "is_del"=>0,
- "addtime"=>date("Y-m-d H:i:s"),
- "updatetime"=>date("Y-m-d H:i:s"),
- ];
- $t= Db::name("company_item")->insert($data,true);
- if($t>0){
- // $orde = ["order_code"=>$repeat_name['name'],"status"=>0,"action_remark"=>'',"action_type"=>"creat"];
- // ActionLog::logAdd($token,$orde,"company_item",0,$orde);
- $depart_link=$spid['depart_link']."{$t}-";
- $level =explode('-',$depart_link);
- $level = array_filter($level);
- $level= count($level);
- $i=['depart_link'=>$depart_link,'level'=>$level];
- $o=Db::name('company_item')->where(['id'=>$t])->update($i);
- if($o){
- // $stn = ["order_code"=>$repeat_name['name'],"status"=>0,"action_remark"=>'',"action_type"=>"edit"];
- // ActionLog::logAdd($token,$stn,"company_item",0,$stn);
- Db::commit();
- return error_show(0,"添加成功");
- }
- }
- Db::rollback();
- }catch (\Exception $e){
- Db::rollback();
- return error_show(1003,$e->getMessage());
- }
- }
- public function refresh(){
- $token = isset($this->post['token']) ? trim($this->post['token']) : "";
- if($token==""){
- return error_show(101,'token不能为空');
- }
- $effetc = VerifyTokens($token);
- if(!empty($effetc) && $effetc['code']!=0){
- return error_show($effetc['code'],$effetc['message']);
- }
- $id = isset($this->post['id'])?intval($this->post['id']):"";
- $items = Db::name("company_item")->where(['id'=>$id,'is_del'=>0])->find();
- if($items==false){
- return error_show(1003,"部门信息不存在");
- }
- $pid = isset($this->post['pid']) && $this->post['pid'] !==""? trim($this->post['pid']):"";
- if($pid==""){
- return error_show(1002,"父级id不能为空");
- }
- $spid=['depart_link'=>""];
- if($pid!=0){
- $spid = Db::name('company_item')->where(['id'=>$pid,'is_del'=>0])->find();
- if(!empty($spid)){
- return error_show(1004,"父级数据不能为空");
- }
- }
- $weight = isset($this->post['weight']) && $this->post['weight'] !==""? trim($this->post['weight']):"1";
- $itemname=isset($this->post['name'])? trim($this->post['name']):"";
- if($itemname==""){
- return error_show(1002,"部门名称不能为空");
- }
- $repeat_name = Db::name("company_item")->where(["is_del"=>0,"name"=>$itemname])->where('id','<>',$id)->find();
- //echo Db::name("company_item")->getLastSql();
- if(!empty($repeat_name)){
- return error_show(1004,"部门名称已存在");
- }
- $depart_link=$spid['depart_link']."{$id}-";
- $itemlevel =explode('-',$depart_link);
- $itemlevel = array_filter($itemlevel);
- $itemlevel= count($itemlevel);
- $item=[
- "id"=>$id,
- "name"=>$itemname,
- "updatetime"=>date("Y-m-d H:i:s"),
- "weight"=>$weight,"pid"=>$pid,
- 'depart_link'=>$depart_link,
- 'level'=>$itemlevel
- ];
- $re = Db::name("company_item")->save($item);
- // $stn = ["order_code"=>$repeat_name['name'],"status"=>0,"action_remark"=>'',"action_type"=>"edit"];
- // ActionLog::logAdd($token,$stn,"company_item",0,$stn);
- return $re ? app_show(0,"信息更新成功"): error_show(1003,"信息更新失败");
- }
- /*查询*/
- public function query(){
- $token = isset($this->post['token']) ? trim($this->post['token']) : "";
- if($token==""){
- return error_show(101,'token不能为空');
- }
- $effetc = VerifyTokens($token);
- if(!empty($effetc) && $effetc['code']!=0){
- return error_show($effetc['code'],$effetc['message']);
- }
- $dati = Db::name("company_item")->where(['pid'=>0,'is_del'=>0])->order("weight desc")->select();
- $k=[];
- foreach ($dati as $key=>$value){
- // $temp=[];
- //$temp= Db::name("company_item")->where(['pid'=>$value['id'],'is_del'=>0])->order("weight desc")->select();
- // $value['child'] =$temp;
- // $k[]=$value;
- $k[]=crea($value);
- }
- return app_show(0,"获取成功",$k);
- }
- public function itemdel(){
- $token = isset($this->post['token']) ? trim($this->post['token']) : "";
- if($token==""){
- return error_show(101,'token不能为空');
- }
- $effetc = VerifyTokens($token);
- if(!empty($effetc) && $effetc['code']!=0){
- return error_show($effetc['code'],$effetc['message']);
- }
- $id = isset($this->post['id']) ? intval($this->post['id']):"";
- $items = Db::name("company_item")->where(['is_del'=>0,'id'=>$id])->find();
- if($items==false){
- return error_show(1003,"部门信息不存在");
- }
- $items['is_del']=1;
- $items['updatetime']=date("Y-m-d H:i:s");
- $result = Db::name("company_item")->save($items);
- // $stn = ["order_code"=>$items['name'],"status"=>0,"action_remark"=>'',"action_type"=>"delete"];
- // ActionLog::logAdd($token,$stn,"company_item",0,$stn);
- return $result ? app_show(0,"删除成功"): error_show(1003,"删除失败");
- }
- }
|