init_origanization_db.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. // Copyright 2019 getensh.com. All rights reserved.
  2. // Use of this source code is governed by getensh.com.
  3. package db
  4. import (
  5. "context"
  6. "cp-organization-management/errors"
  7. "cp-organization-management/model"
  8. pb_v1 "cp-organization-management/pb/v1"
  9. "cp-organization-management/utils"
  10. "encoding/json"
  11. "fmt"
  12. "github.com/jinzhu/gorm"
  13. "github.com/jaryhe/gopkgs/database"
  14. "github.com/jaryhe/gopkgs/logger"
  15. "go.uber.org/zap"
  16. "google.golang.org/grpc/status"
  17. )
  18. func initTable(db *gorm.DB, dbname string) error {
  19. err := model.NewDevice(dbname).CreateTable(db)
  20. if err != nil {
  21. return errors.DataBaseError
  22. }
  23. err = model.NewRbacGroup(dbname).CreateTable(db)
  24. if err != nil {
  25. return errors.DataBaseError
  26. }
  27. err = model.NewRbacUser(dbname).CreateTable(db)
  28. if err != nil {
  29. return errors.DataBaseError
  30. }
  31. err = model.NewRbacNode(dbname).CreateTable(db)
  32. if err != nil {
  33. return errors.DataBaseError
  34. }
  35. err = model.NewStaff(dbname).CreateTable(db)
  36. if err != nil {
  37. return errors.DataBaseError
  38. }
  39. err = model.NewStaffType(dbname).CreateTable(db)
  40. if err != nil {
  41. return errors.DataBaseError
  42. }
  43. err = model.NewStation(dbname).CreateTable(db)
  44. if err != nil {
  45. return errors.DataBaseError
  46. }
  47. err = model.NewUserZone(dbname).CreateTable(db)
  48. if err != nil {
  49. return errors.DataBaseError
  50. }
  51. err = model.NewTLog(dbname).CreateTable(db)
  52. if err != nil {
  53. return errors.DataBaseError
  54. }
  55. err = model.NewZone(dbname).CreateTable(db)
  56. if err != nil {
  57. return errors.DataBaseError
  58. }
  59. zoneIndexSql := fmt.Sprintf("ALTER TABLE `%s`.`t_zone` ADD UNIQUE INDEX `code_index`(`zone_code`) USING BTREE", dbname)
  60. err = db.Exec(zoneIndexSql).Error
  61. if err != nil {
  62. return errors.DataBaseError
  63. }
  64. p := model.NewZone(dbname)
  65. p.ZoneCode = "001"
  66. p.ParentZoneCode = "000"
  67. p.ZoneName = "全部区域"
  68. err = p.Insert(database.DB())
  69. if err != nil {
  70. return errors.DataBaseError
  71. }
  72. return nil
  73. }
  74. func initDb(ctx context.Context, organizationCode string) error {
  75. dbname := utils.GetDbName(organizationCode)
  76. // create database
  77. createDbSql := fmt.Sprintf("CREATE DATABASE IF NOT EXISTS %s DEFAULT CHARSET utf8 COLLATE utf8_general_ci;", dbname)
  78. db := database.DB()
  79. err := db.Exec(createDbSql).Error
  80. if err != nil {
  81. return errors.DataBaseError
  82. }
  83. err = initTable(db, dbname)
  84. if err != nil {
  85. return errors.DataBaseError
  86. }
  87. createViewSql := fmt.Sprintf("CREATE VIEW `%s`.`view_user` AS select t1.*, t2.zone_code, t3.zone_name, t3.level as zone_level from %s.t_rbac_user as t1 left join %s.t_user_zone as t2 on t1.id=t2.user_id left join %s.t_zone as t3 on t2.zone_code=t3.zone_code;", dbname, dbname, dbname, dbname)
  88. err = db.Exec(createViewSql).Error
  89. if err != nil {
  90. return errors.DataBaseError
  91. }
  92. return nil
  93. }
  94. // 初始化机构schema
  95. func InitOrganizationDb(ctx context.Context, req *pb_v1.InitOrganizationDbRequest) (reply *pb_v1.InitOrganizationDbReply, err error) {
  96. reply = &pb_v1.InitOrganizationDbReply{}
  97. // 捕获各个task中的异常并返回给调用者
  98. defer func() {
  99. if r := recover(); r != nil {
  100. err = fmt.Errorf("%+v", r)
  101. e := &status.Status{}
  102. if er := json.Unmarshal([]byte(err.Error()), e); er != nil {
  103. logger.Error("err",
  104. zap.String("system_err", err.Error()),
  105. zap.Stack("stacktrace"))
  106. }
  107. }
  108. }()
  109. if req.OrganizationCode == "" {
  110. return reply, status.Error(10003, "参数错误,机构编码为空")
  111. }
  112. err = initDb(ctx, req.OrganizationCode)
  113. return reply, err
  114. }