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

import (
	"encoding/json"
chenweijian's avatar
chenweijian committed
5
	"git.hilo.cn/hilo-common/_const/common"
chenweijian's avatar
chenweijian committed
6 7 8 9 10 11 12 13 14 15 16
	"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/rpc"
	"git.hilo.cn/hilo-common/sdk/tencentyun"
	"git.hilo.cn/hilo-common/txop/msg"
	"hilo-user/_const/enum/cp_e"
	"hilo-user/domain/model/cp_m"
	"hilo-user/domain/model/diamond_m"
	"hilo-user/domain/model/user_m"
chenweijian's avatar
chenweijian committed
17
	"hilo-user/myerr"
chenweijian's avatar
chenweijian committed
18 19 20
	"hilo-user/myerr/bizerr"
)

chenweijian's avatar
chenweijian committed
21
func InviteCpRelation(myCtx *mycontext.MyContext, myUserId uint64, externalId, lang string) error {
chenweijian's avatar
chenweijian committed
22 23 24 25 26
	model := domain.CreateModelContext(myCtx)
	user, err := user_m.GetUser(model, myUserId)
	if err != nil {
		return err
	}
chenweijian's avatar
chenweijian committed
27
	userInvite, err := user_m.GetUserByExtId(model, externalId)
chenweijian's avatar
chenweijian committed
28 29 30 31
	if err != nil {
		return err
	}

chenweijian's avatar
chenweijian committed
32 33 34 35
	if userInvite.ID == myUserId {
		return bizerr.InvalidParameter
	}

chenweijian's avatar
chenweijian committed
36 37 38 39 40 41
	// 自己是否有cp了
	myCp, err := cp_m.GetCp(model, myUserId)
	if err != nil {
		return err
	}
	if myCp.Id > 0 {
chenweijian's avatar
chenweijian committed
42
		return myerr.ToLocal(msg.GetErrByLanguage(model, common.MSG_ID_ALREADY_HAS_CP, lang, comerr.AlreadyHasCp))
chenweijian's avatar
chenweijian committed
43 44 45 46 47 48 49
	}
	// 对方是否已经有cp了
	inviCp, err := cp_m.GetCp(model, userInvite.ID)
	if err != nil {
		return err
	}
	if inviCp.Id > 0 {
chenweijian's avatar
chenweijian committed
50
		return myerr.ToLocal(msg.GetErrByLanguage(model, common.MSG_ID_ALREADY_HAS_CP, lang, comerr.AlreadyHasCp))
chenweijian's avatar
chenweijian committed
51 52 53 54 55 56 57 58 59 60 61 62
	}

	// 我是否发起过cp邀请,且还未被处理
	myInvite, err := cp_m.GetCpInvite(model, user.ID, 0, cp_e.CpInvite)
	if err != nil {
		model.Log.Errorf("InviteCpRelation myUserId:%d, err:%v", myUserId, err)
		return err
	}
	if myInvite != nil && myInvite.Id > 0 {
		return bizerr.CpAlreadyInvite
	}

chenweijian's avatar
chenweijian committed
63 64 65 66 67 68 69 70
	content, err := msg.GetResMultiTextBy(model, common.MSG_ID_WANT_BE_YOUR_CP, lang)
	if err != nil {
		return err
	}
	tip, err := msg.GetResMultiTextBy(model, common.MSG_ID_EXPIRES_AFTER_24_H, lang)
	if err != nil {
		return err
	}
chenweijian's avatar
chenweijian committed
71 72 73 74 75 76 77 78 79 80 81 82 83
	err = model.Transaction(func(model *domain.Model) error {
		// 创建邀请记录
		cpInvId, err := cp_m.CreateCpInvite(model, myUserId, userInvite.ID, cp_e.CpRelationInviteDiamond)
		if err != nil {
			model.Log.Errorf("InviteCpRelation myUserId:%d, err:%v", myUserId, err)
			return err
		}
		// 扣费
		err = diamond_m.ChangeDiamondAccountDetail(model, diamond_e.CpInvite, cpInvId, myUserId, cp_e.CpRelationInviteDiamond)
		if err != nil {
			model.Log.Errorf("InviteCpRelation myUserId:%d, err:%v", myUserId, err)
			return err
		}
chenweijian's avatar
chenweijian committed
84
		// 发送私信-邀请
chenweijian's avatar
chenweijian committed
85 86
		data, _ := json.Marshal(cp_m.CpInviteMessage{
			Identifier: "CpInviteMessage",
chenweijian's avatar
chenweijian committed
87 88
			Msg:        content,
			Tip:        tip,
chenweijian's avatar
chenweijian committed
89
			Sender:     user_m.ToUserTiny(user),
chenweijian's avatar
chenweijian committed
90 91 92 93 94 95 96 97 98 99 100 101
		})
		if err := tencentyun.BatchSendCustomMsg(model, 1, user.ExternalId, []string{userInvite.ExternalId}, string(data), "cp邀请"); err != nil {
			model.Log.Errorf("BatchSendCustomMsg fail:%v", err)
			return err
		}
		return nil
	})
	if err != nil {
		model.Log.Errorf("InviteCpRelation myUserId:%d, err:%v", myUserId, err)
		return err
	}
	// socket 推送弹窗
chenweijian's avatar
chenweijian committed
102
	go rpc.SendCpInviteNotice(userInvite.ID, user.Code, user.Nick, user.Avatar, content, user.ExternalId)
chenweijian's avatar
chenweijian committed
103 104 105 106

	return nil
}

chenweijian's avatar
chenweijian committed
107
func CancelCpRelation(myCtx *mycontext.MyContext, myUserId uint64, externalId, lang string) error {
chenweijian's avatar
chenweijian committed
108 109 110 111 112
	model := domain.CreateModelContext(myCtx)
	user, err := user_m.GetUser(model, myUserId)
	if err != nil {
		return err
	}
chenweijian's avatar
chenweijian committed
113
	userRec, err := user_m.GetUserByExtId(model, externalId)
chenweijian's avatar
chenweijian committed
114 115 116 117
	if err != nil {
		return err
	}

chenweijian's avatar
chenweijian committed
118 119 120 121
	if userRec.ID == myUserId {
		return bizerr.InvalidParameter
	}

chenweijian's avatar
chenweijian committed
122 123 124 125 126 127 128 129 130
	tip, err := msg.GetResMultiTextBy(model, common.MSG_ID_EXPIRES_AFTER_24_H, lang)
	if err != nil {
		return err
	}
	content, err := msg.GetResMultiTextBy(model, common.MSG_ID_WANT_UNBIND_CP, lang)
	if err != nil {
		return err
	}

chenweijian's avatar
chenweijian committed
131 132 133 134 135 136
	// 自己没有cp了
	myCp, err := cp_m.GetCp(model, myUserId)
	if err != nil {
		return err
	}
	if myCp.Id == 0 {
chenweijian's avatar
chenweijian committed
137
		return myerr.WrapErr(bizerr.InvalidParameter)
chenweijian's avatar
chenweijian committed
138 139 140 141 142 143 144
	}
	// 对方没有cp了
	inviCp, err := cp_m.GetCp(model, userRec.ID)
	if err != nil {
		return err
	}
	if inviCp.Id == 0 {
chenweijian's avatar
chenweijian committed
145
		return myerr.WrapErr(bizerr.InvalidParameter)
chenweijian's avatar
chenweijian committed
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
	}

	// 是否有关于我的cp解除申请,且还未被处理
	myCancel, err := cp_m.GetCpCancelWithMe(model, user.ID, cp_e.CpCancel)
	if err != nil {
		model.Log.Errorf("InviteCpRelation myUserId:%d, err:%v", myUserId, err)
		return err
	}
	if myCancel != nil && myCancel.Id > 0 {
		return bizerr.CpHaveCancelNoDeal
	}

	err = model.Transaction(func(model *domain.Model) error {
		// 创建邀请记录
		_, err = cp_m.CreateCpCancel(model, myUserId, userRec.ID)
		if err != nil {
			model.Log.Errorf("CancelCpRelation myUserId:%d, err:%v", myUserId, err)
			return err
		}
chenweijian's avatar
chenweijian committed
165
		// 发送私信-发起解除
chenweijian's avatar
chenweijian committed
166 167
		data, _ := json.Marshal(cp_m.CpCancelMessage{
			Identifier: "CpCancelMessage",
chenweijian's avatar
chenweijian committed
168 169
			Msg:        content,
			Tip:        tip,
chenweijian's avatar
chenweijian committed
170
			Sender:     user_m.ToUserTiny(user),
chenweijian's avatar
chenweijian committed
171 172 173 174 175 176 177 178 179 180 181 182 183 184
		})
		if err := tencentyun.BatchSendCustomMsg(model, 1, user.ExternalId, []string{userRec.ExternalId}, string(data), "cp解除"); err != nil {
			model.Log.Errorf("CancelCpRelation BatchSendCustomMsg fail:%v", err)
			return err
		}
		return nil
	})
	if err != nil {
		model.Log.Errorf("CancelCpRelation myUserId:%d, err:%v", myUserId, err)
		return err
	}

	return nil
}