123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171 |
- <?php
- namespace app\Repositories\Eloquent;
- use App\Repositories\Eloquent\BaseRepository;
- use App\Repositories\Contracts\StatisticsInterface;
- use Carbon\Carbon;
- use App\Models\DiaryMood;
- use App\Models\DiarySleepLevel;
- use App\Models\DiarySleep;
- /**
- *
- * @author lilin
- *
- */
- class StatisticsFacadeRepository extends BaseRepository implements StatisticsInterface
- {
- public function getShowType($typeStr)
- {
- //是否是近多少日
- $isDay = strstr($typeStr, '日');
- //是否是近多少月
- $isMonth = strstr($typeStr, '月');
- //是否是年
- $isYear = strstr($typeStr, '年');
- if ($isDay){
- $showType = self::SHOW_TYPE_DAY;
- }elseif ($isMonth){
- $showType = self::SHOW_TYPE_MONTH;
- }elseif ($isYear){
- $showType = self::SHOW_TYPE_YEAR;
- }
- return $showType;
- }
- public function select($showDay = FALSE, $day = 1, $showMonth = FALSE, $month = 1, $showYear = FALSE)
- {
- $releaseDate = config('console.release_date');
- $startYear = date('Y', strtotime($releaseDate));
- $thisYear = date('Y');
- $thisMonth = date('m');
- //是否有近七日
- $dayStr = '近'.number2Chinese($day).'日';
- $result = $showDay ? [$dayStr] : [];
- //是否有近6月
- if ($showMonth) {
- $i = 0;
- while ($i <= $month) {
- $lastMonth = Carbon::now()->subMonths($i);
- $result[] = $lastMonth->format('Y年m月');
- $i ++;
- //不能超过上线日期
- if ($lastMonth->getTimestamp() <= strtotime($releaseDate)){
- break;
- }
- }
- }
- //是否显示年
- if ($showYear){
- while ($startYear <= $thisYear) {
- $result[] = $startYear.'年';
- $startYear ++;
- }
- }
- return $this->response($result);
- }
- public function mood(int $uid, string $typeStr)
- {
- $categories = [];
- $seriesData = [];
- //显示的数据类型
- $showType = $this->getShowType($typeStr);
- switch ($showType) {
- case self::SHOW_TYPE_DAY:
- $number = function($str) use(&$number){
- $replaceArr = ['近', '日'];
- foreach ($replaceArr as $search){
- $str = str_replace($search, '', $str);
- }
- return chinese2Number($str);
- };
- for ($i = $number($typeStr); $i >= 0; $i --) {
- $day = Carbon::now()->subDays($i)->toDateString();
- $categories[] = date('m-d', strtotime($day));
- $betweenTime = [
- date('Y-m-d', strtotime($day)),
- date('Y-m-d 23:59:59', strtotime($day)),
- ];
- $mood = DiaryMood::where('uid', $uid)->whereBetween('created_at', $betweenTime)->first();
- $seriesData[] = $mood ? $mood->score : 0;
- }
- break;
- case self::SHOW_TYPE_MONTH:
- $month = function($str){
- return str_replace('月', '', str_replace('年', '-', $str));
- };
- //此月的最后一天
- $lastDay = Carbon::parse($month($typeStr))->lastOfMonth()->format('d');
- for ($i = 0; $i < $lastDay; $i ++) {
- $day = Carbon::parse($month($typeStr))->addDays($i)->toDateString();
- $categories[] = $i % 5 == 0 ? date('d', strtotime($day)) : '';
- $betweenTime = [
- date('Y-m-d', strtotime($day)),
- date('Y-m-d 23:59:59', strtotime($day)),
- ];
- $mood = DiaryMood::where('uid', $uid)->whereBetween('created_at', $betweenTime)->first();
- $seriesData[] = $mood ? $mood->score : 0;
- }
- break;
- }
- $result = [
- 'categories' => $categories,
- 'seriesData' => $seriesData
- ];
- return $this->response($result);
- }
- public function sleep(int $uid, string $type)
- {
- $series = [];
- if ($type == '本月'){
- $month = Carbon::now()->format('Y-m');
- $lastDay = Carbon::parse($month)->lastOfMonth()->toDateString();
- $betweenTime = [
- $month.'-01',
- date('Y-m-d 23:59:59', strtotime($lastDay)),
- ];
- }else{
- $month = function($str){
- return str_replace('月', '', str_replace('年', '-', $str));
- };
- $month = $month($type);
- $lastDay = Carbon::parse($month)->lastOfMonth()->toDateString();
- $betweenTime = [
- $month.'-01',
- date('Y-m-d 23:59:59', strtotime($lastDay)),
- ];
- }
- $levels = DiarySleepLevel::orderBy('rank', 'desc')->get(['id','name','color']);
- $allCount = DiarySleep::whereBetween('created_at', $betweenTime)->count();
- $levels->each(function($item) use($betweenTime, $allCount){
- $item->data = DiarySleep::where('level_id', $item->id)->whereBetween('created_at', $betweenTime)->count();
- $item->percent = $allCount ? sprintf("%.2f", $item->data / $allCount * 100) : 0;
- });
- $result = [
- 'levels' => $levels,
- 'month' => date('Y年m月', strtotime($month))
- ];
- return $this->response($result);
- }
- }
|