Job.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. <?php
  2. namespace app\common\model;
  3. use app\hander\HelpHander;
  4. use think\Db;
  5. use think\Exception;
  6. use think\Model;
  7. class Job extends Model
  8. {
  9. public function add(){
  10. $data = [
  11. 'id' => input('id/d',0),
  12. 'name' => input('name','','trim'),
  13. 'level' => input('level','','trim'),
  14. 'content' => input('content','','trim'),
  15. 'org_id' => input('orgId/d',0),
  16. 'dep_id' => input('depId/d',0),
  17. 'type' => input('type/d',0),
  18. 'leader' => input('leader/d',0),
  19. ];
  20. $result = validate('Job')->check($data,[],'');
  21. if(true !== $result){
  22. HelpHander::error(validate('Job')->getError());
  23. }
  24. $id = $data['id'];
  25. unset($data['id']);
  26. if($id > 0){
  27. unset($data['org_id']);
  28. unset($data['dep_id']);
  29. $ret = $this->allowField(true)->save($data,['id'=>$id]);
  30. }else{
  31. $maxcode = Db::name('job')->order('id desc')->value('code');
  32. if($maxcode){
  33. $data['code'] = $maxcode + 1;
  34. }else{
  35. $data['code'] = '001';
  36. }
  37. $ret = $this->allowField(true)->save($data);
  38. }
  39. if(!$ret){
  40. HelpHander::error('操作失败');
  41. }
  42. return true;
  43. }
  44. public function info($id){
  45. $info = $this->where('id',$id)->where('del',0)->find();
  46. if(!$info){
  47. HelpHander::error('数据不存在');
  48. }
  49. return $info->toArray();
  50. }
  51. public function lists($page,$size){
  52. $lists = $this
  53. ->where('del',0)
  54. ->page($page,$size)
  55. ->order('id asc')
  56. ->select();
  57. return $lists?$lists->toArray():[];
  58. }
  59. public function changeStatus($id,$enable){
  60. $ret = $this->where('id',$id)->setField('enable',$enable);
  61. if(!$ret){
  62. HelpHander::error('操作失败');
  63. }
  64. return true;
  65. }
  66. /**
  67. * 根据部门id集合获取所有岗位id集合
  68. * @param $depids
  69. * @return array
  70. */
  71. public function getJobByDeps($depids){
  72. $children = Db::name('job')
  73. ->where('del',0)
  74. ->where('enable',1)
  75. ->where('dep_id','in',$depids)
  76. ->column('id');
  77. return $children ? $children : [];
  78. }
  79. // 部门下的岗位
  80. public function queryJobByDep($depId){
  81. $lists = $this
  82. ->where('dep_id',$depId)
  83. ->where('del',0)
  84. ->order('id asc')
  85. ->select();
  86. return $lists ? $lists->toArray() : [];
  87. }
  88. // 部门下的岗位
  89. public function queryJobByOrg($orgId){
  90. $lists = $this
  91. ->where('org_id',$orgId)
  92. ->where('del',0)
  93. ->order('id asc')
  94. ->select();
  95. return $lists ? $lists->toArray() : [];
  96. }
  97. //查询当前岗位下未分配的人员
  98. public function screenUserByJob($page,$size,$jobId,$param){
  99. // 去除管理员
  100. $uids = model('User')->getAdminUsers();
  101. // 去除已分配人员
  102. $userids = Db::name('user_job')->where('job_id',$jobId)->column('user_id');
  103. $userids = $userids?$userids:[];
  104. $userids = array_merge($uids,$userids);
  105. if($userids){
  106. $map[] = ['u.id','not in',$userids];
  107. }
  108. if($param){
  109. $map[] = ['ui.name|u.code','like','%'.$param.'%'];
  110. }
  111. $map[] = ['u.del','=',0];
  112. $lists = Db::name('user')
  113. ->alias('u')
  114. ->join('user_info ui','ui.user_id = u.id')
  115. ->where($map)
  116. ->page($page,$size)
  117. ->field('u.id,u.code,u.del,ui.device_sn,u.enable,u.phone,ui.join_time,ui.name as userName')
  118. ->order('u.id desc')
  119. ->select();
  120. $total = Db::name('user')
  121. ->alias('u')
  122. ->join('user_info ui','ui.user_id = u.id')
  123. ->where($map)->count();
  124. $data = [
  125. 'total' => $total,
  126. 'list' => $lists?$lists:[]
  127. ];
  128. return $data;
  129. }
  130. //查询当前岗位分配的人员
  131. public function queryUserByJob($page,$size,$jobId,$param){
  132. if($param){
  133. $map[] = ['ui.name|u.code','like','%'.$param.'%'];
  134. }
  135. $map[] = ['u.del','=',0];
  136. $map[] = ['uj.job_id','=',$jobId];
  137. $lists = Db::name('user_job')
  138. ->alias('uj')
  139. ->join('user_info ui','ui.user_id = uj.user_id')
  140. ->join('user u','uj.user_id = u.id')
  141. ->where($map)
  142. ->page($page,$size)
  143. ->field('u.id,u.code,u.del,ui.device_sn,u.enable,u.phone,ui.join_time,ui.name as userName')
  144. ->order('u.id asc')
  145. ->select();
  146. $total = Db::name('user_job')
  147. ->alias('uj')
  148. ->join('user_info ui','ui.user_id = uj.user_id')
  149. ->join('user u','uj.user_id = u.id')
  150. ->where($map)->count();
  151. $data = [
  152. 'total' => $total,
  153. 'list' => $lists?$lists:[]
  154. ];
  155. return $data;
  156. }
  157. }