diff --git a/app/admin/controller/Home/Home.php b/app/admin/controller/Home/Home.php new file mode 100644 index 0000000..02c1200 --- /dev/null +++ b/app/admin/controller/Home/Home.php @@ -0,0 +1,25 @@ + [ + 'teacher' => self::getTodayYesterdayNumContrastInfo(ModelTeacher::class, $today), + 'info_article' => self::getTodayYesterdayNumContrastInfo(ModelinfoArticle::class, $today), + 'works' => self::getTodayYesterdayNumContrastInfo(ModelWorks::class, $today), + 'flow' => self::getTodayYesterdayNumContrastInfo(ModelFlow::class, $today) + ], + 'statistics' => [ + 'signup' => self::getSignupStatistics(ModelSignup::class, $monthly_data), + 'leave_message' => self::getSignupStatistics(ModelLeaveMessage::class, $monthly_data), + ] + ]; + } + + /** + * 获取今昨日新增数量对比信息 + * + * @param string $model 模型层命名空间 + * @param array $monthly_data 今年各个年份(时间戳)数据数量关联数组 + * + */ + private static function getSignupStatistics(string $model, array $monthly_data): array + { + //模型层实例 + $model = new $model(); + + //新增时间字段名 + $create_time_field = self::buildCreateTimeField($model); + + //数据查询 + $select = $model->field([$create_time_field])->select()->toArray(); + + //各个月份数据统计 + foreach ($select as $value) { + $create_monthly_time_ts = strtotime(date('Y-m', strtotime($value[$create_time_field]))); + foreach ($monthly_data as $monthly_time_ts => &$monthly_time_ts_value) { + if ($monthly_time_ts == $create_monthly_time_ts) { + $monthly_time_ts_value++; + break; + } + }; + }; + + //最终数据 + $data = array_values($monthly_data); + + return $data; + } + + /** + * 获取今昨日新增数量对比信息 + * + * @param string $model 模型层命名空间 + * @param string $today 今日日期时间 + * + * @return array [ + * 'count' 总数 + * 'percentage' 今昨升降百分比数 + * ] + */ + private static function getTodayYesterdayNumContrastInfo(string $model, string $today): array + { + //模型层实例 + $model = new $model(); + + //新增时间字段名 + $create_time_field = self::buildCreateTimeField($model); + + //数据查询 + $data = $model->field([$create_time_field])->select()->toArray(); + + //今日总数获取 + $today_total_count = count($data); + + //昨日总数获取 + $yesterday_total_count = 0; + foreach ($data as $value) { + $create_time_ts = strtotime($value[$create_time_field]); + if ($create_time_ts < strtotime($today)) { + $yesterday_total_count++; + } + }; + + //上升百分比计算 + $percentage = round((($today_total_count - $yesterday_total_count) / $yesterday_total_count) * 100, 2); + + return [ + 'count' => $today_total_count, + 'percentage' => $percentage, + ]; + } + + /** + * 构建新增时间字段 + * + * @param \think\Model $model 模型实例 + */ + private static function buildCreateTimeField(\think\Model $model): string + { + //获取表名 + $table_name = $model->getTable(); + //新增字段名 + $create_time_field = "{$table_name}_create_time"; + + return $create_time_field; + } + + /** + * 构建今年月份(时间戳)关联数组 + * + */ + private static function buildMonthsTsOfThisYearData(): array + { + //今年年份 + $year = date('Y'); + + //月份(时间戳)数组构建 + $monthly_data = []; + for ($i = 1; $i <= 12; $i++) { + $monthly_data[strtotime("{$year}-$i")] = 0; + } + return $monthly_data; + } +}