cp_relation.go 16.3 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
	"git.hilo.cn/hilo-common/_const/common"
chenweijian's avatar
chenweijian committed
7
	"git.hilo.cn/hilo-common/_const/enum/diamond_e"
chenweijian's avatar
chenweijian committed
8
	"git.hilo.cn/hilo-common/_const/enum/msg_e"
chenweijian's avatar
chenweijian committed
9 10 11
	"git.hilo.cn/hilo-common/domain"
	"git.hilo.cn/hilo-common/mycontext"
	"git.hilo.cn/hilo-common/myerr/comerr"
hujiebin's avatar
hujiebin committed
12
	"git.hilo.cn/hilo-common/resource/mysql"
chenweijian's avatar
chenweijian committed
13
	"git.hilo.cn/hilo-common/sdk/tencentyun"
chenweijian's avatar
chenweijian committed
14
	"git.hilo.cn/hilo-common/txop/bag_tx"
chenweijian's avatar
chenweijian committed
15
	"git.hilo.cn/hilo-common/txop/diamond_tx"
chenweijian's avatar
chenweijian committed
16
	"git.hilo.cn/hilo-common/txop/headwear_tx"
chenweijian's avatar
chenweijian committed
17
	"git.hilo.cn/hilo-common/txop/msg"
chenweijian's avatar
chenweijian committed
18
	"git.hilo.cn/hilo-common/utils"
chenweijian's avatar
chenweijian committed
19 20 21
	"github.com/gin-gonic/gin"
	"hilo-user/_const/enum/cp_e"
	"hilo-user/cv/cp_cv"
chenweijian's avatar
chenweijian committed
22
	"hilo-user/cv/user_cv"
chenweijian's avatar
chenweijian committed
23
	"hilo-user/domain/cache/user_c"
chenweijian's avatar
chenweijian committed
24 25
	"hilo-user/domain/model/cp_m"
	"hilo-user/domain/model/user_m"
chenweijian's avatar
chenweijian committed
26
	"hilo-user/domain/service/cp_s"
chenweijian's avatar
chenweijian committed
27
	"hilo-user/myerr"
chenweijian's avatar
chenweijian committed
28
	"hilo-user/myerr/bizerr"
chenweijian's avatar
chenweijian committed
29 30
	"hilo-user/req"
	"hilo-user/resp"
chenweijian's avatar
chenweijian committed
31
	"strconv"
chenweijian's avatar
chenweijian committed
32
	"time"
chenweijian's avatar
chenweijian committed
33 34 35 36
)

// @Tags cp关系
// @Summary 检查用户是否绑定了cp
chenweijian's avatar
chenweijian committed
37
// @Param externalId query string true "用户的externalId"
chenweijian's avatar
chenweijian committed
38 39 40 41
// @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)
chenweijian's avatar
chenweijian committed
42
	externalId := c.Query("externalId")
chenweijian's avatar
chenweijian committed
43 44 45
	if externalId == "" {
		return myCtx, bizerr.InvalidParameter
	}
chenweijian's avatar
chenweijian committed
46 47 48 49 50 51

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

chenweijian's avatar
chenweijian committed
52 53 54 55
	if externalId == "" {
		return myCtx, bizerr.InvalidParameter
	}

chenweijian's avatar
chenweijian committed
56
	model := domain.CreateModelContext(myCtx)
chenweijian's avatar
chenweijian committed
57
	user, err := user_m.GetUserByExtId(model, externalId)
chenweijian's avatar
chenweijian committed
58 59 60 61 62 63 64 65 66
	if err != nil {
		return myCtx, err
	}

	cp, err := cp_m.GetCp(model, user.ID)
	if err != nil {
		return myCtx, err
	}
	if cp.Id > 0 {
chenweijian's avatar
chenweijian committed
67
		return myCtx, myerr.ToLocal(msg.GetErrByLanguage(model, common.MSG_ID_ALREADY_HAS_CP, lang, comerr.AlreadyHasCp))
chenweijian's avatar
chenweijian committed
68 69 70 71
	}
	msg, err := msg.GetResMultiTextBy(model, common.MSG_ID_INVITE_CP, lang)
	if err != nil {
		return myCtx, err
chenweijian's avatar
chenweijian committed
72 73
	}

chenweijian's avatar
chenweijian committed
74
	resp.ResponseOk(c, cp_cv.CheckCpRelationRes{Diamond: cp_e.CpRelationInviteDiamond, Msg: msg})
chenweijian's avatar
chenweijian committed
75 76 77 78
	return myCtx, nil
}

// @Tags cp关系
chenweijian's avatar
chenweijian committed
79
// @Summary 发送cp邀请/发起解除cp
chenweijian's avatar
chenweijian committed
80
// @Param externalId formData string true "对方的externalId"
chenweijian's avatar
chenweijian committed
81
// @Param type formData int true "类型1.发起邀请2.发起解除"
chenweijian's avatar
chenweijian committed
82
// @Success 200
chenweijian's avatar
chenweijian committed
83 84
// @Router /v2/cp/relation [post]
func CpRelation(c *gin.Context) (*mycontext.MyContext, error) {
chenweijian's avatar
chenweijian committed
85
	myCtx := mycontext.CreateMyContext(c.Keys)
chenweijian's avatar
chenweijian committed
86
	externalId := c.PostForm("externalId")
chenweijian's avatar
chenweijian committed
87 88 89
	if externalId == "" {
		return myCtx, bizerr.InvalidParameter
	}
chenweijian's avatar
chenweijian committed
90 91 92 93 94 95

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

chenweijian's avatar
chenweijian committed
96 97 98
	optType, err := strconv.Atoi(c.PostForm("type"))
	if err != nil || optType > 2 || optType < 1 {
		return myCtx, bizerr.InvalidParameter
chenweijian's avatar
chenweijian committed
99 100
	}

chenweijian's avatar
chenweijian committed
101
	if optType == 1 { // 邀请
chenweijian's avatar
chenweijian committed
102
		err = cp_s.InviteCpRelation(myCtx, myUserId, externalId, lang)
chenweijian's avatar
chenweijian committed
103
		if err != nil {
chenweijian's avatar
chenweijian committed
104 105
			myCtx.Log.Errorf("InviteCpRelation myUserId:%d, err:%v", myUserId, err)
			return myCtx, err
chenweijian's avatar
chenweijian committed
106
		}
chenweijian's avatar
chenweijian committed
107 108
	} else {
		// 发起解除
chenweijian's avatar
chenweijian committed
109
		err = cp_s.CancelCpRelation(myCtx, myUserId, externalId, lang)
chenweijian's avatar
chenweijian committed
110
		if err != nil {
chenweijian's avatar
chenweijian committed
111 112
			myCtx.Log.Errorf("CancelCpRelation myUserId:%d, err:%v", myUserId, err)
			return myCtx, err
chenweijian's avatar
chenweijian committed
113
		}
chenweijian's avatar
chenweijian committed
114 115
	}

chenweijian's avatar
chenweijian committed
116
	resp.ResponseOk(c, nil)
chenweijian's avatar
chenweijian committed
117 118
	return myCtx, nil
}
chenweijian's avatar
chenweijian committed
119 120 121

// @Tags cp关系
// @Summary 回应cp邀请
chenweijian's avatar
chenweijian committed
122
// @Param externalId formData string true "对方用户的externalId"
chenweijian's avatar
chenweijian committed
123 124 125 126 127
// @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)
chenweijian's avatar
chenweijian committed
128
	externalId := c.PostForm("externalId")
chenweijian's avatar
chenweijian committed
129
	if externalId == "" {
chenweijian's avatar
chenweijian committed
130
		return myCtx, myerr.WrapErr(bizerr.InvalidParameter)
chenweijian's avatar
chenweijian committed
131
	}
chenweijian's avatar
chenweijian committed
132 133
	optType, err := strconv.Atoi(c.PostForm("type"))
	if err != nil || optType > 2 || optType < 1 {
chenweijian's avatar
chenweijian committed
134
		return myCtx, myerr.WrapErr(bizerr.InvalidParameter)
chenweijian's avatar
chenweijian committed
135 136 137 138 139 140 141 142 143 144 145 146
	}

	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
	}
chenweijian's avatar
chenweijian committed
147
	userSender, err := user_m.GetUserByExtId(model, externalId)
chenweijian's avatar
chenweijian committed
148 149 150
	if err != nil {
		return myCtx, err
	}
chenweijian's avatar
chenweijian committed
151
	if userSender.ID == myUserId {
chenweijian's avatar
chenweijian committed
152
		return myCtx, myerr.WrapErr(bizerr.InvalidParameter)
chenweijian's avatar
chenweijian committed
153
	}
chenweijian's avatar
chenweijian committed
154

chenweijian's avatar
chenweijian committed
155
	cpRecord, err := cp_m.GetCpInvite(model, userSender.ID, user.ID, cp_e.CpInvite)
chenweijian's avatar
chenweijian committed
156 157 158 159 160
	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 {
chenweijian's avatar
chenweijian committed
161
		return myCtx, myerr.ToLocal(msg.GetErrByLanguage(model, common.MSG_ID_ALREADY_EXPIRED, lang, comerr.AlreadyExpired))
chenweijian's avatar
chenweijian committed
162 163
	}

chenweijian's avatar
chenweijian committed
164 165 166 167 168 169 170
	if optType == 1 { // 接受的时候
		// 自己是否有cp了
		myCp, err := cp_m.GetCp(model, myUserId)
		if err != nil {
			return myCtx, err
		}
		if myCp.Id > 0 {
chenweijian's avatar
chenweijian committed
171
			return myCtx, myerr.ToLocal(msg.GetErrByLanguage(model, common.MSG_ID_ALREADY_HAS_CP, lang, comerr.AlreadyHasCp))
chenweijian's avatar
chenweijian committed
172 173 174 175 176 177 178
		}
		// 对方是否已经有cp了
		senderCp, err := cp_m.GetCp(model, userSender.ID)
		if err != nil {
			return myCtx, err
		}
		if senderCp.Id > 0 {
chenweijian's avatar
chenweijian committed
179
			return myCtx, myerr.ToLocal(msg.GetErrByLanguage(model, common.MSG_ID_ALREADY_HAS_CP, lang, comerr.AlreadyHasCp))
chenweijian's avatar
chenweijian committed
180
		}
chenweijian's avatar
chenweijian committed
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
	}
	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关系表
hujiebin's avatar
hujiebin committed
197
			cpId, err := cp_m.CreateCp(model, userSender.ID, user.ID)
chenweijian's avatar
chenweijian committed
198 199 200 201
			if err != nil {
				model.Log.Errorf("ReplyCpInvite userSender:%d, user:%d, status:%d, err:%v", userSender.ID, user.ID, updateStatus, err)
				return err
			}
hujiebin's avatar
hujiebin committed
202 203 204 205 206
			// 初始化cp等级
			if err := cp_m.InitCpLevel(model, mysql.ID(cpId), userSender.ID, user.ID); err != nil {
				model.Log.Errorf("ReplyCpInvite InitCpLevel fail:%v-%v-%v", userSender.ID, user.ID, err)
				return err
			}
hujiebin's avatar
hujiebin committed
207
			// 初始化cp纪念日
hujiebin's avatar
hujiebin committed
208 209
			if err := cp_m.InitCpAnniversary(model, cp_m.CpRelation{
				Id:      mysql.ID(cpId),
hujiebin's avatar
hujiebin committed
210 211
				UserId1: userSender.ID,
				UserId2: user.ID,
212
			}, lang); err != nil {
hujiebin's avatar
hujiebin committed
213 214 215
				model.Log.Errorf("ReplyCpInvite InitCpAnniversary fail:%v-%v-%v", userSender.ID, user.ID, err)
				return err
			}
chenweijian's avatar
chenweijian committed
216
			// 发放告白礼物
chenweijian's avatar
chenweijian committed
217 218 219 220
			if _, err = bag_tx.SendUserBag(model, userSender.ID, 1, cp_e.CpConfessionGiftId, 1, 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
221 222 223 224 225 226
			// 发放头饰
			for _, u := range []*user_m.User{userSender, user} {
				headwearId := cp_e.CpMaleHeadwearId
				if u.Sex == mysql.WOMAN {
					headwearId = cp_e.CpFemaleHeadwearId
				}
chenweijian's avatar
chenweijian committed
227
				if err = headwear_tx.SendHeadwear(model, u.ID, mysql.ID(headwearId), 3000); err != nil {
chenweijian's avatar
chenweijian committed
228 229 230 231 232
					model.Log.Errorf("ReplyCpInvite user:%d, headwearId:%d, err:%v", u.ID, headwearId, err)
					return err
				}
			}

chenweijian's avatar
chenweijian committed
233
			// 私信-接受
chenweijian's avatar
chenweijian committed
234 235 236 237 238 239 240 241
			content, err := msg.GetResMultiTextBy(model, common.MSG_ID_BIND_CP_SUCCEED, lang)
			if err != nil {
				return err
			}
			tip, err := msg.GetResMultiTextBy(model, common.MSG_ID_SEND_CP_GIFT, lang)
			if err != nil {
				return err
			}
chenweijian's avatar
chenweijian committed
242 243
			msgData, _ = json.Marshal(cp_m.CpAcceptInviteMessage{
				Identifier: "CpAcceptInviteMessage",
chenweijian's avatar
chenweijian committed
244 245
				Msg:        content,
				Tip:        fmt.Sprintf(tip, userSender.Code),
chenweijian's avatar
chenweijian committed
246 247
				Sender:     user_m.ToUserTiny(userSender),
				Receiver:   user_m.ToUserTiny(user),
chenweijian's avatar
chenweijian committed
248
			})
chenweijian's avatar
chenweijian committed
249
		} else { // 拒绝
chenweijian's avatar
chenweijian committed
250
			// 退费
chenweijian's avatar
chenweijian committed
251 252
			err = diamond_tx.SendDiamond(model, cpRecord.UserId, diamond_e.CpInviteRefund, cpRecord.Id, cpRecord.DiamondNum,
				msg_e.MgrSendDiamondProperty)
chenweijian's avatar
chenweijian committed
253 254 255 256
			if err != nil {
				model.Log.Errorf("ReplyCpInvite UserId:%d, err:%v", cpRecord.UserId, err)
				return err
			}
chenweijian's avatar
chenweijian committed
257
			// 私信-拒绝
chenweijian's avatar
chenweijian committed
258 259 260 261
			content, err := msg.GetResMultiTextBy(model, common.MSG_ID_REJECTED_CP_INVITE, lang)
			if err != nil {
				return err
			}
chenweijian's avatar
chenweijian committed
262 263
			msgData, _ = json.Marshal(cp_m.CpDenyInviteMessage{
				Identifier: "CpDenyInviteMessage",
chenweijian's avatar
chenweijian committed
264
				Msg:        fmt.Sprintf(content, user.Nick),
chenweijian's avatar
chenweijian committed
265
				Sender:     user_m.ToUserTiny(user),
chenweijian's avatar
chenweijian committed
266 267 268 269 270 271 272 273 274 275 276 277 278
			})
		}
		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
	}

chenweijian's avatar
chenweijian committed
279
	resp.ResponseOk(c, nil)
chenweijian's avatar
chenweijian committed
280 281
	return myCtx, nil
}
chenweijian's avatar
chenweijian committed
282 283 284

// @Tags cp关系
// @Summary 回应cp解除
chenweijian's avatar
chenweijian committed
285
// @Param externalId formData string true "对方的externalId"
chenweijian's avatar
chenweijian committed
286 287 288 289 290
// @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)
chenweijian's avatar
chenweijian committed
291
	externalId := c.PostForm("externalId")
chenweijian's avatar
chenweijian committed
292 293 294
	if externalId == "" {
		return myCtx, bizerr.InvalidParameter
	}
chenweijian's avatar
chenweijian committed
295 296 297 298 299
	optType, err := strconv.Atoi(c.PostForm("type"))
	if err != nil || optType > 2 || optType < 1 {
		return myCtx, bizerr.InvalidParameter
	}

chenweijian's avatar
chenweijian committed
300
	myUserId, lang, err := req.GetUserIdLang(c, myCtx)
chenweijian's avatar
chenweijian committed
301 302 303 304 305 306 307 308 309
	if err != nil {
		return myCtx, err
	}

	model := domain.CreateModelContext(myCtx)
	myUser, err := user_m.GetUser(model, myUserId)
	if err != nil {
		return myCtx, err
	}
chenweijian's avatar
chenweijian committed
310
	user2, err := user_m.GetUserByExtId(model, externalId)
chenweijian's avatar
chenweijian committed
311 312 313 314
	if err != nil {
		return myCtx, err
	}

chenweijian's avatar
chenweijian committed
315 316 317 318
	if user2.ID == myUserId {
		return myCtx, bizerr.InvalidParameter
	}

chenweijian's avatar
chenweijian committed
319 320 321 322 323 324
	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 {
chenweijian's avatar
chenweijian committed
325
		return myCtx, myerr.ToLocal(msg.GetErrByLanguage(model, common.MSG_ID_ALREADY_EXPIRED, lang, comerr.AlreadyExpired))
chenweijian's avatar
chenweijian committed
326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352
	}

	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 { // 撤销解除
			// 私信
chenweijian's avatar
chenweijian committed
353 354 355 356
			content, err := msg.GetResMultiTextBy(model, common.MSG_ID_CANCEL_UNBIND_CP, lang)
			if err != nil {
				return err
			}
chenweijian's avatar
chenweijian committed
357 358
			msgData, _ = json.Marshal(cp_m.CpDealCancelMessage{
				Identifier: "CpDealCancelMessage",
chenweijian's avatar
chenweijian committed
359
				Msg:        fmt.Sprintf(content, myUser.Nick),
chenweijian's avatar
chenweijian committed
360
				Status:     1,
chenweijian's avatar
chenweijian committed
361 362 363 364 365 366 367 368
			})
		} 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
			}
chenweijian's avatar
chenweijian committed
369
			// 私信-接受解除
chenweijian's avatar
chenweijian committed
370 371 372 373
			content, err := msg.GetResMultiTextBy(model, common.MSG_ID_UNBIND_CP_SUCCEED, lang)
			if err != nil {
				return err
			}
chenweijian's avatar
chenweijian committed
374 375
			msgData, _ = json.Marshal(cp_m.CpDealCancelMessage{
				Identifier: "CpDealCancelMessage",
chenweijian's avatar
chenweijian committed
376
				Msg:        content,
chenweijian's avatar
chenweijian committed
377
				Status:     2,
chenweijian's avatar
chenweijian committed
378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393
			})
		}
		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
}
chenweijian's avatar
chenweijian committed
394

chenweijian's avatar
chenweijian committed
395
type CpDetail struct {
chenweijian's avatar
chenweijian committed
396 397
	CpInfo  cp_cv.CvCpInfo  `json:"cpInfo"`  // cp信息
	CpLevel cp_cv.CvCpLevel `json:"cpLevel"` // cp等级
chenweijian's avatar
chenweijian committed
398 399
}

chenweijian's avatar
chenweijian committed
400 401 402 403 404 405 406 407
// @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")
chenweijian's avatar
chenweijian committed
408 409 410
	if externalId == "" {
		return myCtx, bizerr.InvalidParameter
	}
chenweijian's avatar
chenweijian committed
411 412 413 414 415 416 417 418 419 420 421

	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
	}
chenweijian's avatar
chenweijian committed
422
	var res *CpDetail
chenweijian's avatar
chenweijian committed
423 424

	if cp.Id > 0 {
chenweijian's avatar
chenweijian committed
425 426 427 428 429
		userIds := []uint64{cp.UserId1, cp.UserId2}
		userMap, err := user_c.GetUserTinyMap(model, userIds, false)
		if err != nil {
			return myCtx, err
		}
chenweijian's avatar
chenweijian committed
430

chenweijian's avatar
chenweijian committed
431 432 433 434 435 436 437 438 439 440 441 442 443 444 445
		res = new(CpDetail)
		// 返回值
		level := cp_m.GetCpLevel(model, cp.Id)
		res.CpLevel = cp_cv.CvCpLevel{
			Level:          level.Level,
			Points:         cp_e.CpLevelPoints[level.Level] + level.Points,
			StartPoints:    cp_e.CpLevelPoints[level.Level],
			ExpireAtUnix:   level.ExpireAt.Unix(),
			SettlementDate: level.ExpireAt.Format(utils.DATE_FORMAT),
		}
		if res.CpLevel.Level != cp_e.CpLevelMax {
			res.CpLevel.EndPoints = cp_e.CpLevelPoints[res.CpLevel.Level+1]
		}
		res.CpInfo = cp_cv.CvCpInfo{
			UserInfo:    user_cv.UserTinyToCvTiny(userMap[cp.UserId1]),
chenweijian's avatar
chenweijian committed
446
			CpUserInfo:  user_cv.UserTinyToCvTiny(userMap[cp.UserId2]),
chenweijian's avatar
chenweijian committed
447 448
			CpDays:      int(time.Now().Sub(cp.CreatedTime).Hours()/24) + 1,
			CreatedUnix: cp.CreatedTime.Unix(),
chenweijian's avatar
chenweijian committed
449 450 451 452 453 454
		}
	}

	resp.ResponseOk(c, res)
	return myCtx, nil
}
chenweijian's avatar
chenweijian committed
455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485

// @Tags cp关系
// @Summary 检查cp的im是否失效
// @Param msgType query int true "类型"
// @Param msgId query int true "消息id"
// @Success 200 {object} cp_cv.CheckCpImRes
// @Router /v2/cp/im/check [get]
func CheckCpImExpire(c *gin.Context) (*mycontext.MyContext, error) {
	myCtx := mycontext.CreateMyContext(c.Keys)

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

	msgType, err := strconv.Atoi(c.Query("msgType"))
	if err != nil {
		return myCtx, err
	}
	if msgType < 1 || msgType > 2 {
		return myCtx, bizerr.InvalidParameter
	}
	msgId, err := strconv.ParseUint(c.Query("msgId"), 10, 64)
	if err != nil {
		return myCtx, err
	}
	if msgId <= 0 {
		return myCtx, bizerr.InvalidParameter
	}

	model := domain.CreateModelContext(myCtx)
chenweijian's avatar
chenweijian committed
486
	var resId common.MsgIdType
chenweijian's avatar
chenweijian committed
487 488
	switch msgType {
	case 1: // 邀请的消息im检查是否过期
chenweijian's avatar
chenweijian committed
489
		cpRecord, err := cp_m.GetCpInviteById(model, msgId, userId)
chenweijian's avatar
chenweijian committed
490 491 492 493 494
		if err != nil {
			model.Log.Errorf("CheckCpImExpire userId:%d, msgType:%d, msgId:%d, err:%v", userId, msgType, msgId, err)
			return myCtx, err
		}
		if cpRecord == nil || cpRecord.Id == 0 {
chenweijian's avatar
chenweijian committed
495 496 497 498 499 500
			model.Log.Errorf("CheckCpImExpire userId:%d, msgType:%d, msgId:%d, err:%v", userId, msgType, msgId, bizerr.InvalidParameter)
			return myCtx, bizerr.InvalidParameter
		}
		switch cpRecord.Status {
		case cp_e.CpInvite:
			if userId == cpRecord.UserId { // 发起人
chenweijian's avatar
chenweijian committed
501
				resId = common.MSG_ID_WAITING_ACCEPTED // 等待对方接受
chenweijian's avatar
chenweijian committed
502
			}
chenweijian's avatar
chenweijian committed
503
		case cp_e.CpInviteAccept:
chenweijian's avatar
chenweijian committed
504
			resId = common.MSG_ID_INVITATION_ACCEPTED // 已接受
chenweijian's avatar
chenweijian committed
505
		case cp_e.CpInviteRefuse:
chenweijian's avatar
chenweijian committed
506
			resId = common.MSG_ID_INVITATION_REFUSED // 已拒绝
chenweijian's avatar
chenweijian committed
507 508
		case cp_e.CpInviteExpired:
			resId = common.MSG_ID_ALREADY_EXPIRED
chenweijian's avatar
chenweijian committed
509 510
		}
	case 2: // 解除的消息im检查是否过期
chenweijian's avatar
chenweijian committed
511
		cpCancel, err := cp_m.GetCpCancelById(model, msgId, userId)
chenweijian's avatar
chenweijian committed
512 513 514 515 516
		if err != nil {
			model.Log.Errorf("CheckCpImExpire userId:%d, msgType:%d, msgId:%d, err:%v", userId, msgType, msgId, err)
			return myCtx, err
		}
		if cpCancel == nil || cpCancel.Id == 0 {
chenweijian's avatar
chenweijian committed
517 518
			model.Log.Errorf("CheckCpImExpire userId:%d, msgType:%d, msgId:%d, err:%v", userId, msgType, msgId, bizerr.InvalidParameter)
			return myCtx, bizerr.InvalidParameter
chenweijian's avatar
chenweijian committed
519
		}
chenweijian's avatar
chenweijian committed
520 521 522
		switch cpCancel.Status {
		case cp_e.CpCancel:
			if userId == cpCancel.UserId { // 发起人
chenweijian's avatar
chenweijian committed
523
				resId = common.MSG_ID_WAITING_ACCEPTED // 等待对方接受
chenweijian's avatar
chenweijian committed
524
			}
chenweijian's avatar
chenweijian committed
525
		case cp_e.CpCancelRevoke:
chenweijian's avatar
chenweijian committed
526
			resId = common.MSG_ID_HAS_BEEN_CANCELED // 已被取消
chenweijian's avatar
chenweijian committed
527
		case cp_e.CpCancelAccept, cp_e.CpCancelAcceptAuto:
chenweijian's avatar
chenweijian committed
528
			resId = common.MSG_ID_CP_UNBOUND // 已解绑
chenweijian's avatar
chenweijian committed
529 530 531 532
		}
	}
	if resId > 0 {
		return myCtx, myerr.ToLocal(msg.GetErrByLanguage(model, resId, lang, comerr.AlreadyExpired))
chenweijian's avatar
chenweijian committed
533 534 535 536 537
	}

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