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

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

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

chenweijian's avatar
chenweijian committed
48 49 50 51
	if externalId == "" {
		return myCtx, bizerr.InvalidParameter
	}

chenweijian's avatar
chenweijian committed
52
	model := domain.CreateModelContext(myCtx)
chenweijian's avatar
chenweijian committed
53
	user, err := user_m.GetUserByExtId(model, externalId)
chenweijian's avatar
chenweijian committed
54 55 56 57 58 59 60 61 62
	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
63
		return myCtx, msg.GetErrByLanguage(model, 0, lang, comerr.InvalidParameter) // cwj----
chenweijian's avatar
chenweijian committed
64 65
	}

chenweijian's avatar
chenweijian committed
66
	resp.ResponseOk(c, cp_cv.CheckCpRelationRes{Diamond: cp_e.CpRelationInviteDiamond, Msg: "Do you want to be CP with me?"})
chenweijian's avatar
chenweijian committed
67 68 69 70
	return myCtx, nil
}

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

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

chenweijian's avatar
chenweijian committed
88 89 90
	optType, err := strconv.Atoi(c.PostForm("type"))
	if err != nil || optType > 2 || optType < 1 {
		return myCtx, bizerr.InvalidParameter
chenweijian's avatar
chenweijian committed
91 92
	}

chenweijian's avatar
chenweijian committed
93
	if optType == 1 { // 邀请
chenweijian's avatar
chenweijian committed
94
		err = cp_s.InviteCpRelation(myCtx, myUserId, externalId, lang)
chenweijian's avatar
chenweijian committed
95
		if err != nil {
chenweijian's avatar
chenweijian committed
96 97
			myCtx.Log.Errorf("InviteCpRelation myUserId:%d, err:%v", myUserId, err)
			return myCtx, err
chenweijian's avatar
chenweijian committed
98
		}
chenweijian's avatar
chenweijian committed
99 100
	} else {
		// 发起解除
chenweijian's avatar
chenweijian committed
101
		err = cp_s.CancelCpRelation(myCtx, myUserId, externalId, lang)
chenweijian's avatar
chenweijian committed
102
		if err != nil {
chenweijian's avatar
chenweijian committed
103 104
			myCtx.Log.Errorf("CancelCpRelation myUserId:%d, err:%v", myUserId, err)
			return myCtx, err
chenweijian's avatar
chenweijian committed
105
		}
chenweijian's avatar
chenweijian committed
106 107
	}

chenweijian's avatar
chenweijian committed
108
	resp.ResponseOk(c, nil)
chenweijian's avatar
chenweijian committed
109 110
	return myCtx, nil
}
chenweijian's avatar
chenweijian committed
111 112 113

// @Tags cp关系
// @Summary 回应cp邀请
chenweijian's avatar
chenweijian committed
114
// @Param externalId formData string true "对方用户的externalId"
chenweijian's avatar
chenweijian committed
115 116 117 118 119
// @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
120
	externalId := c.PostForm("externalId")
chenweijian's avatar
chenweijian committed
121 122 123
	if externalId == "" {
		return myCtx, bizerr.InvalidParameter
	}
chenweijian's avatar
chenweijian committed
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
	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
	}
chenweijian's avatar
chenweijian committed
139
	userSender, err := user_m.GetUserByExtId(model, externalId)
chenweijian's avatar
chenweijian committed
140 141 142
	if err != nil {
		return myCtx, err
	}
chenweijian's avatar
chenweijian committed
143 144 145
	if userSender.ID == myUserId {
		return myCtx, bizerr.InvalidParameter
	}
chenweijian's avatar
chenweijian committed
146

chenweijian's avatar
chenweijian committed
147
	cpRecord, err := cp_m.GetCpInvite(model, userSender.ID, user.ID, cp_e.CpInvite)
chenweijian's avatar
chenweijian committed
148 149 150 151 152 153 154 155
	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
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
	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
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
	}
	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
189
			cpId, err := cp_m.CreateCp(model, userSender.ID, user.ID)
chenweijian's avatar
chenweijian committed
190 191 192 193
			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
194 195 196 197 198
			// 初始化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
199 200 201 202 203 204 205 206 207
			// 初始化cp纪念日
			if err := cp_m.InitCpAnniversary(model, cp_m.CpRelationTmp{
				Entity:  mysql.Entity{ID: mysql.ID(cpId)},
				UserId1: userSender.ID,
				UserId2: user.ID,
			}); err != nil {
				model.Log.Errorf("ReplyCpInvite InitCpAnniversary fail:%v-%v-%v", userSender.ID, user.ID, err)
				return err
			}
chenweijian's avatar
chenweijian committed
208
			// 发放告白礼物 cwj----
chenweijian's avatar
chenweijian committed
209 210 211 212
			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
213 214 215 216 217 218 219 220 221
			// 发放头饰 cwj----
			//if err = headwear_tx.SendHeadwear(model, userSender.ID, 1, 3); err != nil {
			//	model.Log.Errorf("ReplyCpInvite userSender:%d, user:%d, status:%d, err:%v", userSender.ID, user.ID, updateStatus, err)
			//	return err
			//}
			//if err = headwear_tx.SendHeadwear(model, user.ID, 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
222 223 224
			// 私信-接受
			msgData, _ = json.Marshal(cp_m.CpAcceptInviteMessage{
				Identifier: "CpAcceptInviteMessage",
chenweijian's avatar
chenweijian committed
225
				Msg:        "We are already CP!",
chenweijian's avatar
chenweijian committed
226
				Tip:        fmt.Sprintf("Celebration gifts have been sent to %s's backpack", userSender.Code),
chenweijian's avatar
chenweijian committed
227 228
				Sender:     user_m.ToUserTiny(userSender),
				Receiver:   user_m.ToUserTiny(user),
chenweijian's avatar
chenweijian committed
229
			})
chenweijian's avatar
chenweijian committed
230
		} else { // 拒绝
chenweijian's avatar
chenweijian committed
231 232 233 234 235 236
			// 退费
			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
			}
chenweijian's avatar
chenweijian committed
237 238 239
			// 私信-拒绝
			msgData, _ = json.Marshal(cp_m.CpDenyInviteMessage{
				Identifier: "CpDenyInviteMessage",
chenweijian's avatar
chenweijian committed
240
				Msg:        fmt.Sprintf("%s have declined the CP invitation", user.Nick),
chenweijian's avatar
chenweijian committed
241
				Sender:     user_m.ToUserTiny(user),
chenweijian's avatar
chenweijian committed
242 243 244 245 246 247 248 249 250 251 252 253 254
			})
		}
		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
255
	resp.ResponseOk(c, nil)
chenweijian's avatar
chenweijian committed
256 257
	return myCtx, nil
}
chenweijian's avatar
chenweijian committed
258 259 260

// @Tags cp关系
// @Summary 回应cp解除
chenweijian's avatar
chenweijian committed
261
// @Param externalId formData string true "对方的externalId"
chenweijian's avatar
chenweijian committed
262 263 264 265 266
// @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
267
	externalId := c.PostForm("externalId")
chenweijian's avatar
chenweijian committed
268 269 270
	if externalId == "" {
		return myCtx, bizerr.InvalidParameter
	}
chenweijian's avatar
chenweijian committed
271 272 273 274 275 276 277 278 279 280 281 282 283 284 285
	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
	}
chenweijian's avatar
chenweijian committed
286
	user2, err := user_m.GetUserByExtId(model, externalId)
chenweijian's avatar
chenweijian committed
287 288 289 290
	if err != nil {
		return myCtx, err
	}

chenweijian's avatar
chenweijian committed
291 292 293 294
	if user2.ID == myUserId {
		return myCtx, bizerr.InvalidParameter
	}

chenweijian's avatar
chenweijian committed
295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328
	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 { // 撤销解除
			// 私信
chenweijian's avatar
chenweijian committed
329 330
			msgData, _ = json.Marshal(cp_m.CpDealCancelMessage{
				Identifier: "CpDealCancelMessage",
chenweijian's avatar
chenweijian committed
331
				Msg:        fmt.Sprintf("%s withdrew my application to lift CP", myUser.Nick),
chenweijian's avatar
chenweijian committed
332
				Status:     1,
chenweijian's avatar
chenweijian committed
333 334 335 336 337 338 339 340
			})
		} 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
341 342 343
			// 私信-接受解除
			msgData, _ = json.Marshal(cp_m.CpDealCancelMessage{
				Identifier: "CpDealCancelMessage",
chenweijian's avatar
chenweijian committed
344
				Msg:        "The CP relationship has been unbound, the CP value has been cleared, and the CP privilege has disappeared",
chenweijian's avatar
chenweijian committed
345
				Status:     2,
chenweijian's avatar
chenweijian committed
346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361
			})
		}
		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
362

chenweijian's avatar
chenweijian committed
363
type CpDetail struct {
chenweijian's avatar
chenweijian committed
364 365
	CpInfo  cp_cv.CvCpInfo  `json:"cpInfo"`  // cp信息
	CpLevel cp_cv.CvCpLevel `json:"cpLevel"` // cp等级
chenweijian's avatar
chenweijian committed
366 367
}

chenweijian's avatar
chenweijian committed
368 369 370 371 372 373 374 375
// @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
376 377 378
	if externalId == "" {
		return myCtx, bizerr.InvalidParameter
	}
chenweijian's avatar
chenweijian committed
379 380 381 382 383 384 385 386 387 388 389

	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
390
	var res *CpDetail
chenweijian's avatar
chenweijian committed
391 392

	if cp.Id > 0 {
chenweijian's avatar
chenweijian committed
393 394 395 396 397
		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
398

chenweijian's avatar
chenweijian committed
399 400 401 402 403 404 405 406 407 408 409 410 411 412 413
		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
414
			CpUserInfo:  user_cv.UserTinyToCvTiny(userMap[cp.UserId2]),
chenweijian's avatar
chenweijian committed
415 416
			CpDays:      int(time.Now().Sub(cp.CreatedTime).Hours()/24) + 1,
			CreatedUnix: cp.CreatedTime.Unix(),
chenweijian's avatar
chenweijian committed
417 418 419 420 421 422
		}
	}

	resp.ResponseOk(c, res)
	return myCtx, nil
}