Files

167 lines
5.9 KiB
Go
Raw Normal View History

2021-12-28 09:18:32 +08:00
package migrate
2021-09-28 11:47:19 +08:00
import (
2021-12-01 11:31:55 +08:00
config2 "SciencesServer/app/basic/config"
2021-09-28 11:47:19 +08:00
"SciencesServer/app/common/model"
2021-12-03 15:22:23 +08:00
"SciencesServer/utils"
2022-01-05 16:09:55 +08:00
"fmt"
2021-12-28 09:18:32 +08:00
"gorm.io/gorm"
2021-09-28 11:47:19 +08:00
)
2021-12-28 09:18:32 +08:00
type Instance struct {
gormDB *gorm.DB
2021-09-28 11:47:19 +08:00
}
2021-12-28 09:18:32 +08:00
type Option func(instance *Instance)
type synchronized struct {
2021-09-28 11:47:19 +08:00
iModel model.IModel
iValues func() interface{}
2021-12-28 09:18:32 +08:00
Catch func() interface{}
2021-09-28 11:47:19 +08:00
}
2021-12-28 09:18:32 +08:00
func (this *Instance) Handle() {
fmt.Println("========================\n=== 数据开始迁移 ===\n========================")
2022-01-11 14:54:20 +08:00
2021-12-28 09:18:32 +08:00
db := this.gormDB
2021-09-28 11:47:19 +08:00
2022-01-05 16:09:55 +08:00
successCount, failureCount := 0, 0
2021-09-28 11:47:19 +08:00
function := func(synchronized ...*synchronized) {
for _, v := range synchronized {
if !db.Migrator().HasTable(v.iModel) {
2021-12-03 15:22:23 +08:00
err := db.Migrator().CreateTable(v.iModel)
if err != nil {
2022-01-05 16:09:55 +08:00
failureCount++
2022-01-05 18:40:08 +08:00
} else {
successCount++
2021-12-03 15:22:23 +08:00
}
2021-09-28 11:47:19 +08:00
if v.iValues != nil && v.iValues() != nil {
db.Table(v.iModel.TableName()).Create(v.iValues())
}
} else if v.Catch != nil && v.Catch() != nil {
v.Catch()
}
}
}
function(
2022-01-12 15:05:14 +08:00
&synchronized{iModel: model.NewSysTenant()}, &synchronized{iModel: model.NewSysTenantMenu()}, &synchronized{iModel: model.NewSysTenantAuth()},
2021-09-28 11:47:19 +08:00
&synchronized{iModel: model.NewSysConfig()},
&synchronized{iModel: model.NewSysMenu()}, &synchronized{iModel: model.NewSysAuth()},
&synchronized{iModel: model.NewSysUser(), iValues: func() interface{} {
2022-01-05 18:40:08 +08:00
return &model.SysUser{Account: "admin", Name: "商挈智能", Mobile: "13888888888",
Password: utils.Md5String("123456"),
IsAdmin: model.SysUserAdministratorForAdmin, Remark: "超级管理员"}
2021-09-28 11:47:19 +08:00
}},
2021-12-03 15:22:23 +08:00
&synchronized{iModel: model.NewSysUserRole()}, &synchronized{iModel: model.NewSysUserTenant()},
2021-09-28 11:47:19 +08:00
&synchronized{iModel: model.NewSysDepartment()},
&synchronized{iModel: model.NewSysRole()}, &synchronized{iModel: model.NewSysRoleMenu()}, &synchronized{iModel: model.NewSysRoleAuth()},
2022-01-06 17:11:57 +08:00
&synchronized{iModel: model.NewSysUserDepartment()},
2021-10-27 13:27:01 +08:00
// 配置管理
&synchronized{iModel: model.NewSysConfig(), iValues: func() interface{} {
2021-11-26 17:26:01 +08:00
return nil
}},
2021-12-01 11:31:55 +08:00
&synchronized{iModel: model.NewSysIdentity(), iValues: func() interface{} {
out := make([]*model.SysIdentity, 0)
for k, v := range config2.TenantUserIdentityData {
registerCount := 1
if k == config2.TenantUserIdentityForCompany {
registerCount = -1
}
out = append(out, &model.SysIdentity{
Identity: k,
Name: v,
RegisterCount: registerCount,
IsExamine: 1,
})
}
return out
}},
2021-12-07 16:10:12 +08:00
&synchronized{iModel: model.NewSysIndustry(), iValues: func() interface{} {
file := "./file/sys_industry.json"
src := make([]*config2.MemoryForIndustry, 0)
utils.LoadConfig(file, &src)
out := make([]*model.SysIndustry, 0)
var id uint64 = 1
for _, v := range src {
var parentID uint64 = 0
data := &model.SysIndustry{
Model: model.Model{
ID: id,
},
ParentID: parentID,
Name: v.Name,
}
out = append(out, data)
if v.Children != nil && len(v.Children) > 0 {
parentID = id
for _, val := range v.Children {
id++
data = &model.SysIndustry{
Model: model.Model{
ID: id,
},
ParentID: parentID,
Name: val.Name,
}
out = append(out, data)
}
}
}
return out
}},
2022-01-14 09:56:55 +08:00
&synchronized{iModel: model.NewSysNavigation()}, &synchronized{iModel: model.NewSysAbout()},
2022-01-13 16:50:38 +08:00
&synchronized{iModel: model.NewSysBanner()}, &synchronized{iModel: model.NewSysAgreement()},
2021-09-28 11:47:19 +08:00
// 日志管理
&synchronized{iModel: model.NewSysLog()}, &synchronized{iModel: model.NewSysUserLoginLog()},
2022-01-21 14:16:48 +08:00
&synchronized{iModel: model.NewSysUserExamineLog()},
2021-12-03 10:08:23 +08:00
// 用户管理
&synchronized{iModel: model.NewUserInstance()}, &synchronized{iModel: model.NewUserIdentity()},
&synchronized{iModel: model.NewUserAssets()}, &synchronized{iModel: model.NewUserBank()},
2021-12-03 10:08:23 +08:00
&synchronized{iModel: model.NewUserCompany()}, &synchronized{iModel: model.NewUserExpert()},
&synchronized{iModel: model.NewUserLaboratory()}, &synchronized{iModel: model.NewUserResearch()},
&synchronized{iModel: model.NewUserAgent()},
2022-01-21 14:16:48 +08:00
&synchronized{iModel: model.NewUserVisit()}, &synchronized{iModel: model.NewUserCollect()},
2021-12-08 17:46:47 +08:00
// 数据管理
&synchronized{iModel: model.NewManageCompany()}, &synchronized{iModel: model.NewManageExpert()},
&synchronized{iModel: model.NewManageLaboratory()}, &synchronized{iModel: model.NewManageResearch()},
&synchronized{iModel: model.NewManageAgent()},
2022-01-21 16:52:08 +08:00
&synchronized{iModel: model.NewManageCooperateEnterprise()},
&synchronized{iModel: model.NewTechnologyAchievement()}, &synchronized{iModel: model.NewTechnologyDemand()},
&synchronized{iModel: model.NewTechnologyPaper()}, &synchronized{iModel: model.NewTechnologyProduct()},
&synchronized{iModel: model.NewTechnologyProject()}, &synchronized{iModel: model.NewTechnologyTopic()},
&synchronized{iModel: model.NewTechnologyDemandService()}, &synchronized{iModel: model.NewTechnologyDemandServiceProgress()},
&synchronized{iModel: model.NewTechnologyPatent()}, &synchronized{iModel: model.NewTechnologyPatentClassify()},
2022-01-11 14:54:20 +08:00
&synchronized{iModel: model.NewServiceDocking()},
&synchronized{iModel: model.NewServiceMessage()}, &synchronized{iModel: model.NewServiceMessageLog()},
2022-01-11 10:41:46 +08:00
&synchronized{iModel: model.NewServiceSolutionCase()}, &synchronized{iModel: model.NewServiceSolutionCaseKind()},
2021-12-20 11:17:30 +08:00
&synchronized{iModel: model.NewServiceInnovate()}, &synchronized{iModel: model.NewServiceInnovateKind()},
// 活动管理
&synchronized{iModel: model.NewActivityInstance()}, &synchronized{iModel: model.NewActivityApply()},
2022-01-12 15:05:14 +08:00
&synchronized{iModel: model.NewActivityApplyLog()}, &synchronized{iModel: model.NewActivityJoin()},
2021-09-28 11:47:19 +08:00
)
2022-01-11 14:54:20 +08:00
fmt.Printf("========================\n=== 数据完成迁移,成功【%d】失败【%d】 ===\n========================\n",
successCount, failureCount)
2021-09-28 11:47:19 +08:00
}
2021-12-28 09:18:32 +08:00
func WithGormDBOption(db *gorm.DB) Option {
return func(instance *Instance) {
instance.gormDB = db
2021-09-28 11:47:19 +08:00
}
}
2021-12-28 09:18:32 +08:00
func NewInstance(options ...Option) *Instance {
out := new(Instance)
for _, v := range options {
v(out)
}
return out
2021-09-28 11:47:19 +08:00
}