zhangqian
2023-08-29 d3a78e4c791821b298075b700e9fc14030d12ec2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package system
 
import (
    "go.uber.org/zap"
    "srm/global"
    "srm/model/common/response"
    "srm/model/system/request"
 
    "github.com/gin-gonic/gin"
)
 
type DBApi struct{}
 
// InitDB
// @Tags     InitDB
// @Summary  初始化用户数据库
// @Produce  application/json
// @Param    data  body      request.InitDB                  true  "初始化数据库参数"
// @Success  200   {object}  response.Response{data=string}  "初始化用户数据库"
// @Router   /init/initdb [post]
func (i *DBApi) InitDB(c *gin.Context) {
    if global.GVA_DB != nil {
        global.GVA_LOG.Error("已存在数据库配置!")
        response.FailWithMessage("已存在数据库配置", c)
        return
    }
    var dbInfo request.InitDB
    if err := c.ShouldBindJSON(&dbInfo); err != nil {
        global.GVA_LOG.Error("参数校验不通过!", zap.Error(err))
        response.FailWithMessage("参数校验不通过", c)
        return
    }
    if err := initDBService.InitDB(dbInfo); err != nil {
        global.GVA_LOG.Error("自动创建数据库失败!", zap.Error(err))
        response.FailWithMessage("自动创建数据库失败,请查看后台日志,检查后在进行初始化", c)
        return
    }
    response.OkWithMessage("自动创建数据库成功", c)
}
 
// CheckDB
// @Tags     CheckDB
// @Summary  初始化用户数据库
// @Produce  application/json
// @Success  200  {object}  response.Response{data=map[string]interface{},msg=string}  "初始化用户数据库"
// @Router   /init/checkdb [post]
func (i *DBApi) CheckDB(c *gin.Context) {
    var (
        message  = "前往初始化数据库"
        needInit = true
    )
 
    if global.GVA_DB != nil {
        message = "数据库无需初始化"
        needInit = false
    }
    global.GVA_LOG.Info(message)
    response.OkWithDetailed(gin.H{"needInit": needInit}, message, c)
}