Re-migrate code

This commit is contained in:
2026-03-02 10:31:45 +08:00
parent f80f2b43ce
commit 1cc3097d9b
3 changed files with 32 additions and 14 deletions

View File

@@ -4,6 +4,7 @@ import (
"net/http" "net/http"
"net/url" "net/url"
"strconv" "strconv"
"time"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
@@ -47,11 +48,22 @@ func (c *CompaniesController) FindAll(ctx *gin.Context) {
} }
items := make([]gin.H, 0, len(companies)) items := make([]gin.H, 0, len(companies))
now := time.Now()
for _, company := range companies { 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{ items = append(items, gin.H{
"companyName": company.CompanyName, "companyName": company.CompanyName,
"firstCreated": company.CreatedAt, "firstCreated": company.CreatedAt,
"lastCreated": company.UpdatedAt, "lastCreated": company.UpdatedAt,
"serialCount": serialCount,
"activeCount": activeCount,
"status": map[bool]string{ "status": map[bool]string{
true: "active", true: "active",
false: "disabled", false: "disabled",
@@ -99,7 +111,10 @@ func (c *CompaniesController) FindOne(ctx *gin.Context) {
return return
} }
SuccessResponse(ctx, "获取企业详情成功", data) ctx.JSON(http.StatusOK, gin.H{
"message": "获取企业详情成功",
"data": data,
})
} }
// Create 创建企业 // Create 创建企业
@@ -298,5 +313,8 @@ func (c *CompaniesController) StatsOverview(ctx *gin.Context) {
return return
} }
SuccessResponse(ctx, "获取统计数据成功", stats) ctx.JSON(http.StatusOK, gin.H{
"message": "获取统计数据成功",
"data": stats,
})
} }

View File

@@ -33,17 +33,17 @@ type Company struct {
// Serial 模型 // Serial 模型
type Serial struct { type Serial struct {
ID uint `gorm:"primaryKey"` ID uint `gorm:"primaryKey" json:"id"`
SerialNumber string `gorm:"uniqueIndex;size:255"` SerialNumber string `gorm:"uniqueIndex;size:255" json:"serialNumber"`
CompanyName string `gorm:"index;size:255"` CompanyName string `gorm:"index;size:255" json:"companyName"`
ValidUntil *time.Time ValidUntil *time.Time `json:"validUntil"`
IsActive bool `gorm:"default:true"` IsActive bool `gorm:"default:true" json:"isActive"`
CreatedBy *uint CreatedBy *uint `json:"createdBy"`
CreatedAt time.Time CreatedAt time.Time `json:"createdAt"`
UpdatedAt time.Time UpdatedAt time.Time `json:"updatedAt"`
DeletedAt gorm.DeletedAt `gorm:"index"` DeletedAt gorm.DeletedAt `gorm:"index" json:"-"`
User *User `gorm:"foreignKey:CreatedBy"` User *User `gorm:"foreignKey:CreatedBy" json:"user,omitempty"`
Company *Company `gorm:"foreignKey:CompanyName;references:CompanyName"` Company *Company `gorm:"foreignKey:CompanyName;references:CompanyName" json:"company,omitempty"`
} }
// UserDTO 数据传输对象 // UserDTO 数据传输对象

View File

@@ -37,7 +37,7 @@ func (s *CompaniesService) FindAll(page int, limit int, search string) ([]models
return nil, 0, 0, errors.New("查询企业总数失败") 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 { if result.Error != nil {
return nil, 0, 0, errors.New("查询企业列表失败") return nil, 0, 0, errors.New("查询企业列表失败")
} }