You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
128 lines
4.1 KiB
128 lines
4.1 KiB
package app
|
|
|
|
import (
|
|
"gorm.io/driver/sqlite"
|
|
"gorm.io/gorm"
|
|
"math"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
// DB 用户数据操作
|
|
var DB *gorm.DB
|
|
|
|
func ConfigGormDB() {
|
|
var err error
|
|
DB, err = gorm.Open(sqlite.Open("gorm.db"), &gorm.Config{})
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
if err = DB.AutoMigrate(&User{}, &UserToken{}, &Goods{}, &Price{}); err != nil {
|
|
panic(err)
|
|
}
|
|
hash, err := HashPassword("123456")
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
admin := User{
|
|
ID: 1,
|
|
Name: "超级管理员",
|
|
PhoneNumber: "18982052224",
|
|
Password: hash,
|
|
Admin: true,
|
|
}
|
|
err = DB.Save(&admin).Error
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
}
|
|
|
|
// User 用户
|
|
type User struct {
|
|
ID uint `json:"id" gorm:"primarykey"`
|
|
Name string `json:"name"` // 用户名称
|
|
PhoneNumber string `json:"phone_number"` // 用户手机
|
|
Password string `json:"-"` // 登录密码
|
|
Admin bool `json:"admin"` // 是不是管理员
|
|
CreatedAt time.Time `json:"-"`
|
|
UpdatedAt time.Time `json:"-"`
|
|
DeletedAt gorm.DeletedAt `json:"-" gorm:"index"`
|
|
}
|
|
|
|
// UserToken 用户令牌
|
|
type UserToken struct {
|
|
Code string `json:"code" gorm:"primarykey"` // 主键
|
|
UserID uint `json:"user_id"` // 用户ID
|
|
User *User `json:"user,omitempty"` // 关联用户
|
|
AccessToken string `json:"access_token"` // 授权令牌
|
|
RefreshToken string `json:"refresh_token"` // 刷新令牌
|
|
CreatedAt time.Time `json:"-"` // 创建时间
|
|
}
|
|
|
|
// Goods 商品
|
|
type Goods struct {
|
|
ID uint `json:"id" gorm:"primarykey"`
|
|
Name string `json:"name"` // 商品名称
|
|
Price float32 `json:"price"` // 商品当前价格
|
|
Prices []Price `json:"prices,omitempty"` // 商品价格列表
|
|
CreatedBy uint `json:"created_by"` // 商品创建者ID
|
|
Creator *User `json:"creator,omitempty" gorm:"foreignKey:CreatedBy"` // 商品创建者
|
|
UpdatedBy uint `json:"updated_by"` // 商品更新者ID
|
|
Updater *User `json:"updater,omitempty" gorm:"foreignKey:UpdatedBy"` // 商品更新者
|
|
CreatedAt time.Time `json:"-"`
|
|
UpdatedAt time.Time `json:"-"`
|
|
DeletedAt gorm.DeletedAt `json:"-" gorm:"index"`
|
|
}
|
|
|
|
// Price 商品价格
|
|
type Price struct {
|
|
ID uint `json:"id" gorm:"primarykey"`
|
|
GoodsID uint `json:"goods_id"` // 管理商品
|
|
Price float32 `json:"price"` // 商品价格
|
|
CreatedBy uint `json:"created_by"` // 价格创建者ID
|
|
Creator *User `json:"creator,omitempty" gorm:"foreignKey:CreatedBy"` // 价格创建者
|
|
CreatedAt time.Time `json:"-"`
|
|
DeletedAt gorm.DeletedAt `json:"-" gorm:"index"`
|
|
}
|
|
|
|
// Paginate 分页查询作用域
|
|
func Paginate(r *Request) func(db *gorm.DB) *gorm.DB {
|
|
return func(db *gorm.DB) *gorm.DB {
|
|
page := r.Int("page", 1, func(p int) int {
|
|
return int(math.Max(float64(p), 1))
|
|
})
|
|
perPage := r.Int("per_page", 30, func(i int) int {
|
|
return int(math.Max(float64(i), 1))
|
|
})
|
|
offset := (page - 1) * perPage
|
|
return db.Offset(offset).Limit(perPage)
|
|
}
|
|
}
|
|
|
|
// Search 搜索作用域
|
|
func Search(r *Request, key, query string) func(db *gorm.DB) *gorm.DB {
|
|
return func(db *gorm.DB) *gorm.DB {
|
|
if keyword, ok := r.Get(key); ok {
|
|
return db.Where(query, "%"+keyword+"%")
|
|
} else {
|
|
return db
|
|
}
|
|
}
|
|
}
|
|
|
|
// TimeRange 时间范围作用域
|
|
func TimeRange(r *Request, column string) func(db *gorm.DB) *gorm.DB {
|
|
return func(db *gorm.DB) *gorm.DB {
|
|
var queries []string
|
|
var args []any
|
|
if val, ok := r.Get("start_time"); ok {
|
|
queries = append(queries, column+" >= ?")
|
|
args = append(args, val)
|
|
}
|
|
if val, ok := r.Get("end_time"); ok {
|
|
queries = append(queries, column+" <= ?")
|
|
args = append(args, val)
|
|
}
|
|
return db.Where(strings.Join(queries, " AND "), args...)
|
|
}
|
|
}
|
|
|