蛋蛋星球-客户端
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.
 
 
 
 
 
 

318 lines
12 KiB

  1. package svc
  2. import (
  3. "applet/app/db"
  4. "applet/app/md"
  5. "applet/app/pkg/pb"
  6. "applet/app/utils"
  7. "applet/app/utils/cache"
  8. "code.fnuoos.com/EggPlanet/egg_models.git/src/implement"
  9. "code.fnuoos.com/EggPlanet/egg_models.git/src/model"
  10. md2 "code.fnuoos.com/EggPlanet/egg_system_rules.git/md"
  11. "code.fnuoos.com/EggPlanet/egg_system_rules.git/rule"
  12. "code.fnuoos.com/EggPlanet/egg_system_rules.git/svc"
  13. "errors"
  14. "github.com/google/uuid"
  15. "github.com/shopspring/decimal"
  16. "google.golang.org/protobuf/proto"
  17. "strings"
  18. "time"
  19. )
  20. func DelGrabRedPackage(req *md.GrabRedPackageReq, user *model.User) (resp []byte, grabAmount string, err error) {
  21. var redPacketBalanceAmount, redPacketReceivedUserIds, redPacketReceivedImUserIds, redPackageReceivedUserAmount string
  22. //1、查找红包
  23. redPackageOrdDb := implement.NewImSendRedPackageOrdDb(db.Db)
  24. imSendRedPackageOrd, err := redPackageOrdDb.ImSendRedPackageOrdGetOneByParams(map[string]interface{}{
  25. "key": "id",
  26. "value": req.RedPackageId,
  27. })
  28. if err != nil {
  29. return nil, grabAmount, err
  30. }
  31. if imSendRedPackageOrd == nil {
  32. return nil, grabAmount, errors.New("未查询到对应的红包记录")
  33. }
  34. if imSendRedPackageOrd.RedPacketBalanceNums == 0 || imSendRedPackageOrd.State == int(pb.RedPacketStatusType_RPS_EXPIRE) {
  35. return nil, "-1", nil
  36. }
  37. //2、判断用户是否已领取
  38. receivedUserIds := strings.Split(imSendRedPackageOrd.ReceivedUserIds, ",")
  39. for k, receivedUserId := range receivedUserIds {
  40. if receivedUserId == utils.Int64ToStr(user.Id) {
  41. redPackageReceivedUserAmounts := strings.Split(imSendRedPackageOrd.ReceivedUserAmount, ",")
  42. grabAmount = redPackageReceivedUserAmounts[k]
  43. return nil, grabAmount, err
  44. }
  45. }
  46. //3、判断红包类型
  47. if imSendRedPackageOrd.RedPacketType == int(pb.RedPacketType_RPT_FRIEND) {
  48. //好友红包
  49. grabAmount = imSendRedPackageOrd.Amount
  50. redPacketBalanceAmount = "0"
  51. redPacketReceivedImUserIds = req.UserId
  52. redPacketReceivedUserIds = utils.Int64ToStr(user.Id)
  53. redPackageReceivedUserAmount = imSendRedPackageOrd.Amount
  54. } else {
  55. var receivedImUserIds, receivedUserIds, waitDrawImUserIds, redPackageReceivedUserAmounts []string
  56. if imSendRedPackageOrd.ReceivedImUserIds != "" {
  57. receivedImUserIds = strings.Split(imSendRedPackageOrd.ReceivedImUserIds, ",")
  58. }
  59. if imSendRedPackageOrd.ReceivedUserIds != "" {
  60. receivedUserIds = strings.Split(imSendRedPackageOrd.ReceivedUserIds, ",")
  61. }
  62. if imSendRedPackageOrd.WaitDrawImUserIds != "" {
  63. waitDrawImUserIds = strings.Split(imSendRedPackageOrd.WaitDrawImUserIds, ",")
  64. }
  65. if imSendRedPackageOrd.ReceivedUserAmount != "" {
  66. redPackageReceivedUserAmounts = strings.Split(imSendRedPackageOrd.ReceivedUserAmount, ",")
  67. }
  68. //群组红包
  69. switch imSendRedPackageOrd.RedPacketType {
  70. case int(pb.RedPacketType_RPT_GROUP_NORMAL):
  71. amount, _ := decimal.NewFromString(imSendRedPackageOrd.Amount)
  72. tmpGrabAmount := amount.Div(decimal.NewFromInt(int64(imSendRedPackageOrd.RedPacketNums)))
  73. grabAmount = tmpGrabAmount.String()
  74. tempRedPacketBalanceAmount, _ := decimal.NewFromString(imSendRedPackageOrd.RedPacketBalanceAmount)
  75. redPacketBalanceAmount = tempRedPacketBalanceAmount.Sub(tmpGrabAmount).String()
  76. break
  77. case int(pb.RedPacketType_RPT_GROUP_LUCK):
  78. balanceAmount, err := decimal.NewFromString(imSendRedPackageOrd.RedPacketBalanceAmount)
  79. if err != nil {
  80. return nil, grabAmount, err
  81. }
  82. balanceAmountValue, _ := balanceAmount.Float64()
  83. tmpGrabAmount := float64(utils.DoubleAverage(int64(imSendRedPackageOrd.RedPacketBalanceNums), utils.FloatToInt64(balanceAmountValue*100))) / 100
  84. grabAmount = utils.AnyToString(tmpGrabAmount)
  85. redPacketBalanceAmount = balanceAmount.Sub(decimal.NewFromFloat(tmpGrabAmount)).String()
  86. break
  87. case int(pb.RedPacketType_RPT_GROUP_SPECIALLY):
  88. if !utils.ContainerStr(waitDrawImUserIds, req.UserId) {
  89. return nil, grabAmount, errors.New("非专属用户不能领取")
  90. }
  91. grabAmount = imSendRedPackageOrd.Amount
  92. redPacketBalanceAmount = "0"
  93. redPacketReceivedImUserIds = req.UserId
  94. break
  95. case int(pb.RedPacketType_RPT_SYSTEM_FOR):
  96. if !utils.ContainerStr(waitDrawImUserIds, req.UserId) {
  97. return nil, grabAmount, errors.New("非专属用户不能领取")
  98. }
  99. grabAmount = imSendRedPackageOrd.Amount
  100. redPacketBalanceAmount = "0"
  101. redPacketReceivedImUserIds = req.UserId
  102. break
  103. }
  104. receivedImUserIds = append(receivedImUserIds, req.UserId)
  105. redPacketReceivedImUserIds = strings.Join(receivedImUserIds, ",")
  106. receivedUserIds = append(receivedUserIds, utils.AnyToString(user.Id))
  107. redPacketReceivedUserIds = strings.Join(receivedUserIds, ",")
  108. redPackageReceivedUserAmounts = append(redPackageReceivedUserAmounts, grabAmount)
  109. redPackageReceivedUserAmount = strings.Join(redPackageReceivedUserAmounts, ",")
  110. }
  111. session := db.Db.NewSession()
  112. defer session.Close()
  113. //2、改变红包记录状态
  114. if imSendRedPackageOrd.ReceivedTimes != "" {
  115. receivedTimes := strings.Split(imSendRedPackageOrd.ReceivedTimes, ",")
  116. receivedTimes = append(receivedTimes, time.Now().Format("2006-01-02 15:04:05"))
  117. imSendRedPackageOrd.ReceivedTimes = strings.Join(receivedTimes, ",")
  118. } else {
  119. imSendRedPackageOrd.ReceivedTimes = time.Now().Format("2006-01-02 15:04:05")
  120. }
  121. imSendRedPackageOrd.RedPacketBalanceNums = imSendRedPackageOrd.RedPacketBalanceNums - 1
  122. if imSendRedPackageOrd.RedPacketBalanceNums > 0 {
  123. imSendRedPackageOrd.State = int(pb.RedPacketStatusType_RPS_DRAWING)
  124. } else {
  125. imSendRedPackageOrd.State = int(pb.RedPacketStatusType_RPS_DRAW_OVER)
  126. }
  127. imSendRedPackageOrd.ReceivedUserAmount = redPackageReceivedUserAmount
  128. imSendRedPackageOrd.RedPacketBalanceAmount = redPacketBalanceAmount
  129. imSendRedPackageOrd.ReceivedUserIds = redPacketReceivedUserIds
  130. imSendRedPackageOrd.ReceivedImUserIds = redPacketReceivedImUserIds
  131. _, err = redPackageOrdDb.ImSendRedPackageOrdUpdateBySession(session, imSendRedPackageOrd.Id, imSendRedPackageOrd, "red_packet_balance_nums", "state", "received_user_ids", "received_im_user_ids", "received_times",
  132. "received_user_amount", "create_time", "update_time", "red_packet_balance_amount")
  133. if err != nil {
  134. session.Rollback()
  135. return nil, grabAmount, err
  136. }
  137. //3、加上给用户加上余额
  138. dealUserWalletReq := md2.DealUserWalletReq{
  139. Direction: "add",
  140. Kind: 5,
  141. Title: "领取红包收益",
  142. Uid: user.Id,
  143. Amount: utils.StrToFloat64(grabAmount),
  144. }
  145. err = rule.DealUserWallet(session, dealUserWalletReq)
  146. if err != nil {
  147. session.Rollback()
  148. return nil, grabAmount, err
  149. }
  150. //4、组装消息参数
  151. msg := &pb.RED_PACKAGE{
  152. RedMessageType: pb.RedPacketMessageType_RMT_GRAB,
  153. RedPacketType: pb.RedPacketType(imSendRedPackageOrd.RedPacketType),
  154. RedPacketContent: "",
  155. //RedPacketAmount: float32(utils.AnyToFloat64(req.Amount)),
  156. //RedPacketNums: int32(req.RedPacketNums),
  157. RedPacketBalanceAmount: float32(utils.AnyToFloat64(imSendRedPackageOrd.RedPacketBalanceAmount)),
  158. ReceivedUserId: utils.StrToInt64(req.UserId),
  159. ReceivedUserAmount: utils.StrToFloat32(grabAmount),
  160. ReceivedUserNickname: user.Nickname,
  161. RedPacketStatusType: pb.RedPacketStatusType(imSendRedPackageOrd.State),
  162. RedPacketId: imSendRedPackageOrd.Id,
  163. SendRedPacketUid: int64(imSendRedPackageOrd.ImUid),
  164. SendRedPacketNickname: req.SendRedPackageUserNikeName,
  165. SendRedPacketAvatarUrl: req.SendRedPacketAvatarUrl,
  166. //RedPacketSmallContent: req.RedPacketSmallContent,
  167. RedPacketCover: req.RedPackageCover,
  168. }
  169. utils.FilePutContents("DelGrabRedPackage", utils.SerializeStr(map[string]interface{}{
  170. "args": req,
  171. "msg": msg,
  172. "ord": imSendRedPackageOrd,
  173. }))
  174. resp, err = proto.Marshal(msg)
  175. if err != nil {
  176. return nil, grabAmount, err
  177. }
  178. return resp, grabAmount, nil
  179. }
  180. func DetailGrabRedPackage(redPackageId string) (resp []md.RedPackageDetailUserNode, imSendRedPackageOrd *model.ImSendRedPackageOrd, err error) {
  181. //1、查找红包
  182. redPackageOrdDb := implement.NewImSendRedPackageOrdDb(db.Db)
  183. imSendRedPackageOrd, err = redPackageOrdDb.ImSendRedPackageOrdGetOneByParams(map[string]interface{}{
  184. "key": "id",
  185. "value": redPackageId,
  186. })
  187. if err != nil {
  188. return nil, nil, err
  189. }
  190. if imSendRedPackageOrd == nil {
  191. return resp, imSendRedPackageOrd, errors.New("未查询到对应的红包记录")
  192. }
  193. receivedUserIds := strings.Split(imSendRedPackageOrd.ReceivedUserIds, ",")
  194. userDb := implement.NewUserDb(db.Db)
  195. users, err := userDb.UserFindByParams(map[string]interface{}{
  196. "key": "id",
  197. "value": receivedUserIds,
  198. })
  199. if err != nil {
  200. return resp, imSendRedPackageOrd, err
  201. }
  202. var usersMap = map[string]model.User{}
  203. for _, v := range users {
  204. usersMap[utils.AnyToString(v.Id)] = v
  205. }
  206. receivedImUserIds := strings.Split(imSendRedPackageOrd.ReceivedImUserIds, ",")
  207. receivedTimes := strings.Split(imSendRedPackageOrd.ReceivedTimes, ",")
  208. receivedUserAmount := strings.Split(imSendRedPackageOrd.ReceivedUserAmount, ",")
  209. for k, v := range receivedUserIds {
  210. resp = append(resp, md.RedPackageDetailUserNode{
  211. ImUserId: receivedImUserIds[k],
  212. UserNickName: usersMap[v].Nickname,
  213. UserAvatarUrl: usersMap[v].Avatar,
  214. Amount: receivedUserAmount[k],
  215. ReceiveAt: receivedTimes[k],
  216. })
  217. }
  218. return resp, imSendRedPackageOrd, nil
  219. }
  220. // BalancePayForRedPackage 红包余额支付
  221. func BalancePayForRedPackage(user *model.User, money string, req md.SendRedPackageReq) (resp []byte, redPackageId int64, err error) {
  222. session := db.Db.NewSession()
  223. defer session.Close()
  224. //扣除用户余额
  225. dealUserWalletReq := md2.DealUserWalletReq{
  226. Direction: "sub",
  227. Kind: 6,
  228. Title: "领取红包收益",
  229. Uid: user.Id,
  230. Amount: utils.StrToFloat64(money),
  231. }
  232. err = rule.DealUserWallet(session, dealUserWalletReq)
  233. if err != nil {
  234. session.Rollback()
  235. return nil, 0, err
  236. }
  237. // 新增红包记录
  238. now := time.Now()
  239. ordNo := uuid.New().String()
  240. var waitDrawUserIds string
  241. if req.RedPacketType == int(pb.RedPacketType_RPT_GROUP_SPECIALLY) {
  242. var toUserIds []string
  243. for _, v := range req.ToUserIds {
  244. toUserIds = append(toUserIds, utils.AnyToString(v))
  245. }
  246. waitDrawUserIds = strings.Join(toUserIds, ",")
  247. }
  248. ordDb := implement.NewImSendRedPackageOrdDb(db.Db)
  249. m := model.ImSendRedPackageOrd{
  250. OrdNo: ordNo,
  251. Uid: utils.StrToInt(req.UserId),
  252. Amount: req.Amount,
  253. RedPacketBalanceAmount: req.Amount,
  254. RedPacketType: req.RedPacketType,
  255. RedPacketNums: req.RedPacketNums,
  256. RedPacketBalanceNums: req.RedPacketNums,
  257. State: 0,
  258. WaitDrawUserIds: waitDrawUserIds,
  259. CreateTime: now.Format("2006-01-02 15:04:05"),
  260. UpdateTime: now.Format("2006-01-02 15:04:05"),
  261. }
  262. redPackageOrdId, err := ordDb.ImSendRedPackageOrdInsertBySession(session, &m)
  263. if err != nil {
  264. session.Rollback()
  265. return nil, redPackageId, err
  266. }
  267. err = session.Commit()
  268. if err != nil {
  269. return nil, redPackageOrdId, err
  270. }
  271. redisConn := cache.GetPool().Get()
  272. scheme, domain := svc.ImageBucket(db.Db, redisConn)
  273. msg := &pb.RED_PACKAGE{
  274. RedMessageType: pb.RedPacketMessageType_RMT_SEND,
  275. RedPacketType: pb.RedPacketType(req.RedPacketType),
  276. RedPacketContent: req.RedPacketContent,
  277. RedPacketAmount: float32(utils.AnyToFloat64(req.Amount)),
  278. RedPacketNums: int32(req.RedPacketNums),
  279. RedPacketBalanceAmount: float32(utils.AnyToFloat64(req.Amount)),
  280. ReceivedUserId: utils.StrToInt64(waitDrawUserIds),
  281. ReceivedUserAmount: 0,
  282. ReceivedUserNickname: "",
  283. RedPacketStatusType: 0,
  284. RedPacketId: redPackageOrdId,
  285. SendRedPacketUid: utils.StrToInt64(req.UserId),
  286. SendRedPacketNickname: user.Nickname,
  287. SendRedPacketAvatarUrl: svc.ImageFormatWithBucket(scheme, domain, user.Avatar),
  288. RedPacketSmallContent: req.RedPacketSmallContent,
  289. }
  290. resp, err = proto.Marshal(msg)
  291. if err != nil {
  292. return nil, redPackageId, err
  293. }
  294. redPackageId = redPackageOrdId
  295. return resp, redPackageId, nil
  296. }