cp_relation.go 6.86 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 9 10 11 12 13 14 15 16 17 18
	"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
19
type CpInvite struct {
chenweijian's avatar
chenweijian committed
20 21 22 23 24 25 26 27 28 29 30 31 32 33
	Id           uint64              `json:"id"`
	UserId       uint64              `json:"userId"`
	InviteUserId uint64              `json:"inviteUserId"`
	DiamondNum   uint32              `json:"diamondNum"`
	Status       cp_e.CpInviteStatus `json:"status"`
}

// 发送私信
type CpInviteMessage struct {
	Identifier string `json:"identifier"`
	Msg        string `json:"msg"`
	Status     uint8  `json:"status"` //1.发起邀请2.接受3.拒接
	Avatar1    string `json:"avatar1"`
	Avatar2    string `json:"avatar2"`
chenweijian's avatar
chenweijian committed
34 35
}

chenweijian's avatar
chenweijian committed
36 37 38 39 40 41 42 43 44 45 46 47 48 49
type CpCancel struct {
	Id        uint64              `json:"id"`
	UserId    uint64              `json:"userId"`
	RecUserId uint64              `json:"recUserId"`
	Status    cp_e.CpCancelStatus `json:"status"`
}

// 发送私信(解除)
type CpCancelMessage struct {
	Identifier string `json:"identifier"`
	Msg        string `json:"msg"`
	Status     uint8  `json:"status"` //1.发起解除2.撤销解除3.接受解除
}

chenweijian's avatar
chenweijian committed
50 51 52 53 54 55 56 57
func CreateCp(model *domain.Model, userId1, userId2 uint64) error {
	userIds := []uint64{userId1, userId2}
	result := model.DB().Exec("insert into cp_relation(user_id1, user_id2) values(?,?) where not exists (select user_id1 from cp_relation where user_id1 in (?) or user_id2 in (?));", userId1, userId2, userIds, userIds)
	if result.Error != nil {
		model.Log.Errorf("CreateCp user1:%d, user2:%d, err:%v", userId1, userId2, result.Error)
		return result.Error
	}
	if result.RowsAffected <= 0 {
chenweijian's avatar
chenweijian committed
58
		model.Log.Errorf("CreateCp user1:%d, user2:%d, err:%v", userId1, userId2, bizerr.TransactionFailed)
chenweijian's avatar
chenweijian committed
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
		return bizerr.TransactionFailed
	}
	return nil
}

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
76

chenweijian's avatar
chenweijian committed
77
func GetCpInvite(model *domain.Model, userId, userIdInvite uint64, status cp_e.CpInviteStatus) (*CpInvite, error) {
chenweijian's avatar
chenweijian committed
78
	res := new(CpInvite)
chenweijian's avatar
chenweijian committed
79
	err := model.DB().Model(CpInvite{}).Where(CpInvite{UserId: userId, InviteUserId: userIdInvite, Status: status}).First(&res).Error
chenweijian's avatar
chenweijian committed
80 81 82 83 84 85 86 87 88 89 90 91 92
	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
}

func CreateCpInvite(model *domain.Model, userId, userIdInvite uint64, diamondNum uint32) (uint64, error) {
	cpInvite := CpInvite{UserId: userId, InviteUserId: userIdInvite, DiamondNum: diamondNum, Status: cp_e.CpInvite}
	err := model.DB().Model(CpInvite{}).Create(cpInvite).Error
chenweijian's avatar
chenweijian committed
93 94
	if err != nil {
		model.Log.Errorf("CreateCpInvite user1:%d, user2:%d, diamondNum:%d, err:%v", userId, userIdInvite, diamondNum, err)
chenweijian's avatar
chenweijian committed
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
		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
110 111 112
	}
	return nil
}
chenweijian's avatar
chenweijian committed
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175

func CreateCpCancel(model *domain.Model, userId, recUserId uint64) (uint64, error) {
	cpCancel := CpCancel{UserId: userId, RecUserId: recUserId, Status: cp_e.CpCancel}
	err := model.DB().Model(CpCancel{}).Create(cpCancel).Error
	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
}

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
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195

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
}