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/sdk/tencentyun" "git.hilo.cn/hilo-common/txop/msg" "github.com/gin-gonic/gin" "hilo-user/_const/enum/cp_e" "hilo-user/cv/cp_cv" "hilo-user/domain/cache/user_c" "hilo-user/domain/model/cp_m" "hilo-user/domain/model/diamond_m" "hilo-user/domain/model/user_m" "hilo-user/domain/service/cp_s" "hilo-user/myerr/bizerr" "hilo-user/req" "hilo-user/resp" "strconv" ) // @Tags cp关系 // @Summary 检查用户是否绑定了cp // @Param externalId query string true "用户的externalId" // @Success 200 {object} cp_cv.CheckCpRelationRes // @Router /v2/cp/relation/check [get] func CheckUserCpRelation(c *gin.Context) (*mycontext.MyContext, error) { myCtx := mycontext.CreateMyContext(c.Keys) externalId := c.Query("externalId") _, lang, err := req.GetUserIdLang(c, myCtx) if err != nil { return myCtx, err } if externalId == "" { return myCtx, bizerr.InvalidParameter } model := domain.CreateModelContext(myCtx) user, err := user_m.GetUserByExtId(model, externalId) if err != nil { return myCtx, err } cp, err := cp_m.GetCp(model, user.ID) if err != nil { return myCtx, err } if cp.Id > 0 { return myCtx, msg.GetErrByLanguage(model, 0, lang, comerr.InvalidParameter) // cwj---- } resp.ResponseOk(c, cp_cv.CheckCpRelationRes{}) return myCtx, nil } // @Tags cp关系 // @Summary 发送cp邀请/发起解除cp // @Param externalId formData string true "对方的externalId" // @Param type formData int true "类型1.发起邀请2.发起解除" // @Success 200 // @Router /v2/cp/relation [post] func CpRelation(c *gin.Context) (*mycontext.MyContext, error) { myCtx := mycontext.CreateMyContext(c.Keys) externalId := c.PostForm("externalId") myUserId, lang, err := req.GetUserIdLang(c, myCtx) if err != nil { return myCtx, err } optType, err := strconv.Atoi(c.PostForm("type")) if err != nil || optType > 2 || optType < 1 { return myCtx, bizerr.InvalidParameter } if optType == 1 { // 邀请 err = cp_s.InviteCpRelation(myCtx, myUserId, externalId, lang) if err != nil { myCtx.Log.Errorf("InviteCpRelation myUserId:%d, err:%v", myUserId, err) return myCtx, err } } else { // 发起解除 err = cp_s.CancelCpRelation(myCtx, myUserId, externalId, lang) if err != nil { myCtx.Log.Errorf("CancelCpRelation myUserId:%d, err:%v", myUserId, err) return myCtx, err } } resp.ResponseOk(c, nil) return myCtx, nil } // @Tags cp关系 // @Summary 回应cp邀请 // @Param externalId formData string true "对方用户的externalId" // @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) externalId := c.PostForm("externalId") 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.GetUserByExtId(model, externalId) if err != nil { return myCtx, err } cpRecord, err := cp_m.GetCpInvite(model, userSender.ID, user.ID, cp_e.CpInvite) 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 } if optType == 1 { // 接受的时候 // 自己是否有cp了 myCp, err := cp_m.GetCp(model, myUserId) if err != nil { return myCtx, err } if 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 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 } // 发放告白礼物 cwj---- //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, nil) return myCtx, nil } // @Tags cp关系 // @Summary 回应cp解除 // @Param externalId formData string true "对方的externalId" // @Param type formData int true "类型1.撤销2.接受" // @Success 200 // @Router /v2/cp/relation/cancel/reply [post] func ReplyCpCancel(c *gin.Context) (*mycontext.MyContext, error) { myCtx := mycontext.CreateMyContext(c.Keys) externalId := c.PostForm("externalId") optType, err := strconv.Atoi(c.PostForm("type")) if err != nil || optType > 2 || optType < 1 { return myCtx, bizerr.InvalidParameter } myUserId, _, err := req.GetUserIdLang(c, myCtx) if err != nil { return myCtx, err } model := domain.CreateModelContext(myCtx) myUser, err := user_m.GetUser(model, myUserId) if err != nil { return myCtx, err } user2, err := user_m.GetUserByExtId(model, externalId) if err != nil { return myCtx, err } cpCancel, err := cp_m.GetCpCancel(model, []uint64{myUser.ID, user2.ID}, cp_e.CpCancel) if err != nil { model.Log.Errorf("ReplyCpCancel myUser:%d, user2:%d, err:%v", myUser.ID, user2.ID, err) return myCtx, err } if cpCancel == nil || cpCancel.Id == 0 { return myCtx, bizerr.InvalidParameter } if optType == 1 { // 撤销,只有自己能撤销自己的申请 if cpCancel.UserId != myUserId { return myCtx, bizerr.InvalidParameter } } else { // 接受,只有对方能接受 if cpCancel.RecUserId != myUserId { return myCtx, bizerr.InvalidParameter } } err = model.Transaction(func(model *domain.Model) error { // 更新邀请状态 updateStatus := cp_e.CpCancelRevoke if optType == 2 { // 接受 updateStatus = cp_e.CpCancelAccept } err = cp_m.UpdateStatusCpCancel(model, cpCancel.Id, updateStatus) if err != nil { model.Log.Errorf("ReplyCpCancel myUser:%d, user2:%d, status:%d, err:%v", myUser.ID, user2.ID, updateStatus, err) return err } var msgData []byte if optType == 1 { // 撤销解除 // 私信 msgData, _ = json.Marshal(cp_m.CpCancelMessage{ Identifier: "CpCancelMessage", Msg: fmt.Sprintf("%s withdrew my application to lift CP", myUser.Nick), Status: 2, }) } else { // 接受解除 // 删除cp关系表的记录 err = cp_m.DelCpRelation(model, myUser.ID, user2.ID) if err != nil { model.Log.Errorf("ReplyCpCancel myUser:%d, user2:%d, status:%d, err:%v", myUser.ID, user2.ID, updateStatus, err) return err } // 私信 msgData, _ = json.Marshal(cp_m.CpCancelMessage{ Identifier: "CpCancelMessage", Msg: "The CP relationship has been unbound, the CP value has been cleared, and the CP privilege has disappeared", Status: 3, }) } if err := tencentyun.BatchSendCustomMsg(model, 1, myUser.ExternalId, []string{user2.ExternalId}, string(msgData), "cp解除"); err != nil { model.Log.Errorf("ReplyCpCancel BatchSendCustomMsg fail:%v", err) return err } return nil }) if err != nil { model.Log.Errorf("ReplyCpCancel myUserId:%d, err:%v", myUserId, err) return myCtx, err } resp.ResponseOk(c, cp_cv.CheckCpRelationRes{}) return myCtx, nil } type CpDetail struct { Avatar1 string `json:"avatar1"` Avatar2 string `json:"avatar2"` Nick1 string `json:"nick1"` Nick2 string `json:"nick2"` Grade int `json:"grade"` // 等级 GradeName string `json:"gradeName"` // 等级称号 CreatedTime int64 `json:"createdTime"` // cp创建时间 } // @Tags cp关系 // @Summary 详情页cp数据 // @Param externalId query string true "用户的externalId" // @Success 200 {object} CpDetail // @Router /v2/cp/relation/detail [get] func CpDetailPage(c *gin.Context) (*mycontext.MyContext, error) { myCtx := mycontext.CreateMyContext(c.Keys) externalId := c.Query("externalId") model := domain.CreateModelContext(myCtx) user, err := user_m.GetUserByExtId(model, externalId) if err != nil { return myCtx, err } cp, err := cp_m.GetCp(model, user.ID) if err != nil { return myCtx, err } var res *CpDetail if cp.Id > 0 { res = new(CpDetail) if cp.UserId1 == user.ID { res.Avatar1 = user.Avatar res.Nick1 = user.Nick user2, err := user_c.GetUserTinyById(model, cp.UserId2) if err != nil { return myCtx, err } res.Avatar2 = user2.Avatar res.Nick2 = user2.Nick res.CreatedTime = cp.CreatedTime.Unix() } } resp.ResponseOk(c, res) return myCtx, nil }