StaffDepartment.php 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. <?php
  2. namespace addons\qingdong\model;
  3. use think\Model;
  4. use traits\model\SoftDelete;
  5. /**
  6. *员工部门表
  7. */
  8. class StaffDepartment Extends Model {
  9. use SoftDelete;
  10. // 表名,不含前缀
  11. protected $name = 'qingdong_staff_department';
  12. // 开启自动写入时间戳字段
  13. protected $autoWriteTimestamp = 'int';
  14. // 定义时间戳字段名
  15. protected $createTime = 'createtime';
  16. protected $updateTime = 'updatetime';
  17. protected $deleteTime = 'deletetime';
  18. //员工业绩
  19. public function achievement() {
  20. return $this->belongsTo(Achievement::class, 'id', 'obj_id')->where(['type' => 2]);
  21. }
  22. //获取部门列表
  23. public static function getDepartmentList() {
  24. $department = self::where([])->field('id,name,pid')->select();
  25. $data = [];
  26. foreach ($department as $v) {
  27. $data[$v['pid']][] = $v;
  28. }
  29. return self::getChilds($data, 0);
  30. }
  31. //获取部门及所有下级部门
  32. public static function getDepartmentLowerId($l_ids,$top=true){
  33. $ids=self::where(['pid' =>['in',$l_ids]])->column('id');
  34. if ($ids) {
  35. $w_ids = self::getDepartmentLowerId($ids,false);
  36. $ids = array_merge($ids, $w_ids);
  37. }else{
  38. $ids=[];
  39. }
  40. if($top){
  41. $ids=array_merge($ids,$l_ids);
  42. }
  43. return $ids;
  44. }
  45. //生成树状结构
  46. private static function getChilds($data, $pid) {
  47. $list = [];
  48. if (isset($data[$pid])) {
  49. $list = $data[$pid];
  50. foreach ($list as $k => $v) {
  51. $v['children'] = self::getChilds($data, $v['id']);
  52. $list[$k] = $v;
  53. }
  54. }
  55. return $list;
  56. }
  57. //获取部门列表
  58. public static function getList()
  59. {
  60. return self::where([])->field('id,name')->select();
  61. }
  62. }