DepartUser.php 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. <?php
  2. namespace app\bug\model;
  3. use app\user\model\Account;class DepartUser extends Base
  4. {
  5. //设置字段信息
  6. protected $schema = [
  7. 'id' =>'bigint',//
  8. 'uid' =>'bigint',//员工id
  9. 'nickname' =>'varchar',//员工名称
  10. 'itemid' =>'bigint',//部门id
  11. 'is_del' =>'int',//是否删除
  12. 'status' =>'int',//员工状态 1在职 2离职
  13. 'addtime' =>'datetime',//
  14. 'updatetime' =>'datetime',//
  15. 'position' =>'int',//职位 1普通2负责人
  16. ];
  17. protected $updateTime='updatetime';
  18. protected $createTime='addtime';
  19. public function ItemInfo(){
  20. return $this->belongsTo(CompanyItem::class,"itemid","id")->bind(["depart_name"=>"name"]);
  21. }
  22. public function GetDepartNameByUid($uid){
  23. $list=$this->with(["item_info"])->where(["uid"=>$uid])->select();
  24. return array_column($list->toArray(),"depart_name","uid");
  25. }
  26. //设置角色部门
  27. public function SetItem($uid,$itemArr=[]){
  28. if(empty($itemArr))return $this->where(["uid"=>$uid,"is_del"=>0])->save(["is_del"=>1]);
  29. else{
  30. $this->where(['uid'=>$uid,'is_del'=>0])->whereNotIn("itemid",$itemArr)->save(['is_del'=>1]);
  31. $inItem=$this->where(['uid'=>$uid,'is_del'=>0])->whereIn("itemid",$itemArr)->column("itemid");
  32. $temp=[];
  33. $acc = Account::with(["userInfo"])->findOrEmpty($uid);
  34. if($acc->isEmpty())throw new \Exception("账户不存在");
  35. foreach ($itemArr as $item){
  36. if(in_array($item,$inItem)) continue;
  37. $depin =[
  38. 'uid'=>$uid,
  39. 'nickname'=>$acc->nickname,
  40. 'itemid'=>$item,
  41. ];
  42. $temp[]=$depin;
  43. }
  44. if(!empty($temp))return $this->saveAll($temp);
  45. }
  46. }
  47. }