package admin import ( "context" "gd_admin/apis" "gd_admin/common.in/config" "gd_admin/common.in/jsonrpc2" "gd_admin/common.in/storage" "gd_admin/common.in/utils" "gd_admin/errors" "gd_admin/impl/rbac" "encoding/base64" "encoding/json" "fmt" "github.com/astaxie/beego/orm" "go.uber.org/zap" "time" ) func AdminModify(ctx context.Context, req *apis.AdminModifyReq, reply *apis.AdminModifyReply) error { // 捕获各个task中的异常并返回给调用者 defer func() { if r := recover(); r != nil { err := fmt.Errorf("%+v", r) e := &jsonrpc2.Error{} if er := json.Unmarshal([]byte(err.Error()), e); er != nil { l.Error("err", zap.String("system_err", err.Error()), zap.Stack("stacktrace")) } } }() if req.AdminId == 0 { return errors.ArgsError } updateAdminInfo := func(db orm.Ormer) error { adminInfo := apis.AdminInfo{} err := orm.NewOrm().QueryTable("t_gd_admin").Filter("id", req.AdminId).One(&adminInfo) if err != nil { if err == orm.ErrNoRows { return errors.AdminsNotExist } l.Error("mysql", zap.String("sql", "SELECT * FROM t_gd_admin"), zap.String("fields", utils.MarshalJsonString(req)), zap.String("error", err.Error())) return errors.DataBaseError } if req.AdminPasswd != "" { cryPasswd, err := config.AesEncrypt(req.AdminPasswd, CRYPTO_KEY) if err != nil { return errors.ArgsError } adminInfo.AdminPassword = base64.StdEncoding.EncodeToString(cryPasswd) } adminInfo.AdminPhone = req.AdminPhone adminInfo.AdminEmail = req.AdminEmail adminInfo.UpdatedAt = time.Now().Format("2006-01-02 15:04:05") adminInfo.Status = 0 _, err = orm.NewOrm().Update(&adminInfo) if err != nil { l.Error("mysql", zap.String("sql", "SELECT * FROM t_gd_admin"), zap.String("fields", utils.MarshalJsonString(adminInfo)), zap.String("error", err.Error())) return errors.DataBaseError } return nil } updateGroupInfo := func(db orm.Ormer) error { if req.GroupId == 0 { return nil } //// 查看是否需要新增分组 //groupId, err := rbac.AddCostomGroup(db, req.NodeId) //if err != nil { // return err //} // 修改访问权限分组 err := rbac.UpdateAccess(db, req.AdminId, int64(req.GroupId)) if err != nil { return err } return nil } //处理事务 tasks := []storage.DbaTasker{} tasks = append(tasks, storage.GenerateDbaTask(updateAdminInfo)) tasks = append(tasks, storage.GenerateDbaTask(updateGroupInfo)) storage.ExecTrans(tasks...) return nil } func UpdateStatus(ctx context.Context, req *apis.UpdateStatusReq, reply *apis.UpdateStatusReply) error { // 捕获各个task中的异常并返回给调用者 defer func() { if r := recover(); r != nil { err := fmt.Errorf("%+v", r) e := &jsonrpc2.Error{} if er := json.Unmarshal([]byte(err.Error()), e); er != nil { l.Error("err", zap.String("system_err", err.Error()), zap.Stack("stacktrace")) } } }() if req.AdminId <= 0 { return errors.ArgsError } // 判断用户是否存在 adminInfo := apis.AdminInfo{} err := orm.NewOrm().QueryTable("t_gd_admin").Filter("id", req.AdminId).One(&adminInfo) if err != nil { if err == orm.ErrNoRows { return errors.AdminsNotExist } l.Error("mysql", zap.String("sql", "SELECT * FROM t_gd_admin"), zap.String("fields", utils.MarshalJsonString(req)), zap.String("error", err.Error())) return errors.DataBaseError } // 判断状态 if adminInfo.Status == 0 { reply.Status = 1 } else { reply.Status = 0 } // value value := orm.Params{ "status": reply.Status, "updated_at": time.Now().Format("2006-01-02 15:04:05"), } if _, err := orm.NewOrm().QueryTable("t_gd_admin").Filter("id", req.AdminId).Update(value); err != nil { l.Error("mysql", zap.String("sql", "Update t_gd_admin"), zap.String("fields", utils.MarshalJsonString(req, value)), zap.String("error", err.Error())) return errors.DataBaseError } return nil }