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

232 lines
9.0 KiB

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