golang 的 rabbitmq 消费项目
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.
 
 
 

246 lines
6.2 KiB

  1. package svc
  2. import (
  3. "applet/app/db/model"
  4. "applet/app/md"
  5. db2 "code.fnuoos.com/go_rely_warehouse/zyos_go_order_relate_rule.git/db"
  6. model2 "code.fnuoos.com/go_rely_warehouse/zyos_go_order_relate_rule.git/db/model"
  7. "strconv"
  8. "strings"
  9. "time"
  10. "xorm.io/xorm"
  11. "applet/app/db"
  12. "applet/app/utils"
  13. "applet/app/utils/logx"
  14. )
  15. func CheckNewOrderAuth(eg *xorm.Engine, uid int, thisPrice *model.NewcomersFreePriceType) (bool, *md.PricePerm) {
  16. // 测试开发阶段返回true
  17. user, _ := db.UserFindByID(eg, uid)
  18. userProfile, _ := db.UserProfileFindByID(eg, uid)
  19. perm := &md.PricePerm{}
  20. utils.Unserialize([]byte(thisPrice.Auth), perm)
  21. var permList []bool
  22. if perm.Auth.ValidUser.Open == "1" {
  23. permList = append(permList, checkValidUser(userProfile))
  24. }
  25. if perm.Auth.NewUser.Open == "1" {
  26. permList = append(permList, checkNewUser(userProfile))
  27. }
  28. if perm.Auth.UserLevel.Open == "1" {
  29. permList = append(permList, checkUserLevel(eg, user, perm.Auth.UserLevel.Value))
  30. }
  31. if perm.Auth.Onrecord.Open == "1" {
  32. permList = append(permList, checkOnRecord(userProfile))
  33. }
  34. if perm.Auth.InviteFriends.Open == "1" {
  35. permList = append(permList, checkInviteFriends(eg, user, perm.Auth.InviteFriends.Value))
  36. }
  37. if perm.Auth.Commission.Open == "1" {
  38. permList = append(permList, checkCommission(eg, perm.Auth.Commission.Days, perm.Auth.Commission.Money, user))
  39. }
  40. if perm.Auth.NewDays.Open == "1" {
  41. permList = append(permList, checkNewdaysOrder(eg, perm.Auth.NewDays.Days, user))
  42. }
  43. if perm.Auth.TeamMembers.Open == "1" {
  44. permList = append(permList, checkTeamMembers(eg, perm.Auth.TeamMembers.Amount, user))
  45. }
  46. if perm.Auth.Orders.Open == "1" {
  47. permList = append(permList, checkCompleteOrders(eg, perm.Auth.Orders.Days, perm.Auth.Orders.Amount, user))
  48. }
  49. var r bool
  50. if perm.AuthType == "all" {
  51. r = true
  52. for _, v := range permList {
  53. r = r && v
  54. }
  55. } else {
  56. r = false
  57. for _, v := range permList {
  58. r = r || v
  59. }
  60. }
  61. // 检查福利券是否符合
  62. needNewCoupon := thisPrice.NeedQuan
  63. //如果全部条件都需要满足 但是福利券不满足
  64. if perm.AuthType == "all" && userProfile.FreeRemainTime < needNewCoupon && thisPrice.NeedUseQuan == 1 {
  65. r = false
  66. }
  67. return r, perm
  68. }
  69. // 是否有效用户
  70. func checkValidUser(user *model.UserProfile) bool {
  71. if user.IsVerify == 1 {
  72. return true
  73. }
  74. return false
  75. }
  76. // 是否新用户
  77. func checkNewUser(user *model.UserProfile) bool {
  78. if user.IsNew == 1 {
  79. return true
  80. }
  81. return false
  82. }
  83. // 用户等级
  84. func checkUserLevel(Db *xorm.Engine, user *model.User, needUserLevelId string) bool {
  85. if user == nil {
  86. return false
  87. }
  88. needLevel, err := db2.UserLevelByID(Db, needUserLevelId)
  89. if err != nil {
  90. // 该等级已被删除的话就false
  91. return false
  92. }
  93. userLevel, err := db2.UserLevelByID(Db, user.Level)
  94. if err != nil {
  95. // 该等级已被删除的话就false
  96. return false
  97. }
  98. return userLevel.LevelWeight >= needLevel.LevelWeight
  99. }
  100. // 淘宝备案授权
  101. func checkOnRecord(user *model.UserProfile) bool {
  102. if user.AccTaobaoShareId > 0 {
  103. return true
  104. }
  105. return false
  106. }
  107. // 直推好友
  108. func checkInviteFriends(Db *xorm.Engine, user *model.User, need string) bool {
  109. if user == nil {
  110. return false
  111. }
  112. needPerson := utils.AnyToInt64(need)
  113. total, err := InviteCountlevelOne(Db, user)
  114. if err != nil {
  115. _ = logx.Warn(err)
  116. return false
  117. }
  118. if total >= needPerson {
  119. return true
  120. }
  121. return false
  122. }
  123. func InviteCountlevelOne(Db *xorm.Engine, user *model.User) (total int64, err error) {
  124. relate := new(model2.UserRelate)
  125. total, err = Db.Where("parent_uid=? and level=1", user.Uid).Count(relate)
  126. return
  127. }
  128. // 团队所有成员统计
  129. func InviteCountlevelAll(Db *xorm.Engine, user *model.User) (total int64, err error) {
  130. relate := new(model2.UserRelate)
  131. total, err = Db.Where("parent_uid=?", user.Uid).Count(relate)
  132. return
  133. }
  134. // 检查佣金
  135. func checkCommission(Db *xorm.Engine, leftValue, rightValue string, user *model.User) bool {
  136. if user == nil {
  137. return false
  138. }
  139. needDays, _ := strconv.Atoi(leftValue)
  140. needMoney, _ := strconv.Atoi(rightValue)
  141. t := time.Now()
  142. stime := time.Date(t.Year(), t.Month(), t.Day()-int(needDays), t.Hour(), 0, 0, 0, t.Location()).Unix()
  143. etime := t.Unix()
  144. ms, err := db.OrderListByTimeByState(Db, user.Uid, 3, stime, etime)
  145. if err != nil {
  146. _ = logx.Warn(err)
  147. return false
  148. } else {
  149. money := getSumForOrder(utils.IntToStr(user.Uid), ms)
  150. if money >= utils.AnyToFloat64(needMoney) {
  151. return true
  152. }
  153. }
  154. return false
  155. }
  156. func getSumForOrder(uid string, ol []*model.OrdList) float64 {
  157. var oa []float64
  158. var sum float64
  159. for _, item := range ol {
  160. for _, s := range strings.Split(item.BenefitList, "|") {
  161. if strings.Contains(s, uid) {
  162. uidbenfit := strings.Split(s, ":")
  163. if len(uidbenfit) != 2 {
  164. logx.Warn("OrderList BenefitList is format error")
  165. return 0
  166. }
  167. oa = append(oa, utils.StrToFloat64(uidbenfit[len(uidbenfit)-1]))
  168. break
  169. }
  170. }
  171. }
  172. for _, item := range oa {
  173. sum += item
  174. }
  175. return sum
  176. }
  177. func getSumForOrderRelate(oidMapAmount map[int64]float64, ol []*model.OrdList) float64 {
  178. var sum float64
  179. for _, item := range ol {
  180. if oidMapAmount[item.OrdId] > 0 {
  181. sum += oidMapAmount[item.OrdId]
  182. }
  183. }
  184. return sum
  185. }
  186. // 注册多少天之内没有订单
  187. func checkNewdaysOrder(Db *xorm.Engine, value string, user *model.User) bool {
  188. stime, etime := utils.GetTimeInterval("days", -utils.StrToInt(value))
  189. ordList, err := db.OrderListCountByUIDByTime(Db, user.Uid, stime, etime)
  190. if err != nil {
  191. _ = logx.Warn(err)
  192. return false
  193. }
  194. if len(*ordList) == 0 {
  195. return true
  196. }
  197. return false
  198. }
  199. // 团队数量达到多少个
  200. func checkTeamMembers(Db *xorm.Engine, value string, user *model.User) bool {
  201. needPerson := utils.AnyToInt64(value)
  202. total, err := InviteCountlevelAll(Db, user)
  203. if err != nil {
  204. _ = logx.Warn(err)
  205. return false
  206. }
  207. if total >= needPerson {
  208. return true
  209. }
  210. return false
  211. }
  212. // 几天内,已经结算订单达到几个
  213. func checkCompleteOrders(Db *xorm.Engine, leftValue, rightValue string, user *model.User) bool {
  214. needDays, _ := strconv.Atoi(leftValue)
  215. needAmount, _ := strconv.Atoi(rightValue)
  216. stime, etime := utils.GetTimeInterval("days", -needDays)
  217. ms, err := db.OrderListByTimeByState(Db, user.Uid, 3, stime, etime)
  218. if err != nil {
  219. _ = logx.Warn(err)
  220. return false
  221. }
  222. if len(ms) >= needAmount {
  223. return true
  224. }
  225. return false
  226. }