123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173 |
- <?php
- namespace app\common\model;
- use app\hander\HelpHander;
- use think\Db;
- use think\Exception;
- use think\Model;
- class UserJob extends Model
- {
- public function saveBatch($uids,$jobId){
- $uids = $uids?explode(',',$uids):[];
- $userids = Db::name('user_job')->where('job_id',$jobId)->column('user_id');
- $userids = $userids?$userids:[];
- $jj = array_intersect($userids,$uids);
- if($jj){
- HelpHander::error('选择的人员中已有分配过该岗位');
- }
- Db::startTrans();
- try{
- $data = [];
- foreach ($uids as $k=>$v){
- $data[] = [
- 'user_id' => $v,
- 'job_id' => $jobId,
- ];
- }
- $ret = Db::name('user_job')->insertAll($data);
- if($ret != count($uids)){
- \exception('操作失败');
- }
- Db::commit();
- }catch (Exception $e){
- Db::rollback();
- HelpHander::error('操作失败');
- }
- return true;
- }
- public function del($uid,$jobId){
- $ret = Db::name('user_job')->where('user_id',$uid)->where('job_id',$jobId)->delete();
- if(!$ret){
- HelpHander::error('操作失败');
- }
- return true;
- }
- // 调整岗位
- public function changeJob($data,$userId,$orgId){
- $result = validate('UserInfo')->check($data,[],'job');
- if(true !== $result){
- HelpHander::error(validate('UserInfo')->getError());
- }
- Db::startTrans();
- try{
- if($data['type'] == 1){ // 添加岗位
- // 新增调整岗位
- $ujid = Db::name('user_job')->insertGetId([
- 'user_id' => $data['uid'],
- 'job_id' => $data['jobId'],
- ]);
- if(!$ujid){
- \exception('操作失败');
- }
- } else {
- $ret = Db::name('user_job')
- ->where('user_id',$data['uid'])
- ->where('job_id',$data['jobId'])
- ->delete();
- if(!$ret){
- \exception('操作失败');
- }
- $ujid = 0;
- }
- $ret = Db::name('user_job_log')->insert([
- 'org_id' => $orgId,
- 'user_id' => $data['uid'],
- 'job_id' => $data['jobId'],
- 'create_user_id' => $userId,
- 'create_time' => date('Y-m-d H:i:s'),
- 'ujid' => $ujid,
- 'type' => $data['type'],
- 'remark' => $data['remark'],
- 'imgs' => $data['imgs']
- ]);
- if(!$ret){
- \exception('操作失败');
- }
- Db::commit();
- }catch (Exception $e){
- Db::rollback();
- HelpHander::error('调整岗位失败');
- }
- return true;
- }
- public function jobLogList($page,$size,$orgId,$userId=0){
- $map[] = ['ugl.org_id','=',$orgId];
- if($userId > 0){
- $map[] = ['ugl.user_id','=',$userId];
- }
- $lists = Db::name('user_job_log')
- ->alias('ugl')
- ->join('job j','j.id = ugl.job_id')
- ->where($map)
- ->field('ugl.*,j.dep_id,j.name as jobName')
- ->page($page,$size)
- ->order('ugl.id desc')
- ->select();
- $lists = $lists?$lists:[];
- foreach ($lists as $k=>$v){
- $lists[$k]['depName'] = Db::name('dep')->where('id',$v['dep_id'])->value('name');
- $lists[$k]['createUserName'] = Db::name('user_info')->where('user_id',$v['create_user_id'])->value('name');
- }
- $total = Db::name('user_job_log')
- ->alias('ugl')
- ->join('job j','j.id = ugl.job_id')
- ->where($map)->count();
- $data = [
- 'total' => $total,
- 'list' => $lists?$lists:[]
- ];
- return $data;
- }
- // 根据部门获取用户
- public function getListByDep($depId){
- $jobs = Db::name('job')
- ->where('dep_id',$depId)
- ->where('del',0)
- ->where('enable',1)
- ->column('id');
- if(!$jobs){
- return [];
- }
- $user = Db::name('user')
- ->alias('u')
- ->join('user_info ui',' ui.user_id = u.id')
- ->join('user_job uj','uj.user_id = u.id')
- ->where('u.del', 0)
- ->where('u.enable', 1)
- ->where('uj.job_id','in',$jobs)
- ->distinct(true)
- ->order('u.sorts asc,u.id asc')
- ->field('u.id,ui.name,u.phone')
- ->select();
- $user = $user?$user:[];
- foreach ($user as $k=>$v){
- $user[$k]['type'] = 1;
- $user[$k]['depAndUserDtoList'] = [];
- $ujobs = Db::name('user_job')
- ->alias('uj')
- ->join('job j','j.id = uj.job_id')
- ->where('uj.job_id', 'in', $jobs)
- ->where('uj.user_id',$v['id'])
- ->column('j.name');
- $user[$k]['jobList'] = $ujobs?$ujobs:[];
- }
- return $user?$user:[];
- }
- }
|