iuqt init
This commit is contained in:
7
acquaintances/biz/dal/init.go
Normal file
7
acquaintances/biz/dal/init.go
Normal file
@@ -0,0 +1,7 @@
|
||||
package dal
|
||||
|
||||
import "acquaintances/biz/dal/mysql"
|
||||
|
||||
func Init() {
|
||||
mysql.Init()
|
||||
}
|
28
acquaintances/biz/dal/mysql/area.go
Normal file
28
acquaintances/biz/dal/mysql/area.go
Normal file
@@ -0,0 +1,28 @@
|
||||
package mysql
|
||||
|
||||
import (
|
||||
"acquaintances/biz/model"
|
||||
"acquaintances/biz/model/area"
|
||||
)
|
||||
|
||||
func QueryArea(keyword *string, page, pageSize int64) ([]*area.Area, int64, error) {
|
||||
db := DB.Model(model.Country{})
|
||||
if keyword != nil && len(*keyword) != 0 {
|
||||
db = db.Where(DB.Or("chinese_name like ?", "%"+*keyword+"%").
|
||||
Or("english_name like ?", "%"+*keyword+"%").
|
||||
Or("iso2 like ?", "%"+*keyword+"%").
|
||||
Or("iso3 like ?", "%"+*keyword+"%").
|
||||
Or("num_code like ?", "%"+*keyword+"%").
|
||||
Or("phone_code like ?", "%"+*keyword+"%").
|
||||
Or("domain_suffix like ?", "%"+*keyword+"%"))
|
||||
}
|
||||
var total int64
|
||||
if err := db.Count(&total).Error; err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
var res []*area.Area
|
||||
if err := db.Limit(int(pageSize)).Offset(int(pageSize * (page - 1))).Find(&res).Error; err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
return res, total, nil
|
||||
}
|
53
acquaintances/biz/dal/mysql/init.go
Normal file
53
acquaintances/biz/dal/mysql/init.go
Normal file
@@ -0,0 +1,53 @@
|
||||
package mysql
|
||||
|
||||
import (
|
||||
"acquaintances/biz/model"
|
||||
"acquaintances/config"
|
||||
"fmt"
|
||||
"github.com/cloudwego/hertz/pkg/common/hlog"
|
||||
"gorm.io/driver/mysql"
|
||||
"gorm.io/gorm"
|
||||
"gorm.io/gorm/logger"
|
||||
"time"
|
||||
)
|
||||
|
||||
var dsn = fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8mb4&parseTime=True&loc=Local",
|
||||
config.DbUser,
|
||||
config.DbPassWord,
|
||||
config.DbHost,
|
||||
config.DbPort,
|
||||
config.DbName,
|
||||
)
|
||||
|
||||
var DB *gorm.DB
|
||||
|
||||
func Init() {
|
||||
var err error
|
||||
DB, err = gorm.Open(mysql.Open(dsn), &gorm.Config{
|
||||
SkipDefaultTransaction: true,
|
||||
PrepareStmt: true,
|
||||
Logger: logger.Default.LogMode(logger.Info),
|
||||
})
|
||||
if err != nil {
|
||||
hlog.Error(err.Error())
|
||||
panic(err)
|
||||
}
|
||||
// 自动迁移
|
||||
err = DB.AutoMigrate(model.User{}, model.Country{})
|
||||
if err != nil {
|
||||
hlog.Error("AutoMigrate failed: " + err.Error())
|
||||
return
|
||||
}
|
||||
// 连接池配置
|
||||
sqlDB, _ := DB.DB()
|
||||
sqlDB.SetMaxIdleConns(10)
|
||||
sqlDB.SetMaxOpenConns(100)
|
||||
sqlDB.SetConnMaxLifetime(time.Hour) // 建议设置为 1h 或更低
|
||||
|
||||
//内部数据初始化
|
||||
err = model.InitCountriesTable(DB)
|
||||
if err != nil {
|
||||
hlog.Error("Init Countries Table failed: " + err.Error())
|
||||
return
|
||||
}
|
||||
}
|
34
acquaintances/biz/dal/mysql/user.go
Normal file
34
acquaintances/biz/dal/mysql/user.go
Normal file
@@ -0,0 +1,34 @@
|
||||
package mysql
|
||||
|
||||
import (
|
||||
"acquaintances/biz/model"
|
||||
"acquaintances/biz/model/user"
|
||||
)
|
||||
|
||||
func CreateUser(users []*model.User) error {
|
||||
return DB.Create(users).Error
|
||||
}
|
||||
|
||||
func DeleteUser(userId int64) error {
|
||||
return DB.Where("id = ?", userId).Delete(&user.User{}).Error
|
||||
}
|
||||
|
||||
func UpdateUser(user *user.User) error {
|
||||
return DB.Updates(user).Error
|
||||
}
|
||||
|
||||
func QueryUser(keyword *string, page, pageSize int64) ([]*user.User, int64, error) {
|
||||
db := DB.Model(user.User{})
|
||||
if keyword != nil && len(*keyword) != 0 {
|
||||
db = db.Where(DB.Or("name like ?", "%"+*keyword+"%"))
|
||||
}
|
||||
var total int64
|
||||
if err := db.Count(&total).Error; err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
var res []*user.User
|
||||
if err := db.Limit(int(pageSize)).Offset(int(pageSize * (page - 1))).Find(&res).Error; err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
return res, total, nil
|
||||
}
|
Reference in New Issue
Block a user