calendarRule($data, $user, false); if(!$status) return [$status,$msg]; try { DB::beginTransaction(); $model = Calendar::where('id',$data['id'])->first(); $model->time = $data['time'] ?? 0; $model->work_days = $data['work_days'] ?? 0; $model->save(); $time = time(); CalendarDetails::where('del_time',0) ->where('calendar_id', $model->id) ->update(['del_time' => $time]); $this->saveDetail($model->id, $time, $data); DB::commit(); }catch (\Exception $exception){ DB::rollBack(); return [false,$exception->getMessage()]; } return [true, '']; } public function calendarAdd($data,$user){ list($status,$msg) = $this->calendarRule($data, $user); if(!$status) return [$status,$msg]; try { DB::beginTransaction(); $model = new Calendar(); $model->time = $data['time'] ?? 0; $model->work_days = $data['work_days'] ?? 0; $model->crt_id = $user['id']; $model->top_depart_id = $data['top_depart_id']; $model->save(); $this->saveDetail($model->id, time(), $data); DB::commit(); }catch (\Exception $exception){ DB::rollBack(); return [false,$exception->getMessage()]; } return [true, '']; } private function saveDetail($id, $time, $data){ if(! empty($data['details'])){ $unit = []; foreach ($data['details'] as $value){ $unit[] = [ 'calendar_id' => $id, 'time' => $value['time'], 'is_work' => $value['is_work'], 'crt_time' => $time, 'top_depart_id' => $value['top_depart_id'], 'month' => $value['month'], ]; } if(! empty($unit)) CalendarDetails::insert($unit); } } private function getDetail($id){ $data = CalendarDetails::where('del_time',0) ->where('calendar_id', $id) ->get()->toArray(); $unit = []; foreach ($data as $value){ $unit[] = [ 'time' => date("Y-m-d",$value['time']), 'is_work' => $value['is_work'], ]; } $detail = [ 'details' => $unit, ]; foreach ($detail as $key => $value) { if (empty($value)) { $detail[$key] = (object)[]; // 转成 stdClass 对象 } } return $detail; } public function calendarDel($data){ if($this->isEmpty($data,'id')) return [false,'请选择数据!']; try { DB::beginTransaction(); $time = time(); Calendar::where('del_time',0) ->whereIn('id',$data['id']) ->update(['del_time' => $time]); CalendarDetails::where('del_time',0) ->whereIn('calendar_id', $data['id']) ->update(['del_time' => $time]); DB::commit(); }catch (\Exception $exception){ DB::rollBack(); return [false,$exception->getMessage()]; } return [true, '']; } public function calendarDetail($data, $user){ if($this->isEmpty($data,'id')) return [false,'请选择数据!']; $customer = Calendar::where('del_time',0) ->where('id',$data['id']) ->first(); if(empty($customer)) return [false,'日历设置不存在或已被删除']; $customer = $customer->toArray(); $customer['time'] = ! empty($customer['time']) ? date("Y-m", $customer['time']) : ""; $customer['crt_name'] = Employee::where('id',$customer['crt_id'])->value('title'); $customer['crt_time'] = $customer['crt_time'] ? date("Y-m-d H:i:s",$customer['crt_time']): ''; $details = $this->getDetail($data['id']); $customer = array_merge($customer, $details); return [true, $customer]; } public function calendarCommon($data,$user, $field = []){ if(empty($field)) $field = Calendar::$field; $model = Calendar::Clear($user,$data); $model = $model->where('del_time',0) ->select($field) ->orderby('id', 'desc'); if(! empty($data['id'])) $model->whereIn('id', $data['id']); if(! empty($data['crt_time'][0]) && ! empty($data['crt_time'][1])) { $return = $this->changeDateToTimeStampAboutRange($data['crt_time']); $model->where('crt_time','>=',$return[0]); $model->where('crt_time','<=',$return[1]); } if(! empty($data['time'][0]) && ! empty($data['time'][1])) { $return = $this->changeDateToTimeStampAboutRange($data['time']); $model->where('time','>=',$return[0]); $model->where('time','<=',$return[1]); } return $model; } public function calendarList($data,$user){ $model = $this->calendarCommon($data, $user); $list = $this->limit($model,'',$data); $list = $this->fillData($list,$user,$data); return [true, $list]; } public function calendarRule(&$data, $user, $is_add = true){ $data['top_depart_id'] = $user['top_depart_id']; if(empty($data['time'])) return [false, '年月不能为空']; $data['time'] = $this->changeDateToMonth($data['time']); $res = $this->checkNumber($data['work_days'],0,'positive'); if(! $res['valid']) return [false,'工作日:' . $res['error']]; if(! empty($data['details'])){ foreach ($data['details'] as $key => $value){ if(empty($value['time'])) return [false, '日期不能为空']; $data['details'][$key]['time'] = $this->changeDateToDate($value['time']); if(! isset($value['is_work'])) return [false, '是否工作日不能为空']; $data['details'][$key]['top_depart_id'] = $data['top_depart_id']; $data['details'][$key]['month'] = $data['time']; } } if($is_add){ $bool = Calendar::where('time', $data['time']) ->where('top_depart_id', $data['top_depart_id']) ->where('del_time',0) ->exists(); }else{ if(empty($data['id'])) return [false,'ID不能为空']; $bool = Calendar::where('time',$data['time']) ->where('top_depart_id', $data['top_depart_id']) ->where('id','<>',$data['id']) ->where('del_time',0) ->exists(); } if($bool) return [false, '该年月下的日历设置已存在']; return [true, '']; } public function fillData($data, $user, $search){ if(empty($data['data'])) return $data; $emp = (new EmployeeService())->getEmployeeMap(array_unique(array_column($data['data'],'crt_id'))); foreach ($data['data'] as $key => $value){ $data['data'][$key]['crt_time'] = $value['crt_time'] ? date('Y-m-d H:i:s',$value['crt_time']) : ''; $data['data'][$key]['time'] = $value['time'] ? date('Y-m',$value['time']) : ''; $data['data'][$key]['crt_name'] = $emp[$value['crt_id']] ?? ''; } return $data; } }