StatisticsFacadeRepository.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. <?php
  2. namespace app\Repositories\Eloquent;
  3. use App\Repositories\Eloquent\BaseRepository;
  4. use App\Repositories\Contracts\StatisticsInterface;
  5. use Carbon\Carbon;
  6. use App\Models\DiaryMood;
  7. use App\Models\DiarySleepLevel;
  8. use App\Models\DiarySleep;
  9. /**
  10. *
  11. * @author lilin
  12. *
  13. */
  14. class StatisticsFacadeRepository extends BaseRepository implements StatisticsInterface
  15. {
  16. public function getShowType($typeStr)
  17. {
  18. //是否是近多少日
  19. $isDay = strstr($typeStr, '日');
  20. //是否是近多少月
  21. $isMonth = strstr($typeStr, '月');
  22. //是否是年
  23. $isYear = strstr($typeStr, '年');
  24. if ($isDay){
  25. $showType = self::SHOW_TYPE_DAY;
  26. }elseif ($isMonth){
  27. $showType = self::SHOW_TYPE_MONTH;
  28. }elseif ($isYear){
  29. $showType = self::SHOW_TYPE_YEAR;
  30. }
  31. return $showType;
  32. }
  33. public function select($showDay = FALSE, $day = 1, $showMonth = FALSE, $month = 1, $showYear = FALSE)
  34. {
  35. $releaseDate = config('console.release_date');
  36. $startYear = date('Y', strtotime($releaseDate));
  37. $thisYear = date('Y');
  38. $thisMonth = date('m');
  39. //是否有近七日
  40. $dayStr = '近'.number2Chinese($day).'日';
  41. $result = $showDay ? [$dayStr] : [];
  42. //是否有近6月
  43. if ($showMonth) {
  44. $i = 0;
  45. while ($i <= $month) {
  46. $lastMonth = Carbon::now()->subMonths($i);
  47. $result[] = $lastMonth->format('Y年m月');
  48. $i ++;
  49. //不能超过上线日期
  50. if ($lastMonth->getTimestamp() <= strtotime($releaseDate)){
  51. break;
  52. }
  53. }
  54. }
  55. //是否显示年
  56. if ($showYear){
  57. while ($startYear <= $thisYear) {
  58. $result[] = $startYear.'年';
  59. $startYear ++;
  60. }
  61. }
  62. return $this->response($result);
  63. }
  64. public function mood(int $uid, string $typeStr)
  65. {
  66. $categories = [];
  67. $seriesData = [];
  68. //显示的数据类型
  69. $showType = $this->getShowType($typeStr);
  70. switch ($showType) {
  71. case self::SHOW_TYPE_DAY:
  72. $number = function($str) use(&$number){
  73. $replaceArr = ['近', '日'];
  74. foreach ($replaceArr as $search){
  75. $str = str_replace($search, '', $str);
  76. }
  77. return chinese2Number($str);
  78. };
  79. for ($i = $number($typeStr); $i >= 0; $i --) {
  80. $day = Carbon::now()->subDays($i)->toDateString();
  81. $categories[] = date('m-d', strtotime($day));
  82. $betweenTime = [
  83. date('Y-m-d', strtotime($day)),
  84. date('Y-m-d 23:59:59', strtotime($day)),
  85. ];
  86. $mood = DiaryMood::where('uid', $uid)->whereBetween('created_at', $betweenTime)->first();
  87. $seriesData[] = $mood ? $mood->score : 0;
  88. }
  89. break;
  90. case self::SHOW_TYPE_MONTH:
  91. $month = function($str){
  92. return str_replace('月', '', str_replace('年', '-', $str));
  93. };
  94. //此月的最后一天
  95. $lastDay = Carbon::parse($month($typeStr))->lastOfMonth()->format('d');
  96. for ($i = 0; $i < $lastDay; $i ++) {
  97. $day = Carbon::parse($month($typeStr))->addDays($i)->toDateString();
  98. $categories[] = $i % 5 == 0 ? date('d', strtotime($day)) : '';
  99. $betweenTime = [
  100. date('Y-m-d', strtotime($day)),
  101. date('Y-m-d 23:59:59', strtotime($day)),
  102. ];
  103. $mood = DiaryMood::where('uid', $uid)->whereBetween('created_at', $betweenTime)->first();
  104. $seriesData[] = $mood ? $mood->score : 0;
  105. }
  106. break;
  107. }
  108. $result = [
  109. 'categories' => $categories,
  110. 'seriesData' => $seriesData
  111. ];
  112. return $this->response($result);
  113. }
  114. public function sleep(int $uid, string $type)
  115. {
  116. $series = [];
  117. if ($type == '本月'){
  118. $month = Carbon::now()->format('Y-m');
  119. $lastDay = Carbon::parse($month)->lastOfMonth()->toDateString();
  120. $betweenTime = [
  121. $month.'-01',
  122. date('Y-m-d 23:59:59', strtotime($lastDay)),
  123. ];
  124. }else{
  125. $month = function($str){
  126. return str_replace('月', '', str_replace('年', '-', $str));
  127. };
  128. $month = $month($type);
  129. $lastDay = Carbon::parse($month)->lastOfMonth()->toDateString();
  130. $betweenTime = [
  131. $month.'-01',
  132. date('Y-m-d 23:59:59', strtotime($lastDay)),
  133. ];
  134. }
  135. $levels = DiarySleepLevel::orderBy('rank', 'desc')->get(['id','name','color']);
  136. $allCount = DiarySleep::whereBetween('created_at', $betweenTime)->count();
  137. $levels->each(function($item) use($betweenTime, $allCount){
  138. $item->data = DiarySleep::where('level_id', $item->id)->whereBetween('created_at', $betweenTime)->count();
  139. $item->percent = $allCount ? sprintf("%.2f", $item->data / $allCount * 100) : 0;
  140. });
  141. $result = [
  142. 'levels' => $levels,
  143. 'month' => date('Y年m月', strtotime($month))
  144. ];
  145. return $this->response($result);
  146. }
  147. }