package utility import ( "bp.jydev.jianyu360.cn/BaseService/resourceCenter/rpc/pb" "bp.jydev.jianyu360.cn/BaseService/resourceCenter/rpc/resource" "fmt" "github.com/gogf/gf/v2/frame/g" "github.com/gogf/gf/v2/net/gtrace" "github.com/gogf/gf/v2/os/gctx" "github.com/zeromicro/go-zero/core/discov" "github.com/zeromicro/go-zero/zrpc" ) var ResourceCenterRpc resource.Resource func init() { ctx := gctx.New() ResourceCenterRpc = resource.NewResource(zrpc.MustNewClient(zrpc.RpcClientConf{ Etcd: discov.EtcdConf{ Key: g.Cfg().MustGet(ctx, "etcd.resourceCenter.key", nil).String(), Hosts: g.Cfg().MustGet(ctx, "etcd.resourceCenter.address", nil).Strings(), }, })) } // ResourcePowerDeduct 资源中心消费 // accountId 账户id // entAccountId 企业账户id // funcCode 业务代码 // deductNum 扣除数量 // ids 扣除详情id;例如数据导出信息id等数据 func ResourcePowerDeduct(ctx g.Ctx, accountId, entAccountId int64, id string) error { ctx, span := gtrace.NewSpan(ctx, "ResourcePowerDeduct") defer span.End() res, err := ResourceCenterRpc.Deduction(ctx, &pb.DeductionReq{ Appid: g.Config().MustGet(ctx, "chat.appId").String(), AccountId: accountId, EntAccountId: entAccountId, FunctionCode: g.Config().MustGet(ctx, "chat.resourceCode").String(), Count: 1, Ids: []string{id}, }) if err != nil { return fmt.Errorf("资源异常") } //0:失败 1:成功 -1:不在有效期内 -2:数量不足 -3:没有授权 switch res.Status { case 0: return fmt.Errorf("账户 accountId:%d entAccountId:%d 扣除业务失败 聊天记录:%s权限", accountId, entAccountId, id) case -1: return fmt.Errorf("账户 accountId:%d entAccountId:%d 扣除业务权限失败,权限已过期 聊天记录:%s权限", accountId, entAccountId, id) case -2: return fmt.Errorf("账户 accountId:%d entAccountId:%d 扣除业务权限失败,余额不足 聊天记录:%s权限", accountId, entAccountId, id) case -3: return fmt.Errorf("账户 accountId:%d entAccountId:%d 扣除业务权限失败,没有授权 聊天记录:%s权限", accountId, entAccountId, id) } return nil }