package hdl import ( "applet/app/cfg" "applet/app/db" "applet/app/e" "applet/app/md" "applet/app/pkg/pb" "applet/app/svc" "applet/app/utils" "code.fnuoos.com/EggPlanet/egg_models.git/src/implement" "code.fnuoos.com/EggPlanet/egg_models.git/src/model" "github.com/gin-gonic/gin" "github.com/tidwall/gjson" ) // SendRedPackageDetail // @Summary 蛋蛋星球-即时通讯-发送红包详情 // @Tags 即时通讯 // @Description 发送红包详情 // @Accept json // @Produce json // @param Authorization header string true "验证参数Bearer和token空格拼接" // @Param req body md.SendRedPackageDetailReq true "红包的订单 ID" // @Success 200 {object} md.SendRedPackageDetailResp "具体数据" // @Failure 400 {object} md.Response "具体错误" // @Router /api/v1/im/user/sendRedPackageDetail [POST] func SendRedPackageDetail(c *gin.Context) { var req *md.SendRedPackageDetailReq if err1 := c.ShouldBindJSON(&req); err1 != nil { e.OutErr(c, e.ERR_INVALID_ARGS, err1.Error()) return } //1、查找红包 redPackageOrdDb := implement.NewImSendRedPackageOrdDb(db.Db) imSendRedPackageOrd, err := redPackageOrdDb.ImSendRedPackageOrdGetOneByParams(map[string]interface{}{ "key": "ord_no", "value": req.OrderId, }) if err != nil { e.OutErr(c, e.ERR_DB_ORM, nil) return } var redPackageID int64 im := md.SeqType{ Seq: 0, } if imSendRedPackageOrd != nil { im.Seq = int(gjson.Get(imSendRedPackageOrd.ImData, "seq").Int()) redPackageID = imSendRedPackageOrd.Id } resp := md.SendRedPackageDetailResp{ Im: im, RedPackageID: redPackageID, } e.OutSuc(c, resp, nil) return } // GrabRedPackage // @Summary 蛋蛋星球-即时通讯-领取红包 // @Tags 即时通讯 // @Description 领取红包 // @Accept json // @Produce json // @param Authorization header string true "验证参数Bearer和token空格拼接" // @Param req body md.GrabRedPackageReq true "领取红包信息" // @Success 200 {object} md.GrabRedPackageResp "具体数据" // @Failure 400 {object} md.Response "具体错误" // @Router /api/v1/im/user/grabRedPackage [POST] func GrabRedPackage(c *gin.Context) { var req *md.GrabRedPackageReq if err1 := c.ShouldBindJSON(&req); err1 != nil { e.OutErr(c, e.ERR_INVALID_ARGS, err1.Error()) return } user := svc.GetUser(c) redPackageContent, grabAmount, err := svc.DelGrabRedPackage(req, user) if err != nil { e.OutErr(c, e.ERR, err.Error()) return } resp := md.GrabRedPackageResp{ Im: &pb.SendRedPacketResp{Seq: 0}, GrabAmount: grabAmount, } if redPackageContent == nil { e.OutSuc(c, resp, nil) return } //TODO::调用im GRPC 接口 res, err := utils.GetLogicExtClient(cfg.ImLogicRpc.URL, cfg.ImLogicRpc.PORT).SendRedPacket(utils.GetCtx(req.Token, req.UserId, req.DeviceId), &pb.SendRedPacketReq{ ReceiverType: pb.ReceiverType(req.ReceiverType), ReceiverId: req.ReceiverId, MessageType: pb.MessageType_MT_RED_PACKAGE, MessageContent: redPackageContent, SendTime: req.SendTime, //ToUserIds: args.ToUserIds, MessageContentBack: "", }) if err != nil { e.OutErr(c, e.ERR_IS_GRPC, err.Error()) return } resp.Im = res e.OutSuc(c, resp, nil) return } // RedPackageDetail // @Summary 蛋蛋星球-即时通讯-红包详情(获取) // @Tags 即时通讯 // @Description 红包详情(获取) // @Accept json // @Produce json // @param Authorization header string true "验证参数Bearer和token空格拼接" // @Param red_package_id query string true "红包 ID" // @Success 200 {object} md.RedPackageDetailResp "具体数据" // @Failure 400 {object} md.Response "具体错误" // @Router /api/v1/im/redPackageDetail [GET] func RedPackageDetail(c *gin.Context) { redPackageId := c.DefaultQuery("red_package_id", "") res, imSendRedPackageOrd, err := svc.DetailGrabRedPackage(redPackageId) if err != nil { e.OutErr(c, e.ERR, err.Error()) return } resp := md.RedPackageDetailResp{ List: res, Detail: model.ImSendRedPackageOrd{ OrdNo: imSendRedPackageOrd.OrdNo, Uid: imSendRedPackageOrd.Uid, ImUid: imSendRedPackageOrd.ImUid, Amount: imSendRedPackageOrd.Amount, RedPacketBalanceAmount: imSendRedPackageOrd.RedPacketBalanceAmount, RedPacketType: imSendRedPackageOrd.RedPacketType, RedPacketNums: imSendRedPackageOrd.RedPacketNums, RedPacketBalanceNums: imSendRedPackageOrd.RedPacketBalanceNums, State: imSendRedPackageOrd.State, WaitDrawUserIds: imSendRedPackageOrd.WaitDrawUserIds, WaitDrawImUserIds: imSendRedPackageOrd.WaitDrawImUserIds, ReceivedUserIds: imSendRedPackageOrd.ReceivedUserIds, ReceivedImUserIds: imSendRedPackageOrd.ReceivedImUserIds, ReceivedUserAmount: imSendRedPackageOrd.ReceivedUserAmount, ReceivedTimes: imSendRedPackageOrd.ReceivedTimes, CreateTime: imSendRedPackageOrd.CreateTime, UpdateTime: imSendRedPackageOrd.UpdateTime, }, } e.OutSuc(c, resp, nil) return } // SendRedPackage // @Summary 蛋蛋星球-即时通讯-发送红包 // @Tags 即时通讯 // @Description 发送红包 // @Accept json // @Produce json // @param Authorization header string true "验证参数Bearer和token空格拼接" // @Param req body md.SendRedPackageReq true "发送红包信息" // @Success 200 {object} md.SendRedPackageResp "具体数据" // @Failure 400 {object} md.Response "具体错误" // @Router /api/v1/im/user/sendRedPackage [POST] func SendRedPackage(c *gin.Context) { var args md.SendRedPackageReq if err := c.ShouldBindJSON(&args); err != nil { e.OutErr(c, e.ERR_INVALID_ARGS, err) return } if args.RedPacketNums <= 0 || utils.StrToFloat64(args.Amount)*100 <= 0 || utils.StrToFloat64(args.Amount)*100 < float64(args.RedPacketNums)*utils.RED_PACKET_MIN_MONEY { e.OutErr(c, e.ERR, "传参有误") return } user := svc.GetUser(c) redPackageContent, redPackageId, err := svc.BalancePayForRedPackage(user, args.Amount, args) if err != nil { e.OutErr(c, e.ERR, err) return } //TODO::调用im GRPC 接口 res, err := utils.GetLogicExtClient(cfg.ImLogicRpc.URL, cfg.ImLogicRpc.PORT).SendRedPacket(utils.GetCtx(args.Token, args.UserId, args.DeviceId), &pb.SendRedPacketReq{ ReceiverType: pb.ReceiverType(args.ReceiverType), ReceiverId: args.ReceiverId, MessageType: pb.MessageType_MT_RED_PACKAGE, MessageContent: redPackageContent, SendTime: args.SendTime, ToUserIds: args.ToUserIds, MessageContentBack: "", }) if err != nil { e.OutErr(c, e.ERR_IS_GRPC, err) return } resp := md.SendRedPackageResp{ Im: res, RedPackageId: redPackageId, } // 处理统计发红包数 推到 mq 处理 go svc.AddSendRedPackageTimesPushToMQ(user.Id) e.OutSuc(c, resp, nil) return }