diff --git a/controllers/companies_controller.go b/controllers/companies_controller.go index 0d6ef0d..edc5254 100644 --- a/controllers/companies_controller.go +++ b/controllers/companies_controller.go @@ -4,6 +4,7 @@ import ( "net/http" "net/url" "strconv" + "time" "github.com/gin-gonic/gin" @@ -47,11 +48,22 @@ func (c *CompaniesController) FindAll(ctx *gin.Context) { } items := make([]gin.H, 0, len(companies)) + now := time.Now() for _, company := range companies { + serialCount := len(company.Serials) + activeCount := 0 + for _, serial := range company.Serials { + if serial.IsActive && (serial.ValidUntil == nil || serial.ValidUntil.After(now)) { + activeCount++ + } + } + items = append(items, gin.H{ "companyName": company.CompanyName, "firstCreated": company.CreatedAt, "lastCreated": company.UpdatedAt, + "serialCount": serialCount, + "activeCount": activeCount, "status": map[bool]string{ true: "active", false: "disabled", @@ -99,7 +111,10 @@ func (c *CompaniesController) FindOne(ctx *gin.Context) { return } - SuccessResponse(ctx, "获取企业详情成功", data) + ctx.JSON(http.StatusOK, gin.H{ + "message": "获取企业详情成功", + "data": data, + }) } // Create 创建企业 @@ -298,5 +313,8 @@ func (c *CompaniesController) StatsOverview(ctx *gin.Context) { return } - SuccessResponse(ctx, "获取统计数据成功", stats) + ctx.JSON(http.StatusOK, gin.H{ + "message": "获取统计数据成功", + "data": stats, + }) } diff --git a/models/models.go b/models/models.go index b4266a0..e2de948 100644 --- a/models/models.go +++ b/models/models.go @@ -33,17 +33,17 @@ type Company struct { // Serial 模型 type Serial struct { - ID uint `gorm:"primaryKey"` - SerialNumber string `gorm:"uniqueIndex;size:255"` - CompanyName string `gorm:"index;size:255"` - ValidUntil *time.Time - IsActive bool `gorm:"default:true"` - CreatedBy *uint - CreatedAt time.Time - UpdatedAt time.Time - DeletedAt gorm.DeletedAt `gorm:"index"` - User *User `gorm:"foreignKey:CreatedBy"` - Company *Company `gorm:"foreignKey:CompanyName;references:CompanyName"` + ID uint `gorm:"primaryKey" json:"id"` + SerialNumber string `gorm:"uniqueIndex;size:255" json:"serialNumber"` + CompanyName string `gorm:"index;size:255" json:"companyName"` + ValidUntil *time.Time `json:"validUntil"` + IsActive bool `gorm:"default:true" json:"isActive"` + CreatedBy *uint `json:"createdBy"` + CreatedAt time.Time `json:"createdAt"` + UpdatedAt time.Time `json:"updatedAt"` + DeletedAt gorm.DeletedAt `gorm:"index" json:"-"` + User *User `gorm:"foreignKey:CreatedBy" json:"user,omitempty"` + Company *Company `gorm:"foreignKey:CompanyName;references:CompanyName" json:"company,omitempty"` } // UserDTO 数据传输对象 diff --git a/services/companies_service.go b/services/companies_service.go index 3f58c70..a16d7f6 100644 --- a/services/companies_service.go +++ b/services/companies_service.go @@ -37,7 +37,7 @@ func (s *CompaniesService) FindAll(page int, limit int, search string) ([]models return nil, 0, 0, errors.New("查询企业总数失败") } - result := db.Order("updated_at DESC").Offset(offset).Limit(limit).Find(&companies) + result := db.Preload("Serials").Order("updated_at DESC").Offset(offset).Limit(limit).Find(&companies) if result.Error != nil { return nil, 0, 0, errors.New("查询企业列表失败") }