checkpowerlogic.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. resp := &pb.Resp{}
  27. if in.Appid == "" {
  28. l.Error(fmt.Sprintf("%+v", in), "无效的参数appid")
  29. return resp, nil
  30. } else if in.FunctionCode == "" {
  31. l.Error(fmt.Sprintf("%+v", in), "无效的参数function_code")
  32. return resp, nil
  33. } else if in.UserId == 0 && in.EntId == 0 && in.EntUserId == 0 {
  34. l.Error(fmt.Sprintf("%+v", in), "无效的参数user_id、ent_id、ent_user_id")
  35. return resp, nil
  36. }
  37. function := Base_function.FindByCode(in.Appid, in.FunctionCode)
  38. if function == nil {
  39. l.Error(fmt.Sprintf("%+v", in), "功能原始表中没有找到该功能")
  40. return resp, nil
  41. }
  42. myPowers := Base_power.FindMyPower(in.Appid, in.FunctionCode, in.UserId, in.EntId)
  43. if myPowers == nil || len(*myPowers) == 0 {
  44. resp.Status = -1
  45. l.Info(fmt.Sprintf("%+v", in), "不在有效期内")
  46. return resp, nil
  47. }
  48. if function.Power_rule == 1 { //只对周期进行校验
  49. resp.Status = 1
  50. return resp, nil
  51. } else {
  52. for _, v := range *myPowers {
  53. if v.Power_type == 2 {
  54. hasEmpower := false
  55. empower := Base_ent_empower.FindMyEntId(in.Appid, in.FunctionCode, in.EntId)
  56. if empower != nil {
  57. if len(*empower) == 1 {
  58. if (*empower)[0].Ent_user_id == 0 {
  59. hasEmpower = true
  60. }
  61. } else {
  62. for _, vv := range *empower {
  63. if vv.Ent_id == in.EntId && vv.Ent_user_id == in.EntUserId {
  64. hasEmpower = true
  65. break
  66. }
  67. }
  68. }
  69. }
  70. if !hasEmpower {
  71. resp.Status = -3
  72. l.Info(fmt.Sprintf("%+v", in), "没有对该用户进行授权")
  73. return resp, nil
  74. }
  75. }
  76. if function.Power_rule == 2 { //周期+数量校验
  77. if v.Surplus_count > 0 {
  78. resp.Status = 1
  79. return resp, nil
  80. }
  81. } else if function.Power_rule == 3 { //周期+频率+数量校验
  82. use, err := Base_resource_use.FindLastOne(in.Appid, in.FunctionCode, in.UserId, in.EntId, v.Power_type)
  83. if err != nil {
  84. l.Error(fmt.Sprintf("%+v", in), "查询资源使用记录失败")
  85. return resp, nil
  86. } else if use == nil {
  87. resp.Status = 1
  88. return resp, nil
  89. }
  90. create_time, err := time.ParseInLocation(Date_Full_Layout, use.Create_time, time.Local)
  91. if err != nil {
  92. l.Error(fmt.Sprintf("%+v", in), "资源充值/消耗表创建时间错误", create_time, err)
  93. return resp, nil
  94. }
  95. now := time.Now()
  96. if (*myPowers)[0].Limit_strategy == "1d" {
  97. if create_time.Year() == now.Year() && create_time.Month() == now.Month() && create_time.Day() == now.Day() {
  98. if use.Surplus_count > 0 {
  99. resp.Status = 1
  100. return resp, nil
  101. }
  102. } else {
  103. resp.Status = 1
  104. return resp, nil
  105. }
  106. } else if (*myPowers)[0].Limit_strategy == "1m" {
  107. if create_time.Year() == now.Year() && create_time.Month() == now.Month() {
  108. if use.Surplus_count > 0 {
  109. resp.Status = 1
  110. return resp, nil
  111. }
  112. } else {
  113. resp.Status = 1
  114. return resp, nil
  115. }
  116. } else {
  117. l.Error(fmt.Sprintf("%+v", in), "limit_strategy格式错误", (*myPowers)[0].Limit_strategy)
  118. return resp, nil
  119. }
  120. }
  121. }
  122. resp.Status = -2
  123. return resp, nil
  124. }
  125. return resp, nil
  126. }