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

import (
	"git.hilo.cn/hilo-common/domain"
chenweijian's avatar
chenweijian committed
5
	"git.hilo.cn/hilo-common/utils"
chenweijian's avatar
chenweijian committed
6
	"gorm.io/gorm"
chenweijian's avatar
chenweijian committed
7
	"hilo-user/_const/enum/cp_e"
chenweijian's avatar
chenweijian committed
8
	"hilo-user/domain/model/user_m"
chenweijian's avatar
chenweijian committed
9 10 11 12 13 14 15 16 17 18 19
	"hilo-user/myerr/bizerr"
	"time"
)

type CpRelation struct {
	Id          uint64    `json:"id"`
	UserId1     uint64    `json:"userId1"`
	UserId2     uint64    `json:"userId2"`
	CreatedTime time.Time `json:"createdTime"`
}

chenweijian's avatar
chenweijian committed
20
type CpInvite struct {
chenweijian's avatar
chenweijian committed
21 22 23 24 25 26 27
	Id           uint64              `json:"id"`
	UserId       uint64              `json:"userId"`
	InviteUserId uint64              `json:"inviteUserId"`
	DiamondNum   uint32              `json:"diamondNum"`
	Status       cp_e.CpInviteStatus `json:"status"`
}

chenweijian's avatar
chenweijian committed
28
// 发送私信-发起邀请
chenweijian's avatar
chenweijian committed
29
type CpInviteMessage struct {
chenweijian's avatar
chenweijian committed
30 31
	Identifier string           `json:"identifier"`
	Msg        string           `json:"msg"`
chenweijian's avatar
chenweijian committed
32
	Tip        string           `json:"tip"`
chenweijian's avatar
chenweijian committed
33
	Sender     *user_m.UserTiny `json:"sender"`
chenweijian's avatar
chenweijian committed
34 35
	MsgType    int              `json:"msgType"`
	MsgId      uint64           `json:"msgId"`
chenweijian's avatar
chenweijian committed
36 37 38 39 40 41
}

// 发送私信-接受邀请
type CpAcceptInviteMessage struct {
	Identifier string           `json:"identifier"`
	Msg        string           `json:"msg"`
chenweijian's avatar
chenweijian committed
42
	Tip        string           `json:"tip"`
chenweijian's avatar
chenweijian committed
43 44 45 46 47 48 49 50 51
	Sender     *user_m.UserTiny `json:"sender"`
	Receiver   *user_m.UserTiny `json:"receiver"`
}

// 发送私信-拒绝邀请
type CpDenyInviteMessage struct {
	Identifier string           `json:"identifier"`
	Msg        string           `json:"msg"`
	Sender     *user_m.UserTiny `json:"sender"`
chenweijian's avatar
chenweijian committed
52 53
}

chenweijian's avatar
chenweijian committed
54 55 56 57 58 59 60 61 62
type CpCancel struct {
	Id        uint64              `json:"id"`
	UserId    uint64              `json:"userId"`
	RecUserId uint64              `json:"recUserId"`
	Status    cp_e.CpCancelStatus `json:"status"`
}

// 发送私信(解除)
type CpCancelMessage struct {
chenweijian's avatar
chenweijian committed
63 64
	Identifier string           `json:"identifier"`
	Msg        string           `json:"msg"`
chenweijian's avatar
chenweijian committed
65
	Tip        string           `json:"tip"`
chenweijian's avatar
chenweijian committed
66
	Sender     *user_m.UserTiny `json:"sender"`
chenweijian's avatar
chenweijian committed
67 68
	MsgType    int              `json:"msgType"`
	MsgId      uint64           `json:"msgId"`
chenweijian's avatar
chenweijian committed
69 70 71 72
}

// 发送私信-撤销解除、接受解除
type CpDealCancelMessage struct {
chenweijian's avatar
chenweijian committed
73 74
	Identifier string `json:"identifier"`
	Msg        string `json:"msg"`
chenweijian's avatar
chenweijian committed
75
	Status     uint8  `json:"status"` //1.撤销解除2.接受解除
chenweijian's avatar
chenweijian committed
76 77
}

chenweijian's avatar
chenweijian committed
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
type Cp struct {
	Id               uint64    `json:"id"`
	UserId1          uint64    `json:"userId1"`
	UserId2          uint64    `json:"userId2"`
	DisconnectSecond int64     `json:"disconnectSecond"`
	Score            int32     `json:"score"`
	DayScore         int32     `json:"dayScore"`
	PeriodDay        string    `json:"periodDay"`
	WeekScore        int32     `json:"weekScore"`
	PeriodWeek       string    `json:"periodWeek"`
	MonthScore       int32     `json:"monthScore"`
	PeriodMonth      string    `json:"periodMonth"`
	Status           int8      `json:"status"`
	CreatedTime      time.Time `json:"createdTime"`
}

hujiebin's avatar
hujiebin committed
94
func CreateCp(model *domain.Model, userId1, userId2 uint64) (int64, error) {
chenweijian's avatar
chenweijian committed
95
	userIds := []uint64{userId1, userId2}
chenweijian's avatar
chenweijian committed
96 97 98 99 100
	oldCp := make([]*Cp, 0)
	// 这两个人以前是否有旧的cp关系
	err := model.DB().Model(Cp{}).Where("status = 1 and user_id1 in (?) and user_id2 in (?)", userIds, userIds).Find(&oldCp).Error
	if err != nil {
		model.Log.Errorf("CreateCp user1:%d, user2:%d, err:%v", userId1, userId2, err)
hujiebin's avatar
hujiebin committed
101
		return 0, err
chenweijian's avatar
chenweijian committed
102 103 104 105 106 107 108 109 110 111
	}
	createdTime := time.Now()
	if len(oldCp) > 0 {
		// 旧的cp关系,有效的时间给他加回去
		oldSecond := time.Duration(time.Now().Unix() - oldCp[0].CreatedTime.Unix() - oldCp[0].DisconnectSecond)
		if oldSecond > 0 {
			createdTime = createdTime.Add(-1 * time.Second * time.Duration(time.Now().Unix()-oldCp[0].CreatedTime.Unix()-oldCp[0].DisconnectSecond))
		}
	}

chenweijian's avatar
chenweijian committed
112
	result, err := model.DB().Config.ConnPool.ExecContext(model, "insert into cp_relation(user_id1, user_id2, created_time) select ?,?,? where not exists (select user_id1 from cp_relation where user_id1 in (?,?) or user_id2 in (?,?));", userId1, userId2, createdTime.Format(utils.DATETIME_FORMAT), userId1, userId2, userId1, userId2)
hujiebin's avatar
hujiebin committed
113 114 115
	if err != nil {
		model.Log.Errorf("CreateCp user1:%d, user2:%d, err:%v", userId1, userId2, err)
		return 0, err
chenweijian's avatar
chenweijian committed
116
	}
hujiebin's avatar
hujiebin committed
117 118
	rowAffected, _ := result.RowsAffected()
	if rowAffected <= 0 {
chenweijian's avatar
chenweijian committed
119
		model.Log.Errorf("CreateCp user1:%d, user2:%d, err:%v", userId1, userId2, bizerr.TransactionFailed)
hujiebin's avatar
hujiebin committed
120
		return 0, bizerr.TransactionFailed
chenweijian's avatar
chenweijian committed
121
	}
hujiebin's avatar
hujiebin committed
122 123
	id, err := result.LastInsertId()
	return id, err
chenweijian's avatar
chenweijian committed
124 125 126 127 128 129 130 131 132 133 134 135 136 137
}

func GetCp(model *domain.Model, userId uint64) (*CpRelation, error) {
	res := new(CpRelation)
	err := model.DB().Model(CpRelation{}).Where("user_id1 = ? or user_id2 = ?", userId, userId).First(&res).Error
	if err != nil {
		if err == gorm.ErrRecordNotFound {
			return res, nil
		}
		model.Log.Errorf("CreateCp userId:%d, err:%v", userId, err)
		return nil, err
	}
	return res, nil
}
chenweijian's avatar
chenweijian committed
138

chenweijian's avatar
chenweijian committed
139
func GetCpInvite(model *domain.Model, userId, userIdInvite uint64, status cp_e.CpInviteStatus) (*CpInvite, error) {
chenweijian's avatar
chenweijian committed
140
	res := new(CpInvite)
chenweijian's avatar
chenweijian committed
141
	err := model.DB().Model(CpInvite{}).Where(CpInvite{UserId: userId, InviteUserId: userIdInvite, Status: status}).First(&res).Error
chenweijian's avatar
chenweijian committed
142 143 144 145 146 147 148 149 150 151
	if err != nil {
		if err == gorm.ErrRecordNotFound {
			return nil, nil
		}
		model.Log.Errorf("GetCpInvite user1:%d, user2:%d, err:%v", userId, userIdInvite, err)
		return nil, err
	}
	return res, nil
}

chenweijian's avatar
chenweijian committed
152
func GetCpInviteById(model *domain.Model, id, userId uint64) (*CpInvite, error) {
chenweijian's avatar
chenweijian committed
153
	res := new(CpInvite)
chenweijian's avatar
chenweijian committed
154
	err := model.DB().Model(CpInvite{}).Where(CpInvite{Id: id}).Where("user_id = ? or invite_user_id = ?", userId, userId).First(&res).Error
chenweijian's avatar
chenweijian committed
155 156 157 158 159 160 161 162 163 164
	if err != nil {
		if err == gorm.ErrRecordNotFound {
			return nil, nil
		}
		model.Log.Errorf("GetCpInviteById id:%d, err:%v", id, err)
		return nil, err
	}
	return res, nil
}

chenweijian's avatar
chenweijian committed
165 166
func CreateCpInvite(model *domain.Model, userId, userIdInvite uint64, diamondNum uint32) (uint64, error) {
	cpInvite := CpInvite{UserId: userId, InviteUserId: userIdInvite, DiamondNum: diamondNum, Status: cp_e.CpInvite}
chenweijian's avatar
chenweijian committed
167
	err := model.DB().Model(CpInvite{}).Create(&cpInvite).Error
chenweijian's avatar
chenweijian committed
168 169
	if err != nil {
		model.Log.Errorf("CreateCpInvite user1:%d, user2:%d, diamondNum:%d, err:%v", userId, userIdInvite, diamondNum, err)
chenweijian's avatar
chenweijian committed
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
		return 0, err
	}
	return cpInvite.Id, nil
}

// userId:发起邀请者
func UpdateStatusCpInvite(model *domain.Model, id uint64, status cp_e.CpInviteStatus) error {
	result := model.DB().Exec("update cp_invite set status=? where id=? and status=? limit 1", status, id, cp_e.CpInvite)
	if result.Error != nil {
		model.Log.Errorf("UpdateStatusCpInvite id:%d, status:%d, err:%v", id, status, result.Error)
		return result.Error
	}
	if result.RowsAffected <= 0 {
		model.Log.Errorf("UpdateStatusCpInvite id:%d, status:%d, err:%v", id, status, bizerr.TransactionFailed)
		return bizerr.TransactionFailed
chenweijian's avatar
chenweijian committed
185 186 187
	}
	return nil
}
chenweijian's avatar
chenweijian committed
188 189 190

func CreateCpCancel(model *domain.Model, userId, recUserId uint64) (uint64, error) {
	cpCancel := CpCancel{UserId: userId, RecUserId: recUserId, Status: cp_e.CpCancel}
chenweijian's avatar
chenweijian committed
191
	err := model.DB().Model(CpCancel{}).Create(&cpCancel).Error
chenweijian's avatar
chenweijian committed
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
	if err != nil {
		model.Log.Errorf("CreateCpCancel user1:%d, user2:%d, err:%v", userId, recUserId, err)
		return 0, err
	}
	return cpCancel.Id, nil
}

func GetCpCancel(model *domain.Model, userIds []uint64, status cp_e.CpCancelStatus) (*CpCancel, error) {
	res := new(CpCancel)
	err := model.DB().Model(CpCancel{}).Where("status = ? and user_id in (?) and rec_user_id in (?)", status, userIds, userIds).First(&res).Error
	if err != nil {
		if err == gorm.ErrRecordNotFound {
			return nil, nil
		}
		model.Log.Errorf("GetCpCancel users:%d, err:%v", userIds, err)
		return nil, err
	}
	return res, nil
}

chenweijian's avatar
chenweijian committed
212
func GetCpCancelById(model *domain.Model, id, userId uint64) (*CpCancel, error) {
chenweijian's avatar
chenweijian committed
213
	res := new(CpCancel)
chenweijian's avatar
chenweijian committed
214
	err := model.DB().Model(CpCancel{}).Where("id = ? and (user_id = ? or rec_user_id = ?)", id, userId, userId).First(&res).Error
chenweijian's avatar
chenweijian committed
215 216 217 218 219 220 221 222 223 224
	if err != nil {
		if err == gorm.ErrRecordNotFound {
			return nil, nil
		}
		model.Log.Errorf("GetCpCancelById id:%d, err:%v", id, err)
		return nil, err
	}
	return res, nil
}

chenweijian's avatar
chenweijian committed
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
func GetCpCancelWithMe(model *domain.Model, userId uint64, status cp_e.CpCancelStatus) (*CpCancel, error) {
	res := new(CpCancel)
	err := model.DB().Model(CpCancel{}).Where("status = ? and (user_id = ? or rec_user_id = ?)", status, userId, userId).First(&res).Error
	if err != nil {
		if err == gorm.ErrRecordNotFound {
			return nil, nil
		}
		model.Log.Errorf("GetCpCancel user1:%d, err:%v", userId, err)
		return nil, err
	}
	return res, nil
}

func UpdateStatusCpCancel(model *domain.Model, id uint64, status cp_e.CpCancelStatus) error {
	result := model.DB().Exec("update cp_cancel set status=? where id=? and status=? limit 1", status, id, cp_e.CpCancel)
	if result.Error != nil {
		model.Log.Errorf("UpdateStatusCpCancel id:%d, status:%d, err:%v", id, status, result.Error)
		return result.Error
	}
	if result.RowsAffected <= 0 {
		model.Log.Errorf("UpdateStatusCpCancel id:%d, status:%d, err:%v", id, status, bizerr.TransactionFailed)
		return bizerr.TransactionFailed
	}
	return nil
}

func DelCpRelation(model *domain.Model, userId1, userId2 uint64) error {
	userIds := []uint64{userId1, userId2}
	result := model.DB().Exec("delete from cp_relation where user_id1 in (?) and user_id2 in (?) limit 1;", userIds, userIds)
	if result.Error != nil {
		model.Log.Errorf("DelCpRelation user1:%d, user2:%d, err:%v", userId1, userId2, result.Error)
		return result.Error
	}
	if result.RowsAffected <= 0 {
		model.Log.Errorf("DelCpRelation user1:%d, user2:%d, err:%v", userId1, userId2, bizerr.TransactionFailed)
		return bizerr.TransactionFailed
	}
	return nil
}
chenweijian's avatar
chenweijian committed
264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283

func GetCpInviteByTime(model *domain.Model, expTime time.Time) ([]*CpInvite, error) {
	res := make([]*CpInvite, 0)
	err := model.DB().Model(CpInvite{}).Where("status = ? and created_time <= ?", cp_e.CpInvite, expTime.Format(utils.DATETIME_FORMAT)).Find(&res).Error
	if err != nil {
		model.Log.Errorf("GetCpInviteByTime err:%v", err)
		return nil, err
	}
	return res, nil
}

func GetCpCancelByTime(model *domain.Model, expTime time.Time) ([]*CpCancel, error) {
	res := make([]*CpCancel, 0)
	err := model.DB().Model(CpCancel{}).Where("status = ? and created_time <= ?", cp_e.CpCancel, expTime.Format(utils.DATETIME_FORMAT)).Find(&res).Error
	if err != nil {
		model.Log.Errorf("GetCpCancelByTime err:%v", err)
		return nil, err
	}
	return res, nil
}