DashboardFacadeRepository.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. <?php
  2. namespace App\Repositories\Eloquent;
  3. use App\Models\BrowseRecord;
  4. use App\Models\NewMediaAccount;
  5. use App\Models\User;
  6. use App\Repositories\Contracts\DashboardInterface;
  7. class DashboardFacadeRepository extends BaseRepository implements DashboardInterface
  8. {
  9. /**
  10. * 获取指定时间段内的注册数据
  11. *
  12. * @param string $start 开始时间
  13. * @param string $end 结束时间
  14. * @param string $dateType 日期类型 0:按照起始时间:1日 日 当日+往前6天 2周 当前周+往前6周 3月 起始时间所在月,往前+5个月的数据
  15. * @return array 返回包含总数据、计数数据和按日期分组的数据数组
  16. */
  17. public function getPeriodRegisiter($start, $end, $dateType = 0)
  18. {
  19. //从配置中获取x轴字段,默认为['社心小程序', '新媒体']
  20. $xField = config('dashboard.UG.xField', ['社心小程序', '新媒体']);
  21. $ret = [];
  22. $mpCount = 0;
  23. $nmCount = 0;
  24. if ($dateType == 0) {
  25. //查询指定时间段内的浏览记录
  26. $result = BrowseRecord::whereBetween('created_at', [$start, $end])->get();
  27. //初始化数据结构,为每个日期创建一个数据项,并将x_field关联的数据初始化为0
  28. while (strtotime($start) <= strtotime($end)) {
  29. $ret[$start] = [
  30. 'x_data' => $start,
  31. 'y_field' => $xField,
  32. 'y_data' => array_fill(0, count($xField), 0),
  33. ];
  34. $startTime = date('Y-m-d', strtotime("+1 day", strtotime($start)));
  35. }
  36. //遍历查询结果,统计每个日期的社心小程序浏览量
  37. foreach ($result as $item) {
  38. $mpCount++;
  39. $ret[date('Y-m-d', strtotime($item->created_at))]['y_data'][array_search('社心小程序', $xField)]++;
  40. }
  41. } elseif ($dateType == 1) {
  42. $endTime = date('Y-m-d');
  43. $startTime = date('Y-m-d', strtotime("-6 days", strtotime($endTime)));
  44. //查询指定时间段内的浏览记录
  45. $result = BrowseRecord::whereBetween('created_at', [$startTime, $endTime])->get();
  46. //初始化数据结构,为每个日期创建一个数据项,并将x_field关联的数据初始化为0
  47. while (strtotime($startTime) <= strtotime($endTime)) {
  48. $ret[$startTime] = [
  49. 'x_data' => $startTime,
  50. 'y_field' => $xField,
  51. 'y_data' => array_fill(0, count($xField), 0),
  52. ];
  53. $startTime = date('Y-m-d', strtotime("+1 day", strtotime($startTime)));
  54. }
  55. //遍历查询结果,统计每个日期的社心小程序浏览量
  56. foreach ($result as $item) {
  57. $mpCount++;
  58. $ret[date('Y-m-d', strtotime($item->created_at))]['y_data'][array_search('社心小程序', $xField)]++;
  59. }
  60. } elseif ($dateType == 2){
  61. //当前周+往前6周
  62. $week = 0;
  63. while($week <7) {
  64. // 获取当前日期所在周的周日
  65. $endTime = date('Y-m-d', strtotime('sunday -'.$week.' week', strtotime($end)));
  66. // 获取六个自然周前的周一
  67. $startTime = date('Y-m-d', strtotime('monday -'.($week+1).' week', strtotime($end)));
  68. $result = BrowseRecord::whereBetween('created_at', [$startTime, $endTime])->get();
  69. $ret[] = [
  70. 'x_data' => $startTime.'~'.$endTime,
  71. 'y_field' => $xField,
  72. 'y_data' => [$result->count(), 0],
  73. ];
  74. $mpCount+= $result->count();
  75. $week++;
  76. }
  77. }elseif ($dateType == 3){
  78. //当前周+往前6周
  79. $month = 0;
  80. while($month <6) {
  81. $endTime = date('Y-m-t', strtotime(' -'.$month.' month', strtotime($end)));
  82. $startTime = date('Y-m-1', strtotime(' -'.$month.' month', strtotime($end)));
  83. $result = BrowseRecord::whereBetween('created_at', [$startTime, $endTime])->get();
  84. $ret[] = [
  85. 'x_data' => date('Y-m', strtotime($startTime)),
  86. 'y_field' => $xField,
  87. 'y_data' => [$result->count(), 0],
  88. ];
  89. $mpCount+= $result->count();
  90. $month++;
  91. }
  92. }
  93. //返回数据包括:总数据、计数数据和按日期分组的数据
  94. return [
  95. 'total' => [['label' => '社心小程序', 'value' => User::count()], ['label' => '新媒体', 'value' => NewMediaAccount::sum('fans_num')]],
  96. 'count' => [['label' => '社心小程序', 'value' => $mpCount], ['label' => '新媒体', 'value' => 0]],
  97. 'data' => array_values($ret),
  98. ];
  99. }
  100. }