作者 lyh

gx脚本统计数据

  1 +<?php
  2 +/**
  3 + * @remark :
  4 + * @name :CountProject.php
  5 + * @author :lyh
  6 + * @method :post
  7 + * @time :2024/9/26 14:19
  8 + */
  9 +
  10 +namespace App\Console\Commands\Project;
  11 +
  12 +use App\Enums\Common\Code;
  13 +use App\Models\Domain\DomainInfo;
  14 +use App\Models\Project\Project;
  15 +use Illuminate\Console\Command;
  16 +use PhpOffice\PhpSpreadsheet\Spreadsheet;
  17 +use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
  18 +
  19 +class countProject extends Command
  20 +{
  21 + /**
  22 + * The name and signature of the console command.
  23 + *
  24 + * @var string
  25 + */
  26 + protected $signature = 'month_count_project';
  27 +
  28 + /**
  29 + * The console command description.
  30 + *
  31 + * @var string
  32 + */
  33 + protected $description = '项目数据统计生成文件';
  34 +
  35 + public function handle(){
  36 + $start = '2024-01';
  37 + $end = '2024-09';
  38 + $data = $this->exportDataProject($start,$end);
  39 + @file_put_contents(storage_path('logs/lyh_error.log'), var_export($data, true) . PHP_EOL, FILE_APPEND);
  40 + $result = $this->exportData($data);
  41 + echo date('Y-m-d H:i:s') . ' ' . $result . PHP_EOL;
  42 + return $result;
  43 +
  44 + }
  45 +
  46 + public function exportData($data){
  47 + // 创建一个新的 Excel 电子表格实例
  48 + $spreadsheet = new Spreadsheet();
  49 + $sheet = $spreadsheet->getActiveSheet();
  50 + // 添加表头
  51 + $sheet->setCellValue('A1', '月份');
  52 + $sheet->setCellValue('B1', '开始时间');
  53 + $sheet->setCellValue('C1', '结束时间');
  54 + $sheet->setCellValue('D1', '创建项目数量');
  55 + $sheet->setCellValue('E1', '上线项目数量');
  56 + $sheet->setCellValue('F1', '上线比例');
  57 + $sheet->setCellValue('G1', '项目总数');
  58 + $sheet->setCellValue('H1', '上线项目总数');
  59 + $sheet->setCellValue('I1', '推广项目总数');
  60 + $sheet->setCellValue('J1', '建站项目总数');
  61 + $sheet->setCellValue('K1', '未上线项目数量');
  62 + $sheet->setCellValue('L1', '删除项目数量');
  63 + $sheet->setCellValue('M1', '上线最快时间');
  64 + $sheet->setCellValue('N1', '上线最慢');
  65 + $sheet->setCellValue('O1', '平均上线天数');
  66 + $rowCount = 2;
  67 + $allData = $this->countAll();
  68 + foreach ($data as $v) {
  69 + $sheet->setCellValue('A' . $rowCount, $v['month']);
  70 + $sheet->setCellValue('B' . $rowCount, $v['start']);
  71 + $sheet->setCellValue('C' . $rowCount, $v['end']);
  72 + $sheet->setCellValue('D' . $rowCount, $v['month_create_project_count']);
  73 + $sheet->setCellValue('E' . $rowCount, $v['month_project_go_online_count']);
  74 + $sheet->setCellValue('F' . $rowCount, $v['month_project_online_rate']);
  75 + $sheet->setCellValue('G' . $rowCount, $allData['count']);
  76 + $sheet->setCellValue('H' . $rowCount, $allData['go_online_count']);
  77 + $sheet->setCellValue('I' . $rowCount, $allData['promotion_web_count']);
  78 + $sheet->setCellValue('J' . $rowCount, $allData['create_web_count']);
  79 + $sheet->setCellValue('K' . $rowCount, $allData['no_go_oline_count']);
  80 + $sheet->setCellValue('L' . $rowCount, $allData['delete_project_count']);
  81 + $sheet->setCellValue('M' . $rowCount, $allData['min_project_count']);
  82 + $sheet->setCellValue('N' . $rowCount, $allData['max_project_count']);
  83 + $sheet->setCellValue('0' . $rowCount, $allData['average']);
  84 + $rowCount++;
  85 + }
  86 + // 创建一个新的 Excel Writer 对象
  87 + $writer = new Xlsx($spreadsheet);
  88 + $filename = time().'.xlsx';
  89 + // 设置导出文件的保存路径和文件名
  90 + $filePath = public_path('upload/excel/'.$filename);
  91 + // 导出 Excel 文件
  92 + $writer->save($filePath);
  93 + // 返回导出文件的响应
  94 + return ['file_link'=>url('upload/excel/'.$filename)];
  95 + }
  96 + /**
  97 + * @remark :获取所有月份
  98 + * @name :getMonthsBetween
  99 + * @author :lyh
  100 + * @method :post
  101 + * @time :2024/9/26 10:43
  102 + */
  103 + public function getMonthsBetween($startDate, $endDate)
  104 + {
  105 + // 创建 DateTime 对象
  106 + $start = new DateTime($startDate);
  107 + $end = new DateTime($endDate);
  108 + // 确保结束时间是该月份的最后一天,以包含结束月份
  109 + $end->modify('first day of next month');
  110 + // 用于存储所有月份
  111 + $months = [];
  112 + // 循环遍历每个月
  113 + while ($start < $end) {
  114 + $months[] = $start->format('Y-m'); // 格式化成 YYYY-MM
  115 + $start->modify('+1 month'); // 增加一个月
  116 + }
  117 + return $months;
  118 + }
  119 +
  120 + /**
  121 + * @remark :根据时间获取开始时间+结束时间
  122 + * @name :getStartAndEndOfMonth
  123 + * @author :lyh
  124 + * @method :post
  125 + * @time :2024/9/26 10:44
  126 + */
  127 + public function getStartAndEndOfMonth($month) {
  128 + // 创建指定月份的 DateTime 对象
  129 + $start = new DateTime($month . '-01'); // 月份的第一天
  130 + // 复制开始日期并获取该月的最后一天
  131 + $end = clone $start;
  132 + $end->modify('last day of this month'); // 修改为该月的最后一天
  133 + // 格式化成 'Y-m-d H:i:s'
  134 + $startTime = $start->format('Y-m-d 00:00:00'); // 开始时间,精确到天的开始
  135 + $endTime = $end->format('Y-m-d 23:59:59'); // 结束时间,精确到天的结束
  136 + return [
  137 + 'start' => $startTime,
  138 + 'end' => $endTime,
  139 + ];
  140 + }
  141 +
  142 + /**
  143 + * @remark :导出数据
  144 + * @name :exportData
  145 + * @author :lyh
  146 + * @method :post
  147 + * @time :2023/8/1 16:45
  148 + */
  149 + public function exportDataProject($start_month,$end_month){
  150 + $data = [];
  151 + $monthData = $this->getMonthsBetween($start_month,$end_month);
  152 + $projectModel = new Project();
  153 + foreach ($monthData as $v){
  154 + $data[$v]['month'] = $v;
  155 + $timeArr = $this->getStartAndEndOfMonth($v);
  156 + $start_time = $timeArr['start'];
  157 + $end_time = $timeArr['end'];
  158 + $data[$v]['start'] = $start_time;
  159 + $data[$v]['end'] = $end_time;
  160 + //每月创建项目数据
  161 + $data[$v]['month_create_project_count'] = $projectModel->count(['created_at'=>['between',[$start_time,$end_time]],'deleted_at'=>0]);//每月创建项目数量
  162 + $data[$v]['month_project_go_online_count'] = $projectModel->count(['uptime'=>['between',[$start_time,$end_time]],'deleted_at'=>0]);//当月上线项目数量
  163 + $data[$v]['month_project_online_rate'] = 0;
  164 + if($data[$v]['month_create_project_count'] != 0){
  165 + $data[$v]['month_project_online_rate'] = $data[$v]['month_project_go_online_count'] / $data[$v]['month_create_project_count'];//比例
  166 + }
  167 +// $data[$v]['count'] = $projectModel->count(['deleted_at'=>0]);//所有项目总数
  168 +// $data[$v]['go_online_count'] = $projectModel->count(['uptime'=>['!=',null],'deleted_at'=>0]);//上线项目总数
  169 +// $data[$v]['promotion_web_count'] = $projectModel->count(['type'=>3,'deleted_at'=>0]);//推广项目总数
  170 +// $data[$v]['create_web_count'] = $projectModel->count(['type'=>2,'deleted_at'=>0]);//建站项目总数
  171 +// $data[$v]['no_go_oline_count'] = $projectModel->count(['uptime'=>null,'deleted_at'=>0]);//未上线项目数量
  172 +// $data[$v]['delete_project_count'] = $projectModel->count(['deleted_at'=>1]);//未上线项目数量
  173 +// $min_info = $projectModel->select('diff')->selectRaw('(uptime - created_at) as diff')->where('uptime','!=',null)->orderByRaw('diff ASC')->first();
  174 +// $data[$v]['min_project_count'] = $min_info['diff'];
  175 +// $max_info = $projectModel->select('diff')->selectRaw('(uptime - created_at) as diff')->where('uptime','!=',null)->orderByRaw('diff Desc')->first();
  176 +// $data[$v]['max_project_count'] = $max_info['diff'];
  177 +// $data[$v]['average'] = ceil(($max_info['diff'] + $min_info['diff']) / 2);
  178 + }
  179 + return $data;
  180 + }
  181 +
  182 + /**
  183 + * @remark :所有项目总数
  184 + * @name :countAll
  185 + * @author :lyh
  186 + * @method :post
  187 + * @time :2024/9/26 15:11
  188 + */
  189 + public function countAll(){
  190 + $projectModel = new Project();
  191 + $data['count'] = $projectModel->count(['deleted_at'=>0]);//所有项目总数
  192 + $data['go_online_count'] = $projectModel->count(['uptime'=>['!=',null],'deleted_at'=>0]);//上线项目总数
  193 + $data['promotion_web_count'] = $projectModel->count(['type'=>3,'deleted_at'=>0]);//推广项目总数
  194 + $data['create_web_count'] = $projectModel->count(['type'=>2,'deleted_at'=>0]);//建站项目总数
  195 + $data['no_go_oline_count'] = $projectModel->count(['uptime'=>null,'deleted_at'=>0]);//未上线项目数量
  196 + $data['delete_project_count'] = $projectModel->count(['deleted_at'=>1]);//删除项目数量
  197 + $min_info = $projectModel->select('diff')->selectRaw('(uptime - created_at) as diff')->where('uptime','!=',null)->orderByRaw('diff ASC')->first();
  198 + $data['min_project_count'] = $min_info['diff'];
  199 + $max_info = $projectModel->select('diff')->selectRaw('(uptime - created_at) as diff')->where('uptime','!=',null)->orderByRaw('diff Desc')->first();
  200 + $data['max_project_count'] = $max_info['diff'];
  201 + $data['average'] = ceil(($max_info['diff'] + $min_info['diff']) / 2);
  202 + return $data;
  203 + }
  204 +}
@@ -21,6 +21,8 @@ use Illuminate\Support\Facades\Cache; @@ -21,6 +21,8 @@ use Illuminate\Support\Facades\Cache;
21 use Illuminate\Support\Facades\DB; 21 use Illuminate\Support\Facades\DB;
22 use Illuminate\Support\Facades\Hash; 22 use Illuminate\Support\Facades\Hash;
23 use Illuminate\Support\Facades\Log; 23 use Illuminate\Support\Facades\Log;
  24 +use PhpOffice\PhpSpreadsheet\Spreadsheet;
  25 +use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
24 26
25 /** 27 /**
26 * Class IndexController 28 * Class IndexController
@@ -167,4 +169,6 @@ class IndexController extends BaseController @@ -167,4 +169,6 @@ class IndexController extends BaseController
167 } 169 }
168 $this->response('success',Code::SUCCESS,['dynamic_password'=>$dynamic_password]); 170 $this->response('success',Code::SUCCESS,['dynamic_password'=>$dynamic_password]);
169 } 171 }
  172 +
  173 +
170 } 174 }
@@ -316,7 +316,7 @@ class UserLoginLogic @@ -316,7 +316,7 @@ class UserLoginLogic
316 $is_amp = $amp_info ? $amp_info['amp_status'] : 0; 316 $is_amp = $amp_info ? $amp_info['amp_status'] : 0;
317 } 317 }
318 $info['is_amp'] = $is_amp; 318 $info['is_amp'] = $is_amp;
319 - $info['login_source'] = User::LOGIN_PASSWORD_SOURCE; 319 + $info['login_source'] = User::LOGIN_PASSWORD_SOURCE;//账号密码登录返回
320 //保存项目缓存 320 //保存项目缓存
321 Cache::put('user-'.$info['project_id'],$project,12 * 3600); 321 Cache::put('user-'.$info['project_id'],$project,12 * 3600);
322 return $this->success($info); 322 return $this->success($info);
@@ -116,6 +116,17 @@ class Base extends Model @@ -116,6 +116,17 @@ class Base extends Model
116 } 116 }
117 117
118 /** 118 /**
  119 + * @remark :统计数量
  120 + * @name :count
  121 + * @author :lyh
  122 + * @method :post
  123 + * @time :2024/9/26 10:52
  124 + */
  125 + public function count($condition){
  126 + $condition = $this->filterRequestData($condition);
  127 + return $this->formatQuery($condition)->count();
  128 + }
  129 + /**
119 * @remark :编辑 130 * @remark :编辑
120 * @name :edit 131 * @name :edit
121 * @author :lyh 132 * @author :lyh