checkpowerlogic.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. package logic
  2. import (
  3. "context"
  4. "fmt"
  5. "time"
  6. . "app.yhyue.com/moapp/jybase/date"
  7. . "bp.jydev.jianyu360.cn/BaseService/resourceCenter/rpc/internal/entity"
  8. "bp.jydev.jianyu360.cn/BaseService/resourceCenter/rpc/internal/svc"
  9. "bp.jydev.jianyu360.cn/BaseService/resourceCenter/rpc/pb"
  10. "github.com/zeromicro/go-zero/core/logx"
  11. )
  12. type CheckPowerLogic struct {
  13. ctx context.Context
  14. svcCtx *svc.ServiceContext
  15. logx.Logger
  16. }
  17. func NewCheckPowerLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CheckPowerLogic {
  18. return &CheckPowerLogic{
  19. ctx: ctx,
  20. svcCtx: svcCtx,
  21. Logger: logx.WithContext(ctx),
  22. }
  23. }
  24. //检查用户权益
  25. func (l *CheckPowerLogic) CheckPower(in *pb.CheckPowerReq) (*pb.Resp, error) {
  26. l.Info("检查用户权益请求参数", fmt.Sprintf("%+v", in))
  27. resp := &pb.Resp{}
  28. if in.Appid == "" {
  29. l.Error(fmt.Sprintf("%+v", in), "无效的参数appid")
  30. return resp, nil
  31. } else if in.FunctionCode == "" {
  32. l.Error(fmt.Sprintf("%+v", in), "无效的参数function_code")
  33. return resp, nil
  34. } else if in.UserId == 0 && in.EntId == 0 && in.EntUserId == 0 {
  35. l.Error(fmt.Sprintf("%+v", in), "无效的参数user_id、ent_id、ent_user_id")
  36. return resp, nil
  37. }
  38. function := Base_function.FindByCode(in.Appid, in.FunctionCode)
  39. if function == nil {
  40. l.Error(fmt.Sprintf("%+v", in), "功能原始表中没有找到该功能")
  41. return resp, nil
  42. }
  43. myPowers := Base_power.FindMyPower(in.Appid, in.FunctionCode, in.UserId, in.EntId)
  44. if myPowers == nil || len(*myPowers) == 0 {
  45. resp.Status = -1
  46. l.Info(fmt.Sprintf("%+v", in), "不在有效期内")
  47. return resp, nil
  48. }
  49. if function.Power_rule == 1 { //只对周期进行校验
  50. resp.Status = 1
  51. return resp, nil
  52. } else {
  53. for _, v := range *myPowers {
  54. if v.Power_type == 2 {
  55. hasEmpower := false
  56. empower := Base_ent_empower.FindMyEntId(in.Appid, in.FunctionCode, in.EntId)
  57. if empower != nil {
  58. if len(*empower) == 1 {
  59. if (*empower)[0].Ent_user_id == 0 {
  60. hasEmpower = true
  61. }
  62. } else {
  63. for _, vv := range *empower {
  64. if vv.Ent_id == in.EntId && vv.Ent_user_id == in.EntUserId {
  65. hasEmpower = true
  66. break
  67. }
  68. }
  69. }
  70. }
  71. if !hasEmpower {
  72. resp.Status = -3
  73. l.Info(fmt.Sprintf("%+v", in), "没有对该用户进行授权")
  74. return resp, nil
  75. }
  76. }
  77. if function.Power_rule == 2 { //周期+数量校验
  78. if v.Surplus_count > 0 {
  79. resp.Status = 1
  80. return resp, nil
  81. }
  82. } else if function.Power_rule == 3 { //周期+频率+数量校验
  83. use, err := Base_resource_use.FindLastOne(in.Appid, in.FunctionCode, in.UserId, in.EntId, v.Power_type)
  84. if err != nil {
  85. l.Error(fmt.Sprintf("%+v", in), "查询资源使用记录失败")
  86. return resp, nil
  87. } else if use == nil {
  88. resp.Status = 1
  89. return resp, nil
  90. }
  91. create_time, err := time.ParseInLocation(Date_Full_Layout, use.Create_time, time.Local)
  92. if err != nil {
  93. l.Error(fmt.Sprintf("%+v", in), "资源充值/消耗表创建时间错误", create_time, err)
  94. return resp, nil
  95. }
  96. now := time.Now()
  97. if (*myPowers)[0].Limit_strategy == "1d" {
  98. if create_time.Year() == now.Year() && create_time.Month() == now.Month() && create_time.Day() == now.Day() {
  99. if use.Surplus_count > 0 {
  100. resp.Status = 1
  101. return resp, nil
  102. }
  103. } else {
  104. resp.Status = 1
  105. return resp, nil
  106. }
  107. } else if (*myPowers)[0].Limit_strategy == "1m" {
  108. if create_time.Year() == now.Year() && create_time.Month() == now.Month() {
  109. if use.Surplus_count > 0 {
  110. resp.Status = 1
  111. return resp, nil
  112. }
  113. } else {
  114. resp.Status = 1
  115. return resp, nil
  116. }
  117. } else {
  118. l.Error(fmt.Sprintf("%+v", in), "limit_strategy格式错误", (*myPowers)[0].Limit_strategy)
  119. return resp, nil
  120. }
  121. }
  122. }
  123. resp.Status = -2
  124. return resp, nil
  125. }
  126. return resp, nil
  127. }