蛋蛋星球-客户端
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

168 lines
5.6 KiB

  1. package svc
  2. import (
  3. "applet/app/db"
  4. "applet/app/e"
  5. "applet/app/utils"
  6. "code.fnuoos.com/EggPlanet/egg_models.git/src/implement"
  7. "code.fnuoos.com/EggPlanet/egg_models.git/src/model"
  8. "code.fnuoos.com/EggPlanet/egg_system_rules.git/rule/egg_energy/md"
  9. "errors"
  10. "github.com/gin-gonic/gin"
  11. "github.com/shopspring/decimal"
  12. "math"
  13. "strings"
  14. "time"
  15. )
  16. func CheckWithdraw(c *gin.Context, amount string) (err error, realAmount, fee string, isAuto, isFirst bool) {
  17. realAmount = amount
  18. user := GetUser(c)
  19. now := time.Now()
  20. amountValue, _ := decimal.NewFromString(amount)
  21. //1、查询 fin_withdraw_setting 提现设置表
  22. finWithdrawSettingDb := implement.NewFinWithdrawSettingDb(db.Db)
  23. withdrawSetting, err := finWithdrawSettingDb.FinWithdrawSettingGetOne()
  24. if err != nil {
  25. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  26. return
  27. }
  28. //2、判断“是否实名”
  29. if withdrawSetting.IsRealName == 1 && user.IsRealName != 1 {
  30. return errors.New("非实名用户不可提现"), realAmount, fee, isAuto, isFirst
  31. }
  32. //3、判断“提现金额”
  33. if utils.StrToFloat64(withdrawSetting.WithdrawAmountLimit) > 0 {
  34. withdrawAmountLimitValue, _ := decimal.NewFromString(withdrawSetting.WithdrawAmountLimit)
  35. if amountValue.GreaterThan(withdrawAmountLimitValue) {
  36. return errors.New("非可提现金额"), realAmount, fee, isAuto, isFirst
  37. }
  38. }
  39. //4、判断“提现倍数”
  40. if utils.StrToFloat64(withdrawSetting.WithdrawMultipleLimit) > 0 {
  41. result := utils.StrToFloat64(amount) / utils.StrToFloat64(withdrawSetting.WithdrawMultipleLimit)
  42. // 检查结果是否非常接近一个整数
  43. roundedResult := math.Round(result)
  44. if !(math.Abs(roundedResult-result) < 1e-9) {
  45. return errors.New("非可提现倍数"), realAmount, fee, isAuto, isFirst
  46. }
  47. }
  48. //5、验证会员等级
  49. if withdrawSetting.VipLevelLimit > 0 && withdrawSetting.VipLevelLimit > user.Level {
  50. return errors.New("非可提现会员等级"), realAmount, fee, isAuto, isFirst
  51. }
  52. //6、 验证小数点
  53. if withdrawSetting.IsSupportDecimalPoint > 0 && strings.Contains(amount, ".") {
  54. return errors.New("不支持的提现金额"), realAmount, fee, isAuto, isFirst
  55. }
  56. //7、验证时段
  57. if withdrawSetting.WithdrawTimeInterval != "" {
  58. withdrawTimeInterval := strings.Split(withdrawSetting.WithdrawTimeInterval, "-")
  59. // 定义要比较的时间格式
  60. layout := "15:04"
  61. // 解析给定的时间字符串
  62. start, _ := time.Parse(layout, withdrawTimeInterval[0])
  63. end, _ := time.Parse(layout, withdrawTimeInterval[1])
  64. // 设置为今天的日期
  65. start = time.Date(now.Year(), now.Month(), now.Day(), start.Hour(), start.Minute(), 0, 0, now.Location())
  66. end = time.Date(now.Year(), now.Month(), now.Day(), end.Hour(), end.Minute(), 0, 0, now.Location())
  67. // 如果结束时间小于开始时间,说明跨天了
  68. if end.Before(start) {
  69. if !(now.After(start) || now.Before(end)) {
  70. return errors.New("非可提现时间段"), realAmount, fee, isAuto, isFirst
  71. }
  72. } else { // 否则,在同一天内比较
  73. if !(now.After(start) && now.Before(end)) {
  74. return errors.New("非可提现时间段"), realAmount, fee, isAuto, isFirst
  75. }
  76. }
  77. }
  78. //8、验证“提现频率”
  79. var frequency md.WithdrawFrequencySettingStruct
  80. utils.Unserialize([]byte(withdrawSetting.FrequencySet), &frequency)
  81. if frequency.Duration == 2 {
  82. day := now.Weekday()
  83. if !utils.InArr(utils.IntToStr(int(day)), frequency.Num) {
  84. return errors.New("非可提现日期"), realAmount, fee, isAuto, isFirst
  85. }
  86. }
  87. if frequency.Duration == 3 {
  88. day := now.Day()
  89. if !utils.InArr(utils.IntToStr(day), frequency.Num) {
  90. return errors.New("非可提现日期"), realAmount, fee, isAuto, isFirst
  91. }
  92. }
  93. if withdrawSetting.WithdrawNumsLimit > 0 {
  94. var withdrawNums int64
  95. var startOfDay, endOfDay time.Time
  96. if frequency.Duration == 1 { //按天
  97. startOfDay = time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, now.Location())
  98. endOfDay = startOfDay.Add(24 * time.Hour)
  99. }
  100. if frequency.Duration == 2 { //按周
  101. startOfDay = utils.GetStartOfWeek(now)
  102. endOfDay = startOfDay.Add(7 * 24 * time.Hour)
  103. }
  104. if frequency.Duration == 3 { //按月
  105. startOfDay = utils.GetFirstDateOfMonth(now)
  106. endOfDay = utils.GetLastDateOfMonth(now)
  107. }
  108. withdrawNums, err = db.Db.Where("create_at >= ?", startOfDay.Format("2006-01-02 15:04:05")).
  109. And("create_at < ?", endOfDay.Format("2006-01-02 15:04:05")).
  110. And("uid =?", user.Id).
  111. And("state != 3"). //失败不计入
  112. Count(&model.FinWithdrawApply{})
  113. if err != nil {
  114. return err, realAmount, fee, isAuto, isFirst
  115. }
  116. if int(withdrawNums) >= withdrawSetting.WithdrawNumsLimit {
  117. return errors.New("当前已无可提现次数"), realAmount, fee, isAuto, isFirst
  118. }
  119. }
  120. //9、计算手续费
  121. var feeSet md.WithdrawFeeSetStruct
  122. utils.Unserialize([]byte(withdrawSetting.WithdrawFeeSet), &feeSet)
  123. if feeSet.Value > 0 {
  124. feeSerValue := decimal.NewFromInt(int64(feeSet.Value))
  125. if feeSet.Kind == 2 { //固定比例
  126. feeSerValue = amountValue.Mul(feeSerValue.Div(decimal.NewFromInt(100)))
  127. fee = amountValue.Mul(feeSerValue.Div(decimal.NewFromInt(100))).String()
  128. }
  129. fee = feeSerValue.String()
  130. realAmount = amountValue.Sub(feeSerValue).String()
  131. }
  132. //10、判断是否为自动提现
  133. if withdrawSetting.IsAuto > 0 {
  134. autoAmountLimit, _ := decimal.NewFromString(withdrawSetting.IsAutoAmountLimit)
  135. if amountValue.GreaterThanOrEqual(autoAmountLimit) {
  136. isAuto = true
  137. }
  138. }
  139. //11、判断是否为第一次提现
  140. has, err := db.Db.Where("uid = ? and ", user.Id).
  141. And("uid =?", user.Id).
  142. Get(model.FinWithdrawApply{})
  143. if has {
  144. isFirst = true
  145. }
  146. if utils.StrToFloat64(realAmount) <= 0 {
  147. return errors.New("当前提现金额有误"), realAmount, fee, isAuto, isFirst
  148. }
  149. return
  150. }