golang 的 rabbitmq 消费项目
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。
 
 
 

301 行
8.1 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. func MoreCheckNewOrderAuth(eg *xorm.Engine, uid int, thisPrice *model.MoreNewcomersFreePriceType) (bool, *md.PricePerm) {
  70. // 测试开发阶段返回true
  71. user, _ := db.UserFindByID(eg, uid)
  72. userProfile, _ := db.UserProfileFindByID(eg, uid)
  73. perm := &md.PricePerm{}
  74. utils.Unserialize([]byte(thisPrice.Auth), perm)
  75. var permList []bool
  76. if perm.Auth.ValidUser.Open == "1" {
  77. permList = append(permList, checkValidUser(userProfile))
  78. }
  79. if perm.Auth.NewUser.Open == "1" {
  80. permList = append(permList, checkNewUser(userProfile))
  81. }
  82. if perm.Auth.UserLevel.Open == "1" {
  83. permList = append(permList, checkUserLevel(eg, user, perm.Auth.UserLevel.Value))
  84. }
  85. if perm.Auth.Onrecord.Open == "1" {
  86. permList = append(permList, checkOnRecord(userProfile))
  87. }
  88. if perm.Auth.InviteFriends.Open == "1" {
  89. permList = append(permList, checkInviteFriends(eg, user, perm.Auth.InviteFriends.Value))
  90. }
  91. if perm.Auth.Commission.Open == "1" {
  92. permList = append(permList, checkCommission(eg, perm.Auth.Commission.Days, perm.Auth.Commission.Money, user))
  93. }
  94. if perm.Auth.NewDays.Open == "1" {
  95. permList = append(permList, checkNewdaysOrder(eg, perm.Auth.NewDays.Days, user))
  96. }
  97. if perm.Auth.TeamMembers.Open == "1" {
  98. permList = append(permList, checkTeamMembers(eg, perm.Auth.TeamMembers.Amount, user))
  99. }
  100. if perm.Auth.Orders.Open == "1" {
  101. permList = append(permList, checkCompleteOrders(eg, perm.Auth.Orders.Days, perm.Auth.Orders.Amount, user))
  102. }
  103. var r bool
  104. if perm.AuthType == "all" {
  105. r = true
  106. for _, v := range permList {
  107. r = r && v
  108. }
  109. } else {
  110. r = false
  111. for _, v := range permList {
  112. r = r || v
  113. }
  114. }
  115. // 检查福利券是否符合
  116. needNewCoupon := thisPrice.NeedQuan
  117. //如果全部条件都需要满足 但是福利券不满足
  118. if perm.AuthType == "all" && userProfile.FreeRemainTime < needNewCoupon && thisPrice.NeedUseQuan == 1 {
  119. r = false
  120. }
  121. return r, perm
  122. }
  123. // 是否有效用户
  124. func checkValidUser(user *model.UserProfile) bool {
  125. if user.IsVerify == 1 {
  126. return true
  127. }
  128. return false
  129. }
  130. // 是否新用户
  131. func checkNewUser(user *model.UserProfile) bool {
  132. if user.IsNew == 1 {
  133. return true
  134. }
  135. return false
  136. }
  137. // 用户等级
  138. func checkUserLevel(Db *xorm.Engine, user *model.User, needUserLevelId string) bool {
  139. if user == nil {
  140. return false
  141. }
  142. needLevel, err := db2.UserLevelByID(Db, needUserLevelId)
  143. if err != nil {
  144. // 该等级已被删除的话就false
  145. return false
  146. }
  147. userLevel, err := db2.UserLevelByID(Db, user.Level)
  148. if err != nil {
  149. // 该等级已被删除的话就false
  150. return false
  151. }
  152. return userLevel.LevelWeight >= needLevel.LevelWeight
  153. }
  154. // 淘宝备案授权
  155. func checkOnRecord(user *model.UserProfile) bool {
  156. if user.AccTaobaoShareId > 0 {
  157. return true
  158. }
  159. return false
  160. }
  161. // 直推好友
  162. func checkInviteFriends(Db *xorm.Engine, user *model.User, need string) bool {
  163. if user == nil {
  164. return false
  165. }
  166. needPerson := utils.AnyToInt64(need)
  167. total, err := InviteCountlevelOne(Db, user)
  168. if err != nil {
  169. _ = logx.Warn(err)
  170. return false
  171. }
  172. if total >= needPerson {
  173. return true
  174. }
  175. return false
  176. }
  177. func InviteCountlevelOne(Db *xorm.Engine, user *model.User) (total int64, err error) {
  178. relate := new(model2.UserRelate)
  179. total, err = Db.Where("parent_uid=? and level=1", user.Uid).Count(relate)
  180. return
  181. }
  182. // 团队所有成员统计
  183. func InviteCountlevelAll(Db *xorm.Engine, user *model.User) (total int64, err error) {
  184. relate := new(model2.UserRelate)
  185. total, err = Db.Where("parent_uid=?", user.Uid).Count(relate)
  186. return
  187. }
  188. // 检查佣金
  189. func checkCommission(Db *xorm.Engine, leftValue, rightValue string, user *model.User) bool {
  190. if user == nil {
  191. return false
  192. }
  193. needDays, _ := strconv.Atoi(leftValue)
  194. needMoney, _ := strconv.Atoi(rightValue)
  195. t := time.Now()
  196. stime := time.Date(t.Year(), t.Month(), t.Day()-int(needDays), t.Hour(), 0, 0, 0, t.Location()).Unix()
  197. etime := t.Unix()
  198. ms, err := db.OrderListByTimeByState(Db, user.Uid, 3, stime, etime)
  199. if err != nil {
  200. _ = logx.Warn(err)
  201. return false
  202. } else {
  203. money := getSumForOrder(utils.IntToStr(user.Uid), ms)
  204. if money >= utils.AnyToFloat64(needMoney) {
  205. return true
  206. }
  207. }
  208. return false
  209. }
  210. func getSumForOrder(uid string, ol []*model.OrdList) float64 {
  211. var oa []float64
  212. var sum float64
  213. for _, item := range ol {
  214. for _, s := range strings.Split(item.BenefitList, "|") {
  215. if strings.Contains(s, uid) {
  216. uidbenfit := strings.Split(s, ":")
  217. if len(uidbenfit) != 2 {
  218. logx.Warn("OrderList BenefitList is format error")
  219. return 0
  220. }
  221. oa = append(oa, utils.StrToFloat64(uidbenfit[len(uidbenfit)-1]))
  222. break
  223. }
  224. }
  225. }
  226. for _, item := range oa {
  227. sum += item
  228. }
  229. return sum
  230. }
  231. func getSumForOrderRelate(oidMapAmount map[int64]float64, ol []*model.OrdList) float64 {
  232. var sum float64
  233. for _, item := range ol {
  234. if oidMapAmount[item.OrdId] > 0 {
  235. sum += oidMapAmount[item.OrdId]
  236. }
  237. }
  238. return sum
  239. }
  240. // 注册多少天之内没有订单
  241. func checkNewdaysOrder(Db *xorm.Engine, value string, user *model.User) bool {
  242. stime, etime := utils.GetTimeInterval("days", -utils.StrToInt(value))
  243. ordList, err := db.OrderListCountByUIDByTime(Db, user.Uid, stime, etime)
  244. if err != nil {
  245. _ = logx.Warn(err)
  246. return false
  247. }
  248. if len(*ordList) == 0 {
  249. return true
  250. }
  251. return false
  252. }
  253. // 团队数量达到多少个
  254. func checkTeamMembers(Db *xorm.Engine, value string, user *model.User) bool {
  255. needPerson := utils.AnyToInt64(value)
  256. total, err := InviteCountlevelAll(Db, user)
  257. if err != nil {
  258. _ = logx.Warn(err)
  259. return false
  260. }
  261. if total >= needPerson {
  262. return true
  263. }
  264. return false
  265. }
  266. // 几天内,已经结算订单达到几个
  267. func checkCompleteOrders(Db *xorm.Engine, leftValue, rightValue string, user *model.User) bool {
  268. needDays, _ := strconv.Atoi(leftValue)
  269. needAmount, _ := strconv.Atoi(rightValue)
  270. stime, etime := utils.GetTimeInterval("days", -needDays)
  271. ms, err := db.OrderListByTimeByState(Db, user.Uid, 3, stime, etime)
  272. if err != nil {
  273. _ = logx.Warn(err)
  274. return false
  275. }
  276. if len(ms) >= needAmount {
  277. return true
  278. }
  279. return false
  280. }