From c9d2b27028dcddde9705c71f3a9d3a72fbb08f18 Mon Sep 17 00:00:00 2001 From: chenweijian <820961417@qq.com> Date: Mon, 29 May 2023 17:16:45 +0800 Subject: [PATCH] =?UTF-8?q?cp=E9=82=80=E8=AF=B7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- domain/model/cp_m/cp_relation.go | 55 +++++++++++-- route/cp_r/cp_relation.go | 136 +++++++++++++++++++++++++++++-- 2 files changed, 173 insertions(+), 18 deletions(-) diff --git a/domain/model/cp_m/cp_relation.go b/domain/model/cp_m/cp_relation.go index 2a137e9..3114635 100644 --- a/domain/model/cp_m/cp_relation.go +++ b/domain/model/cp_m/cp_relation.go @@ -2,7 +2,6 @@ package cp_m import ( "git.hilo.cn/hilo-common/domain" - "git.hilo.cn/hilo-common/resource/mysql" "gorm.io/gorm" "hilo-user/_const/enum/cp_e" "hilo-user/myerr/bizerr" @@ -17,11 +16,20 @@ type CpRelation struct { } type CpInvite struct { - Id uint64 `json:"id"` - UserId uint64 `json:"userId"` - InviteUserId uint64 `json:"inviteUserId"` - DiamondNum uint32 `json:"diamondNum"` - Status mysql.Type `json:"status"` + Id uint64 `json:"id"` + UserId uint64 `json:"userId"` + InviteUserId uint64 `json:"inviteUserId"` + DiamondNum uint32 `json:"diamondNum"` + Status cp_e.CpInviteStatus `json:"status"` +} + +// 发送私信 +type CpInviteMessage struct { + Identifier string `json:"identifier"` + Msg string `json:"msg"` + Status uint8 `json:"status"` //1.发起邀请2.接受3.拒接 + Avatar1 string `json:"avatar1"` + Avatar2 string `json:"avatar2"` } func CreateCp(model *domain.Model, userId1, userId2 uint64) error { @@ -32,6 +40,7 @@ func CreateCp(model *domain.Model, userId1, userId2 uint64) error { return result.Error } if result.RowsAffected <= 0 { + model.Log.Errorf("CreateCp user1:%d, user2:%d, err:%v", userId1, userId2, bizerr.TransactionFailed) return bizerr.TransactionFailed } return nil @@ -50,11 +59,39 @@ func GetCp(model *domain.Model, userId uint64) (*CpRelation, error) { return res, nil } -func CreateCpInvite(model *domain.Model, userId, userIdInvite uint64, diamondNum uint32) error { - err := model.DB().Model(CpInvite{}).Create(CpInvite{UserId: userId, InviteUserId: userIdInvite, DiamondNum: diamondNum, Status: mysql.Type(cp_e.CpInvite)}).Error +func GetCpInvite(model *domain.Model, userId, userIdInvite uint64) (*CpInvite, error) { + res := new(CpInvite) + err := model.DB().Model(CpInvite{}).Where(CpInvite{UserId: userId, InviteUserId: userIdInvite}).First(&res).Error + if err != nil { + if err == gorm.ErrRecordNotFound { + return nil, nil + } + model.Log.Errorf("GetCpInvite user1:%d, user2:%d, err:%v", userId, userIdInvite, err) + return nil, err + } + return res, nil +} + +func CreateCpInvite(model *domain.Model, userId, userIdInvite uint64, diamondNum uint32) (uint64, error) { + cpInvite := CpInvite{UserId: userId, InviteUserId: userIdInvite, DiamondNum: diamondNum, Status: cp_e.CpInvite} + err := model.DB().Model(CpInvite{}).Create(cpInvite).Error if err != nil { model.Log.Errorf("CreateCpInvite user1:%d, user2:%d, diamondNum:%d, err:%v", userId, userIdInvite, diamondNum, err) - return err + return 0, err + } + return cpInvite.Id, nil +} + +// userId:发起邀请者 +func UpdateStatusCpInvite(model *domain.Model, id uint64, status cp_e.CpInviteStatus) error { + result := model.DB().Exec("update cp_invite set status=? where id=? and status=? limit 1", status, id, cp_e.CpInvite) + if result.Error != nil { + model.Log.Errorf("UpdateStatusCpInvite id:%d, status:%d, err:%v", id, status, result.Error) + return result.Error + } + if result.RowsAffected <= 0 { + model.Log.Errorf("UpdateStatusCpInvite id:%d, status:%d, err:%v", id, status, bizerr.TransactionFailed) + return bizerr.TransactionFailed } return nil } diff --git a/route/cp_r/cp_relation.go b/route/cp_r/cp_relation.go index a7a2e97..09ef9ed 100644 --- a/route/cp_r/cp_relation.go +++ b/route/cp_r/cp_relation.go @@ -2,12 +2,14 @@ package cp_r import ( "encoding/json" + "fmt" "git.hilo.cn/hilo-common/_const/enum/diamond_e" "git.hilo.cn/hilo-common/domain" "git.hilo.cn/hilo-common/mycontext" "git.hilo.cn/hilo-common/myerr/comerr" "git.hilo.cn/hilo-common/rpc" "git.hilo.cn/hilo-common/sdk/tencentyun" + "git.hilo.cn/hilo-common/txop/bag_tx" "git.hilo.cn/hilo-common/txop/msg" "github.com/gin-gonic/gin" "hilo-user/_const/enum/cp_e" @@ -15,8 +17,10 @@ import ( "hilo-user/domain/model/cp_m" "hilo-user/domain/model/diamond_m" "hilo-user/domain/model/user_m" + "hilo-user/myerr/bizerr" "hilo-user/req" "hilo-user/resp" + "strconv" ) // @Tags cp关系 @@ -58,7 +62,7 @@ func CheckUserCpRelation(c *gin.Context) (*mycontext.MyContext, error) { // @Router /v2/cp/relation/invite [post] func InviteCpRelation(c *gin.Context) (*mycontext.MyContext, error) { myCtx := mycontext.CreateMyContext(c.Keys) - userCode := c.Query("code") + userCode := c.PostForm("code") myUserId, lang, err := req.GetUserIdLang(c, myCtx) if err != nil { @@ -92,25 +96,23 @@ func InviteCpRelation(c *gin.Context) (*mycontext.MyContext, error) { return myCtx, msg.GetErrByLanguage(model, 0, lang, comerr.InvalidParameter) } err = model.Transaction(func(model *domain.Model) error { - // 扣费 - err = diamond_m.ChangeDiamondAccountDetail(model, diamond_e.CpInvite, userInvite.ID, myUserId, cp_e.CpRelationInviteDiamond) + // 创建邀请记录 + cpInvId, err := cp_m.CreateCpInvite(model, myUserId, userInvite.ID, cp_e.CpRelationInviteDiamond) if err != nil { model.Log.Errorf("InviteCpRelation myUserId:%d, err:%v", myUserId, err) return err } - err = cp_m.CreateCpInvite(model, myUserId, userInvite.ID, cp_e.CpRelationInviteDiamond) + // 扣费 + err = diamond_m.ChangeDiamondAccountDetail(model, diamond_e.CpInvite, cpInvId, myUserId, cp_e.CpRelationInviteDiamond) if err != nil { model.Log.Errorf("InviteCpRelation myUserId:%d, err:%v", myUserId, err) return err } // 发送私信 - type CpInviteMessage struct { - Identifier string `json:"identifier"` - Msg string `json:"msg"` - } - data, _ := json.Marshal(CpInviteMessage{ + data, _ := json.Marshal(cp_m.CpInviteMessage{ Identifier: "CpInviteMessage", Msg: "Do you want to be CP with me?", + Status: uint8(cp_e.CpInvite), }) if err := tencentyun.BatchSendCustomMsg(model, 1, user.ExternalId, []string{userInvite.ExternalId}, string(data), "cp邀请"); err != nil { model.Log.Errorf("BatchSendCustomMsg fail:%v", err) @@ -128,3 +130,119 @@ func InviteCpRelation(c *gin.Context) (*mycontext.MyContext, error) { resp.ResponseOk(c, cp_cv.CheckCpRelationRes{}) return myCtx, nil } + +// @Tags cp关系 +// @Summary 回应cp邀请 +// @Param code formData int true "对方的用户code" +// @Param type formData int true "类型1.接受2.拒绝" +// @Success 200 +// @Router /v2/cp/relation/invite/reply [post] +func ReplyCpInvite(c *gin.Context) (*mycontext.MyContext, error) { + myCtx := mycontext.CreateMyContext(c.Keys) + userCode := c.PostForm("code") + optType, err := strconv.Atoi(c.PostForm("type")) + if err != nil || optType > 2 || optType < 1 { + return myCtx, bizerr.InvalidParameter + } + + myUserId, lang, err := req.GetUserIdLang(c, myCtx) + if err != nil { + return myCtx, err + } + + model := domain.CreateModelContext(myCtx) + user, err := user_m.GetUser(model, myUserId) + if err != nil { + return myCtx, err + } + userSender, err := user_m.GetUserByCode(model, userCode) + if err != nil { + return myCtx, err + } + + cpRecord, err := cp_m.GetCpInvite(model, userSender.ID, user.ID) + if err != nil { + model.Log.Errorf("ReplyCpInvite userSender:%d, user:%d, err:%v", userSender.ID, user.ID, err) + return myCtx, err + } + if cpRecord == nil || cpRecord.Id == 0 { + return myCtx, bizerr.InvalidParameter + } + + // 自己是否有cp了 + myCp, err := cp_m.GetCp(model, myUserId) + if err != nil { + return myCtx, err + } + if optType == 1 && myCp.Id > 0 { // 接受的时候 + return myCtx, msg.GetErrByLanguage(model, 0, lang, comerr.InvalidParameter) + } + // 对方是否已经有cp了 + senderCp, err := cp_m.GetCp(model, userSender.ID) + if err != nil { + return myCtx, err + } + if optType == 1 && senderCp.Id > 0 { + return myCtx, msg.GetErrByLanguage(model, 0, lang, comerr.InvalidParameter) + } + err = model.Transaction(func(model *domain.Model) error { + // 更新邀请状态 + updateStatus := cp_e.CpInviteAccept + if optType == 2 { // 拒接 + updateStatus = cp_e.CpInviteRefuse + } + err = cp_m.UpdateStatusCpInvite(model, cpRecord.Id, updateStatus) + if err != nil { + model.Log.Errorf("ReplyCpInvite userSender:%d, user:%d, status:%d, err:%v", userSender.ID, user.ID, updateStatus, err) + return err + } + + var msgData []byte + if optType == 1 { // 接受 + // 写入cp关系表 + err = cp_m.CreateCp(model, userSender.ID, user.ID) + if err != nil { + model.Log.Errorf("ReplyCpInvite userSender:%d, user:%d, status:%d, err:%v", userSender.ID, user.ID, updateStatus, err) + return err + } + // 发放告白礼物 + if _, err = bag_tx.SendUserBag(model, userSender.ID, 1, award.GiftId, award.GiftN, 3, "告白礼物"); err != nil { + model.Log.Errorf("ReplyCpInvite userSender:%d, user:%d, status:%d, err:%v", userSender.ID, user.ID, updateStatus, err) + return err + } + // 私信 + msgData, _ = json.Marshal(cp_m.CpInviteMessage{ + Identifier: "CpInviteMessage", + Msg: "We are already CP!", + Status: uint8(cp_e.CpInviteAccept), + Avatar1: userSender.Avatar, + Avatar2: user.Avatar, + }) + } else { // 拒接 + // 退费 + err = diamond_m.ChangeDiamondAccountDetail(model, diamond_e.CpInviteRefund, cpRecord.Id, cpRecord.UserId, cpRecord.DiamondNum) + if err != nil { + model.Log.Errorf("ReplyCpInvite UserId:%d, err:%v", cpRecord.UserId, err) + return err + } + // 私信 + msgData, _ = json.Marshal(cp_m.CpInviteMessage{ + Identifier: "CpInviteMessage", + Msg: fmt.Sprintf("%s have declined the CP invitation", user.Nick), + Status: uint8(cp_e.CpInviteRefuse), + }) + } + if err := tencentyun.BatchSendCustomMsg(model, 1, userSender.ExternalId, []string{user.ExternalId}, string(msgData), "cp邀请"); err != nil { + model.Log.Errorf("BatchSendCustomMsg fail:%v", err) + return err + } + return nil + }) + if err != nil { + model.Log.Errorf("ReplyCpInvite myUserId:%d, err:%v", myUserId, err) + return myCtx, err + } + + resp.ResponseOk(c, cp_cv.CheckCpRelationRes{}) + return myCtx, nil +} -- 2.22.0