diff --git a/niucloud/app/api/controller/apiController/Attendance.php b/niucloud/app/api/controller/apiController/Attendance.php new file mode 100644 index 00000000..3524d828 --- /dev/null +++ b/niucloud/app/api/controller/apiController/Attendance.php @@ -0,0 +1,141 @@ + $request->param('campus_id', ''),// + 'staff_id' => $request->param('staff_id', ''),// + 'attendance_date' => $request->param('attendance_date', ''),// + 'status' => $request->param('status', ''),// + 'status_arr' => $request->param('status_arr', []),// + ]; + + $res = (new AttendanceService())->getList($where); + return success($res); + } + + //员工考勤-编辑(员工打卡/请假/签退) + public function edit(Request $request){ + $campus_id =$request->param('campus_id','');//校区ID + $staff_id =$this->member_id;//人员ID + $attendance_date =$request->param('attendance_date','');//考勤日期 + $remarks =$request->param('remarks','');//备注 + $status =$request->param('status','');//考勤状态: present-出勤, absent-缺勤, late-迟到, leave_early-早退,leave-请假,sign_out-签退 + $longitude = $request->param('longitude','');//经度 + $latitude = $request->param('latitude','');//纬度 + $coordinate = '';//坐标 + if(!empty($longitude) && !empty($latitude)){ + $coordinate = "{$longitude},{$latitude}"; + } + + if(empty($campus_id) || empty($attendance_date) || empty($status)){ + return fail('缺少参数'); + } + if(!in_array($status,['present','absent','late','leave_early','leave','sign_out'])){ + return fail('状态类型不正确'); + } + + //查询数据是否存在,一天一个校区同一个人只能产生一条数据 + $date_h = date('H:i:s'); + + $obj = (new AttendanceService()); + $info = $obj->info([ + 'campus_id'=>$campus_id, + 'staff_id'=>$staff_id, + 'attendance_date'=>$attendance_date, + ])['data']; + + //如果是签退的时候就是修改 + if($status == 'sign_out'){ + + if (($info['status'] ?? '') != 'present') { + return fail('未查询到今日签到信息,无法签退'); + } + + //签退的情况 + $where = [ + 'id'=>$info['id'] + ]; + $data = [ + 'check_out_time'=>$date_h,//签退时间 + 'coordinate'=>$coordinate,//坐标|经度,纬度 + ]; + //执行修改操作 + $res = $obj->editData($where,$data); + }else{ + //如果是签到/请假 + $data = [ + 'campus_id'=>$campus_id,//校区ID + 'staff_id'=>$staff_id,//人员ID + 'attendance_date'=>$attendance_date,//考勤日期 + 'coordinate'=>$coordinate,//坐标|经度,纬度 + 'status'=>$status,//考勤状态 + ]; + //如果是签到的情况 + if($info){ + if($status == 'present' && $info['status'] == 'present'){ + //更新签到数据 + //执行修改操作 + $edit_data = [ + 'check_in_time'=>$date_h,//签到时间 + 'coordinate'=>$coordinate,//坐标|经度,纬度 + 'status'=>$status//考勤状态 + ]; + $res = $obj->editData(['id'=>$info['id']],$edit_data); + if(!$res['code']){ + return fail($res['msg']); + } + return success($res['data']); + } + $status_arr = (new \app\model\attendance\Attendance())::STATUS; + $status_name = $status_arr[$info['status']] ?? ''; + if(!$status_name){ + $status_name = '考勤'; + } + return fail("今日已{$status_name}重复操作"); + }else{ + $data['check_in_time'] = $date_h;//签到时间 + if($remarks){ + $data['remarks'] = $remarks; + } + //执行创建操作 + $res = $obj->addData($data); + if(!$res['code']){ + return fail($res['msg']); + } + return success($res['data']); + } + + + + + + + + } + } +} diff --git a/niucloud/app/api/route/route.php b/niucloud/app/api/route/route.php index 8c10a1c0..52ae6296 100644 --- a/niucloud/app/api/route/route.php +++ b/niucloud/app/api/route/route.php @@ -233,7 +233,12 @@ Route::group(function () { Route::post('communicationRecords/add', 'apiController.CommunicationRecords/add'); //校区-获取员工下的全部校区 - Route::get('campus/getPersonnelCampus', 'apiController.campus/getPersonnelCampus'); + Route::get('campus/getPersonnelCampus', 'apiController.Campus/getPersonnelCampus'); + + //员工考勤-列表(员工打卡/请假/签退) + Route::post('attendance/index', 'apiController.Attendance/index'); + //员工考勤-编辑(员工打卡/请假/签退) + Route::post('attendance/edit', 'apiController.Attendance/edit'); diff --git a/niucloud/app/model/attendance/Attendance.php b/niucloud/app/model/attendance/Attendance.php index dc07ee30..744b22a6 100644 --- a/niucloud/app/model/attendance/Attendance.php +++ b/niucloud/app/model/attendance/Attendance.php @@ -11,6 +11,7 @@ namespace app\model\attendance; +use app\model\dict\Dict; use core\base\BaseModel; use think\model\concern\SoftDelete; use think\model\relation\HasMany; @@ -42,7 +43,14 @@ class Attendance extends BaseModel */ protected $name = 'attendance'; - + //状态类型 + const STATUS = [ + 'present'=>'出勤', + 'absent'=>'缺勤', + 'late'=>'迟到', + 'leave_early'=>'早退', + 'leave'=>'请假' + ]; @@ -93,6 +101,35 @@ class Attendance extends BaseModel $query->where("status", $value); } } + + + + /** + * 获取考勤状态状态类型名称 + * @param $value + * @param $data + * @return array|mixed|string + */ + public function getStatusNameAttr($value, $data) + { + $key = 'kq_status'; + $val = (String)$data['status']; + if ((!empty($val) || isset($val)) && $val !== '') { + $dict = Dict::where('key',$key)->find(); + $dictionary = $dict['dictionary'] ?? []; + // 查找匹配的 name + $res = ''; + foreach ($dictionary as $item) { + if ($item['value'] == $val) { + $res = $item['name']; + break; + } + } + return $res; + } else { + return ''; + } + } diff --git a/niucloud/app/service/api/apiService/AttendanceService.php b/niucloud/app/service/api/apiService/AttendanceService.php new file mode 100644 index 00000000..1eb2a255 --- /dev/null +++ b/niucloud/app/service/api/apiService/AttendanceService.php @@ -0,0 +1,156 @@ +model = (new Attendance()); + } + + //列表 + public function getList(array $where, string $field = '*') + { + $page_params = $this->getPageParam();//获取请求参数中的页码+分页数 + $page = $page_params['page']; + $limit = $page_params['limit']; + + $model = $this->model; + + if (!empty($where['campus_id'])) { + $model = $model->where('campus_id', $where['campus_id']); + } + if (!empty($where['staff_id'])) { + $model = $model->where('staff_id', $where['staff_id']); + } + if (!empty($where['attendance_date'])) { + $model = $model->where('attendance_date', $where['attendance_date']); + } + if (!empty($where['status'])) { + $model = $model->where('status', $where['status']); + } + if (!empty($where['status_arr'])) { + $model = $model->whereIn('status', $where['status_arr']); + } + + $res = $model->with([ + 'campus', + 'personnel', + ])->append([ + 'status_name' + ]) + ->order('created_at','desc') + ->paginate([ + 'list_rows' => $limit, + 'page' => $page, + ])->toArray(); + return $res; + } + + //查询详情 + public function info(array $where, string $field = '*') + { + $res = [ + 'code' => 0, + 'msg' => '操作失败', + 'data' => [], + ]; + if (empty($where)) { + $res['msg'] = '筛选条件不能唯空'; + return $res; + } + $model = $this->model; + if (!empty($where['id'])) { + $model = $model->where('id', $where['id']); + } + if (!empty($where['campus_id'])) { + $model = $model->where('campus_id', $where['campus_id']); + } + if (!empty($where['staff_id'])) { + $model = $model->where('staff_id', $where['staff_id']); + } + if (!empty($where['attendance_date'])) { + $model = $model->where('attendance_date', $where['attendance_date']); + } + $data = $model->field($field)->find(); + if ($data) { + $res = [ + 'code' => 1, + 'msg' => '操作成功', + 'data' => $data->toArray(), + ]; + } else { + $res['msg'] = '暂无数据'; + } + + return $res; + } + + //添加数据 + public function addData(array $data) + { + + $add = $this->model->create($data); + if ($add) { + $res = [ + 'code' => 1, + 'msg' => '操作成功', + 'data' => [], + ]; + } else { + $res = [ + 'code' => 0, + 'msg' => '操作失败', + 'data' => [], + ]; + } + return $res; + } + + //编辑数据 + public function editData(array $where, array $data) + { + + $data['updated_at'] = date('Y-m-d H:i:s'); + $model = $this->model; + if (!empty($where['id'])) { + $model = $model->where('id', $where['id']); + } + $edit = $model->update($data); + if ($edit) { + $res = [ + 'code' => 1, + 'msg' => '操作成功', + 'data' => [], + ]; + } else { + $res = [ + 'code' => 0, + 'msg' => '操作失败', + 'data' => [], + ]; + } + return $res; + } + + +}