package cp_s import ( "encoding/json" "git.hilo.cn/hilo-common/_const/common" "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" "hilo-user/myerr" "hilo-user/myerr/bizerr" ) func InviteCpRelation(myCtx *mycontext.MyContext, myUserId uint64, externalId, lang string) error { model := domain.CreateModelContext(myCtx) user, err := user_m.GetUser(model, myUserId) if err != nil { return err } userInvite, err := user_m.GetUserByExtId(model, externalId) if err != nil { return err } if userInvite.ID == myUserId { return bizerr.InvalidParameter } // 自己是否有cp了 myCp, err := cp_m.GetCp(model, myUserId) if err != nil { return err } if myCp.Id > 0 { return myerr.ToLocal(msg.GetErrByLanguage(model, common.MSG_ID_ALREADY_HAS_CP, lang, comerr.AlreadyHasCp)) } // 对方是否已经有cp了 inviCp, err := cp_m.GetCp(model, userInvite.ID) if err != nil { return err } if inviCp.Id > 0 { return myerr.ToLocal(msg.GetErrByLanguage(model, common.MSG_ID_ALREADY_HAS_CP, lang, comerr.AlreadyHasCp)) } // 我是否发起过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 } 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 } 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 } // 发送私信-邀请 data, _ := json.Marshal(cp_m.CpInviteMessage{ Identifier: "CpInviteMessage", Msg: content, Tip: tip, Sender: user_m.ToUserTiny(user), MsgType: 1, MsgId: cpInvId, }) 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 推送弹窗 go rpc.SendCpInviteNotice(userInvite.ID, user.Code, user.Nick, user.Avatar, content, user.ExternalId) return nil } func CancelCpRelation(myCtx *mycontext.MyContext, myUserId uint64, externalId, lang string) error { model := domain.CreateModelContext(myCtx) user, err := user_m.GetUser(model, myUserId) if err != nil { return err } userRec, err := user_m.GetUserByExtId(model, externalId) if err != nil { return err } if userRec.ID == myUserId { return bizerr.InvalidParameter } 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 } // 我和对方是否是cp,且cp关系存在 cpRelation, err := cp_m.GetCpByIds(model, myUserId, userRec.ID) if err != nil { return err } if cpRelation.Id == 0 { model.Log.Errorf("CancelCpRelation cp关系不存在 id1:%v, id2:%v", myUserId, userRec.ID) return myerr.WrapErr(bizerr.InvalidParameter) } //// 自己没有cp了 //myCp, err := cp_m.GetCp(model, myUserId) //if err != nil { // return err //} //if myCp.Id == 0 { // return myerr.WrapErr(bizerr.InvalidParameter) //} //// 对方没有cp了 //inviCp, err := cp_m.GetCp(model, userRec.ID) //if err != nil { // return err //} //if inviCp.Id == 0 { // return myerr.WrapErr(bizerr.InvalidParameter) //} // 是否有关于我的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 { // 创建邀请记录 cancelId, err := cp_m.CreateCpCancel(model, myUserId, userRec.ID) if err != nil { model.Log.Errorf("CancelCpRelation myUserId:%d, err:%v", myUserId, err) return err } // 发送私信-发起解除 data, _ := json.Marshal(cp_m.CpCancelMessage{ Identifier: "CpCancelMessage", Msg: content, Tip: tip, Sender: user_m.ToUserTiny(user), MsgType: 2, MsgId: cancelId, }) 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 }