surplus.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. package service
  2. import (
  3. "errors"
  4. "time"
  5. . "app.yhyue.com/moapp/jybase/date"
  6. . "bp.jydev.jianyu360.cn/BaseService/resourceCenter/public/entity"
  7. )
  8. /*
  9. * 获取资源剩余情况
  10. * @param appid
  11. * @param function_code 功能代码
  12. * @param account_id 账户id
  13. * @param ent_account_id 企业账户id
  14. * @param ent_id 企业id
  15. * @return 状态 0:失败 1:成功 -1:不在有效期内 -2:数量不足 -3:没有授权
  16. * @return 总数
  17. * @return 剩余数
  18. * @return 错误信息
  19. */
  20. func Surplus(appid, function_code string, account_id, ent_account_id, ent_id, ent_user_id int64) (int64, int64, int64, string, string, error) {
  21. status, use_count, surplus_count, err := func() (int64, int64, int64, error) {
  22. if appid == "" {
  23. return 0, 0, 0, errors.New("无效的参数appid")
  24. } else if function_code == "" {
  25. return 0, 0, 0, errors.New("无效的参数function_code")
  26. } else if account_id == 0 && ent_account_id == 0 {
  27. return 0, 0, 0, errors.New("无效的参数account_id、ent_account_id")
  28. }
  29. function := Base_function.FindByCode(appid, function_code)
  30. if function == nil {
  31. return 0, 0, 0, errors.New("功能原始表中没有找到该功能")
  32. }
  33. myPowers := Base_power.FindMyPowersByFc(appid, function_code, account_id, ent_account_id)
  34. if myPowers == nil || len(*myPowers) == 0 {
  35. return -1, 0, 0, errors.New("不在有效期内")
  36. }
  37. if function.Power_rule == 1 { //只对周期进行校验
  38. for _, v := range *myPowers {
  39. if v.Power_type == 2 && !Base_ent_empower.HasEmpower(appid, function_code, ent_id, ent_user_id) {
  40. return -3, 0, 0, errors.New("没有对该用户进行授权")
  41. }
  42. }
  43. return 1, 0, 0, nil
  44. } else if function.Power_rule == 2 { //周期+数量校验
  45. use_count, surplus_count := int64(0), int64(0)
  46. for _, v := range *myPowers {
  47. if v.Power_type == 2 && !Base_ent_empower.HasEmpower(appid, function_code, ent_id, ent_user_id) {
  48. return -3, 0, 0, errors.New("没有对该用户进行授权")
  49. }
  50. use_count += v.Use_count
  51. surplus_count += v.Surplus_count
  52. }
  53. if surplus_count > 0 {
  54. return 1, use_count, surplus_count, nil
  55. } else {
  56. return -2, use_count, surplus_count, nil
  57. }
  58. } else if function.Power_rule == 3 { //周期+频率+数量校验
  59. for _, v := range *myPowers {
  60. if v.Power_type == 2 && !Base_ent_empower.HasEmpower(appid, function_code, ent_id, ent_user_id) {
  61. return -3, 0, 0, errors.New("没有对该用户进行授权")
  62. }
  63. var use_account_id int64
  64. if v.Power_type == 1 {
  65. use_account_id = account_id
  66. } else if v.Power_type == 2 {
  67. use_account_id = ent_account_id
  68. } else {
  69. continue
  70. }
  71. use, err := Base_resource_use.FindLastOne(appid, function_code, use_account_id)
  72. if err != nil {
  73. return 0, 0, 0, errors.New("查询资源使用记录失败")
  74. } else if use == nil {
  75. return 1, v.Strategy_count, v.Strategy_count, nil
  76. }
  77. create_time, err := time.ParseInLocation(Date_Full_Layout, use.Create_time, time.Local)
  78. if err != nil {
  79. return 0, 0, 0, errors.New("资源充值/消耗表创建时间错误," + err.Error())
  80. }
  81. now := time.Now()
  82. if (*myPowers)[0].Limit_strategy == "1d" {
  83. if create_time.Year() == now.Year() && create_time.Month() == now.Month() && create_time.Day() == now.Day() {
  84. if use.Surplus_count > 0 {
  85. return 1, v.Strategy_count, use.Surplus_count, nil
  86. } else {
  87. return -2, v.Strategy_count, use.Surplus_count, nil
  88. }
  89. } else {
  90. return 1, v.Strategy_count, v.Strategy_count, nil
  91. }
  92. } else if (*myPowers)[0].Limit_strategy == "1m" {
  93. if create_time.Year() == now.Year() && create_time.Month() == now.Month() {
  94. if use.Surplus_count > 0 {
  95. return 1, v.Strategy_count, use.Surplus_count, nil
  96. } else {
  97. return -2, v.Strategy_count, use.Surplus_count, nil
  98. }
  99. } else {
  100. return 1, v.Strategy_count, v.Strategy_count, nil
  101. }
  102. } else {
  103. return 0, 0, 0, errors.New("limit_strategy格式错误")
  104. }
  105. }
  106. }
  107. return 0, 0, 0, nil
  108. }()
  109. start_time, end_time := "", ""
  110. if status != 0 {
  111. lastPower := Base_power.FindMyLastPowerByFc(appid, function_code, account_id, ent_account_id)
  112. if lastPower != nil {
  113. start_time = lastPower.Start_time
  114. end_time = lastPower.End_time
  115. }
  116. }
  117. return status, use_count, surplus_count, start_time, end_time, err
  118. }