refactor: database init module
This commit is contained in:
+14
-4
@@ -7,8 +7,18 @@ import (
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
// 初始化数据
|
||||
func InitData(c *gin.Context) {
|
||||
errCode := service.InitData()
|
||||
response.Result(errCode, nil, c)
|
||||
type CommonApi struct {
|
||||
commonService *service.CommonService
|
||||
}
|
||||
|
||||
func NewCommonApi() *CommonApi {
|
||||
return &CommonApi{
|
||||
commonService: service.NewCommonService(),
|
||||
}
|
||||
}
|
||||
|
||||
// 初始化数据库
|
||||
func (c *CommonApi) InitDatabase(context *gin.Context) {
|
||||
errCode := c.commonService.InitDatabase()
|
||||
response.Result(errCode, nil, context)
|
||||
}
|
||||
@@ -4,6 +4,9 @@ import (
|
||||
"context"
|
||||
"crm/global"
|
||||
"crm/models"
|
||||
"log"
|
||||
"os"
|
||||
"strings"
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -20,6 +23,10 @@ const (
|
||||
// 空值
|
||||
NumberNull = 0
|
||||
StringNull = ""
|
||||
|
||||
// 运行环境
|
||||
Dev = "dev"
|
||||
Prod = "prod"
|
||||
)
|
||||
|
||||
var ctx = context.Background()
|
||||
@@ -38,3 +45,37 @@ func restPage(page models.Page, name string, query interface{}, dest interface{}
|
||||
res := global.Db.Table(name).Where(query).Find(bind)
|
||||
return res.RowsAffected, res.Error
|
||||
}
|
||||
|
||||
type CommonDao struct {
|
||||
}
|
||||
|
||||
func NewCommonDao() *CommonDao {
|
||||
return &CommonDao{}
|
||||
}
|
||||
|
||||
func (c *CommonDao) InitDatabase() error {
|
||||
env := global.Config.Server.Runenv
|
||||
if env == Prod {
|
||||
// fn := "/home/ubuntu/crmapi/crm.sql"
|
||||
// fn := "/Users/xuzxc/Downloads/crm.sql"
|
||||
dbFile := global.Config.Mysql.DbFile
|
||||
sql, err := os.ReadFile(dbFile)
|
||||
if err != nil {
|
||||
log.Printf("Common.InitDatabase.Error: read file %s error: %s", dbFile, err)
|
||||
return err
|
||||
}
|
||||
sqls := strings.Split(string(sql), ";")
|
||||
for _, sql := range sqls {
|
||||
s := strings.TrimSpace(sql)
|
||||
if s == StringNull {
|
||||
continue
|
||||
}
|
||||
if err := global.Db.Exec(s).Error; err != nil {
|
||||
log.Printf("Common.InitDatabase.Error: %s", err)
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -27,8 +27,8 @@ func Router() {
|
||||
route.DELETE("/user/delete", api.NewUserApi().Delete)
|
||||
route.POST("/subscribe/payback", api.NewSubscribeApi().PayBack)
|
||||
|
||||
// 初始化数据
|
||||
route.POST("/init/data", api.InitData)
|
||||
// 通用接口
|
||||
route.POST("/common/database/init", api.NewCommonApi().InitDatabase)
|
||||
|
||||
// Jwt中间件
|
||||
route.Use(middleware.JwtAuth())
|
||||
|
||||
@@ -7,8 +7,7 @@ const (
|
||||
ErrCodeNoLogin = 3 // 未登录或非法访问
|
||||
ErrCodeTokenExpire = 4 // Token过期
|
||||
|
||||
ErrCodeInitDataSuccess = 10 // 初始化数据成功
|
||||
ErrCodeInitDataFailed = 11 // 初始化数据失败
|
||||
ErrCodeInitDataFailed = 10 // 初始化数据失败
|
||||
|
||||
ErrCodeUserHasExist = 10001 // 用户已经存在
|
||||
ErrCodeUserNotExist = 10002 // 用户不存在
|
||||
@@ -40,8 +39,7 @@ var msg = map[int]string{
|
||||
ErrCodeParamInvalid: "param invalid",
|
||||
ErrCodeNoLogin: "no login",
|
||||
ErrCodeTokenExpire: "token expire",
|
||||
ErrCodeInitDataSuccess: "init data success",
|
||||
ErrCodeInitDataFailed: "init data failed",
|
||||
ErrCodeInitDataFailed: "data init failed",
|
||||
ErrCodeUserHasExist: "user has exist",
|
||||
ErrCodeUserNotExist: "user not exist",
|
||||
ErrCOdeUserEmailOrPass: "user email or password error",
|
||||
|
||||
+15
-27
@@ -1,12 +1,8 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"crm/global"
|
||||
"crm/dao"
|
||||
"crm/response"
|
||||
"os"
|
||||
|
||||
"log"
|
||||
"strings"
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -37,28 +33,20 @@ const (
|
||||
SUBSCRIBE_NOTICE_TEMPLATE2 = "你订阅了高级版"
|
||||
)
|
||||
|
||||
// 初始化数据库数据
|
||||
func InitData() int {
|
||||
env := global.Config.Server.Runenv
|
||||
if env == Prod {
|
||||
fn := "/home/ubuntu/crmapi/crm.sql"
|
||||
sql, err := os.ReadFile(fn)
|
||||
if err != nil {
|
||||
log.Printf("[ERROR] read file %s error: %s", fn, err)
|
||||
return response.ErrCodeInitDataFailed
|
||||
}
|
||||
sqls := strings.Split(string(sql), ";")
|
||||
for _, sql := range sqls {
|
||||
s := strings.TrimSpace(sql)
|
||||
if s == StringNull {
|
||||
continue
|
||||
}
|
||||
if err := global.Db.Exec(s).Error; err != nil {
|
||||
log.Printf("[ERROR] datebase flie import error: %s", err)
|
||||
return response.ErrCodeInitDataFailed
|
||||
}
|
||||
}
|
||||
return response.ErrCodeInitDataSuccess
|
||||
type CommonService struct {
|
||||
commonDao *dao.CommonDao
|
||||
}
|
||||
|
||||
func NewCommonService() *CommonService {
|
||||
return &CommonService{
|
||||
commonDao: dao.NewCommonDao(),
|
||||
}
|
||||
}
|
||||
|
||||
// 初始化数据库
|
||||
func (c *CommonService) InitDatabase() int {
|
||||
if err := c.commonDao.InitDatabase(); err != nil {
|
||||
return response.ErrCodeInitDataFailed
|
||||
}
|
||||
return response.ErrCodeSuccess
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user