package moto import ( "context" "git.nobla.cn/golang/moto/common/db" "git.nobla.cn/golang/moto/models" ) var ( defaultUsers = map[string]string{ "1000": "admin", } defaultDepartments = map[string]string{ "admin": "客服部", } defaultRoles = map[string]string{ "admin": "管理员", } ) func migrate(ctx context.Context) (err error) { var ( count int64 deptID string roleID string ) tx := db.DB().WithContext(ctx) tx.Model(&models.Department{}).Count(&count) if count <= 0 { for _, v := range defaultDepartments { deptModel := &models.Department{ Parent: "", Name: v, } if err = tx.Save(deptModel).Error; err == nil { if deptID == "" { deptID = deptModel.ID } } } } tx.Model(&models.Role{}).Count(&count) if count <= 0 { for _, v := range defaultRoles { roleModel := &models.Role{ Name: v, Permissions: "[]", } if err = tx.Save(roleModel).Error; err == nil { if roleID == "" { roleID = roleModel.ID } } } } tx.Model(&models.User{}).Count(&count) if count <= 0 { for k, v := range defaultUsers { tx.Save(&models.User{ UID: k, Department: deptID, Role: roleID, Username: v, Password: "Passwd#" + k, Email: k + "@echo.me", Avatar: "/profile/avatar/" + k, Gender: "man", }) } } return }