初始化

This commit is contained in:
2024-08-30 11:19:25 +08:00
parent 037f7ee76e
commit c233e5d9b4
114 changed files with 7054 additions and 0 deletions
+42
View File
@@ -0,0 +1,42 @@
package model
import (
"gorm.io/gorm"
"time"
"vote-admin-api/global"
)
// AdminUser 后台-用户表
type AdminUser struct {
UserId int64 `gorm:"column:user_id;type:bigint(19);primary_key;comment:主键id" json:"user_id"`
Access string `gorm:"column:access;type:varchar(64);comment:账号;NOT NULL" json:"access"`
Password string `gorm:"column:password;type:varchar(128);comment:密码;NOT NULL" json:"password"`
Salt string `gorm:"column:salt;type:varchar(255);comment:密码掩码;NOT NULL" json:"salt"`
NickName string `gorm:"column:nick_name;type:varchar(255);comment:昵称" json:"nick_name"`
Status int `gorm:"column:status;type:tinyint(1);default:2;comment:状态(1:正常 2:审核中 3:审核失败)" json:"status"`
IsDeleted int `gorm:"column:is_deleted;type:tinyint(1);default:0;comment:是否被删除(0:否 1:是)" json:"is_deleted"`
IsDisabled int `gorm:"column:is_disabled;type:tinyint(1);default:0;comment:是否被禁用(0:否 1:是)" json:"is_disabled"`
Phone string `gorm:"column:phone;type:varchar(11);comment:手机号" json:"phone"`
Avatar string `gorm:"column:avatar;type:varchar(255);comment:头像" json:"avatar"`
Sex int `gorm:"column:sex;type:tinyint(1);comment:性别(1:男 2:女)" json:"sex"`
Email string `gorm:"column:email;type:varchar(100);comment:邮箱" json:"email"`
Model
}
func (m *AdminUser) TableName() string {
return "admin_user"
}
func (m *AdminUser) 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
}
+37
View File
@@ -0,0 +1,37 @@
package model
import (
"gorm.io/gorm"
"time"
"vote-admin-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"`
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
ArticleAuthor []*ArticleAuthor `gorm:"foreignKey:ArticleId;references:article_id" json:"article_author"`
Rank *int `gorm:"column:rank;type:tinyint(1) unsigned;comment:排名" json:"rank"`
}
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
}
+35
View File
@@ -0,0 +1,35 @@
package model
import (
"gorm.io/gorm"
"time"
"vote-admin-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
BaseHospital *BaseHospital `gorm:"foreignKey:HospitalId;references:hospital_id" json:"base_hospital"`
}
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
}
+34
View File
@@ -0,0 +1,34 @@
package model
import (
"gorm.io/gorm"
"time"
"vote-admin-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
}
+33
View File
@@ -0,0 +1,33 @@
package model
import (
"gorm.io/gorm"
"time"
"vote-admin-api/global"
)
// BaseAgreement 基础-协议
type BaseAgreement struct {
AgreementId int64 `gorm:"column:agreement_id;type:bigint(19);primary_key;comment:主键id" json:"agreement_id"`
AgreementType int `gorm:"column:agreement_type;type:tinyint(1);comment:协议类型(1:大赛介绍 2:投票规则);NOT NULL" json:"agreement_type"`
AgreementContent string `gorm:"column:agreement_content;type:text;comment:协议内容" json:"agreement_content"`
Model
}
func (m *BaseAgreement) TableName() string {
return "base_agreement"
}
func (m *BaseAgreement) BeforeCreate(tx *gorm.DB) error {
if m.AgreementId == 0 {
m.AgreementId = 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
}
+26
View File
@@ -0,0 +1,26 @@
package model
import (
"gorm.io/gorm"
"vote-admin-api/global"
)
// BaseArea 地区表
type BaseArea struct {
AreaId int64 `gorm:"column:area_id;type:bigint(19);primary_key;comment:地区编号" json:"area_id"`
AreaName string `gorm:"column:area_name;type:varchar(255);comment:名称" json:"area_name"`
ParentId int64 `gorm:"column:parent_id;type:bigint(19);comment:上级编号" json:"parent_id"`
Zip string `gorm:"column:zip;type:varchar(10);comment:邮编" json:"zip"`
AreaType int `gorm:"column:area_type;type:tinyint(4);comment:类型(1:国家,2:省,3:市,4:区县)" json:"area_type"`
}
func (m *BaseArea) TableName() string {
return "base_area"
}
func (m *BaseArea) BeforeCreate(tx *gorm.DB) error {
if m.AreaId == 0 {
m.AreaId = global.Snowflake.Generate().Int64()
}
return nil
}
+46
View File
@@ -0,0 +1,46 @@
package model
import (
"gorm.io/gorm"
"time"
"vote-admin-api/global"
)
// BaseHospital 医院表
type BaseHospital struct {
HospitalId int64 `gorm:"column:hospital_id;type:bigint(19);primary_key;comment:主键id" json:"hospital_id"`
HospitalName string `gorm:"column:hospital_name;type:varchar(255);comment:医院名称" json:"hospital_name"`
HospitalStatus int `gorm:"column:hospital_status;type:tinyint(1);default:1;comment:状态(0:禁用 1:正常 2:删除)" json:"hospital_status"`
HospitalLevelName string `gorm:"column:hospital_level_name;type:varchar(20);comment:医院等级名称" json:"hospital_level_name"`
PostCode string `gorm:"column:post_code;type:varchar(50);comment:邮政编码" json:"post_code"`
TelePhone string `gorm:"column:tele_phone;type:varchar(20);comment:电话" json:"tele_phone"`
ProvinceId int `gorm:"column:province_id;type:int(11);comment:省份id" json:"province_id"`
Province string `gorm:"column:province;type:varchar(50);comment:省份" json:"province"`
CityId int `gorm:"column:city_id;type:int(11);comment:城市id" json:"city_id"`
City string `gorm:"column:city;type:varchar(50);comment:城市" json:"city"`
CountyId int `gorm:"column:county_id;type:int(11);comment:区县id" json:"county_id"`
County string `gorm:"column:county;type:varchar(50);comment:区县" json:"county"`
Address string `gorm:"column:address;type:varchar(255);comment:地址" json:"address"`
Lat string `gorm:"column:lat;type:varchar(255);comment:纬度" json:"lat"`
Lng string `gorm:"column:lng;type:varchar(255);comment:经度" json:"lng"`
Desc string `gorm:"column:desc;type:varchar(255);comment:简介" json:"desc"`
Model
}
func (m *BaseHospital) TableName() string {
return "base_hospital"
}
func (m *BaseHospital) BeforeCreate(tx *gorm.DB) error {
if m.HospitalId == 0 {
m.HospitalId = 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
}
+33
View File
@@ -0,0 +1,33 @@
package model
import (
"gorm.io/gorm"
"time"
"vote-admin-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;default:0;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
}
+33
View File
@@ -0,0 +1,33 @@
package model
import (
"gorm.io/gorm"
"time"
"vote-admin-api/global"
)
// SystemTime 配置-时间
type SystemTime struct {
SystemTimeId int64 `gorm:"column:system_time_id;type:bigint(19);primary_key;comment:主键id" json:"system_time_id"`
StartTime *LocalTime `gorm:"column:start_time;type:datetime;comment:开始投票时间;NOT NULL" json:"start_time"`
EndTime *LocalTime `gorm:"column:end_time;type:datetime;comment:结束投票时间;NOT NULL" json:"end_time"`
Model
}
func (m *SystemTime) TableName() string {
return "system_time"
}
func (m *SystemTime) BeforeCreate(tx *gorm.DB) error {
if m.SystemTimeId == 0 {
m.SystemTimeId = 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
}
+36
View File
@@ -0,0 +1,36 @@
package model
import (
"gorm.io/gorm"
"time"
"vote-admin-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
}
+36
View File
@@ -0,0 +1,36 @@
package model
import (
"gorm.io/gorm"
"time"
"vote-admin-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"`
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
VideoAuthor []*VideoAuthor `gorm:"foreignKey:VideoId;references:video_id" json:"video_author"`
}
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
}
+35
View File
@@ -0,0 +1,35 @@
package model
import (
"gorm.io/gorm"
"time"
"vote-admin-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
BaseHospital *BaseHospital `gorm:"foreignKey:HospitalId;references:hospital_id" json:"base_hospital"`
}
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
}
+34
View File
@@ -0,0 +1,34 @@
package model
import (
"gorm.io/gorm"
"time"
"vote-admin-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
}
+87
View File
@@ -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)
}
}