123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223 |
- <?php
- namespace App\Http\Controllers\StatisticsApi;
- use App\Http\Controllers\Controller;
- use Illuminate\Http\Request;
- use App\Models\User;
- use App\Models\BrowseRecord;
- use Carbon\Carbon;
- use Illuminate\Support\Facades\Artisan;
- class IndexController extends Controller
- {
- /**
- * Display a listing of the resource.
- *
- * @return \Illuminate\Http\Response
- */
- public function index(Request $request)
- {
- //关注人数
- $users = User::count();
-
- //测评总人次
- $cp = $this->_cp();
-
- //科普总人次
- $kp = $this->_kp();
-
- //训练总人次
- $xl = $this->_xl();
-
- //挂号总人次
- $gh = $this->_gh();
-
- //测评人次统计
- $cpTj = $this->_cpTj(request('cp_type', 1), 1);
-
- //科普人次统计
- $kpTj = $this->_cpTj(request('kp_type', 1), 2);
-
- //训练人次统计
- $xlTj = $this->_cpTj(request('xl_type', 1), 3);
-
- $result = [
- 'years' => $this->_getYearArr(),
- 'users' => $users,
- 'cp' => $cp,
- 'kp' => $kp,
- 'xl' => $xl,
- 'gh' => $gh,
- 'cp_tj' => $cpTj,
- 'kp_tj' => $kpTj,
- 'xl_tj' => $xlTj
- ];
- return $result;
- }
-
- /**
- * 测评人次统计
- *
- * @param int $id 1:近7天 其它:具体的一年
- * @return []
- */
- private function _cpTj($id,$column=1) {
- $categories = $this->_getTjCategory($id);
-
- return [
- 'categories' => $categories,
- 'series' => $this->_getTjSeries($column, $id, $categories)
- ];
- }
-
- /**
- * 获取统计数据
- *
- * @param int $column 1.测评人次统计 2.科普人次统计 3.训练人次统计
- * @param int $type 1:近7天 其它:具体的一年
- * @param array $categories 日期 近七天还是月
- * @return []
- */
-
- private function _getTjSeries(int $column, int $type, $categories) {
- $result = [];
-
- $funcName = $this->_getTypeFunc($column);
-
- foreach ($categories as $k=>$v){
- if($type ===1){
- $getDay = strtotime($v);
- $start = date("Y-m-d 00:00:00" ,$getDay);
- $end_day = date("Y-m-d 23:59:59" ,$getDay);
- $nums = $this->$funcName([$start,$end_day]);
- }else{
- $times = $this->_trunDate($v);
- $nums = $this->$funcName($times);
- }
- $result[$k] = $nums;
- }
- return $result;
- }
-
- private function _getTypeFunc($column){
- switch ($column){
- case 1://
- $funcName="_cp" ;
- break;
- case 2://
- $funcName="_kp" ;
- break;
- case 3://
- $funcName="_xl" ;
- break;
- default :$funcName="_cp" ;break;
- }
-
- return $funcName;
- }
- /*
- * 月份转换时间
- * */
- private function _trunDate($getMonth){
- $UpNum =['一', '二', '三', '四', '五', '六', '七', '八', '九', '十', '十一', '十二'];
- $num = ['01','02','03','04','05','06','07','08','09','10','11',"12"];
- $numMonth =mb_substr($getMonth,0,-1);
- // var_dump($numMonth);
- foreach ($UpNum as $k=>$v){
- if($v===$numMonth){
- $month = $num[$k];
- }
- }
- // $monthString = $month;
- $year = date('Y');// 获取当前年份
- // $month = date('m', strtotime($monthString));// 将月份字符串转换为对应的数字
- $startDate = $year . '-' . $month . '-01 00:00:00';
- $endDay= date("Y-m-d 00:00:00",strtotime('+1 month', strtotime($startDate .'-01')));
- return [$startDate,$endDay];
-
- }
-
- /**
- * 测评总人次
- *
- */
- private function _cp($time_arr =null) {
- if($time_arr ===null){
- return BrowseRecord::where('column', BrowseRecord::COLUMN_1)->count();
- }else{
- return BrowseRecord::where('column', BrowseRecord::COLUMN_1)->whereBetween("created_at",$time_arr)->count();
- }
- }
-
- /**
- * 科普总人次
- *
- */
- private function _kp($time_arr =null) {
-
- if($time_arr ===null){
- return BrowseRecord::where('column', BrowseRecord::COLUMN_3)->count();
- }else{
- return BrowseRecord::where('column', BrowseRecord::COLUMN_3)->whereBetween("created_at",$time_arr)->count();
- }
- }
-
- /**
- * 训练总人次
- *
- */
- private function _xl($time_arr =null) {
- // return BrowseRecord::where('column', BrowseRecord::COLUMN_2)->count();
- if($time_arr ===null){
- return BrowseRecord::where('column', BrowseRecord::COLUMN_2)->count();
- }else{
- return BrowseRecord::where('column', BrowseRecord::COLUMN_2)->whereBetween("created_at",$time_arr)->count();
- }
- }
-
- /**
- * 挂号总人次
- *
- */
- private function _gh() {
- return 185;
- }
-
- /**
- * 数据的x轴日期的处理
- *
- * @param int $type 1:近7日 2:年
- * @return []
- */
- private function _getTjCategory(int $type) {
-
- $result = [];
- if ($type === 1) {
- for ($i = 7; $i >= 0; $i--) {
- $result[] = Carbon::now()->subDays($i)->toDateString();
- }
- }else{
- $result = ['一月', '二月', '三月', '四月', '五月', '六月', '七月', '八月', '九月', '十月', '十一月', '十二月'];
- }
-
- return $result;
- }
-
- /**
- * 获取近7日和历年
- *
- * @return []
- */
- private function _getYearArr() {
- $startYear = 2023;
- $nowYear = date('Y');
-
- $yearArr = [['id'=>1, 'name'=>'近7日']];
- for ($i = $nowYear; $i >= $startYear; $i--) {
- $yearArr[] = ['id'=>$i, 'name'=>$i.'年'];
- }
-
- return $yearArr;
- }
- }
|