123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143 |
- // Copyright 2019 getensh.com. All rights reserved.
- // Use of this source code is governed by getensh.com.
- package organization
- import (
- "context"
- "cp-system-management/errors"
- dbmodel "cp-system-management/model"
- "cp-system-management/pb"
- pb_v1 "cp-system-management/pb/v1"
- "cp-system-management/utils"
- "encoding/base64"
- "encoding/json"
- "fmt"
- "strings"
- "time"
- "github.com/jaryhe/gopkgs/cache"
- "github.com/jaryhe/gopkgs/database"
- "github.com/jaryhe/gopkgs/util"
- "github.com/jaryhe/gopkgs/logger"
- "go.uber.org/zap"
- "google.golang.org/grpc/status"
- )
- const CRYPTO_KEY = "2D29BP43U3Y1B4N6REQW3F319DD23455"
- func MakeOrganizationKey(code string) (string) {
- cryPasswd, _ := util.AesEncrypt(fmt.Sprintf("%s%s", code, utils.GenerateRandomStr(6, "mix")), CRYPTO_KEY)
- ret := base64.StdEncoding.EncodeToString(cryPasswd)
- ret = strings.TrimRight(ret, "==")
- return ret
- }
- func createDb(ctx context.Context, organizationCode string) error {
- // 创建设备数据schema
- /*
- deviceInitReq := &pb_v1.InitDeviceDbRequest{OrganizationCode: organizationCode}
- _, err := pb.Device.InitDeviceDb(ctx, deviceInitReq)
- if err != nil {
- return err
- }
- */
- // 创建组织管理库
- organizationInitReq := &pb_v1.InitOrganizationDbRequest{OrganizationCode: organizationCode}
- _, err := pb.Organization.InitOrganizationDb(ctx, organizationInitReq)
- if err != nil {
- // TODO删除设备数据schema
- return err
- }
- return nil
- }
- // 创建机构
- func CreateOrganization(ctx context.Context, req *pb_v1.CreateOrganizationRequest) (reply *pb_v1.CreateOrganizationReply, err error) {
- reply = &pb_v1.CreateOrganizationReply{}
- // 捕获各个task中的异常并返回给调用者
- defer func() {
- if r := recover(); r != nil {
- err = fmt.Errorf("%+v", r)
- e := &status.Status{}
- if er := json.Unmarshal([]byte(err.Error()), e); er != nil {
- logger.Error("err",
- zap.String("system_err", err.Error()),
- zap.Stack("stacktrace"))
- }
- }
- }()
- if req.OrganizationName == "" {
- return reply, status.Error(10003, "参数错误,机构名为空")
- }
- if req.Month > 2400 || req.Month < -2400{
- return reply, status.Error(10003, "参数错误,有效期增减范围过大")
- }
- // 分布式锁
- dLock := &cache.Lock{Key: "create_organization", Ttl: 60}
- if !dLock.RedisLock() {
- return nil, status.Error(10001, "系统繁忙")
- }
- defer dLock.RedisUnlock()
- p := &dbmodel.Organization{}
- db := database.DB().Begin()
- // 校验机构是否存在
- where := map[string]interface{}{"organization_name": req.OrganizationName}
- err = p.Find(db, where)
- if err == nil {
- return reply, errors.OrganizationExist
- }
- // 获取最后一条数据
- p.Last(db)
- // 生成机构编码
- p.OrganizationCode = util.GenerateCode(p.OrganizationCode, "", 3)
- p.OrganizationName = req.OrganizationName
- p.Id = 0
- now := time.Now()
- if req.Month > 0 {
- p.EndTime = util.AddDate(now, 0, int(req.Month), 0).Unix()
- p.StartTime = now.Unix()
- } else {
- p.EndTime = 0
- p.StartTime = 0
- p.IsExpire = true
- }
- p.IsDisable = req.IsDisable
- p.Key = MakeOrganizationKey(p.OrganizationCode)
- if p.IsDisable {
- cache.Redis().Set("organization_active"+p.OrganizationCode, "0")
- } else {
- cache.Redis().Set("organization_active"+p.OrganizationCode, "1")
- }
- cache.Redis().Set("organization_end_time"+p.OrganizationCode, fmt.Sprintf("%d", p.EndTime))
- // 插入机构数据
- err = p.Insert(db)
- if err != nil {
- return reply, errors.DataBaseError
- }
- // 创建schema,失败回滚
- err = createDb(ctx, p.OrganizationCode)
- if err != nil {
- db.Rollback()
- } else {
- db.Commit()
- }
- reply.OrganizationCode = p.OrganizationCode
- return reply, err
- }
|