DiaryMoodFacadeRepository.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. <?php
  2. namespace app\Repositories\Eloquent;
  3. use App\Repositories\Eloquent\BaseRepository;
  4. use App\Repositories\Contracts\DiaryMoodInterface;
  5. use App\Models\DiaryMood;
  6. use Carbon\Carbon;
  7. use App\Models\DiarySleep;
  8. /**
  9. *
  10. * @author lilin
  11. *
  12. */
  13. class DiaryMoodFacadeRepository extends BaseRepository implements DiaryMoodInterface
  14. {
  15. public function getList(array $conditions, array $fields, string $sort, int $page, int $limit)
  16. {
  17. $result = DiaryMood::with(['level'])->where(function($query) use($conditions){
  18. if (isset($conditions['uid'])){
  19. $query->where('uid', $conditions['uid']);
  20. }
  21. })->orderByRaw($sort)->paginate($limit, $fields);
  22. $result->each(function($item){
  23. $carbon = Carbon::parse($item->updated_at);
  24. $item->month = number2Chinese(intval($carbon->format('m')));
  25. $item->day = $carbon->format('d');
  26. $item->time = $carbon->format('H:i');
  27. $item->week = week2Chinese($carbon->dayOfWeek);
  28. });
  29. return $this->response($result);
  30. }
  31. public function findBy(array $conditions, array $fields){
  32. $result = DiaryMood::where(function($query) use($conditions){
  33. if (isset($conditions['id'])){
  34. $query->where('id', $conditions['id']);
  35. }
  36. if (isset($conditions['uid'])){
  37. $query->where('uid', $conditions['uid']);
  38. }
  39. })->first($fields);
  40. return $result ? $this->response($result) : $this->error()->dataDoesNotExist();
  41. }
  42. public function create(array $data)
  43. {
  44. $result = DiaryMood::create($data);
  45. return $result ? $this->response($result) : $this->error()->fail();
  46. }
  47. public function updateBy(array $conditions, array $data)
  48. {
  49. $result = DiaryMood::where(function($query) use($conditions){
  50. if (isset($conditions['id'])){
  51. $query->where('id', $conditions['id']);
  52. }
  53. if (isset($conditions['uid'])){
  54. $query->where('uid', $conditions['uid']);
  55. }
  56. })->update($data);
  57. return $result ? $this->response($result) : $this->error()->fail();
  58. }
  59. public function deleteBy(array $conditions)
  60. {
  61. $result = DiaryMood::where(function($query) use($conditions){
  62. if (isset($conditions['id'])){
  63. $query->where('id', $conditions['id']);
  64. }
  65. if (isset($conditions['uid'])){
  66. $query->where('uid', $conditions['uid']);
  67. }
  68. })->delete();
  69. return $result ? $this->response($result) : $this->error()->fail();
  70. }
  71. public function getWeek($uid, $page)
  72. {
  73. $now = Carbon::now();
  74. $today = date('Y年m月d日');
  75. $day = date('d');
  76. $week = date('w');
  77. $dateArr = [];
  78. for ($i = $week, $y = -1; $i >= 0; $i--) {
  79. $cloneNow = clone $now;
  80. $y++;
  81. $pageY = ($page - 1) * 7 + $y;
  82. $dateArr[] = $cloneNow->subDays($pageY)->toDateString();
  83. }
  84. $nextStartDate = $dateArr[0];
  85. $i2 = $pageY + 1;
  86. $x = 1;
  87. while ($i2 <= ($page - 1) * 7 + 6) {
  88. $cloneNow = Carbon::parse($nextStartDate);
  89. $dateArr[] = $cloneNow->addDays($x)->toDateString();
  90. $x++;
  91. $i2 ++;
  92. }
  93. sort($dateArr);
  94. $resultWeek = [];
  95. $hasToday = FALSE;
  96. foreach ($dateArr as $key => $value) {
  97. $dateWeek = date('w', strtotime($value));
  98. $chineseWeek = week2Chinese($dateWeek);
  99. if ($value == Carbon::now()->toDateString()){
  100. $isToday = $hasToday = TRUE;
  101. }else{
  102. $isToday = FALSE;
  103. }
  104. $betweenTime = [
  105. date('Y-m-d', strtotime($value)),
  106. date('Y-m-d 23:59:59', strtotime($value)),
  107. ];
  108. $resultWeek[] = [
  109. 'week' => $chineseWeek,
  110. 'day' => $value,
  111. 'alias_day' => date('d', strtotime($value)),
  112. 'is_today' => $isToday,
  113. 'default' => FALSE,
  114. 'mood' => DiaryMood::where('uid', $uid)->whereBetween('created_at', $betweenTime)->exists(),
  115. 'sleep' => DiarySleep::where('uid', $uid)->where('day', date('Y-m-d', strtotime($value)))->exists(),
  116. ];
  117. }
  118. if (!$hasToday){
  119. $resultWeek[0]['default'] = TRUE;
  120. }
  121. $result = [
  122. 'week' => $resultWeek,
  123. 'today' => $page > 1 ? date('Y年m月d日', strtotime($resultWeek[0]['day'])) :$today
  124. ];
  125. return $this->response($result);
  126. }
  127. public function getWeekDetailByDay(int $uid, string $day)
  128. {
  129. $betweenTime = [
  130. date('Y-m-d', strtotime($day)),
  131. date('Y-m-d 23:59:59', strtotime($day)),
  132. ];
  133. $moods = DiaryMood::with(['level'])->where('uid', $uid)->whereBetween('created_at', $betweenTime)->get();
  134. $moods->each(function($item){
  135. $carbon = Carbon::parse($item->updated_at);
  136. $item->month = number2Chinese(intval($carbon->format('m')));
  137. $item->day = $carbon->format('d');
  138. $item->time = $carbon->format('H:i');
  139. $item->week = week2Chinese($carbon->dayOfWeek);
  140. });
  141. $sleeps = DiarySleep::with(['level'])->where('uid', $uid)->where('day', $day)->get();
  142. $sleeps->each(function($item){
  143. $item->day = date('m月d', strtotime($item->day));
  144. $item->start_time = date('H:i', strtotime($item->start_time));
  145. $item->end_time = date('H:i', strtotime($item->end_time));
  146. });
  147. $result = [
  148. 'moods' => $moods,
  149. 'sleeps' => $sleeps
  150. ];
  151. return $this->response($result);
  152. }
  153. }