123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183 |
- <?php
- namespace app\Repositories\Eloquent;
- use App\Repositories\Eloquent\BaseRepository;
- use App\Repositories\Contracts\DiaryMoodInterface;
- use App\Models\DiaryMood;
- use Carbon\Carbon;
- use App\Models\DiarySleep;
- /**
- *
- * @author lilin
- *
- */
- class DiaryMoodFacadeRepository extends BaseRepository implements DiaryMoodInterface
- {
- public function getList(array $conditions, array $fields, string $sort, int $page, int $limit)
- {
- $result = DiaryMood::with(['level'])->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);
- }
- }
|