Staff.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  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 info()
  140. {
  141. if (StaffAuth::instance()->id) {
  142. return StaffAuth::instance();
  143. }
  144. $auth = new Auth();
  145. if ($auth->isLogin()) {
  146. $base = new Base();
  147. return $base->getStaff();
  148. }
  149. return null;
  150. }
  151. public static function getLowerStaffId()
  152. {
  153. $staff = self::info();
  154. $groupIds = AuthGroupAccess::where(['uid' => $staff->admin_id])->column('group_id');
  155. $role_type = StaffRole::where(['id' => $staff->role])->value('role_type');
  156. switch ($role_type) {
  157. case 1://本人
  158. $l_ids = [];
  159. break;
  160. case 2://本人及下属
  161. $l_ids = self::where([
  162. 'parent_id' => $staff->id,
  163. ])->column('id');
  164. break;
  165. case 3://本部门
  166. $uids = AuthGroupAccess::where(['group_id' => ['in', $groupIds]])->column('uid');
  167. $l_ids = self::where([
  168. 'admin_id' => ['in', $uids],
  169. 'status' => 1,
  170. 'id' => ['neq', $staff->id]
  171. ])->column('id');
  172. break;
  173. case 4://仅下属部门
  174. $groupIds = self::getLowerId($groupIds, false);
  175. $uids = AuthGroupAccess::where(['group_id' => ['in', $groupIds]])->column('uid');
  176. $l_ids = self::where([
  177. 'admin_id' => ['in', $uids],
  178. 'status' => 1,
  179. 'id' => ['neq', $staff->id]
  180. ])->column('id');
  181. break;
  182. case 5://本部门及下属部门
  183. $groupIds = self::getLowerId($groupIds, true);
  184. $uids = AuthGroupAccess::where(['group_id' => ['in', $groupIds]])->column('uid');
  185. $l_ids = self::where([
  186. 'admin_id' => ['in', $uids],
  187. 'status' => 1,
  188. 'id' => ['neq', $staff->id]
  189. ])->column('id');
  190. break;
  191. case 6://全部
  192. $l_ids = self::where([
  193. 'status' => 1,
  194. 'id' => ['neq', $staff->id]
  195. ])->column('id');
  196. break;
  197. }
  198. if (empty($l_ids)) {//返回空 下属查询 会查询全部
  199. return ['-1'];
  200. }
  201. return $l_ids;
  202. }
  203. //绑定admin账号
  204. public static function getLowerId($l_ids, $top = true)
  205. {
  206. if (!is_array($l_ids)) {
  207. $l_ids = explode(',', $l_ids);
  208. }
  209. $ids = AuthGroup::where(['pid' => ['in', $l_ids]])->column('id');
  210. if ($ids) {
  211. $w_ids = self::getLowerId($ids, false);
  212. $ids = array_merge($ids, $w_ids);
  213. } else {
  214. $ids = [];
  215. }
  216. if ($top) {
  217. $ids = array_merge($ids, $l_ids);
  218. }
  219. return array_unique($ids);
  220. }
  221. //角色
  222. /**
  223. * 获取权限列表
  224. */
  225. public static function getStaffRule($type)
  226. {
  227. $row = StaffRule::where(['name' => $type])->find();
  228. if (!$row) {
  229. return [];
  230. }
  231. $rules = StaffRule::where(['pid' => $row->id])->column('name', 'id');
  232. $staff = self::info();
  233. $staffRules = StaffRole::where(['id' => $staff->role])->value('rules');
  234. $staffRules = explode(',', $staffRules) ?? [];
  235. $value = [];
  236. foreach ($staffRules as $r) {
  237. if (isset($rules[$r])) {
  238. $value[] = $rules[$r];
  239. }
  240. }
  241. return $value;
  242. }
  243. //上级
  244. public static function getOneGroupStaffIds($department_id, $name = '')
  245. {
  246. $where = [];
  247. if ($name) {
  248. $where['name'] = ['like', "%{$name}%"];
  249. }
  250. $department_id = intval($department_id);
  251. $ids = self::where($where)
  252. ->where('', 'exp', "FIND_IN_SET({$department_id},group_ids)")
  253. ->column('id');
  254. return $ids;
  255. }
  256. //获取员工
  257. protected static function init()
  258. {
  259. self::beforeInsert(function ($row) {
  260. $changed = $row->getChangedData();
  261. $admin = [
  262. 'username' => $changed['mobile'],
  263. 'nickname' => $changed['name'],
  264. 'password' => $changed['password'],
  265. 'salt' => $changed['salt'],
  266. 'avatar' => $changed['img'],
  267. 'email' => $changed['email'],
  268. ];
  269. if (isset($changed['admin_id']) && $changed['admin_id']) {
  270. return true;
  271. }
  272. $adminModel = new Admin();
  273. $result = $adminModel->validate('Admin.add')->save($admin);
  274. if ($result == false) {
  275. exception($adminModel->getError());
  276. }
  277. $row->admin_id = $adminModel->getLastInsID();
  278. $group = explode(',', $changed['group_ids']);
  279. foreach ($group as $value) {
  280. $dataset[] = ['uid' => $row->admin_id, 'group_id' => $value];
  281. }
  282. model('AuthGroupAccess')->saveAll($dataset);
  283. return $row;
  284. });
  285. self::beforeUpdate(function ($row) {
  286. $changed = $row->getChangedData();
  287. if (!isset($row->id)) {
  288. return true;
  289. }
  290. $staff = self::get($row->id);
  291. if (empty($staff->admin_id)) {
  292. return $row;
  293. }
  294. //admin用户不更新
  295. if ($staff->admin_id == 1) {
  296. return true;
  297. }
  298. if (isset($changed['deletetime']) && $changed['deletetime']) {
  299. Admin::where(['id' => $staff->admin_id])->delete();
  300. return true;
  301. }
  302. if (isset($changed['mobile']) || isset($changed['name'])
  303. || isset($changed['email']) || isset($changed['img'])) {
  304. $params = [];
  305. if (isset($changed['mobile'])) {
  306. $params['username'] = $changed['mobile'];
  307. }
  308. if (isset($changed['name'])) {
  309. $params['nickname'] = $changed['name'];
  310. }
  311. if (isset($changed['img'])) {
  312. $params['avatar'] = $changed['img'];
  313. }
  314. if (isset($changed['email'])) {
  315. $params['email'] = $changed['email'];
  316. }
  317. //如果有修改密码
  318. if (isset($changed['password'])) {
  319. if ($changed['password']) {
  320. $params['password'] = $changed['password'];
  321. $params['salt'] = $changed['salt'];
  322. }
  323. }
  324. $adminModel = new Admin();
  325. $result = $adminModel->save($params, ['id' => $staff->admin_id]);
  326. if ($result === false) {
  327. exception($row->getError());
  328. }
  329. }
  330. if (isset($changed['group_ids'])) {
  331. // 先移除所有权限
  332. model('AuthGroupAccess')->where('uid', $staff->admin_id)->delete();
  333. $group = explode(',', $changed['group_ids']);
  334. foreach ($group as $value) {
  335. $dataset[] = ['uid' => $staff->admin_id, 'group_id' => $value];
  336. }
  337. model('AuthGroupAccess')->saveAll($dataset);
  338. }
  339. return $row;
  340. });
  341. }
  342. //包含当前角色
  343. public function getGroupTextAttr($value, $data)
  344. {
  345. if (!isset($data['group_ids'])) {
  346. return '';
  347. }
  348. $names = AuthGroup::where(['id' => ['in', $data['group_ids']]])->column('name');
  349. return implode(',', $names);
  350. }
  351. //获取下属员工IDs
  352. public function getImgAttr($value)
  353. {
  354. if ($value) {
  355. return cdnurl($value, true);
  356. } else {
  357. return $value;
  358. }
  359. }
  360. //获取全部ID
  361. public function getCreatetimeAttr($value)
  362. {
  363. return date('Y-m-d H:i:s', $value);
  364. }
  365. public function achievement()
  366. {
  367. return $this->belongsTo(Achievement::class, 'id', 'obj_id')->where(['type' => 3]);
  368. }
  369. public function teamAchievement()
  370. {
  371. $condition['type'] = ['in', [2, 4]];
  372. return $this->belongsTo(Achievement::class, 'id', 'obj_id')->where($condition);
  373. }
  374. public function department()
  375. {
  376. return $this->belongsTo(StaffDepartment::class, 'department_id', 'id')->field('id,name as department_name')->bind('department_name');
  377. }
  378. //获取自己及下属员工
  379. public function admin()
  380. {
  381. return $this->belongsTo(Admin::class, 'admin_id', 'id')->field('id,username');
  382. }
  383. public function staffrole()
  384. {
  385. return $this->hasOne(StaffRole::class, 'id', 'role')->field('id,name');
  386. }
  387. //获取一个部门下 员工列表
  388. public function parent()
  389. {
  390. return $this->belongsTo(Staff::class, 'parent_id', 'id')->field('id,name as parent_name')->bind('parent_name');
  391. }
  392. //客户
  393. public function customer()
  394. {
  395. return $this->belongsTo(Customer::class, 'id', 'create_staff_id')->group('create_staff_id')->field('create_staff_id,count(*) as achieve')->bind('achieve');
  396. }
  397. //拜访
  398. public function visit()
  399. {
  400. return $this->belongsTo(Event::class, 'id', 'staff_id')->group('staff_id')->field('staff_id,count(*) as achieve')->where(['type' => 3, 'status' => 2])->bind('achieve');
  401. }
  402. //回款
  403. public function receivables()
  404. {
  405. 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');
  406. }
  407. //工单
  408. public function workorder()
  409. {
  410. return $this->belongsTo(Workorder::class, 'id', 'owner_staff_id')->group('owner_staff_id')->field('owner_staff_id,count(*) as achieve')->bind('achieve');
  411. }
  412. //合同
  413. public function contract()
  414. {
  415. 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');
  416. }
  417. }