golang 的 rabbitmq 消费项目
25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.

svc_acquisition.go 11 KiB

6 ay önce
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. package svc
  2. import (
  3. "applet/app/db"
  4. "applet/app/db/model"
  5. "applet/app/md"
  6. "applet/app/utils"
  7. "applet/app/utils/logx"
  8. "fmt"
  9. "github.com/shopspring/decimal"
  10. "github.com/syyongx/php2go"
  11. "log"
  12. "strings"
  13. "time"
  14. "xorm.io/xorm"
  15. )
  16. // 拉新活动 acquisition
  17. // 通用acq handle
  18. func commHandleAcq1(logText, acqText string, acqCfg *md.AcquisitionCfg, user *md.User, eg *xorm.Engine) {
  19. now := time.Now().Unix()
  20. startTime := utils.TimeParseStd(acqCfg.StartTime).Unix()
  21. endTime := utils.TimeParseStd(acqCfg.EndTime).Unix()
  22. //判断活动时间是否符合
  23. if now < startTime || now > endTime || utils.StrToInt(acqCfg.Status) != 1 {
  24. return
  25. }
  26. //时间不符合条件关掉
  27. if startTime == 0 || endTime == 0 || user.Info.CreateAt.Unix() < startTime || user.Info.CreateAt.Unix() > endTime {
  28. return
  29. }
  30. if user == nil || user.Profile.Uid == 0 {
  31. return
  32. }
  33. oneUser, err := db.UserFindByID(eg, user.Profile.Uid)
  34. if oneUser == nil || err != nil {
  35. return
  36. }
  37. //查出有没有奖励记录
  38. acqRewardLog := &model.AcquisitionRewardLog{
  39. ToUid: user.Profile.Uid,
  40. Source: 1,
  41. }
  42. acqRewardLog, has, err := db.GetAcquisitionRewardLog(eg, acqRewardLog)
  43. if err != nil {
  44. _ = logx.Warn(err)
  45. return
  46. }
  47. // 没有rewardLog的话插入
  48. if !has {
  49. acqRewardLog = &model.AcquisitionRewardLog{
  50. Uid: user.Profile.ParentUid,
  51. ToUid: user.Profile.Uid,
  52. Title: user.Info.Nickname,
  53. Source: 1,
  54. SourceText: "直推好友",
  55. Money: acqCfg.RewardRule.DirectSuccess,
  56. CreatedAt: int(time.Now().Unix()),
  57. State: 0,
  58. CoinId: utils.StrToInt(acqCfg.RewardRule.RewardCoinId),
  59. RewardType: utils.StrToInt(acqCfg.RewardRule.RewardType),
  60. }
  61. if acqCfg.RewardRule.RewardType == "1" {
  62. acqRewardLog.Money = Rands(acqCfg.RewardRule.DirectSuccess, acqCfg.RewardRule.DirectSuccessMax)
  63. }
  64. db.InsertRewardLog(eg, acqRewardLog)
  65. }
  66. // 拉新日志
  67. acqLog, has, err := db.GetAcquisitionLog(eg, &model.AcquisitionLog{
  68. Uid: user.Profile.Uid,
  69. })
  70. // err 退出
  71. if err != nil {
  72. _ = logx.Warn(err)
  73. return
  74. }
  75. if !has {
  76. // 没有拉新记录
  77. var state int
  78. state = 0
  79. acqLog = &model.AcquisitionLog{
  80. ParentUid: user.Profile.ParentUid,
  81. Uid: user.Profile.Uid,
  82. InviteTime: time.Now(),
  83. State: state,
  84. CompleteCon: acqText,
  85. }
  86. db.InsertAcqLog(eg, acqLog)
  87. } else {
  88. // 有记录
  89. completeCon := strings.Split(acqLog.CompleteCon, ",")
  90. completeCon = append(completeCon, acqText)
  91. completeConStr := php2go.Implode(",", completeCon)
  92. acqLog.CompleteCon = completeConStr
  93. db.UpdateAcqLog(eg, acqLog)
  94. }
  95. }
  96. // 拉新条件--首单条件 firstOrder
  97. func AcquisitionHookFirstOrder(eg *xorm.Engine, masterId string, ord *model.OrdList) {
  98. // 没有context用Db来查
  99. log.Println("firstOrder hook start")
  100. cfg := db.SysCfgGetWithDb(eg, masterId, "acquisition_cfg")
  101. if cfg == "" {
  102. _ = logx.Warn("acqCfg error")
  103. return
  104. }
  105. var acqCfgLocal md.AcquisitionCfg
  106. utils.Unserialize([]byte(cfg), &acqCfgLocal)
  107. acqCfg := &acqCfgLocal
  108. user, err := db.UserAllInfoByUid(eg, ord.Uid)
  109. if err != nil {
  110. _ = logx.Warn(err)
  111. return
  112. }
  113. if user == nil {
  114. log.Println("user is nil")
  115. return
  116. }
  117. if user.Info == nil || user.Profile == nil {
  118. return
  119. }
  120. if user.Profile.ParentUid == 0 {
  121. log.Println("无上级")
  122. return
  123. }
  124. fmt.Println(utils.SerializeStr(acqCfg.SuccessConditions))
  125. if acqCfg.SuccessConditions.FirstOrder.Open == "1" && utils.StrToInt(acqCfg.SuccessConditions.FirstOrder.Day) > 0 {
  126. now := utils.Int64ToStr(time.Now().Unix())
  127. if (utils.StrToInt(now)-ord.CreateAt)/86400 > utils.StrToInt(acqCfg.SuccessConditions.FirstOrder.Day) {
  128. log.Println("超过天数")
  129. return
  130. }
  131. commHandleAcq1("首单条件开启", "firstOrder", acqCfg, user, eg)
  132. }
  133. if acqCfg.SuccessConditions.SelfOrder.Open == "1" {
  134. if ord.UserCommission < utils.AnyToFloat64(acqCfg.SuccessConditions.SelfOrder.Money) {
  135. log.Println("佣金不达标")
  136. return
  137. }
  138. commHandleAcq1("自购佣金条件开启", "selfOrder", acqCfg, user, eg)
  139. }
  140. if acqCfg.SuccessConditions.OrderPay.Open == "1" {
  141. if ord.PaidPrice < utils.AnyToFloat64(acqCfg.SuccessConditions.OrderPay.Money) {
  142. log.Println("支付金额不达标")
  143. return
  144. }
  145. commHandleAcq1("付款金额条件开启", "orderPay", acqCfg, user, eg)
  146. }
  147. if acqCfg.SuccessConditions.OrderStatus.Open == "1" {
  148. if ord.State < utils.StrToInt(acqCfg.SuccessConditions.OrderStatus.Status) {
  149. log.Println("订单状态不达标")
  150. return
  151. }
  152. commHandleAcq1("订单状态条件开启", "orderStatus", acqCfg, user, eg)
  153. }
  154. //检测有些记录之前没写入
  155. checkAcqLogInsert(eg, acqCfg, user)
  156. return
  157. }
  158. //检测有些记录之前没写入
  159. func checkAcqLogInsert(eg *xorm.Engine, acqCfg *md.AcquisitionCfg, user *md.User) {
  160. now := time.Now().Unix()
  161. startTime := utils.TimeParseStd(acqCfg.StartTime).Unix()
  162. endTime := utils.TimeParseStd(acqCfg.EndTime).Unix()
  163. //判断活动时间是否符合
  164. if now < startTime || now > endTime || utils.StrToInt(acqCfg.Status) != 1 {
  165. return
  166. }
  167. //时间不符合条件关掉
  168. if startTime == 0 || endTime == 0 || user.Info.CreateAt.Unix() < startTime || user.Info.CreateAt.Unix() > endTime {
  169. return
  170. }
  171. //判断有没有记录
  172. acqLog, _, _ := db.GetAcquisitionLog(eg, &model.AcquisitionLog{
  173. Uid: user.Profile.Uid,
  174. })
  175. var acqText string
  176. completeCon := strings.Split(acqLog.CompleteCon, ",")
  177. completeCon = append(completeCon, acqText)
  178. //completeConStr := php2go.Implode(",", completeCon)
  179. if acqCfg.SuccessConditions.Register.Open == "1" {
  180. t := php2go.InArray("register", completeCon)
  181. if t == false {
  182. commHandleAcq1("注册条件开启", "register", acqCfg, user, eg)
  183. }
  184. }
  185. if acqCfg.SuccessConditions.BindPhone.Open == "1" {
  186. t := php2go.InArray("bindPhone", completeCon)
  187. if t == false {
  188. commHandleAcq1("绑定手机开启", "bindPhone", acqCfg, user, eg)
  189. }
  190. }
  191. if acqCfg.SuccessConditions.TaobaoAuthorization.Open == "1" {
  192. t := php2go.InArray("taobaoAuth", completeCon)
  193. if t == false {
  194. commHandleAcq1("淘宝授权条件开启", "taobaoAuth", acqCfg, user, eg)
  195. }
  196. }
  197. }
  198. func Rands(minVal, maxVal string) string {
  199. min := int(utils.StrToFloat64(minVal) * 100)
  200. max := int(utils.StrToFloat64(maxVal) * 100)
  201. return utils.Float64ToStrByPrec(float64(utils.RandInt(min, max))/100, 3)
  202. }
  203. // 发送直接奖励,间推奖励 ,插入奖励记录
  204. func SendCommReward(eg *xorm.Engine, cfg *md.AcquisitionCfg, user *md.User, rewardLog *model.AcquisitionRewardLog) {
  205. if rewardLog == nil {
  206. return
  207. }
  208. if rewardLog.IsFrozen == 1 {
  209. return
  210. }
  211. now := time.Now().Unix()
  212. startTime := utils.TimeParseStd(cfg.StartTime).Unix()
  213. endTime := utils.TimeParseStd(cfg.EndTime).Unix()
  214. //判断活动时间是否符合
  215. if now < startTime || now > endTime || utils.StrToInt(cfg.Status) != 1 {
  216. return
  217. }
  218. //时间不符合条件关掉
  219. if startTime == 0 || endTime == 0 || user.Info.CreateAt.Unix() < startTime || user.Info.CreateAt.Unix() > endTime {
  220. return
  221. }
  222. //判断是否发放过
  223. list, err := db.GetFinUserFlowByOIDANDORDTYPE(eg, "11", utils.IntToStr(rewardLog.Id), "acq")
  224. if err != nil || list != nil {
  225. return
  226. }
  227. var delUser = model.UserDeleteInfo{}
  228. eg.Where("phone=?", user.Info.Phone).Get(&delUser)
  229. if delUser.Id > 0 {
  230. return
  231. }
  232. rewardLog.CoinId = utils.StrToInt(cfg.RewardRule.RewardCoinId)
  233. rewardLog.RewardType = utils.StrToInt(cfg.RewardRule.RewardType)
  234. InvitedReward := cfg.RewardRule.InvitedReward
  235. DirectSuccess := cfg.RewardRule.DirectSuccess
  236. IndirectSuccess := cfg.RewardRule.IndirectSuccess
  237. if cfg.RewardRule.RewardType == "1" {
  238. InvitedReward = Rands(cfg.RewardRule.InvitedReward, cfg.RewardRule.InvitedRewardMax)
  239. DirectSuccess = Rands(cfg.RewardRule.DirectSuccess, cfg.RewardRule.DirectSuccessMax)
  240. IndirectSuccess = Rands(cfg.RewardRule.IndirectSuccess, cfg.RewardRule.IndirectSuccessMax)
  241. }
  242. // 被邀请人
  243. sendAcqFin(eg, user.Profile.Uid, InvitedReward, user.Profile.Uid, rewardLog.Id, cfg.RewardRule.RewardCoinId)
  244. // 直推
  245. _, _ = db.UpdateRewardLog(eg, rewardLog)
  246. sendAcqFin(eg, user.Profile.ParentUid, DirectSuccess, user.Profile.Uid, rewardLog.Id, cfg.RewardRule.RewardCoinId)
  247. // 间推
  248. nextUserRelate, err := db.UserProfileFindByID(eg, user.Profile.ParentUid)
  249. if err != nil {
  250. _ = logx.Warn(err)
  251. return
  252. }
  253. if nextUserRelate.ParentUid == 0 {
  254. return
  255. }
  256. db.InsertRewardLog(eg, &model.AcquisitionRewardLog{
  257. Uid: nextUserRelate.ParentUid,
  258. ToUid: user.Profile.Uid,
  259. Title: user.Info.Nickname,
  260. Source: 2,
  261. SourceText: "间推好友",
  262. Money: IndirectSuccess,
  263. CreatedAt: int(time.Now().Unix()),
  264. GivenAt: int(time.Now().Unix()),
  265. State: 1,
  266. CoinId: utils.StrToInt(cfg.RewardRule.RewardCoinId),
  267. RewardType: utils.StrToInt(cfg.RewardRule.RewardType),
  268. })
  269. sendAcqFin(eg, nextUserRelate.ParentUid, IndirectSuccess, user.Profile.Uid, rewardLog.Id, cfg.RewardRule.RewardCoinId)
  270. }
  271. func sendAcqFin(eg *xorm.Engine, uid interface{}, money string, firstUid int, id int, rewardCoinId string) {
  272. if uid == 0 {
  273. return
  274. }
  275. // 发放余额
  276. user, err := db.UserAllInfoByUid(eg, uid)
  277. if err != nil {
  278. _ = logx.Warn(err)
  279. return
  280. }
  281. if user == nil { //用户可能不见了,但是关系链还存在
  282. return
  283. }
  284. if utils.StrToInt(rewardCoinId) > 0 {
  285. userVirtualCoin := db.GetUserVirtualCoinAmount(eg, int(utils.AnyToInt64(uid)), rewardCoinId)
  286. if userVirtualCoin == nil {
  287. userVirtualCoin = &model.UserVirtualAmount{CoinId: utils.StrToInt(rewardCoinId), Uid: int(utils.AnyToInt64(uid))}
  288. eg.Insert(userVirtualCoin)
  289. }
  290. finValid := utils.AnyToFloat64(userVirtualCoin.Amount)
  291. userVirtualCoin.Amount = utils.AnyToString(finValid + utils.StrToFloat64(money))
  292. affect, err := db.UserVirtualCoinAmountUpdate(eg, userVirtualCoin.Id, userVirtualCoin, "amount")
  293. if err != nil || affect != 1 {
  294. return
  295. }
  296. db.NewUserVirtualCoinFlowInsert(eg, int(utils.AnyToInt64(uid)), 1, utils.StrToInt(rewardCoinId), "", "拉新奖励", 180, money, utils.Float64ToStr(finValid), userVirtualCoin.Amount)
  297. } else {
  298. moneyA, _ := decimal.NewFromString(money)
  299. moneyB, _ := decimal.NewFromString(user.Profile.FinValid)
  300. newM := moneyA.Add(moneyB)
  301. user.Profile.FinValid = newM.String()
  302. _, _ = db.UserProfileUpdate(eg, uid, user.Profile, "fin_valid")
  303. if err := db.FinUserFlowInsertOne(
  304. eg,
  305. &model.FinUserFlow{
  306. Type: 0,
  307. SysFee: "0",
  308. Uid: user.Profile.Uid,
  309. Amount: money,
  310. BeforeAmount: utils.Float64ToStr(utils.StrToFloat64(user.Profile.FinValid) - utils.StrToFloat64(money)),
  311. AfterAmount: newM.String(),
  312. OrdType: "acq",
  313. OrdAction: 11,
  314. OrdTitle: "拉新奖励",
  315. OrdTime: int(time.Now().Unix()),
  316. State: 2,
  317. OrdDetail: utils.IntToStr(firstUid),
  318. OrdId: utils.IntToStr(id),
  319. }); err != nil {
  320. _ = logx.Warn(err)
  321. return
  322. }
  323. }
  324. }