IndexController.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. <?php
  2. namespace App\Http\Controllers\StatisticsApi;
  3. use App\Http\Controllers\Controller;
  4. use Illuminate\Http\Request;
  5. use App\Models\User;
  6. use App\Models\BrowseRecord;
  7. use Carbon\Carbon;
  8. use Illuminate\Support\Facades\Artisan;
  9. class IndexController extends Controller
  10. {
  11. /**
  12. * Display a listing of the resource.
  13. *
  14. * @return \Illuminate\Http\Response
  15. */
  16. public function index(Request $request)
  17. {
  18. //关注人数
  19. $users = User::count();
  20. //测评总人次
  21. $cp = $this->_cp();
  22. //科普总人次
  23. $kp = $this->_kp();
  24. //训练总人次
  25. $xl = $this->_xl();
  26. //挂号总人次
  27. $gh = $this->_gh();
  28. //测评人次统计
  29. $cpTj = $this->_cpTj(request('cp_type', 1), 1);
  30. //科普人次统计
  31. $kpTj = $this->_cpTj(request('kp_type', 1), 2);
  32. //训练人次统计
  33. $xlTj = $this->_cpTj(request('xl_type', 1), 3);
  34. $result = [
  35. 'years' => $this->_getYearArr(),
  36. 'users' => $users,
  37. 'cp' => $cp,
  38. 'kp' => $kp,
  39. 'xl' => $xl,
  40. 'gh' => $gh,
  41. 'cp_tj' => $cpTj,
  42. 'kp_tj' => $kpTj,
  43. 'xl_tj' => $xlTj
  44. ];
  45. return $result;
  46. }
  47. /**
  48. * 测评人次统计
  49. *
  50. * @param int $id 1:近7天 其它:具体的一年
  51. * @return []
  52. */
  53. private function _cpTj($id,$column=1) {
  54. $categories = $this->_getTjCategory($id);
  55. return [
  56. 'categories' => $categories,
  57. 'series' => $this->_getTjSeries($column, $id, $categories)
  58. ];
  59. }
  60. /**
  61. * 获取统计数据
  62. *
  63. * @param int $column 1.测评人次统计 2.科普人次统计 3.训练人次统计
  64. * @param int $type 1:近7天 其它:具体的一年
  65. * @param array $categories 日期 近七天还是月
  66. * @return []
  67. */
  68. private function _getTjSeries(int $column, int $type, $categories) {
  69. $result = [];
  70. $funcName = $this->_getTypeFunc($column);
  71. foreach ($categories as $k=>$v){
  72. if($type ===1){
  73. $getDay = strtotime($v);
  74. $start = date("Y-m-d 00:00:00" ,$getDay);
  75. $end_day = date("Y-m-d 23:59:59" ,$getDay);
  76. $nums = $this->$funcName([$start,$end_day]);
  77. }else{
  78. $times = $this->_trunDate($v);
  79. $nums = $this->$funcName($times);
  80. }
  81. $result[$k] = $nums;
  82. }
  83. return $result;
  84. }
  85. private function _getTypeFunc($column){
  86. switch ($column){
  87. case 1://
  88. $funcName="_cp" ;
  89. break;
  90. case 2://
  91. $funcName="_kp" ;
  92. break;
  93. case 3://
  94. $funcName="_xl" ;
  95. break;
  96. default :$funcName="_cp" ;break;
  97. }
  98. return $funcName;
  99. }
  100. /*
  101. * 月份转换时间
  102. * */
  103. private function _trunDate($getMonth){
  104. $UpNum =['一', '二', '三', '四', '五', '六', '七', '八', '九', '十', '十一', '十二'];
  105. $num = ['01','02','03','04','05','06','07','08','09','10','11',"12"];
  106. $numMonth =mb_substr($getMonth,0,-1);
  107. // var_dump($numMonth);
  108. foreach ($UpNum as $k=>$v){
  109. if($v===$numMonth){
  110. $month = $num[$k];
  111. }
  112. }
  113. // $monthString = $month;
  114. $year = date('Y');// 获取当前年份
  115. // $month = date('m', strtotime($monthString));// 将月份字符串转换为对应的数字
  116. $startDate = $year . '-' . $month . '-01 00:00:00';
  117. $endDay= date("Y-m-d 00:00:00",strtotime('+1 month', strtotime($startDate .'-01')));
  118. return [$startDate,$endDay];
  119. }
  120. /**
  121. * 测评总人次
  122. *
  123. */
  124. private function _cp($time_arr =null) {
  125. if($time_arr ===null){
  126. return BrowseRecord::where('column', BrowseRecord::COLUMN_1)->count();
  127. }else{
  128. return BrowseRecord::where('column', BrowseRecord::COLUMN_1)->whereBetween("created_at",$time_arr)->count();
  129. }
  130. }
  131. /**
  132. * 科普总人次
  133. *
  134. */
  135. private function _kp($time_arr =null) {
  136. if($time_arr ===null){
  137. return BrowseRecord::where('column', BrowseRecord::COLUMN_3)->count();
  138. }else{
  139. return BrowseRecord::where('column', BrowseRecord::COLUMN_3)->whereBetween("created_at",$time_arr)->count();
  140. }
  141. }
  142. /**
  143. * 训练总人次
  144. *
  145. */
  146. private function _xl($time_arr =null) {
  147. // return BrowseRecord::where('column', BrowseRecord::COLUMN_2)->count();
  148. if($time_arr ===null){
  149. return BrowseRecord::where('column', BrowseRecord::COLUMN_2)->count();
  150. }else{
  151. return BrowseRecord::where('column', BrowseRecord::COLUMN_2)->whereBetween("created_at",$time_arr)->count();
  152. }
  153. }
  154. /**
  155. * 挂号总人次
  156. *
  157. */
  158. private function _gh() {
  159. return 185;
  160. }
  161. /**
  162. * 数据的x轴日期的处理
  163. *
  164. * @param int $type 1:近7日 2:年
  165. * @return []
  166. */
  167. private function _getTjCategory(int $type) {
  168. $result = [];
  169. if ($type === 1) {
  170. for ($i = 7; $i >= 0; $i--) {
  171. $result[] = Carbon::now()->subDays($i)->toDateString();
  172. }
  173. }else{
  174. $result = ['一月', '二月', '三月', '四月', '五月', '六月', '七月', '八月', '九月', '十月', '十一月', '十二月'];
  175. }
  176. return $result;
  177. }
  178. /**
  179. * 获取近7日和历年
  180. *
  181. * @return []
  182. */
  183. private function _getYearArr() {
  184. $startYear = 2023;
  185. $nowYear = date('Y');
  186. $yearArr = [['id'=>1, 'name'=>'近7日']];
  187. for ($i = $nowYear; $i >= $startYear; $i--) {
  188. $yearArr[] = ['id'=>$i, 'name'=>$i.'年'];
  189. }
  190. return $yearArr;
  191. }
  192. }