123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252 |
- <?php
- namespace app\common\model;
- use think\Db;
- use think\Exception;
- class AttendanceGroup extends Base{
- public function updates(){
- $data = request()->post();
- $data['org_id'] = cur_org_id();
- $result = validate('AttendanceClass')->check($data,[],'');
- if(true !== $result){
- $this->error = validate('AttendanceClass')->getError();
- return false;
- }
- $id = $data['id'];
- $users = $data['users'];
- $data['class_id'] = $data['classId'];
- unset($data['users'],$data['id'],$data['classId']);
- Db::startTrans();
- try {
- if($id > 0){
- $data['update_time'] = date('Y-m-d H:i:s');
- $this->allowField(true)->save($data,['id'=>$id]);
- $groupId = $id;
- }else{
- $data['create_time'] = date('Y-m-d H:i:s');
- $groupId = db('attendance_group')->insertGetId($data);
- }
- // 更新考勤组用户
- $userids = $users?explode(',',$users):[];
- if($userids){
- // 删除已在其他组的人
- Db::name('attendance_group_user')->whereIn('user_id',$userids)->delete();
- Db::name('attendance_group_user')->whereNotIn('user_id',$userids)->where('group_id',$groupId)->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{
- $del = Db::name('attendance_group_user')->where('group_id',$groupId)->delete();
- if(!$del){
- exception('操作失败');
- }
- }
- Db::commit();
- return true;
- } catch (Exception $e) {
- $this->error = $e->getMessage();
- Db::rollback();
- return false;
- }
- }
- public function info($id){
- $info = Db::name('attendance_group')->where('id',$id)->find();
- if(!$info){
- return false;
- }
- $data = $info;
- $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'] = $this->getNameById($v['class_id']);
- }
- }
- foreach ($data['content']['sign'] as $k=>$v){
- if(!empty($v['class_id'])){
- $data['content']['sign'][$k]['class_name'] = $this->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'] = $this->getNameById($v['class_id'],2);
- $data['content']['circle']['class'][$k]['tname'] = $this->getNameById($v['class_id']);
- }
- }
- }
- if($data['content']){
- $data['content'] = json_encode($data['content']);
- }
- $userids = [];
- $users = Db::name('attendance_group_user')->field('user_id')->where('group_id',$data['id'])->select();
- $users = $users?$users:[];
- foreach ($users as $k=>$v){
- $userids[] = $v['user_id'];
- }
- if($data['class_id']){
- $classList = Db('attendance_class') ->whereIn('id',$data['class_id'])->select();
- foreach ($classList as $k=>$v){
- $classList[$k]['tname'] = $this->formatClassName($v);
- }
- $data['class_list'] = $classList?$classList:[];
- }else{
- $data['class_list'] = [];
- }
- $data['userIds'] = $userids?$userids:[];
- return $data;
- }
- public function getNameById($id,$type=1){
- $info = Db::name('attendance_class')->where('id',$id)->find();
- if(!$info){
- return false;
- }
- return $type==1?$this->formatClassName($info):$info['name'];
- }
- public function formatClassName($info){
- $info['content'] = json_decode($info['content'],true);
- $names = [];
- foreach ($info['content']['dates'] as $k=>$v){
- $str = '';
- if ($v['snext'] == 1) {
- $str .= '次日'.$v['stime'];
- } else {
- $str .= $v['stime'];
- }
- $str .= '-';
- if ($v['enext'] === 1) {
- $str .= '次日'.$v['etime'];
- } else {
- $str .= $v['etime'];
- }
- $names[] = $str;
- }
- return $info['name'].' '.implode(',',$names);
- }
- public function showOne($id){
- $info = Db::name('attendance_group')->where('id',$id)->find();
- return $info;
- }
- public function groupUpdates(){
- $data = request()->post();
- $gid = $data['gid'];
- $info = $this->showOne($gid);
- if(!$info){
- $this->error = '记录不存在';
- return false;
- }
- $content = json_decode($info['content'],true);
- $circle = $content['circle'];
- if($data['type'] == 1){
- if($data['class_id'] == 0){
- Db::name('attendance_group_class')->where('group_id',$gid)->where('day',$data['day'])->delete();
- }else{
- $info = Db::name('attendance_group_class')->where('group_id',$gid)->where('day',$data['day'])->find();
- if($info){
- if($info['day'] == $data['day']){
- Db::name('attendance_group_class')->where('id',$info['id'])->update(['class_id'=>$data['class_id']]);
- }
- }else{
- Db::name('attendance_group_class')->where('id',$info['id'])->insert([
- 'org_id' => cur_org_id(),
- 'group_id' => $gid,
- 'class_id' => $data['class_id'],
- 'day' => $data['day']
- ]);
- }
- }
- }else{
- if(!$data['week']){
- Db::name('attendance_group_class')->where('group_id',$gid)->where('day','>=',$data['day'])->delete();
- }else{
- $class = $circle['class'];
- $curmonth = date('Y-m',strtotime($data['day']));
- $days = [$data['day']];
- $i = 1;
- while (true){
- $tt = strtotime($data['day']) + $i*24*60*60;
- $month = date('Y-m',$tt);
- if($month == $curmonth){
- $days[] = date('Y-m-d',$tt);
- $i++;
- }else{
- break;
- }
- }
- $i = 0;
- $max = $circle['days'] - 1;
- foreach ($days as $k=>$v){
- if($class[$i]['class_id'] == 0) {
- Db::name('attendance_group_class')->where('group_id',$gid)->where('day',$v)->delete();
- }else {
- $info = Db::name('attendance_group_class')->where('group_id',$gid)->where('day',$v)->find();
- if($info){
- if($info['day'] != $v){
- Db::name('attendance_group_class')->where('id',$info['id'])->update(['class_id'=>$data[$i]['class_id']]);
- }
- }else{
- Db::name('attendance_group_class')->where('id',$info['id'])->insert([
- 'org_id' => cur_org_id(),
- 'group_id' => $gid,
- 'class_id' => $class[$i]['class_id'],
- 'day' => $v
- ]);
- }
- }
- if($i == $max) {
- $i=0;
- }else{
- $i++;
- }
- }
- }
- }
- return true;
- }
- }
|