package svc import ( "applet/app/db" "applet/app/md" "applet/app/pkg/pb" "applet/app/utils" "code.fnuoos.com/EggPlanet/egg_models.git/src/implement" "code.fnuoos.com/EggPlanet/egg_models.git/src/model" md2 "code.fnuoos.com/EggPlanet/egg_system_rules.git/md" "code.fnuoos.com/EggPlanet/egg_system_rules.git/rule" "errors" "github.com/shopspring/decimal" "google.golang.org/protobuf/proto" "strings" "time" ) func DelGrabRedPackage(req *md.GrabRedPackageReq, user *model.User) (resp []byte, grabAmount string, err error) { var redPacketBalanceAmount, redPacketReceivedUserIds, redPacketReceivedImUserIds, redPackageReceivedUserAmount string //1、查找红包 redPackageOrdDb := implement.NewImSendRedPackageOrdDb(db.Db) imSendRedPackageOrd, err := redPackageOrdDb.ImSendRedPackageOrdGetOneByParams(map[string]interface{}{ "key": "id", "value": req.RedPackageId, }) if err != nil { return nil, grabAmount, err } if imSendRedPackageOrd == nil { return nil, grabAmount, errors.New("未查询到对应的红包记录") } if imSendRedPackageOrd.RedPacketBalanceNums == 0 || imSendRedPackageOrd.State == int(pb.RedPacketStatusType_RPS_EXPIRE) { return nil, "-1", nil } //2、判断用户是否已领取 receivedUserIds := strings.Split(imSendRedPackageOrd.ReceivedUserIds, ",") for k, receivedUserId := range receivedUserIds { if receivedUserId == utils.Int64ToStr(user.Id) { redPackageReceivedUserAmounts := strings.Split(imSendRedPackageOrd.ReceivedUserAmount, ",") grabAmount = redPackageReceivedUserAmounts[k] return nil, grabAmount, err } } //3、判断红包类型 if imSendRedPackageOrd.RedPacketType == int(pb.RedPacketType_RPT_FRIEND) { //好友红包 grabAmount = imSendRedPackageOrd.Amount redPacketBalanceAmount = "0" redPacketReceivedImUserIds = req.UserId redPacketReceivedUserIds = utils.Int64ToStr(user.Id) redPackageReceivedUserAmount = imSendRedPackageOrd.Amount } else { var receivedImUserIds, receivedUserIds, waitDrawImUserIds, redPackageReceivedUserAmounts []string if imSendRedPackageOrd.ReceivedImUserIds != "" { receivedImUserIds = strings.Split(imSendRedPackageOrd.ReceivedImUserIds, ",") } if imSendRedPackageOrd.ReceivedUserIds != "" { receivedUserIds = strings.Split(imSendRedPackageOrd.ReceivedUserIds, ",") } if imSendRedPackageOrd.WaitDrawImUserIds != "" { waitDrawImUserIds = strings.Split(imSendRedPackageOrd.WaitDrawImUserIds, ",") } if imSendRedPackageOrd.ReceivedUserAmount != "" { redPackageReceivedUserAmounts = strings.Split(imSendRedPackageOrd.ReceivedUserAmount, ",") } //群组红包 switch imSendRedPackageOrd.RedPacketType { case int(pb.RedPacketType_RPT_GROUP_NORMAL): amount, _ := decimal.NewFromString(imSendRedPackageOrd.Amount) tmpGrabAmount := amount.Div(decimal.NewFromInt(int64(imSendRedPackageOrd.RedPacketNums))) grabAmount = tmpGrabAmount.String() tempRedPacketBalanceAmount, _ := decimal.NewFromString(imSendRedPackageOrd.RedPacketBalanceAmount) redPacketBalanceAmount = tempRedPacketBalanceAmount.Sub(tmpGrabAmount).String() break case int(pb.RedPacketType_RPT_GROUP_LUCK): balanceAmount, err := decimal.NewFromString(imSendRedPackageOrd.RedPacketBalanceAmount) if err != nil { return nil, grabAmount, err } balanceAmountValue, _ := balanceAmount.Float64() tmpGrabAmount := float64(utils.DoubleAverage(int64(imSendRedPackageOrd.RedPacketBalanceNums), utils.FloatToInt64(balanceAmountValue*100))) / 100 grabAmount = utils.AnyToString(tmpGrabAmount) redPacketBalanceAmount = balanceAmount.Sub(decimal.NewFromFloat(tmpGrabAmount)).String() break case int(pb.RedPacketType_RPT_GROUP_SPECIALLY): if !utils.ContainerStr(waitDrawImUserIds, req.UserId) { return nil, grabAmount, errors.New("非专属用户不能领取") } grabAmount = imSendRedPackageOrd.Amount redPacketBalanceAmount = "0" redPacketReceivedImUserIds = req.UserId break case int(pb.RedPacketType_RPT_SYSTEM_FOR): if !utils.ContainerStr(waitDrawImUserIds, req.UserId) { return nil, grabAmount, errors.New("非专属用户不能领取") } grabAmount = imSendRedPackageOrd.Amount redPacketBalanceAmount = "0" redPacketReceivedImUserIds = req.UserId break } receivedImUserIds = append(receivedImUserIds, req.UserId) redPacketReceivedImUserIds = strings.Join(receivedImUserIds, ",") receivedUserIds = append(receivedUserIds, utils.AnyToString(user.Id)) redPacketReceivedUserIds = strings.Join(receivedUserIds, ",") redPackageReceivedUserAmounts = append(redPackageReceivedUserAmounts, grabAmount) redPackageReceivedUserAmount = strings.Join(redPackageReceivedUserAmounts, ",") } session := db.Db.NewSession() defer session.Close() //2、改变红包记录状态 if imSendRedPackageOrd.ReceivedTimes != "" { receivedTimes := strings.Split(imSendRedPackageOrd.ReceivedTimes, ",") receivedTimes = append(receivedTimes, time.Now().Format("2006-01-02 15:04:05")) imSendRedPackageOrd.ReceivedTimes = strings.Join(receivedTimes, ",") } else { imSendRedPackageOrd.ReceivedTimes = time.Now().Format("2006-01-02 15:04:05") } imSendRedPackageOrd.RedPacketBalanceNums = imSendRedPackageOrd.RedPacketBalanceNums - 1 if imSendRedPackageOrd.RedPacketBalanceNums > 0 { imSendRedPackageOrd.State = int(pb.RedPacketStatusType_RPS_DRAWING) } else { imSendRedPackageOrd.State = int(pb.RedPacketStatusType_RPS_DRAW_OVER) } imSendRedPackageOrd.ReceivedUserAmount = redPackageReceivedUserAmount imSendRedPackageOrd.RedPacketBalanceAmount = redPacketBalanceAmount imSendRedPackageOrd.ReceivedUserIds = redPacketReceivedUserIds imSendRedPackageOrd.ReceivedImUserIds = redPacketReceivedImUserIds _, err = redPackageOrdDb.ImSendRedPackageOrdUpdateBySession(session, imSendRedPackageOrd.Id, imSendRedPackageOrd, "red_packet_balance_nums", "state", "received_user_ids", "received_im_user_ids", "received_times", "received_user_amount", "create_time", "update_time", "red_packet_balance_amount") if err != nil { session.Rollback() return nil, grabAmount, err } //3、加上给用户加上余额 dealUserWalletReq := md2.DealUserWalletReq{ Direction: "add", Kind: 5, Title: "领取红包收益", Uid: user.Id, Amount: utils.StrToFloat64(grabAmount), } err = rule.DealUserWallet(session, dealUserWalletReq) if err != nil { session.Rollback() return nil, grabAmount, err } //4、组装消息参数 msg := &pb.RED_PACKAGE{ RedMessageType: pb.RedPacketMessageType_RMT_GRAB, RedPacketType: pb.RedPacketType(imSendRedPackageOrd.RedPacketType), RedPacketContent: "", //RedPacketAmount: float32(utils.AnyToFloat64(req.Amount)), //RedPacketNums: int32(req.RedPacketNums), RedPacketBalanceAmount: float32(utils.AnyToFloat64(imSendRedPackageOrd.RedPacketBalanceAmount)), ReceivedUserId: utils.StrToInt64(req.UserId), ReceivedUserAmount: utils.StrToFloat32(grabAmount), ReceivedUserNickname: user.Nickname, RedPacketStatusType: pb.RedPacketStatusType(imSendRedPackageOrd.State), RedPacketId: imSendRedPackageOrd.Id, SendRedPacketUid: int64(imSendRedPackageOrd.ImUid), SendRedPacketNickname: req.SendRedPackageUserNikeName, SendRedPacketAvatarUrl: req.SendRedPacketAvatarUrl, //RedPacketSmallContent: req.RedPacketSmallContent, RedPacketCover: req.RedPackageCover, } utils.FilePutContents("DelGrabRedPackage", utils.SerializeStr(map[string]interface{}{ "args": req, "msg": msg, "ord": imSendRedPackageOrd, })) resp, err = proto.Marshal(msg) if err != nil { return nil, grabAmount, err } return resp, grabAmount, nil } func DetailGrabRedPackage(redPackageId string) (resp []md.RedPackageDetailUserNode, imSendRedPackageOrd *model.ImSendRedPackageOrd, err error) { //1、查找红包 redPackageOrdDb := implement.NewImSendRedPackageOrdDb(db.Db) imSendRedPackageOrd, err = redPackageOrdDb.ImSendRedPackageOrdGetOneByParams(map[string]interface{}{ "key": "id", "value": redPackageId, }) if err != nil { return nil, nil, err } if imSendRedPackageOrd == nil { return resp, imSendRedPackageOrd, errors.New("未查询到对应的红包记录") } receivedUserIds := strings.Split(imSendRedPackageOrd.ReceivedUserIds, ",") userDb := implement.NewUserDb(db.Db) users, err := userDb.UserFindByParams(map[string]interface{}{ "key": "id", "value": receivedUserIds, }) if err != nil { return resp, imSendRedPackageOrd, err } var usersMap = map[string]model.User{} for _, v := range users { usersMap[utils.AnyToString(v.Id)] = v } receivedImUserIds := strings.Split(imSendRedPackageOrd.ReceivedImUserIds, ",") receivedTimes := strings.Split(imSendRedPackageOrd.ReceivedTimes, ",") receivedUserAmount := strings.Split(imSendRedPackageOrd.ReceivedUserAmount, ",") for k, v := range receivedUserIds { resp = append(resp, md.RedPackageDetailUserNode{ ImUserId: receivedImUserIds[k], UserNickName: usersMap[v].Nickname, UserAvatarUrl: usersMap[v].Avatar, Amount: receivedUserAmount[k], ReceiveAt: receivedTimes[k], }) } return resp, imSendRedPackageOrd, nil }