Commit c9d2b270 authored by chenweijian's avatar chenweijian

cp邀请

parent d3cfb084
...@@ -2,7 +2,6 @@ package cp_m ...@@ -2,7 +2,6 @@ package cp_m
import ( import (
"git.hilo.cn/hilo-common/domain" "git.hilo.cn/hilo-common/domain"
"git.hilo.cn/hilo-common/resource/mysql"
"gorm.io/gorm" "gorm.io/gorm"
"hilo-user/_const/enum/cp_e" "hilo-user/_const/enum/cp_e"
"hilo-user/myerr/bizerr" "hilo-user/myerr/bizerr"
...@@ -21,7 +20,16 @@ type CpInvite struct { ...@@ -21,7 +20,16 @@ type CpInvite struct {
UserId uint64 `json:"userId"` UserId uint64 `json:"userId"`
InviteUserId uint64 `json:"inviteUserId"` InviteUserId uint64 `json:"inviteUserId"`
DiamondNum uint32 `json:"diamondNum"` DiamondNum uint32 `json:"diamondNum"`
Status mysql.Type `json:"status"` 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 { func CreateCp(model *domain.Model, userId1, userId2 uint64) error {
...@@ -32,6 +40,7 @@ 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 return result.Error
} }
if result.RowsAffected <= 0 { if result.RowsAffected <= 0 {
model.Log.Errorf("CreateCp user1:%d, user2:%d, err:%v", userId1, userId2, bizerr.TransactionFailed)
return bizerr.TransactionFailed return bizerr.TransactionFailed
} }
return nil return nil
...@@ -50,11 +59,39 @@ func GetCp(model *domain.Model, userId uint64) (*CpRelation, error) { ...@@ -50,11 +59,39 @@ func GetCp(model *domain.Model, userId uint64) (*CpRelation, error) {
return res, nil return res, nil
} }
func CreateCpInvite(model *domain.Model, userId, userIdInvite uint64, diamondNum uint32) error { func GetCpInvite(model *domain.Model, userId, userIdInvite uint64) (*CpInvite, error) {
err := model.DB().Model(CpInvite{}).Create(CpInvite{UserId: userId, InviteUserId: userIdInvite, DiamondNum: diamondNum, Status: mysql.Type(cp_e.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 { if err != nil {
model.Log.Errorf("CreateCpInvite user1:%d, user2:%d, diamondNum:%d, err:%v", userId, userIdInvite, diamondNum, err) 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 return nil
} }
...@@ -2,12 +2,14 @@ package cp_r ...@@ -2,12 +2,14 @@ package cp_r
import ( import (
"encoding/json" "encoding/json"
"fmt"
"git.hilo.cn/hilo-common/_const/enum/diamond_e" "git.hilo.cn/hilo-common/_const/enum/diamond_e"
"git.hilo.cn/hilo-common/domain" "git.hilo.cn/hilo-common/domain"
"git.hilo.cn/hilo-common/mycontext" "git.hilo.cn/hilo-common/mycontext"
"git.hilo.cn/hilo-common/myerr/comerr" "git.hilo.cn/hilo-common/myerr/comerr"
"git.hilo.cn/hilo-common/rpc" "git.hilo.cn/hilo-common/rpc"
"git.hilo.cn/hilo-common/sdk/tencentyun" "git.hilo.cn/hilo-common/sdk/tencentyun"
"git.hilo.cn/hilo-common/txop/bag_tx"
"git.hilo.cn/hilo-common/txop/msg" "git.hilo.cn/hilo-common/txop/msg"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"hilo-user/_const/enum/cp_e" "hilo-user/_const/enum/cp_e"
...@@ -15,8 +17,10 @@ import ( ...@@ -15,8 +17,10 @@ import (
"hilo-user/domain/model/cp_m" "hilo-user/domain/model/cp_m"
"hilo-user/domain/model/diamond_m" "hilo-user/domain/model/diamond_m"
"hilo-user/domain/model/user_m" "hilo-user/domain/model/user_m"
"hilo-user/myerr/bizerr"
"hilo-user/req" "hilo-user/req"
"hilo-user/resp" "hilo-user/resp"
"strconv"
) )
// @Tags cp关系 // @Tags cp关系
...@@ -58,7 +62,7 @@ func CheckUserCpRelation(c *gin.Context) (*mycontext.MyContext, error) { ...@@ -58,7 +62,7 @@ func CheckUserCpRelation(c *gin.Context) (*mycontext.MyContext, error) {
// @Router /v2/cp/relation/invite [post] // @Router /v2/cp/relation/invite [post]
func InviteCpRelation(c *gin.Context) (*mycontext.MyContext, error) { func InviteCpRelation(c *gin.Context) (*mycontext.MyContext, error) {
myCtx := mycontext.CreateMyContext(c.Keys) myCtx := mycontext.CreateMyContext(c.Keys)
userCode := c.Query("code") userCode := c.PostForm("code")
myUserId, lang, err := req.GetUserIdLang(c, myCtx) myUserId, lang, err := req.GetUserIdLang(c, myCtx)
if err != nil { if err != nil {
...@@ -92,25 +96,23 @@ func InviteCpRelation(c *gin.Context) (*mycontext.MyContext, error) { ...@@ -92,25 +96,23 @@ func InviteCpRelation(c *gin.Context) (*mycontext.MyContext, error) {
return myCtx, msg.GetErrByLanguage(model, 0, lang, comerr.InvalidParameter) return myCtx, msg.GetErrByLanguage(model, 0, lang, comerr.InvalidParameter)
} }
err = model.Transaction(func(model *domain.Model) error { 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 { if err != nil {
model.Log.Errorf("InviteCpRelation myUserId:%d, err:%v", myUserId, err) model.Log.Errorf("InviteCpRelation myUserId:%d, err:%v", myUserId, err)
return 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 { if err != nil {
model.Log.Errorf("InviteCpRelation myUserId:%d, err:%v", myUserId, err) model.Log.Errorf("InviteCpRelation myUserId:%d, err:%v", myUserId, err)
return err return err
} }
// 发送私信 // 发送私信
type CpInviteMessage struct { data, _ := json.Marshal(cp_m.CpInviteMessage{
Identifier string `json:"identifier"`
Msg string `json:"msg"`
}
data, _ := json.Marshal(CpInviteMessage{
Identifier: "CpInviteMessage", Identifier: "CpInviteMessage",
Msg: "Do you want to be CP with me?", 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 { if err := tencentyun.BatchSendCustomMsg(model, 1, user.ExternalId, []string{userInvite.ExternalId}, string(data), "cp邀请"); err != nil {
model.Log.Errorf("BatchSendCustomMsg fail:%v", err) model.Log.Errorf("BatchSendCustomMsg fail:%v", err)
...@@ -128,3 +130,119 @@ func InviteCpRelation(c *gin.Context) (*mycontext.MyContext, error) { ...@@ -128,3 +130,119 @@ func InviteCpRelation(c *gin.Context) (*mycontext.MyContext, error) {
resp.ResponseOk(c, cp_cv.CheckCpRelationRes{}) resp.ResponseOk(c, cp_cv.CheckCpRelationRes{})
return myCtx, nil 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
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment