package api import ( "encoding/json" "fmt" "strconv" "strings" "time" ) type IDS []int // 合并为以逗号分隔的字符串 func (ids IDS) ToString() (str string) { return strings.Trim(strings.Join(strings.Fields(fmt.Sprint(ids)), ","), "[]") } type ImageList []Image // 按照ID排序 func (image *ImageList) SortByIDList(id_list []string) { var sortedImageList ImageList for _, id := range id_list { id_number, _ := strconv.Atoi(id) for _, image := range *image { if image.ID == id_number { sortedImageList = append(sortedImageList, image) } } } *image = sortedImageList } // 取到所有的文章ID, 去除重复 func (images *ImageList) ToAllArticleID() (uniqueIds IDS) { article_ids := make(map[int]bool) for _, image := range *images { article_ids[image.ArticleID] = true } for id := range article_ids { uniqueIds = append(uniqueIds, id) } return uniqueIds } // 取到所有的用户ID, 去除重复 func (images *ImageList) ToAllUserID() (uniqueIds IDS) { user_ids := make(map[int]bool) for _, image := range *images { user_ids[image.UserID] = true } for id := range user_ids { uniqueIds = append(uniqueIds, id) } return uniqueIds } type Image struct { ID int `json:"id" db:"id" gorm:"primaryKey"` Width int `json:"width" db:"width"` Height int `json:"height" db:"height"` Content string `json:"content" db:"content"` Remark string `json:"remark" db:"remark"` Description string `json:"description" db:"description"` Tags string `json:"tags" db:"tags"` Rank string `json:"rank" db:"rank"` CommentNum int `json:"comment_num" db:"comment_num"` PraiseCount int `json:"praise_count" db:"praise_count"` CollectCount int `json:"collect_count" db:"collect_count"` ArticleID int `json:"article_id" db:"article_id"` UserID int `json:"user_id" db:"user_id"` CreateTime time.Time `json:"create_time" db:"create_time"` UpdateTime time.Time `json:"update_time" db:"update_time"` Text TextList `json:"text" db:"text" gorm:"type:json"` User User `json:"user" gorm:"foreignKey:UserID"` Article Article `json:"article" gorm:"foreignKey:ArticleID"` } func (Image) TableName() string { return "web_images" } type TextList []struct { Text string `json:"text"` Confidence float64 `json:"confidence"` Coordinate [][]float64 `json:"coordinate"` } func (a *TextList) Scan(value interface{}) error { // 如果数据库中的值为NULL,则返回nil if value == nil || len(value.([]byte)) == 0 { *a = TextList{} return nil } return json.Unmarshal(value.([]byte), a) } type User struct { ID int `json:"id" db:"id" gorm:"primaryKey"` UserName string `json:"user_name" db:"user_name"` Avatar string `json:"avatar" db:"avatar"` Rank string `json:"rank" db:"rank"` Price int `json:"price" db:"price"` CreateTime time.Time `json:"create_time" db:"create_time"` UpdateTime time.Time `json:"update_time" db:"update_time"` } func (User) TableName() string { return "web_member" } type Article struct { ID int `json:"id" db:"id" gorm:"primaryKey"` Title string `json:"title" db:"title"` Orientation string `json:"orientation" db:"orientation"` Device string `json:"device" db:"device"` Era string `json:"era" db:"era"` Tags string `json:"tags" db:"tags"` UserId int `json:"user_id" db:"user_id"` User User `json:"user" gorm:"foreignKey:UserId"` CreateTime time.Time `json:"create_time" db:"create_time"` UpdateTime time.Time `json:"update_time" db:"update_time"` } func (Article) TableName() string { return "web_article" } type Category struct { ID int `json:"id"` Title string `json:"title"` Keyword string `json:"keyword"` ParentID int `json:"parent_id"` Status int `json:"status"` Content string `json:"content"` Sort int `json:"sort"` Image string `json:"image"` ImageNum int `json:"image_num"` ArticleNum int `json:"article_num"` CreateTime time.Time `json:"create_time"` UpdateTime time.Time `json:"update_time"` } // TableName 方法用于自定义表名 func (Category) TableName() string { return "web_article_category" } // 输入配置 type ConfigMysql struct { Host string Port int Database string UserName string Password string } type Config struct { Mysql ConfigMysql }