Re-migrate code
This commit is contained in:
@@ -3,6 +3,7 @@ package services
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"sort"
|
||||
"time"
|
||||
|
||||
"gorm.io/gorm"
|
||||
@@ -311,8 +312,14 @@ func (s *CompaniesService) GetStats() (map[string]any, error) {
|
||||
return nil, errors.New("查询序列号统计失败")
|
||||
}
|
||||
|
||||
var employeeSerials []models.EmployeeSerial
|
||||
if err := database.DB.Order("created_at DESC").Find(&employeeSerials).Error; err != nil {
|
||||
return nil, errors.New("查询员工序列号统计失败")
|
||||
}
|
||||
|
||||
companyCount := len(companies)
|
||||
serialCount := len(serials)
|
||||
employeeSerialCount := len(employeeSerials)
|
||||
activeCount := 0
|
||||
for _, serial := range serials {
|
||||
if serial.IsActive && (serial.ValidUntil == nil || serial.ValidUntil.After(now)) {
|
||||
@@ -355,24 +362,43 @@ func (s *CompaniesService) GetStats() (map[string]any, error) {
|
||||
}
|
||||
|
||||
recentSerials := make([]map[string]any, 0)
|
||||
for i, serial := range serials {
|
||||
if i >= 10 {
|
||||
break
|
||||
}
|
||||
// 添加企业序列号
|
||||
for _, serial := range serials {
|
||||
recentSerials = append(recentSerials, map[string]any{
|
||||
"serialNumber": serial.SerialNumber,
|
||||
"companyName": serial.CompanyName,
|
||||
"isActive": serial.IsActive,
|
||||
"createdAt": serial.CreatedAt,
|
||||
"type": "company",
|
||||
})
|
||||
}
|
||||
// 添加员工序列号
|
||||
for _, serial := range employeeSerials {
|
||||
recentSerials = append(recentSerials, map[string]any{
|
||||
"serialNumber": serial.SerialNumber,
|
||||
"companyName": serial.CompanyName,
|
||||
"isActive": serial.IsActive,
|
||||
"createdAt": serial.CreatedAt,
|
||||
"type": "employee",
|
||||
"department": serial.Department,
|
||||
"employeeName": serial.EmployeeName,
|
||||
})
|
||||
}
|
||||
// 按创建时间排序,保留最新的10条
|
||||
sort.Slice(recentSerials, func(i, j int) bool {
|
||||
return recentSerials[i]["createdAt"].(time.Time).After(recentSerials[j]["createdAt"].(time.Time))
|
||||
})
|
||||
if len(recentSerials) > 10 {
|
||||
recentSerials = recentSerials[:10]
|
||||
}
|
||||
|
||||
return map[string]any{
|
||||
"overview": map[string]any{
|
||||
"totalCompanies": companyCount,
|
||||
"totalSerials": serialCount,
|
||||
"activeSerials": activeCount,
|
||||
"inactiveSerials": inactiveCount,
|
||||
"totalCompanies": companyCount,
|
||||
"totalSerials": serialCount,
|
||||
"totalEmployeeSerials": employeeSerialCount,
|
||||
"activeSerials": activeCount,
|
||||
"inactiveSerials": inactiveCount,
|
||||
},
|
||||
"monthlyStats": monthlyItems,
|
||||
"recentCompanies": recentCompanies,
|
||||
|
||||
Reference in New Issue
Block a user