123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189 |
- <?php
- namespace app\common\model;
- use app\hander\HelpHander;
- use think\Db;
- use think\Exception;
- use think\Model;
- class AttendanceGroup extends Model
- {
- public function add(){
- $data = [
- 'id' => input('id/d',0),
- 'name' => input('name','','trim'),
- 'type' => input('type/d',0),
- 'cate' => input('cate','','trim'),
- 'addr' => input('addr','','trim'),
- 'content' => input('content','','trim'),
- 'class_id' => input('classId','','trim'),
- 'org_id' => input('orgId/d',0),
- 'users' => input('users','','trim')
- ];
- $result = validate('AttendanceGroup')->check($data,[],'');
- if(true !== $result){
- HelpHander::error(validate('AttendanceGroup')->getError());
- }
- $id = $data['id'];
- unset($data['id']);
- $users = $data['users'];
- Db::startTrans();
- try{
- $groupId = 0;
- if($id > 0){
- $groupId = $id;
- $data['update_time'] = date('Y-m-d H:i:s');
- $ret = $this->allowField(true)->save($data,['id'=>$id]);
- }else{
- $data['create_time'] = date('Y-m-d H:i:s');
- $ret = $this->allowField(true)->save($data);
- $groupId = $this->id;
- }
- if(!$ret){
- \exception('操作失败');
- }
- // 更新考勤组用户
- $users = json_decode($users,true);
- $userids = [];
- $jobs = [];
- foreach ($users as $k=>$v){
- if($v['type'] == 2){
- $userids[] = $v['id'];
- }else{
- $jres = model('Dep')->getJobByDep($v['id']);
- $jobs = array_merge($jobs,$jres);
- }
- }
- if($jobs){
- $userids2 = Db::name('user_job')->where('job_id','in',$jobs)->column('user_id');
- if($userids2){
- $userids = array_unique(array_merge($userids,$userids2));
- }
- }
- if($userids){
- // 删除已在其他组的人
- Db::name('attendance_group_user')->where('user_id','in',$userids)->delete();
- // 删除本组的其他人
- Db::name('attendance_group_user')->where('group_id',$groupId)->where('user_id','not in',$userids)->delete();
- // TODO::删除人员分组
- $arr = [];
- foreach ($userids as $k=>$v){
- $arr[] = [
- 'org_id' => $data['org_id'],
- 'group_id' => $groupId,
- 'user_id' => $v
- ];
- if(count($arr) == 50){
- $count = Db::name('attendance_group_user')->insertAll($arr);
- if($count != count($arr)){
- \exception('保存失败');
- }else{
- $arr = [];
- }
- }
- }
- if(count($arr) > 0){
- $count = Db::name('attendance_group_user')->insertAll($arr);
- if($count != count($arr)){
- \exception('保存失败');
- }
- }
- }else{ // 为空删除组的所有人
- // 删除本组的人
- Db::name('attendance_group_user')->where('group_id',$groupId)->delete();
- }
- Db::commit();
- }catch (Exception $e){
- Db::rollback();
- HelpHander::error($e->getMessage());
- }
- return true;
- }
- public function info($id){
- $info = $this->where('id',$id)->find();
- if(!$info){
- HelpHander::error('数据不存在');
- }
- $data = $info->toArray();
- $data['content'] = $data?json_decode($data['content'],true):'';
- if($data['type'] == 1 && $data['content']){
- foreach ($data['content']['week'] as $k=>$v){
- if(!empty($v['class_id'])){
- $data['content']['week'][$k]['class_name'] = model('AttendanceClass')->getNameById($v['class_id']);
- }
- }
- foreach ($data['content']['sign'] as $k=>$v){
- if(!empty($v['class_id'])){
- $data['content']['sign'][$k]['class_name'] = model('AttendanceClass')->getNameById($v['class_id']);
- }
- }
- }else if($data['type'] == 2 && $data['content'] && isset($data['content']['circle'])){
- foreach ($data['content']['circle']['class'] as $k=>$v){
- if(!empty($v['class_id'])){
- $data['content']['circle']['class'][$k]['name'] = Db::name('AttendanceClass')->where('id',$v['class_id'])->value('name');
- $data['content']['circle']['class'][$k]['tname'] = model('AttendanceClass')->getNameById($v['class_id']);
- }
- }
- }
- if($data['content']){
- $data['content'] = json_encode($data['content']);
- }
- $userids = Db::name('attendance_group_user')->where('group_id',$data['id'])->column('user_id');
- if($data['class_id']){
- $classList = Db::name('attendance_class')->where('id','in',$data['class_id'])->select();
- foreach ($classList as $k=>$v){
- $classList[$k]['tname'] = model('AttendanceClass')->formatClassName($v);
- }
- $data['class_list'] = $classList?$classList:[];
- }else{
- $data['class_list'] = [];
- }
- $data['userIds'] = $userids?$userids:[];
- return $data;
- }
- public function lists($page,$size,$name,$orgId){
- $map[] = ['del','=',0];
- $map[] = ['org_id','=',$orgId];
- if($name != ''){
- $map[] = ['name','like','%'.$name.'%'];
- }
- $lists = $this
- ->where($map)
- ->page($page,$size)
- ->order('id desc')
- ->select();
- $total = $this->where($map)->count();
- $data = [
- 'total' => $total,
- 'list' => $lists?$lists->toArray():[]
- ];
- return $data;
- }
- public function del($id){
- $ret = $this->where('id',$id)->setField('del',1);
- if(!$ret){
- HelpHander::error('删除失败');
- }
- return true;
- }
- }
|