UserJob.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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 UserJob extends Model
  8. {
  9. public function saveBatch($uids,$jobId){
  10. $uids = $uids?explode(',',$uids):[];
  11. $userids = Db::name('user_job')->where('job_id',$jobId)->column('user_id');
  12. $userids = $userids?$userids:[];
  13. $jj = array_intersect($userids,$uids);
  14. if($jj){
  15. HelpHander::error('选择的人员中已有分配过该岗位');
  16. }
  17. Db::startTrans();
  18. try{
  19. $data = [];
  20. foreach ($uids as $k=>$v){
  21. $data[] = [
  22. 'user_id' => $v,
  23. 'job_id' => $jobId,
  24. ];
  25. }
  26. $ret = Db::name('user_job')->insertAll($data);
  27. if($ret != count($uids)){
  28. \exception('操作失败');
  29. }
  30. Db::commit();
  31. }catch (Exception $e){
  32. Db::rollback();
  33. HelpHander::error('操作失败');
  34. }
  35. return true;
  36. }
  37. public function del($uid,$jobId){
  38. $ret = Db::name('user_job')->where('user_id',$uid)->where('job_id',$jobId)->delete();
  39. if(!$ret){
  40. HelpHander::error('操作失败');
  41. }
  42. return true;
  43. }
  44. // 调整岗位
  45. public function changeJob($data,$userId,$orgId){
  46. $result = validate('UserInfo')->check($data,[],'job');
  47. if(true !== $result){
  48. HelpHander::error(validate('UserInfo')->getError());
  49. }
  50. Db::startTrans();
  51. try{
  52. if($data['type'] == 1){ // 添加岗位
  53. // 新增调整岗位
  54. $ujid = Db::name('user_job')->insertGetId([
  55. 'user_id' => $data['uid'],
  56. 'job_id' => $data['jobId'],
  57. ]);
  58. if(!$ujid){
  59. \exception('操作失败');
  60. }
  61. } else {
  62. $ret = Db::name('user_job')
  63. ->where('user_id',$data['uid'])
  64. ->where('job_id',$data['jobId'])
  65. ->delete();
  66. if(!$ret){
  67. \exception('操作失败');
  68. }
  69. $ujid = 0;
  70. }
  71. $ret = Db::name('user_job_log')->insert([
  72. 'org_id' => $orgId,
  73. 'user_id' => $data['uid'],
  74. 'job_id' => $data['jobId'],
  75. 'create_user_id' => $userId,
  76. 'create_time' => date('Y-m-d H:i:s'),
  77. 'ujid' => $ujid,
  78. 'type' => $data['type'],
  79. 'remark' => $data['remark'],
  80. 'imgs' => $data['imgs']
  81. ]);
  82. if(!$ret){
  83. \exception('操作失败');
  84. }
  85. Db::commit();
  86. }catch (Exception $e){
  87. Db::rollback();
  88. HelpHander::error('调整岗位失败');
  89. }
  90. return true;
  91. }
  92. public function jobLogList($page,$size,$orgId,$userId=0){
  93. $map[] = ['ugl.org_id','=',$orgId];
  94. if($userId > 0){
  95. $map[] = ['ugl.user_id','=',$userId];
  96. }
  97. $lists = Db::name('user_job_log')
  98. ->alias('ugl')
  99. ->join('job j','j.id = ugl.job_id')
  100. ->where($map)
  101. ->field('ugl.*,j.dep_id,j.name as jobName')
  102. ->page($page,$size)
  103. ->order('ugl.id desc')
  104. ->select();
  105. $lists = $lists?$lists:[];
  106. foreach ($lists as $k=>$v){
  107. $lists[$k]['depName'] = Db::name('dep')->where('id',$v['dep_id'])->value('name');
  108. $lists[$k]['createUserName'] = Db::name('user_info')->where('user_id',$v['create_user_id'])->value('name');
  109. }
  110. $total = Db::name('user_job_log')
  111. ->alias('ugl')
  112. ->join('job j','j.id = ugl.job_id')
  113. ->where($map)->count();
  114. $data = [
  115. 'total' => $total,
  116. 'list' => $lists?$lists:[]
  117. ];
  118. return $data;
  119. }
  120. // 根据部门获取用户
  121. public function getListByDep($depId){
  122. $jobs = Db::name('job')
  123. ->where('dep_id',$depId)
  124. ->where('del',0)
  125. ->where('enable',1)
  126. ->column('id');
  127. if(!$jobs){
  128. return [];
  129. }
  130. $user = Db::name('user')
  131. ->alias('u')
  132. ->join('user_info ui',' ui.user_id = u.id')
  133. ->join('user_job uj','uj.user_id = u.id')
  134. ->where('u.del', 0)
  135. ->where('u.enable', 1)
  136. ->where('uj.job_id','in',$jobs)
  137. ->distinct(true)
  138. ->order('u.sorts asc,u.id asc')
  139. ->field('u.id,ui.name,u.phone')
  140. ->select();
  141. $user = $user?$user:[];
  142. foreach ($user as $k=>$v){
  143. $user[$k]['type'] = 1;
  144. $user[$k]['depAndUserDtoList'] = [];
  145. $ujobs = Db::name('user_job')
  146. ->alias('uj')
  147. ->join('job j','j.id = uj.job_id')
  148. ->where('uj.job_id', 'in', $jobs)
  149. ->where('uj.user_id',$v['id'])
  150. ->column('j.name');
  151. $user[$k]['jobList'] = $ujobs?$ujobs:[];
  152. }
  153. return $user?$user:[];
  154. }
  155. }