初始化
This commit is contained in:
@@ -0,0 +1,37 @@
|
||||
package model
|
||||
|
||||
import (
|
||||
"gorm.io/gorm"
|
||||
"time"
|
||||
"vote-api/global"
|
||||
)
|
||||
|
||||
// Article 文章表
|
||||
type Article struct {
|
||||
ArticleId int64 `gorm:"column:article_id;type:bigint(19);primary_key;comment:主键id" json:"article_id"`
|
||||
ArticleTitle string `gorm:"column:article_title;type:varchar(200);comment:文章标题" json:"article_title"`
|
||||
ArticleStatus int `gorm:"column:article_status;type:tinyint(1);default:1;comment:文章状态(1:正常 2:禁用)" json:"article_status"`
|
||||
StartTime *LocalTime `gorm:"column:start_time;type:datetime;comment:开始投票时间" json:"start_time"`
|
||||
EndTime *LocalTime `gorm:"column:end_time;type:datetime;comment:结束投票时间" json:"end_time"`
|
||||
VoteNum uint `gorm:"column:vote_num;type:int(10) unsigned;default:0;comment:总票数" json:"vote_num"`
|
||||
ArticleContent string `gorm:"column:article_content;type:text;comment:文章内容" json:"article_content"`
|
||||
Model
|
||||
}
|
||||
|
||||
func (m *Article) TableName() string {
|
||||
return "article"
|
||||
}
|
||||
|
||||
func (m *Article) BeforeCreate(tx *gorm.DB) error {
|
||||
if m.ArticleId == 0 {
|
||||
m.ArticleId = global.Snowflake.Generate().Int64()
|
||||
}
|
||||
|
||||
m.CreatedAt = LocalTime(time.Now())
|
||||
tx.Statement.SetColumn("CreatedAt", m.CreatedAt)
|
||||
|
||||
m.UpdatedAt = LocalTime(time.Now())
|
||||
tx.Statement.SetColumn("UpdatedAt", m.UpdatedAt)
|
||||
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
package model
|
||||
|
||||
import (
|
||||
"gorm.io/gorm"
|
||||
"time"
|
||||
"vote-api/global"
|
||||
)
|
||||
|
||||
// ArticleAuthor 文章-作者表
|
||||
type ArticleAuthor struct {
|
||||
AuthorId int64 `gorm:"column:author_id;type:bigint(19);primary_key;comment:主键id" json:"author_id"`
|
||||
ArticleId int64 `gorm:"column:article_id;type:bigint(19);comment:文章id;NOT NULL" json:"article_id"`
|
||||
AuthorName string `gorm:"column:author_name;type:varchar(100);comment:作者姓名" json:"author_name"`
|
||||
HospitalId int64 `gorm:"column:hospital_id;type:bigint(19);comment:作者所属医院id" json:"hospital_id"`
|
||||
Model
|
||||
}
|
||||
|
||||
func (m *ArticleAuthor) TableName() string {
|
||||
return "article_author"
|
||||
}
|
||||
|
||||
func (m *ArticleAuthor) BeforeCreate(tx *gorm.DB) error {
|
||||
if m.AuthorId == 0 {
|
||||
m.AuthorId = global.Snowflake.Generate().Int64()
|
||||
}
|
||||
|
||||
m.CreatedAt = LocalTime(time.Now())
|
||||
tx.Statement.SetColumn("CreatedAt", m.CreatedAt)
|
||||
|
||||
m.UpdatedAt = LocalTime(time.Now())
|
||||
tx.Statement.SetColumn("UpdatedAt", m.UpdatedAt)
|
||||
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
package model
|
||||
|
||||
import (
|
||||
"gorm.io/gorm"
|
||||
"time"
|
||||
"vote-api/global"
|
||||
)
|
||||
|
||||
// ArticleVoteDay 文章-每日投票
|
||||
type ArticleVoteDay struct {
|
||||
VoteDayId int64 `gorm:"column:vote_day_id;type:bigint(19);primary_key;comment:主键id" json:"vote_day_id"`
|
||||
ArticleId int64 `gorm:"column:article_id;type:bigint(19);comment:文章id;NOT NULL" json:"article_id"`
|
||||
UserId int64 `gorm:"column:user_id;type:bigint(19);comment:用户id;NOT NULL" json:"user_id"`
|
||||
VotedAt *LocalTime `gorm:"column:voted_at;type:date;comment:投票时间(日)" json:"voted_at"`
|
||||
Model
|
||||
}
|
||||
|
||||
func (m *ArticleVoteDay) TableName() string {
|
||||
return "article_vote_day"
|
||||
}
|
||||
|
||||
func (m *ArticleVoteDay) BeforeCreate(tx *gorm.DB) error {
|
||||
if m.VoteDayId == 0 {
|
||||
m.VoteDayId = global.Snowflake.Generate().Int64()
|
||||
}
|
||||
|
||||
m.CreatedAt = LocalTime(time.Now())
|
||||
tx.Statement.SetColumn("CreatedAt", m.CreatedAt)
|
||||
|
||||
m.UpdatedAt = LocalTime(time.Now())
|
||||
tx.Statement.SetColumn("UpdatedAt", m.UpdatedAt)
|
||||
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
package model
|
||||
|
||||
import (
|
||||
"gorm.io/gorm"
|
||||
"time"
|
||||
"vote-api/global"
|
||||
)
|
||||
|
||||
// Data 数据表
|
||||
type Data struct {
|
||||
DataId int64 `gorm:"column:data_id;type:bigint(19);primary_key;comment:主键id" json:"data_id"`
|
||||
ViewNum uint `gorm:"column:view_num;type:int(10) unsigned;default:0;comment:浏览数量" json:"view_num"`
|
||||
VoteNum uint `gorm:"column:vote_num;type:int(10) unsigned;comment:投票数量" json:"vote_num"`
|
||||
Model
|
||||
}
|
||||
|
||||
func (m *Data) TableName() string {
|
||||
return "data"
|
||||
}
|
||||
|
||||
func (m *Data) BeforeCreate(tx *gorm.DB) error {
|
||||
if m.DataId == 0 {
|
||||
m.DataId = global.Snowflake.Generate().Int64()
|
||||
}
|
||||
|
||||
m.CreatedAt = LocalTime(time.Now())
|
||||
tx.Statement.SetColumn("CreatedAt", m.CreatedAt)
|
||||
|
||||
m.UpdatedAt = LocalTime(time.Now())
|
||||
tx.Statement.SetColumn("UpdatedAt", m.UpdatedAt)
|
||||
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
package model
|
||||
|
||||
import (
|
||||
"gorm.io/gorm"
|
||||
"time"
|
||||
"vote-api/global"
|
||||
)
|
||||
|
||||
// User 用户表
|
||||
type User struct {
|
||||
UserId int64 `gorm:"column:user_id;type:bigint(19);primary_key;comment:用户id" json:"user_id"`
|
||||
AppIden string `gorm:"column:app_iden;type:varchar(50);comment:app唯一标识" json:"app_iden"`
|
||||
UserStatus int `gorm:"column:user_status;type:tinyint(1);default:1;comment:状态(1:正常 2:禁用)" json:"user_status"`
|
||||
OpenId string `gorm:"column:open_id;type:varchar(100);comment:用户微信标识" json:"open_id"`
|
||||
LoginAt *LocalTime `gorm:"column:login_at;type:datetime;comment:登陆时间" json:"login_at"`
|
||||
LoginIp string `gorm:"column:login_ip;type:varchar(255);comment:登陆ip" json:"login_ip"`
|
||||
Model
|
||||
}
|
||||
|
||||
func (m *User) TableName() string {
|
||||
return "user"
|
||||
}
|
||||
|
||||
func (m *User) BeforeCreate(tx *gorm.DB) error {
|
||||
if m.UserId == 0 {
|
||||
m.UserId = global.Snowflake.Generate().Int64()
|
||||
}
|
||||
|
||||
m.CreatedAt = LocalTime(time.Now())
|
||||
tx.Statement.SetColumn("CreatedAt", m.CreatedAt)
|
||||
|
||||
m.UpdatedAt = LocalTime(time.Now())
|
||||
tx.Statement.SetColumn("UpdatedAt", m.UpdatedAt)
|
||||
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
package model
|
||||
|
||||
import (
|
||||
"gorm.io/gorm"
|
||||
"time"
|
||||
"vote-api/global"
|
||||
)
|
||||
|
||||
// Video 视频表
|
||||
type Video struct {
|
||||
VideoId int64 `gorm:"column:video_id;type:bigint(19);primary_key;comment:主键id" json:"video_id"`
|
||||
VideoTitle string `gorm:"column:video_title;type:varchar(200);comment:视频标题" json:"video_title"`
|
||||
VideoStatus int `gorm:"column:video_status;type:tinyint(1);default:1;comment:视频状态(1:正常 2:禁用)" json:"video_status"`
|
||||
StartTime *LocalTime `gorm:"column:start_time;type:datetime;comment:开始投票时间" json:"start_time"`
|
||||
EndTime *LocalTime `gorm:"column:end_time;type:datetime;comment:结束投票时间" json:"end_time"`
|
||||
VoteNum uint `gorm:"column:vote_num;type:int(10) unsigned;default:0;comment:总票数" json:"vote_num"`
|
||||
VideoUrl string `gorm:"column:video_url;type:varchar(255);comment:视频地址" json:"video_url"`
|
||||
Model
|
||||
}
|
||||
|
||||
func (m *Video) TableName() string {
|
||||
return "video"
|
||||
}
|
||||
|
||||
func (m *Video) BeforeCreate(tx *gorm.DB) error {
|
||||
if m.VideoId == 0 {
|
||||
m.VideoId = global.Snowflake.Generate().Int64()
|
||||
}
|
||||
|
||||
m.CreatedAt = LocalTime(time.Now())
|
||||
tx.Statement.SetColumn("CreatedAt", m.CreatedAt)
|
||||
|
||||
m.UpdatedAt = LocalTime(time.Now())
|
||||
tx.Statement.SetColumn("UpdatedAt", m.UpdatedAt)
|
||||
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
package model
|
||||
|
||||
import (
|
||||
"gorm.io/gorm"
|
||||
"time"
|
||||
"vote-api/global"
|
||||
)
|
||||
|
||||
// VideoAuthor 视频-作者表
|
||||
type VideoAuthor struct {
|
||||
AuthorId int64 `gorm:"column:author_id;type:bigint(19);primary_key;comment:主键id" json:"author_id"`
|
||||
VideoId int64 `gorm:"column:video_id;type:bigint(19);comment:视频id;NOT NULL" json:"video_id"`
|
||||
AuthorName string `gorm:"column:author_name;type:varchar(100);comment:作者姓名" json:"author_name"`
|
||||
HospitalId int64 `gorm:"column:hospital_id;type:bigint(19);comment:作者所属医院id" json:"hospital_id"`
|
||||
Model
|
||||
}
|
||||
|
||||
func (m *VideoAuthor) TableName() string {
|
||||
return "video_author"
|
||||
}
|
||||
|
||||
func (m *VideoAuthor) BeforeCreate(tx *gorm.DB) error {
|
||||
if m.AuthorId == 0 {
|
||||
m.AuthorId = global.Snowflake.Generate().Int64()
|
||||
}
|
||||
|
||||
m.CreatedAt = LocalTime(time.Now())
|
||||
tx.Statement.SetColumn("CreatedAt", m.CreatedAt)
|
||||
|
||||
m.UpdatedAt = LocalTime(time.Now())
|
||||
tx.Statement.SetColumn("UpdatedAt", m.UpdatedAt)
|
||||
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
package model
|
||||
|
||||
import (
|
||||
"gorm.io/gorm"
|
||||
"time"
|
||||
"vote-api/global"
|
||||
)
|
||||
|
||||
// VideoVoteDay 视频-每日投票
|
||||
type VideoVoteDay struct {
|
||||
VoteDayId int64 `gorm:"column:vote_day_id;type:bigint(19);primary_key;comment:主键id" json:"vote_day_id"`
|
||||
VideoId int64 `gorm:"column:video_id;type:bigint(19);comment:视频id;NOT NULL" json:"video_id"`
|
||||
UserId int64 `gorm:"column:user_id;type:bigint(19);comment:用户id;NOT NULL" json:"user_id"`
|
||||
VotedAt *LocalTime `gorm:"column:voted_at;type:date;comment:投票时间(日)" json:"voted_at"`
|
||||
Model
|
||||
}
|
||||
|
||||
func (m *VideoVoteDay) TableName() string {
|
||||
return "video_vote_day"
|
||||
}
|
||||
|
||||
func (m *VideoVoteDay) BeforeCreate(tx *gorm.DB) error {
|
||||
if m.VoteDayId == 0 {
|
||||
m.VoteDayId = global.Snowflake.Generate().Int64()
|
||||
}
|
||||
|
||||
m.CreatedAt = LocalTime(time.Now())
|
||||
tx.Statement.SetColumn("CreatedAt", m.CreatedAt)
|
||||
|
||||
m.UpdatedAt = LocalTime(time.Now())
|
||||
tx.Statement.SetColumn("UpdatedAt", m.UpdatedAt)
|
||||
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,87 @@
|
||||
package model
|
||||
|
||||
import (
|
||||
"database/sql/driver"
|
||||
"errors"
|
||||
"fmt"
|
||||
"gorm.io/gorm"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
type Model struct {
|
||||
CreatedAt LocalTime `gorm:"column:created_at;type:datetime;comment:创建时间" json:"created_at"`
|
||||
UpdatedAt LocalTime `gorm:"column:updated_at;type:datetime;comment:修改时间" json:"updated_at"`
|
||||
}
|
||||
|
||||
// LocalTime 自定义数据类型
|
||||
type LocalTime time.Time
|
||||
|
||||
func (t *LocalTime) UnmarshalJSON(data []byte) error {
|
||||
if string(data) == "null" {
|
||||
return nil
|
||||
}
|
||||
var err error
|
||||
// 前端接收的时间字符串
|
||||
str := string(data)
|
||||
// 去除接收的str收尾多余的"
|
||||
timeStr := strings.Trim(str, "\"")
|
||||
t1, err := time.Parse("2006-01-02 15:04:05", timeStr)
|
||||
*t = LocalTime(t1)
|
||||
return err
|
||||
}
|
||||
|
||||
func (t LocalTime) MarshalJSON() ([]byte, error) {
|
||||
formatted := fmt.Sprintf("\"%v\"", time.Time(t).Format("2006-01-02 15:04:05"))
|
||||
return []byte(formatted), nil
|
||||
}
|
||||
|
||||
func (t LocalTime) Value() (driver.Value, error) {
|
||||
// MyTime 转换成 time.Time 类型
|
||||
tTime := time.Time(t)
|
||||
return tTime.Format("2006-01-02 15:04:05"), nil
|
||||
}
|
||||
|
||||
func (t *LocalTime) Scan(v interface{}) error {
|
||||
switch vt := v.(type) {
|
||||
case time.Time:
|
||||
// 字符串转成 time.Time 类型
|
||||
*t = LocalTime(vt)
|
||||
default:
|
||||
return errors.New("类型处理错误")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (t *LocalTime) String() string {
|
||||
return fmt.Sprintf("hhh:%s", time.Time(*t).String())
|
||||
}
|
||||
|
||||
func (t *LocalTime) IsEmpty() bool {
|
||||
return time.Time(*t).IsZero()
|
||||
}
|
||||
|
||||
func (m *Model) BeforeUpdate(tx *gorm.DB) (err error) {
|
||||
m.UpdatedAt = LocalTime(time.Now())
|
||||
tx.Statement.SetColumn("UpdatedAt", m.UpdatedAt)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func Paginate(page, pageSize int) func(db *gorm.DB) *gorm.DB {
|
||||
return func(db *gorm.DB) *gorm.DB {
|
||||
if page <= 0 {
|
||||
page = 1
|
||||
}
|
||||
|
||||
switch {
|
||||
case pageSize > 100:
|
||||
pageSize = 100
|
||||
case pageSize <= 0:
|
||||
pageSize = 10
|
||||
}
|
||||
|
||||
offset := (page - 1) * pageSize
|
||||
return db.Offset(offset).Limit(pageSize)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user