This commit is contained in:
2024-06-19 14:30:11 +08:00
parent 9a9f3b71ff
commit 737c0eb28f
64 changed files with 3925 additions and 1 deletions
+42
View File
@@ -0,0 +1,42 @@
package model
import (
"gorm.io/gorm"
"knowledge/global"
"time"
)
// AdminUser 后台-用户表
type AdminUser struct {
UserId int64 `gorm:"column:user_id;type:bigint(20);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 "kb_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
}
+34
View File
@@ -0,0 +1,34 @@
package model
import (
"gorm.io/gorm"
"knowledge/global"
"time"
)
// Label 标签表
type Label struct {
LabelId int64 `gorm:"column:label_id;type:bigint(19);primary_key;comment:主键id" json:"label_id"`
LabelName string `gorm:"column:label_name;type:varchar(255);comment:标签名称" json:"label_name"`
ParentId int64 `gorm:"column:parent_id;type:bigint(19);comment:父级ID0表示一级)" json:"parent_id"`
LabelLevel int `gorm:"column:label_level;type:tinyint(1);default:1;comment:级别(1:1级 2:2级 3:3级)" json:"label_level"`
Model
}
func (m *Label) TableName() string {
return "kb_label"
}
func (m *Label) BeforeCreate(tx *gorm.DB) error {
if m.LabelId == 0 {
m.LabelId = 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
}
+44
View File
@@ -0,0 +1,44 @@
package model
import (
"gorm.io/gorm"
"knowledge/global"
"time"
)
// Question 题目表(单选-多选-问答-判断)
type Question struct {
QuestionId int64 `gorm:"column:question_id;type:bigint(19);primary_key;comment:主键id" json:"question_id"`
QuestionName string `gorm:"column:question_name;type:varchar(1000);comment:题目名称;NOT NULL" json:"question_name"`
QuestionType int `gorm:"column:question_type;type:tinyint(1);default:1;comment:题目类型(1:单选 2:多选 3:问答 4:判断);NOT NULL" json:"question_type"`
QuestionStatus int `gorm:"column:question_status;type:tinyint(1);default:1;comment:状态(1:正常 2:禁用)" json:"question_status"`
IsDelete int `gorm:"column:is_delete;type:tinyint(1);default:0;comment:是否删除(0:否 1:是)" json:"is_delete"`
QuestionSource int `gorm:"column:question_source;type:tinyint(1);comment:题目来源(1:本题库 2:外部数据);NOT NULL" json:"question_source"`
QuestionImage string `gorm:"column:question_image;type:varchar(500);comment:题目图片(逗号分隔)" json:"question_image"`
QuestionAnswer string `gorm:"column:question_answer;type:varchar(255);comment:答案" json:"question_answer"`
QuestionAnalysis string `gorm:"column:question_analysis;type:text;comment:解析" json:"question_analysis"`
Difficulty int `gorm:"column:difficulty;type:tinyint(1);default:0;comment:难度(0:未知 1:低 2:中 3:高)" json:"difficulty"`
FirstLabelId *int64 `gorm:"column:first_label_id;type:bigint(19);comment:一级标签id" json:"first_label_id"`
SecondLabelId *int64 `gorm:"column:second_label_id;type:bigint(19);comment:二级标签id" json:"second_label_id"`
Model
FirstLabel *Label `gorm:"foreignKey:FirstLabelId;references:label_id" json:"first_label"`
SecondLabel *Label `gorm:"foreignKey:SecondLabelId;references:label_id" json:"second_label"`
}
func (m *Question) TableName() string {
return "kb_question"
}
func (m *Question) BeforeCreate(tx *gorm.DB) error {
if m.QuestionId == 0 {
m.QuestionId = 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"
"knowledge/global"
"time"
)
// QuestionOption 题目选项表
type QuestionOption struct {
OptionId int64 `gorm:"column:option_id;type:bigint(19);primary_key;comment:主键id" json:"option_id"`
QuestionId int64 `gorm:"column:question_id;type:bigint(19);comment:题目id" json:"question_id"`
OptionValue string `gorm:"column:option_value;type:varchar(255);comment:选项内容" json:"option_value"`
Model
}
func (m *QuestionOption) TableName() string {
return "kb_question_option"
}
func (m *QuestionOption) BeforeCreate(tx *gorm.DB) error {
if m.OptionId == 0 {
m.OptionId = 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
}
+29
View File
@@ -0,0 +1,29 @@
package model
import (
"gorm.io/gorm"
"time"
)
type Testpaper12 struct {
Id uint64 `gorm:"column:id;type:bigint(20) unsigned;primary_key;AUTO_INCREMENT" json:"id"`
StemType string `gorm:"column:stem_type;type:varchar(1000);comment:题干名称" json:"stem_type"`
Name string `gorm:"column:name;type:varchar(1000);comment:题目名称" json:"name"`
Item string `gorm:"column:item;type:varchar(1000);comment:选项" json:"item"`
Answer string `gorm:"column:answer;type:varchar(1000);comment:答案" json:"answer"`
Explain string `gorm:"column:explain;type:varchar(2000);comment:解析" json:"explain"`
QuestionType uint `gorm:"column:question_type;type:tinyint(4) unsigned;comment:题目类型" json:"question_type"`
Level uint `gorm:"column:level;type:tinyint(4) unsigned;comment:难易程度" json:"level"`
KnowledgeType uint `gorm:"column:knowledge_type;type:tinyint(4) unsigned;comment:知识题库分类" json:"knowledge_type"`
PaperUuid string `gorm:"column:paper_uuid;type:varchar(50);comment:所属paper;NOT NULL" json:"paper_uuid"`
CreateDate time.Time `gorm:"column:create_date;type:datetime" json:"create_date"`
IsMigrate uint `gorm:"column:is_migrate;type:tinyint(4) unsigned;comment:是否迁移" json:"is_migrate"`
}
func (m *Testpaper12) TableName() string {
return "tb_testpaper12"
}
func (m *Testpaper12) BeforeCreate(tx *gorm.DB) error {
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)
}
}