create.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. // Copyright 2019 getensh.com. All rights reserved.
  2. // Use of this source code is governed by getensh.com.
  3. package organization
  4. import (
  5. "context"
  6. "cp-system-management/errors"
  7. dbmodel "cp-system-management/model"
  8. "cp-system-management/pb"
  9. pb_v1 "cp-system-management/pb/v1"
  10. "cp-system-management/utils"
  11. "encoding/base64"
  12. "encoding/json"
  13. "fmt"
  14. "strings"
  15. "time"
  16. "github.com/jaryhe/gopkgs/cache"
  17. "github.com/jaryhe/gopkgs/database"
  18. "github.com/jaryhe/gopkgs/util"
  19. "github.com/jaryhe/gopkgs/logger"
  20. "go.uber.org/zap"
  21. "google.golang.org/grpc/status"
  22. )
  23. const CRYPTO_KEY = "2D29BP43U3Y1B4N6REQW3F319DD23455"
  24. func MakeOrganizationKey(code string) (string) {
  25. cryPasswd, _ := util.AesEncrypt(fmt.Sprintf("%s%s", code, utils.GenerateRandomStr(6, "mix")), CRYPTO_KEY)
  26. ret := base64.StdEncoding.EncodeToString(cryPasswd)
  27. ret = strings.TrimRight(ret, "==")
  28. return ret
  29. }
  30. func createDb(ctx context.Context, organizationCode string) error {
  31. // 创建设备数据schema
  32. /*
  33. deviceInitReq := &pb_v1.InitDeviceDbRequest{OrganizationCode: organizationCode}
  34. _, err := pb.Device.InitDeviceDb(ctx, deviceInitReq)
  35. if err != nil {
  36. return err
  37. }
  38. */
  39. // 创建组织管理库
  40. organizationInitReq := &pb_v1.InitOrganizationDbRequest{OrganizationCode: organizationCode}
  41. _, err := pb.Organization.InitOrganizationDb(ctx, organizationInitReq)
  42. if err != nil {
  43. // TODO删除设备数据schema
  44. return err
  45. }
  46. return nil
  47. }
  48. // 创建机构
  49. func CreateOrganization(ctx context.Context, req *pb_v1.CreateOrganizationRequest) (reply *pb_v1.CreateOrganizationReply, err error) {
  50. reply = &pb_v1.CreateOrganizationReply{}
  51. // 捕获各个task中的异常并返回给调用者
  52. defer func() {
  53. if r := recover(); r != nil {
  54. err = fmt.Errorf("%+v", r)
  55. e := &status.Status{}
  56. if er := json.Unmarshal([]byte(err.Error()), e); er != nil {
  57. logger.Error("err",
  58. zap.String("system_err", err.Error()),
  59. zap.Stack("stacktrace"))
  60. }
  61. }
  62. }()
  63. if req.OrganizationName == "" {
  64. return reply, status.Error(10003, "参数错误,机构名为空")
  65. }
  66. if req.Month > 2400 || req.Month < -2400{
  67. return reply, status.Error(10003, "参数错误,有效期增减范围过大")
  68. }
  69. // 分布式锁
  70. dLock := &cache.Lock{Key: "create_organization", Ttl: 60}
  71. if !dLock.RedisLock() {
  72. return nil, status.Error(10001, "系统繁忙")
  73. }
  74. defer dLock.RedisUnlock()
  75. p := &dbmodel.Organization{}
  76. db := database.DB().Begin()
  77. // 校验机构是否存在
  78. where := map[string]interface{}{"organization_name": req.OrganizationName}
  79. err = p.Find(db, where)
  80. if err == nil {
  81. return reply, errors.OrganizationExist
  82. }
  83. // 获取最后一条数据
  84. p.Last(db)
  85. // 生成机构编码
  86. p.OrganizationCode = util.GenerateCode(p.OrganizationCode, "", 3)
  87. p.OrganizationName = req.OrganizationName
  88. p.Id = 0
  89. now := time.Now()
  90. if req.Month > 0 {
  91. p.EndTime = util.AddDate(now, 0, int(req.Month), 0).Unix()
  92. p.StartTime = now.Unix()
  93. } else {
  94. p.EndTime = 0
  95. p.StartTime = 0
  96. p.IsExpire = true
  97. }
  98. p.IsDisable = req.IsDisable
  99. p.Key = MakeOrganizationKey(p.OrganizationCode)
  100. if p.IsDisable {
  101. cache.Redis().Set("organization_active"+p.OrganizationCode, "0")
  102. } else {
  103. cache.Redis().Set("organization_active"+p.OrganizationCode, "1")
  104. }
  105. cache.Redis().Set("organization_end_time"+p.OrganizationCode, fmt.Sprintf("%d", p.EndTime))
  106. // 插入机构数据
  107. err = p.Insert(db)
  108. if err != nil {
  109. return reply, errors.DataBaseError
  110. }
  111. // 创建schema,失败回滚
  112. err = createDb(ctx, p.OrganizationCode)
  113. if err != nil {
  114. db.Rollback()
  115. } else {
  116. db.Commit()
  117. }
  118. reply.OrganizationCode = p.OrganizationCode
  119. return reply, err
  120. }