where(function($query) use($conditions){ if (isset($conditions['uid'])){ $query->where('uid', $conditions['uid']); } })->orderByRaw($sort)->paginate($limit, $fields); $result->each(function($item){ $carbon = Carbon::parse($item->updated_at); $item->month = number2Chinese(intval($carbon->format('m'))); $item->day = $carbon->format('d'); $item->time = $carbon->format('H:i'); $item->week = week2Chinese($carbon->dayOfWeek); }); return $this->response($result); } public function findBy(array $conditions, array $fields){ $result = DiaryMood::where(function($query) use($conditions){ if (isset($conditions['id'])){ $query->where('id', $conditions['id']); } if (isset($conditions['uid'])){ $query->where('uid', $conditions['uid']); } })->first($fields); return $result ? $this->response($result) : $this->error()->dataDoesNotExist(); } public function create(array $data) { $result = DiaryMood::create($data); return $result ? $this->response($result) : $this->error()->fail(); } public function updateBy(array $conditions, array $data) { $result = DiaryMood::where(function($query) use($conditions){ if (isset($conditions['id'])){ $query->where('id', $conditions['id']); } if (isset($conditions['uid'])){ $query->where('uid', $conditions['uid']); } })->update($data); return $result ? $this->response($result) : $this->error()->fail(); } public function deleteBy(array $conditions) { $result = DiaryMood::where(function($query) use($conditions){ if (isset($conditions['id'])){ $query->where('id', $conditions['id']); } if (isset($conditions['uid'])){ $query->where('uid', $conditions['uid']); } })->delete(); return $result ? $this->response($result) : $this->error()->fail(); } public function getWeek($uid, $page) { $now = Carbon::now(); $today = date('Y年m月d日'); $day = date('d'); $week = date('w'); $dateArr = []; for ($i = $week, $y = -1; $i >= 0; $i--) { $cloneNow = clone $now; $y++; $pageY = ($page - 1) * 7 + $y; $dateArr[] = $cloneNow->subDays($pageY)->toDateString(); } $nextStartDate = $dateArr[0]; $i2 = $pageY + 1; $x = 1; while ($i2 <= ($page - 1) * 7 + 6) { $cloneNow = Carbon::parse($nextStartDate); $dateArr[] = $cloneNow->addDays($x)->toDateString(); $x++; $i2 ++; } sort($dateArr); $resultWeek = []; $hasToday = FALSE; foreach ($dateArr as $key => $value) { $dateWeek = date('w', strtotime($value)); $chineseWeek = week2Chinese($dateWeek); if ($value == Carbon::now()->toDateString()){ $isToday = $hasToday = TRUE; }else{ $isToday = FALSE; } $betweenTime = [ date('Y-m-d', strtotime($value)), date('Y-m-d 23:59:59', strtotime($value)), ]; $resultWeek[] = [ 'week' => $chineseWeek, 'day' => $value, 'alias_day' => date('d', strtotime($value)), 'is_today' => $isToday, 'default' => FALSE, 'mood' => DiaryMood::where('uid', $uid)->whereBetween('created_at', $betweenTime)->exists(), 'sleep' => DiarySleep::where('uid', $uid)->where('day', date('Y-m-d', strtotime($value)))->exists(), ]; } if (!$hasToday){ $resultWeek[0]['default'] = TRUE; } $result = [ 'week' => $resultWeek, 'today' => $page > 1 ? date('Y年m月d日', strtotime($resultWeek[0]['day'])) :$today ]; return $this->response($result); } public function getWeekDetailByDay(int $uid, string $day) { $betweenTime = [ date('Y-m-d', strtotime($day)), date('Y-m-d 23:59:59', strtotime($day)), ]; $moods = DiaryMood::with(['level'])->where('uid', $uid)->whereBetween('created_at', $betweenTime)->get(); $moods->each(function($item){ $carbon = Carbon::parse($item->updated_at); $item->month = number2Chinese(intval($carbon->format('m'))); $item->day = $carbon->format('d'); $item->time = $carbon->format('H:i'); $item->week = week2Chinese($carbon->dayOfWeek); }); $sleeps = DiarySleep::with(['level'])->where('uid', $uid)->where('day', $day)->get(); $sleeps->each(function($item){ $item->day = date('m月d', strtotime($item->day)); $item->start_time = date('H:i', strtotime($item->start_time)); $item->end_time = date('H:i', strtotime($item->end_time)); }); $result = [ 'moods' => $moods, 'sleeps' => $sleeps ]; return $this->response($result); } }