Staff.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495
  1. <?php
  2. namespace addons\qingdongams\model;
  3. use addons\qingdongams\library\StaffAuth;
  4. use app\admin\controller\qingdongams\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. {
  17. use SoftDelete;
  18. // 表名,不含前缀
  19. protected $name = 'qingdongams_staff';
  20. // 开启自动写入时间戳字段
  21. protected $autoWriteTimestamp = 'int';
  22. // 定义时间戳字段名
  23. protected $createTime = 'createtime';
  24. protected $updateTime = 'updatetime';
  25. protected $deleteTime = 'deletetime';
  26. // 追加属性
  27. protected $append = [
  28. 'group_text',
  29. ];
  30. public static function getList($notInIds = [])
  31. {
  32. $where = ['status' => 1];
  33. if ($notInIds) {
  34. $where['id'] = ['not in', $notInIds];
  35. }
  36. return self::where($where)->field('id,name')->select();
  37. }
  38. //获取部门下 员工列表,树状结构
  39. public static function getDepartmentStaff($department, $name)
  40. {
  41. foreach ($department as $k => $v) {
  42. $v['staffs'] = self::getOneDepartmentStaffList($v['id'], $name);
  43. $v['count'] = count($v['staffs']);
  44. if ($v['children']) {
  45. $v['children'] = self::getDepartmentStaff($v['children'],$name);
  46. }
  47. $department[$k] = $v;
  48. }
  49. return $department;
  50. }
  51. //获取一个部门下 员工列表
  52. public static function getOneDepartmentStaffList($department_id, $name = '')
  53. {
  54. $where = ['department_id' => $department_id];
  55. if ($name) {
  56. $where['name'] = ['like', "%{$name}%"];
  57. }
  58. $staffs = self::where($where)->with(['parent'])
  59. ->field('id,name,post,department_id,parent_id,img,mobile')->select();
  60. $parents = [];
  61. $ids = [];
  62. foreach ($staffs as $v) {
  63. $ids[] = $v['id'];
  64. }
  65. foreach ($staffs as $v) {
  66. $parents[0][] = $v;
  67. }
  68. return self::getChilds($parents, 0);
  69. }
  70. //生成树状结构
  71. private static function getChilds($data, $pid)
  72. {
  73. $list = [];
  74. if (isset($data[$pid])) {
  75. $list = $data[$pid];
  76. foreach ($list as $k => $v) {
  77. $v['children'] = self::getChilds($data, $v['id']);
  78. $list[$k] = $v;
  79. }
  80. }
  81. return $list;
  82. }
  83. public static function getStaffCount($department)
  84. {
  85. foreach ($department as $k => $v) {
  86. $v['count'] = count($v['staffs']);
  87. if ($v['children']) {
  88. $v['count'] += self::getChildrenCount($v['children']);
  89. $v['children'] = self::getStaffCount($v['children']);
  90. }
  91. $department[$k] = $v;
  92. }
  93. return $department;
  94. }
  95. public static function getChildrenCount($children, $count = 0)
  96. {
  97. foreach ($children as $k => $v) {
  98. $count += count($v['staffs']);
  99. if ($v['children']) {
  100. $count += self::getChildrenCount($v['children']);
  101. }
  102. }
  103. return $count;
  104. }
  105. // 图片
  106. public static function getKeyList()
  107. {
  108. $staffs = self::where([])->field('id,name,img,group_ids')->select();
  109. $data = [];
  110. foreach ($staffs as $v) {
  111. $data[$v['id']] = $v;
  112. }
  113. return $data;
  114. }
  115. public static function getGroupStaffIds($group_id)
  116. {
  117. return self::where('', 'exp', Db::raw('FIND_IN_SET(' . intval($group_id) . ',group_ids)'))->column('id');
  118. }
  119. public static function getStaff()
  120. {
  121. return self::where([])->column('name', 'id');
  122. }
  123. public static function allList($notInIds = [])
  124. {
  125. $where['id'] = ['in', self::getMyStaffIds()];
  126. $where['status'] = 1;
  127. return self::where($where)->field('id,name')->select();
  128. }
  129. //员工业绩
  130. public static function getMyStaffIds()
  131. {
  132. $staff = self::info();
  133. $ids = [$staff->id];
  134. $l_ids = self::getLowerStaffId();
  135. $ids = array_merge($ids, $l_ids);
  136. return $ids;
  137. }
  138. //获取类型员工
  139. public static function getYkList($dep_id){
  140. $l_ids = self::where([
  141. 'status' => 1,
  142. 'role' =>$dep_id,
  143. ])->field("id,name")->select();
  144. return $l_ids;
  145. }
  146. //团队业绩
  147. public static function info()
  148. {
  149. if (StaffAuth::instance()->id) {
  150. return StaffAuth::instance();
  151. }
  152. $auth = new Auth();
  153. if ($auth->isLogin()) {
  154. $base = new Base();
  155. return $base->getStaff();
  156. }
  157. return null;
  158. }
  159. public static function getLowerStaffId()
  160. {
  161. $staff = self::info();
  162. $groupIds = AuthGroupAccess::where(['uid' => $staff->admin_id])->column('group_id');
  163. $role_type = StaffRole::where(['id' => $staff->role])->value('role_type');
  164. switch ($role_type) {
  165. case 1://本人
  166. $l_ids = [];
  167. break;
  168. case 2://本人及下属
  169. $l_ids = self::where([
  170. 'parent_id' => $staff->id,
  171. ])->column('id');
  172. break;
  173. case 3://本部门
  174. $uids = AuthGroupAccess::where(['group_id' => ['in', $groupIds]])->column('uid');
  175. $l_ids = self::where([
  176. 'admin_id' => ['in', $uids],
  177. 'status' => 1,
  178. 'id' => ['neq', $staff->id]
  179. ])->column('id');
  180. break;
  181. case 4://仅下属部门
  182. $groupIds = self::getLowerId($groupIds, false);
  183. $uids = AuthGroupAccess::where(['group_id' => ['in', $groupIds]])->column('uid');
  184. $l_ids = self::where([
  185. 'admin_id' => ['in', $uids],
  186. 'status' => 1,
  187. 'id' => ['neq', $staff->id]
  188. ])->column('id');
  189. break;
  190. case 5://本部门及下属部门
  191. $groupIds = self::getLowerId($groupIds, true);
  192. $uids = AuthGroupAccess::where(['group_id' => ['in', $groupIds]])->column('uid');
  193. $l_ids = self::where([
  194. 'admin_id' => ['in', $uids],
  195. 'status' => 1,
  196. 'id' => ['neq', $staff->id]
  197. ])->column('id');
  198. break;
  199. case 6://全部
  200. $l_ids = self::where([
  201. 'status' => 1,
  202. 'id' => ['neq', $staff->id]
  203. ])->column('id');
  204. break;
  205. }
  206. if (empty($l_ids)) {//返回空 下属查询 会查询全部
  207. return ['-1'];
  208. }
  209. return $l_ids;
  210. }
  211. //绑定admin账号
  212. public static function getLowerId($l_ids, $top = true)
  213. {
  214. if (!is_array($l_ids)) {
  215. $l_ids = explode(',', $l_ids);
  216. }
  217. $ids = AuthGroup::where(['pid' => ['in', $l_ids]])->column('id');
  218. if ($ids) {
  219. $w_ids = self::getLowerId($ids, false);
  220. $ids = array_merge($ids, $w_ids);
  221. } else {
  222. $ids = [];
  223. }
  224. if ($top) {
  225. $ids = array_merge($ids, $l_ids);
  226. }
  227. return array_unique($ids);
  228. }
  229. //角色
  230. /**
  231. * 获取权限列表
  232. */
  233. public static function getStaffRule($type)
  234. {
  235. $row = StaffRule::where(['name' => $type])->find();
  236. if (!$row) {
  237. return [];
  238. }
  239. $rules = StaffRule::where(['pid' => $row->id])->column('name', 'id');
  240. $staff = self::info();
  241. $staffRules = StaffRole::where(['id' => $staff->role])->value('rules');
  242. $staffRules = explode(',', $staffRules) ?? [];
  243. $value = [];
  244. foreach ($staffRules as $r) {
  245. if (isset($rules[$r])) {
  246. $value[] = $rules[$r];
  247. }
  248. }
  249. return $value;
  250. }
  251. //获取类型员工
  252. public static function getYkListIds($department_id, $name = ''){
  253. $where = [];
  254. if ($name) {
  255. $where['name'] = ['like', "%{$name}%"];
  256. }
  257. $department_id = intval($department_id);
  258. $where['role']=$department_id;
  259. $ids = self::where($where)
  260. ->column('id');
  261. return $ids;
  262. }
  263. //上级
  264. public static function getOneGroupStaffIds($department_id, $name = '')
  265. {
  266. $where = [];
  267. if ($name) {
  268. $where['name'] = ['like', "%{$name}%"];
  269. }
  270. $department_id = intval($department_id);
  271. $ids = self::where($where)
  272. ->where('', 'exp', "FIND_IN_SET({$department_id},group_ids)")
  273. ->column('id');
  274. return $ids;
  275. }
  276. //获取员工
  277. protected static function init()
  278. {
  279. self::beforeInsert(function ($row) {
  280. $changed = $row->getChangedData();
  281. $admin = [
  282. 'username' => $changed['username'],
  283. 'mobile' => $changed['mobile'],
  284. 'nickname' => $changed['name'],
  285. 'password' => $changed['password'],
  286. 'salt' => $changed['salt'],
  287. 'avatar' => $changed['img'],
  288. 'email' => $changed['email'],
  289. ];
  290. if (isset($changed['admin_id']) && $changed['admin_id']) {
  291. return true;
  292. }
  293. $adminModel = new Admin();
  294. $result = $adminModel->validate('Admin.add')->save($admin);
  295. if ($result == false) {
  296. exception($adminModel->getError());
  297. }
  298. $row->admin_id = $adminModel->getLastInsID();
  299. $group = explode(',', $changed['group_ids']);
  300. foreach ($group as $value) {
  301. $dataset[] = ['uid' => $row->admin_id, 'group_id' => $value];
  302. }
  303. model('AuthGroupAccess')->saveAll($dataset);
  304. return $row;
  305. });
  306. self::beforeUpdate(function ($row) {
  307. $changed = $row->getChangedData();
  308. if (!isset($row->id)) {
  309. return true;
  310. }
  311. $staff = self::get($row->id);
  312. if (empty($staff->admin_id)) {
  313. return $row;
  314. }
  315. //admin用户不更新
  316. if ($staff->admin_id == 1) {
  317. return true;
  318. }
  319. if (isset($changed['deletetime']) && $changed['deletetime']) {
  320. Admin::where(['id' => $staff->admin_id])->delete();
  321. return true;
  322. }
  323. if (isset($changed['mobile']) || isset($changed['name'])
  324. || isset($changed['email']) || isset($changed['img'])) {
  325. $params = [];
  326. if (isset($changed['mobile'])) {
  327. $params['mobile'] = $changed['mobile'];
  328. }
  329. if (isset($changed['username'])) {
  330. $params['username']= $changed['username'];
  331. }
  332. if (isset($changed['name'])) {
  333. $params['nickname'] = $changed['name'];
  334. }
  335. if (isset($changed['img'])) {
  336. $params['avatar'] = $changed['img'];
  337. }
  338. if (isset($changed['email'])) {
  339. $params['email'] = $changed['email'];
  340. }
  341. //如果有修改密码
  342. if (isset($changed['password'])) {
  343. if ($changed['password']) {
  344. $params['password'] = $changed['password'];
  345. $params['salt'] = $changed['salt'];
  346. }
  347. }
  348. $adminModel = new Admin();
  349. $result = $adminModel->save($params, ['id' => $staff->admin_id]);
  350. if ($result === false) {
  351. exception($row->getError());
  352. }
  353. }
  354. if (isset($changed['group_ids'])) {
  355. // 先移除所有权限
  356. model('AuthGroupAccess')->where('uid', $staff->admin_id)->delete();
  357. $group = explode(',', $changed['group_ids']);
  358. foreach ($group as $value) {
  359. $dataset[] = ['uid' => $staff->admin_id, 'group_id' => $value];
  360. }
  361. model('AuthGroupAccess')->saveAll($dataset);
  362. }
  363. return $row;
  364. });
  365. }
  366. //包含当前角色
  367. public function getGroupTextAttr($value, $data)
  368. {
  369. if (!isset($data['group_ids'])) {
  370. return '';
  371. }
  372. $names = AuthGroup::where(['id' => ['in', $data['group_ids']]])->column('name');
  373. return implode(',', $names);
  374. }
  375. //获取下属员工IDs
  376. public function getImgAttr($value)
  377. {
  378. if ($value) {
  379. return cdnurl($value, true);
  380. } else {
  381. return $value;
  382. }
  383. }
  384. //获取全部ID
  385. public function getCreatetimeAttr($value)
  386. {
  387. return date('Y-m-d H:i:s', $value);
  388. }
  389. public function achievement()
  390. {
  391. return $this->belongsTo(Achievement::class, 'id', 'obj_id')->where(['type' => 3]);
  392. }
  393. public function teamAchievement()
  394. {
  395. $condition['type'] = ['in', [2, 4]];
  396. return $this->belongsTo(Achievement::class, 'id', 'obj_id')->where($condition);
  397. }
  398. public function department()
  399. {
  400. return $this->belongsTo(StaffDepartment::class, 'department_id', 'id')->field('id,name as department_name')->bind('department_name');
  401. }
  402. //获取自己及下属员工
  403. public function admin()
  404. {
  405. return $this->belongsTo(Admin::class, 'admin_id', 'id')->field('id,username');
  406. }
  407. public function staffrole()
  408. {
  409. return $this->hasOne(StaffRole::class, 'id', 'role')->field('id,name');
  410. }
  411. //获取一个部门下 员工列表
  412. public function parent()
  413. {
  414. return $this->belongsTo(Staff::class, 'parent_id', 'id')->field('id,name as parent_name')->bind('parent_name');
  415. }
  416. //客户
  417. public function customer()
  418. {
  419. return $this->belongsTo(Customer::class, 'id', 'create_staff_id')->group('create_staff_id')->field('create_staff_id,count(*) as achieve')->bind('achieve');
  420. }
  421. //拜访
  422. public function visit()
  423. {
  424. return $this->belongsTo(Event::class, 'id', 'staff_id')->group('staff_id')->field('staff_id,count(*) as achieve')->where(['type' => 3, 'status' => 2])->bind('achieve');
  425. }
  426. //回款
  427. public function receivables()
  428. {
  429. return $this->belongsTo(Receivables::class, 'id', 'create_staff_id')->where(['check_status' => 2])->group('create_staff_id')->field('create_staff_id,sum(money) as achieve')->bind('achieve');
  430. }
  431. //工单
  432. public function workorder()
  433. {
  434. return $this->belongsTo(Workorder::class, 'id', 'owner_staff_id')->group('owner_staff_id')->field('owner_staff_id,count(*) as achieve')->bind('achieve');
  435. }
  436. //合同
  437. public function contract()
  438. {
  439. return $this->belongsTo(Contract::class, 'id', 'create_staff_id')->where(['check_status' => 2])->group('create_staff_id')->field('create_staff_id,sum(money) as achieve')->bind('achieve');
  440. }
  441. }