resource.go 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. package utility
  2. import (
  3. "bp.jydev.jianyu360.cn/BaseService/resourceCenter/rpc/pb"
  4. "bp.jydev.jianyu360.cn/BaseService/resourceCenter/rpc/resource"
  5. "fmt"
  6. "github.com/gogf/gf/v2/frame/g"
  7. "github.com/gogf/gf/v2/net/gtrace"
  8. "github.com/gogf/gf/v2/os/gctx"
  9. "github.com/zeromicro/go-zero/core/discov"
  10. "github.com/zeromicro/go-zero/zrpc"
  11. )
  12. var ResourceCenterRpc resource.Resource
  13. func init() {
  14. ctx := gctx.New()
  15. ResourceCenterRpc = resource.NewResource(zrpc.MustNewClient(zrpc.RpcClientConf{
  16. Etcd: discov.EtcdConf{
  17. Key: g.Cfg().MustGet(ctx, "etcd.resourceCenter.key", nil).String(),
  18. Hosts: g.Cfg().MustGet(ctx, "etcd.resourceCenter.address", nil).Strings(),
  19. },
  20. }))
  21. }
  22. // ResourcePowerDeduct 资源中心消费
  23. // accountId 账户id
  24. // entAccountId 企业账户id
  25. // funcCode 业务代码
  26. // deductNum 扣除数量
  27. // ids 扣除详情id;例如数据导出信息id等数据
  28. func ResourcePowerDeduct(ctx g.Ctx, accountId, entAccountId int64, id string) error {
  29. ctx, span := gtrace.NewSpan(ctx, "ResourcePowerDeduct")
  30. defer span.End()
  31. res, err := ResourceCenterRpc.Deduction(ctx, &pb.DeductionReq{
  32. Appid: g.Config().MustGet(ctx, "chat.appId").String(),
  33. AccountId: accountId,
  34. EntAccountId: entAccountId,
  35. FunctionCode: g.Config().MustGet(ctx, "chat.resourceCode").String(),
  36. Count: 1,
  37. Ids: []string{id},
  38. })
  39. if err != nil {
  40. return fmt.Errorf("资源异常")
  41. }
  42. //0:失败 1:成功 -1:不在有效期内 -2:数量不足 -3:没有授权
  43. switch res.Status {
  44. case 0:
  45. return fmt.Errorf("账户 accountId:%d entAccountId:%d 扣除业务失败 聊天记录:%s权限", accountId, entAccountId, id)
  46. case -1:
  47. return fmt.Errorf("账户 accountId:%d entAccountId:%d 扣除业务权限失败,权限已过期 聊天记录:%s权限", accountId, entAccountId, id)
  48. case -2:
  49. return fmt.Errorf("账户 accountId:%d entAccountId:%d 扣除业务权限失败,余额不足 聊天记录:%s权限", accountId, entAccountId, id)
  50. case -3:
  51. return fmt.Errorf("账户 accountId:%d entAccountId:%d 扣除业务权限失败,没有授权 聊天记录:%s权限", accountId, entAccountId, id)
  52. }
  53. return nil
  54. }