cp_relation.go 9.08 KB
Newer Older
chenweijian's avatar
chenweijian committed
1 2 3 4
package cp_r

import (
	"encoding/json"
chenweijian's avatar
chenweijian committed
5
	"fmt"
chenweijian's avatar
chenweijian committed
6 7 8 9 10 11 12 13 14 15 16 17
	"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/model/cp_m"
	"hilo-user/domain/model/diamond_m"
	"hilo-user/domain/model/user_m"
chenweijian's avatar
chenweijian committed
18
	"hilo-user/domain/service/cp_s"
chenweijian's avatar
chenweijian committed
19
	"hilo-user/myerr/bizerr"
chenweijian's avatar
chenweijian committed
20 21
	"hilo-user/req"
	"hilo-user/resp"
chenweijian's avatar
chenweijian committed
22
	"strconv"
chenweijian's avatar
chenweijian committed
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
)

// @Tags cp关系
// @Summary 检查用户是否绑定了cp
// @Param code query int true "用户code"
// @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)
	userCode := c.Query("code")

	_, lang, err := req.GetUserIdLang(c, myCtx)
	if err != nil {
		return myCtx, err
	}

	model := domain.CreateModelContext(myCtx)
	user, err := user_m.GetUserByCode(model, userCode)
	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)
	}

	resp.ResponseOk(c, cp_cv.CheckCpRelationRes{})
	return myCtx, nil
}

// @Tags cp关系
chenweijian's avatar
chenweijian committed
58 59 60
// @Summary 发送cp邀请/发起解除cp
// @Param code formData int true "对方用户code"
// @Param type formData int true "类型1.发起邀请2.发起解除"
chenweijian's avatar
chenweijian committed
61
// @Success 200
chenweijian's avatar
chenweijian committed
62 63
// @Router /v2/cp/relation [post]
func CpRelation(c *gin.Context) (*mycontext.MyContext, error) {
chenweijian's avatar
chenweijian committed
64
	myCtx := mycontext.CreateMyContext(c.Keys)
chenweijian's avatar
chenweijian committed
65
	userCode := c.PostForm("code")
chenweijian's avatar
chenweijian committed
66 67 68 69 70 71

	myUserId, lang, err := req.GetUserIdLang(c, myCtx)
	if err != nil {
		return myCtx, err
	}

chenweijian's avatar
chenweijian committed
72 73 74
	optType, err := strconv.Atoi(c.PostForm("type"))
	if err != nil || optType > 2 || optType < 1 {
		return myCtx, bizerr.InvalidParameter
chenweijian's avatar
chenweijian committed
75 76
	}

chenweijian's avatar
chenweijian committed
77 78
	if optType == 1 { // 邀请
		err = cp_s.InviteCpRelation(myCtx, myUserId, userCode, lang)
chenweijian's avatar
chenweijian committed
79
		if err != nil {
chenweijian's avatar
chenweijian committed
80 81
			myCtx.Log.Errorf("InviteCpRelation myUserId:%d, err:%v", myUserId, err)
			return myCtx, err
chenweijian's avatar
chenweijian committed
82
		}
chenweijian's avatar
chenweijian committed
83 84 85
	} else {
		// 发起解除
		err = cp_s.CancelCpRelation(myCtx, myUserId, userCode, lang)
chenweijian's avatar
chenweijian committed
86
		if err != nil {
chenweijian's avatar
chenweijian committed
87 88
			myCtx.Log.Errorf("CancelCpRelation myUserId:%d, err:%v", myUserId, err)
			return myCtx, err
chenweijian's avatar
chenweijian committed
89
		}
chenweijian's avatar
chenweijian committed
90 91
	}

chenweijian's avatar
chenweijian committed
92
	resp.ResponseOk(c, nil)
chenweijian's avatar
chenweijian committed
93 94
	return myCtx, nil
}
chenweijian's avatar
chenweijian committed
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124

// @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
	}

chenweijian's avatar
chenweijian committed
125
	cpRecord, err := cp_m.GetCpInvite(model, userSender.ID, user.ID, cp_e.CpInvite)
chenweijian's avatar
chenweijian committed
126 127 128 129 130 131 132 133
	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
	}

chenweijian's avatar
chenweijian committed
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
	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)
		}
chenweijian's avatar
chenweijian committed
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
	}
	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
			}
chenweijian's avatar
chenweijian committed
172 173 174 175 176
			// 发放告白礼物 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
			//}
chenweijian's avatar
chenweijian committed
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
			// 私信
			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
}
chenweijian's avatar
chenweijian committed
213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309

// @Tags cp关系
// @Summary 回应cp解除
// @Param code formData int true "对方的用户code"
// @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)
	userCode := c.PostForm("code")
	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.GetUserByCode(model, userCode)
	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
}