Staff.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. <?php
  2. namespace addons\qingdong\model;
  3. use addons\qingdong\library\StaffAuth;
  4. use app\admin\controller\qingdong\Base;
  5. use app\admin\library\Auth;
  6. use app\admin\model\AuthGroup;
  7. use app\admin\model\AuthGroupAccess;
  8. use think\Db;
  9. use think\Model;
  10. use traits\model\SoftDelete;
  11. use app\admin\model\Admin;
  12. /**
  13. *员工表
  14. */
  15. class Staff Extends Model {
  16. use SoftDelete;
  17. // 表名,不含前缀
  18. protected $name = 'qingdong_staff';
  19. // 开启自动写入时间戳字段
  20. protected $autoWriteTimestamp = 'int';
  21. // 定义时间戳字段名
  22. protected $createTime = 'createtime';
  23. protected $updateTime = 'updatetime';
  24. protected $deleteTime = 'deletetime';
  25. // 追加属性
  26. protected $append = [
  27. 'group_text',
  28. ];
  29. protected static function init()
  30. {
  31. self::beforeInsert(function ($row) {
  32. $changed = $row->getChangedData();
  33. $admin = [
  34. 'username' => $changed['mobile'],
  35. 'nickname' => $changed['name'],
  36. 'password' => $changed['password'],
  37. 'salt' => $changed['salt'],
  38. 'avatar' => $changed['img'],
  39. 'email' => $changed['email'],
  40. ];
  41. if(isset($changed['admin_id']) && $changed['admin_id']){
  42. return true;
  43. }
  44. $adminModel = new Admin();
  45. $result=$adminModel->validate('Admin.add')->save($admin);
  46. if($result == false){
  47. exception($adminModel->getError());
  48. }
  49. $row->admin_id = $adminModel->getLastInsID();
  50. $group = explode(',', $changed['group_ids']);
  51. foreach ($group as $value) {
  52. $dataset[] = ['uid' => $row->admin_id, 'group_id' => $value];
  53. }
  54. model('AuthGroupAccess')->saveAll($dataset);
  55. return $row;
  56. });
  57. self::beforeUpdate(function ($row) {
  58. $changed = $row->getChangedData();
  59. if(!isset($row->id)){
  60. return true;
  61. }
  62. $staff = self::get($row->id);
  63. if (empty($staff->admin_id)) {
  64. return $row;
  65. }
  66. //admin用户不更新
  67. if($staff->admin_id == 1){
  68. return true;
  69. }
  70. if(isset($changed['deletetime']) && $changed['deletetime']){
  71. Admin::where(['id' => $staff->admin_id])->delete();
  72. return true;
  73. }
  74. if(isset($changed['mobile']) || isset($changed['name'])
  75. || isset($changed['email']) || isset($changed['img']) ){
  76. $params = [];
  77. if(isset($changed['mobile'])){
  78. $params['username']=$changed['mobile'];
  79. }
  80. if(isset($changed['name'])){
  81. $params['nickname']=$changed['name'];
  82. }
  83. if(isset($changed['img'])){
  84. $params['avatar']=$changed['img'];
  85. }
  86. if(isset($changed['email'])){
  87. $params['email']=$changed['email'];
  88. }
  89. //如果有修改密码
  90. if (isset($changed['password'])) {
  91. if ($changed['password']) {
  92. $params['password'] = $changed['password'];
  93. $params['salt'] = $changed['salt'];
  94. }
  95. }
  96. $adminModel = new Admin();
  97. $result = $adminModel->save($params, ['id' => $staff->admin_id]);
  98. if ($result === false) {
  99. exception($row->getError());
  100. }
  101. }
  102. if(isset($changed['group_ids'])){
  103. // 先移除所有权限
  104. model('AuthGroupAccess')->where('uid', $staff->admin_id)->delete();
  105. $group = explode(',', $changed['group_ids']);
  106. foreach ($group as $value) {
  107. $dataset[] = ['uid' => $staff->admin_id, 'group_id' => $value];
  108. }
  109. model('AuthGroupAccess')->saveAll($dataset);
  110. }
  111. return $row;
  112. });
  113. }
  114. // 图片
  115. public function getGroupTextAttr($value, $data)
  116. {
  117. if(!isset($data['group_ids'])){
  118. return '';
  119. }
  120. $names=AuthGroup::where(['id'=>['in',$data['group_ids']]])->column('name');
  121. return implode(',',$names);
  122. }
  123. public static function info()
  124. {
  125. if (StaffAuth::instance()->id) {
  126. return StaffAuth::instance();
  127. }
  128. $auth = new Auth();
  129. if ($auth->isLogin()) {
  130. $base = new Base();
  131. return $base->getStaff();
  132. }
  133. return null;
  134. }
  135. public function getImgAttr($value) {
  136. if ($value) {
  137. return cdnurl($value, true);
  138. } else {
  139. return $value;
  140. }
  141. }
  142. public function getCreatetimeAttr($value){
  143. return date('Y-m-d H:i:s',$value);
  144. }
  145. //员工业绩
  146. public function achievement() {
  147. return $this->belongsTo(Achievement::class, 'id', 'obj_id')->where(['type' => 3]);
  148. }
  149. //团队业绩
  150. public function teamAchievement() {
  151. $condition['type'] = ['in',[2,4]];
  152. return $this->belongsTo(Achievement::class, 'id', 'obj_id')->where($condition);
  153. }
  154. public function department() {
  155. return $this->belongsTo(StaffDepartment::class, 'department_id', 'id')->field('id,name as department_name')->bind('department_name');
  156. }
  157. //绑定admin账号
  158. public function admin() {
  159. return $this->belongsTo(Admin::class, 'admin_id', 'id')->field('id,username');
  160. }
  161. //角色
  162. public function staffrole() {
  163. return $this->hasOne(StaffRole::class, 'id', 'role')->field('id,name');
  164. }
  165. //上级
  166. public function parent() {
  167. return $this->belongsTo(Staff::class, 'parent_id', 'id')->field('id,name as parent_name')->bind('parent_name');
  168. }
  169. //获取员工
  170. public static function getList($notInIds=[]) {
  171. $where=['status'=>1];
  172. if($notInIds){
  173. $where['id']=['not in',$notInIds];
  174. }
  175. return self::where($where)->field('id,name')->select();
  176. }
  177. //包含当前角色
  178. public static function getLowerId($l_ids,$top=true){
  179. if(!is_array($l_ids)){
  180. $l_ids=explode(',',$l_ids);
  181. }
  182. $ids=AuthGroup::where(['pid' =>['in',$l_ids]])->column('id');
  183. if ($ids) {
  184. $w_ids = self::getLowerId($ids,false);
  185. $ids = array_merge($ids, $w_ids);
  186. }else{
  187. $ids=[];
  188. }
  189. if($top){
  190. $ids=array_merge($ids,$l_ids);
  191. }
  192. return array_unique($ids);
  193. }
  194. //获取下属员工IDs
  195. public static function getLowerStaffId()
  196. {
  197. $staff = self::info();
  198. $groupIds = AuthGroupAccess::where(['uid' => $staff->admin_id])->column('group_id');
  199. $role_type = StaffRole::where(['id' => $staff->role])->value('role_type');
  200. switch ($role_type) {
  201. case 1://本人
  202. $l_ids = [];
  203. break;
  204. case 2://本人及下属
  205. $l_ids = self::where([
  206. 'parent_id' => $staff->id,
  207. ])->column('id');
  208. break;
  209. case 3://本部门
  210. $uids = AuthGroupAccess::where(['group_id' => ['in', $groupIds]])->column('uid');
  211. $l_ids = self::where([
  212. 'admin_id' => ['in', $uids],
  213. 'status' => 1,
  214. 'id' => ['neq', $staff->id]
  215. ])->column('id');
  216. break;
  217. case 4://仅下属部门
  218. $groupIds = self::getLowerId($groupIds, false);
  219. $uids = AuthGroupAccess::where(['group_id' => ['in', $groupIds]])->column('uid');
  220. $l_ids = self::where([
  221. 'admin_id' => ['in', $uids],
  222. 'status' => 1,
  223. 'id' => ['neq', $staff->id]
  224. ])->column('id');
  225. break;
  226. case 5://本部门及下属部门
  227. $groupIds = self::getLowerId($groupIds, true);
  228. $uids = AuthGroupAccess::where(['group_id' => ['in', $groupIds]])->column('uid');
  229. $l_ids = self::where([
  230. 'admin_id' => ['in', $uids],
  231. 'status' => 1,
  232. 'id' => ['neq', $staff->id]
  233. ])->column('id');
  234. break;
  235. case 6://全部
  236. $l_ids = self::where([
  237. 'status' => 1,
  238. 'id' => ['neq', $staff->id]
  239. ])->column('id');
  240. break;
  241. }
  242. if (empty($l_ids)) {//返回空 下属查询 会查询全部
  243. return ['-1'];
  244. }
  245. return $l_ids;
  246. }
  247. //获取全部ID
  248. public static function getMyStaffIds() {
  249. $staff = self::info();
  250. $ids = [$staff->id];
  251. $l_ids = self::getLowerStaffId();
  252. $ids = array_merge($ids, $l_ids);
  253. return $ids;
  254. }
  255. public static function getKeyList() {
  256. $staffs=self::where([])->field('id,name,img,group_ids')->select();
  257. $data=[];
  258. foreach ($staffs as $v){
  259. $data[$v['id']]=$v;
  260. }
  261. return $data;
  262. }
  263. public static function getGroupStaffIds($group_id)
  264. {
  265. return self::where('', 'exp', Db::raw('FIND_IN_SET(' . intval($group_id) . ',group_ids)'))->column('id');
  266. }
  267. public static function getStaff(){
  268. return self::where([])->column('name', 'id');
  269. }
  270. //获取自己及下属员工
  271. public static function allList($notInIds=[]) {
  272. $where['id']=['in',self::getMyStaffIds()];
  273. $where['status']=1;
  274. return self::where($where)->field('id,name')->select();
  275. }
  276. /**
  277. * 获取权限列表
  278. */
  279. public static function getStaffRule($type)
  280. {
  281. $row = StaffRule::where(['name' => $type])->find();
  282. if (!$row) {
  283. return [];
  284. }
  285. $rules = StaffRule::where(['pid' => $row->id])->column('name', 'id');
  286. $staff = self::info();
  287. $staffRules = StaffRole::where(['id' => $staff->role])->value('rules');
  288. $staffRules = explode(',', $staffRules) ?? [];
  289. $value = [];
  290. foreach ($staffRules as $r) {
  291. if (isset($rules[$r])) {
  292. $value[] = $rules[$r];
  293. }
  294. }
  295. return $value;
  296. }
  297. }