User.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. <?php
  2. namespace app\admin\model;
  3. use think\Model;
  4. use think\Db;
  5. use fast\Tree;
  6. class User extends Model
  7. {
  8. // 表名
  9. protected $table = 'user';
  10. // 自动写入时间戳字段
  11. protected $autoWriteTimestamp = 'integer';
  12. // 定义时间戳字段名
  13. protected $createTime = 'createtime';
  14. protected $updateTime = 'updatetime';
  15. protected $deleteTime = false;
  16. // 追加属性
  17. protected $append = [
  18. 'gender_text',
  19. 'prevtime_text',
  20. 'logintime_text',
  21. 'jointime_text',
  22. 'status_text',
  23. 'idcard_status_text'
  24. ];
  25. public function getGenderList()
  26. {
  27. return ['1' => __('Gender 1'), '0' => __('Gender 0')];
  28. }
  29. public function getStatusList()
  30. {
  31. return ['1' => __('Status 1'), '0' => __('Status 0'), '-1' => __('Status -1')];
  32. }
  33. public function getIdcardStatusList()
  34. {
  35. return ['-1' => __('Idcard_status -1'), '0' => __('Idcard_status 0'), '1' => __('Idcard_status 1'), '2' => __('Idcard_status 2')];
  36. }
  37. public function getGenderTextAttr($value, $data)
  38. {
  39. $value = $value ? $value : (isset($data['gender']) ? $data['gender'] : '');
  40. $list = $this->getGenderList();
  41. return isset($list[$value]) ? $list[$value] : '';
  42. }
  43. public function getPrevtimeTextAttr($value, $data)
  44. {
  45. $value = $value ? $value : (isset($data['prevtime']) ? $data['prevtime'] : '');
  46. return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
  47. }
  48. public function getLogintimeTextAttr($value, $data)
  49. {
  50. $value = $value ? $value : (isset($data['logintime']) ? $data['logintime'] : '');
  51. return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
  52. }
  53. public function getJointimeTextAttr($value, $data)
  54. {
  55. $value = $value ? $value : (isset($data['jointime']) ? $data['jointime'] : '');
  56. return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
  57. }
  58. public function getStatusTextAttr($value, $data)
  59. {
  60. $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
  61. $list = $this->getStatusList();
  62. return isset($list[$value]) ? $list[$value] : '';
  63. }
  64. public function getIdcardStatusTextAttr($value, $data)
  65. {
  66. $value = $value ? $value : (isset($data['idcard_status']) ? $data['idcard_status'] : '');
  67. $list = $this->getIdcardStatusList();
  68. return isset($list[$value]) ? $list[$value] : '';
  69. }
  70. protected function setPrevtimeAttr($value)
  71. {
  72. return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
  73. }
  74. protected function setLogintimeAttr($value)
  75. {
  76. return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
  77. }
  78. protected function setJointimeAttr($value)
  79. {
  80. return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
  81. }
  82. public function gangwei()
  83. {
  84. return $this->belongsTo('app\admin\model\user\Gangwei', 'gangwei_id', 'id', [], 'LEFT')->setEagerlyType(0);
  85. }
  86. public function jigou()
  87. {
  88. return $this->belongsTo('app\admin\model\user\Jigou', 'jigou_id', 'id', [], 'LEFT')->setEagerlyType(0);
  89. }
  90. public static function getTreeList($selected = [])
  91. {
  92. //用户列表,重置id,防止和岗位表的id重复
  93. $userlist = Db::name('user')->field('id,gangwei_id as pid,nickname as name')->select();
  94. foreach($userlist as $key => $user){
  95. $userlist[$key]['id'] = 'u_'.$user['id'];
  96. $userlist[$key]['spacer'] = '';
  97. $userlist[$key]['haschild'] = 0;
  98. }
  99. //岗位树
  100. $ruleList = Db::name('user_gangwei')->field('id,pid,name')->select();
  101. Tree::instance()->init($ruleList);
  102. Tree::instance()->icon = ['','',''];
  103. Tree::instance()->nbsp = '';
  104. $ruleList = Tree::instance()->getTreeList(Tree::instance()->getTreeArray(0), 'name');
  105. //修改haschild
  106. //拿到hasChildrens
  107. $hasChildrens = [];
  108. foreach ($ruleList as $k => $v)
  109. {
  110. foreach($userlist as $key => $user){
  111. if($user['pid'] == $v['id']){
  112. $v['haschild'] = 1;
  113. }
  114. }
  115. $ruleList[$k] = $v;
  116. if ($v['haschild']){
  117. $hasChildrens[] = $v['id'];
  118. }
  119. }
  120. //合并
  121. $ruleList = array_merge($ruleList,$userlist);
  122. //最终数据
  123. $nodeList = [];
  124. foreach ($ruleList as $k => $v) {
  125. $state = array('selected' => in_array($v['id'], $selected) && !in_array($v['id'], $hasChildrens));
  126. $nodeList[] = array('id' => $v['id'], 'parent' => $v['pid'] ? $v['pid'] : '#', 'text' => $v['name'], 'type' => 'menu', 'state' => $state);
  127. }
  128. return $nodeList;
  129. }
  130. }