invite_apply.go 2.97 KB
Newer Older
chenweijian's avatar
chenweijian committed
1 2
package invite_m

chenweijian's avatar
chenweijian committed
3 4
import (
	"git.hilo.cn/hilo-common/domain"
chenweijian's avatar
chenweijian committed
5 6
	"git.hilo.cn/hilo-common/resource/mysql"
	"gorm.io/gorm"
chenweijian's avatar
chenweijian committed
7 8
	"time"
)
chenweijian's avatar
chenweijian committed
9 10

type InviteApply struct {
chenweijian's avatar
chenweijian committed
11 12 13 14
	Id           uint64    `json:"id"`
	UserId       uint64    `json:"user_id"`
	NewUserId    uint64    `json:"new_user_id"`
	Platform     string    `json:"platform"`
chenweijian's avatar
chenweijian committed
15
	PlatformId   string    `json:"platform_id"`
chenweijian's avatar
chenweijian committed
16 17 18 19 20
	RechargeInfo string    `json:"recharge_info"`
	Status       uint8     `json:"status"` // 状态0.未审核1.已通过2.已拒绝
	VideoUrl     string    `json:"video_url"`
	Level        string    `json:"level"`
	CreatedTime  time.Time `json:"created_time"`
chenweijian's avatar
chenweijian committed
21 22
	Reason       int       `json:"reason"`      // 1.已申请2.待审核3.已通过4.已拒绝"
	SubUserId    uint64    `json:"sub_user_id"` // 提交人
chenweijian's avatar
chenweijian committed
23 24
}

chenweijian's avatar
chenweijian committed
25
func CreateInviteApply(model *domain.Model, userId, newUserId, subUserId uint64, platform, platformId, recharge, videoUrl string) error {
chenweijian's avatar
chenweijian committed
26
	err := model.DB().Create(&InviteApply{
chenweijian's avatar
chenweijian committed
27
		UserId: userId, NewUserId: newUserId, Platform: platform, PlatformId: platformId, RechargeInfo: recharge, VideoUrl: videoUrl,
chenweijian's avatar
chenweijian committed
28
		CreatedTime: time.Now(), SubUserId: subUserId}).Error
chenweijian's avatar
chenweijian committed
29 30 31 32 33 34
	if err != nil {
		model.Log.Errorf("CreateInviteApply err:%v", err)
		return err
	}
	return nil
}
chenweijian's avatar
chenweijian committed
35

chenweijian's avatar
chenweijian committed
36 37 38
func GetApplyList(model *domain.Model, subUserId uint64, userIds []uint64, pageIndex, pageSize, gType int, beginTime,
	endTime time.Time) ([]*InviteApply, int64, error) {
	db := model.DB().Model(InviteApply{}).Where("user_id in (?) or sub_user_id = ?", userIds, subUserId).Where("created_time >= ? and created_time <= ?", beginTime, endTime)
chenweijian's avatar
chenweijian committed
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
	switch gType { // 1.已申请2.待审核3.已通过4.已拒绝
	case 2:
		db = db.Where("`status` = ?", 0)
	case 3:
		db = db.Where("`status` = ?", 1)
	case 4:
		db = db.Where("`status` = ?", 2)
	}
	res := make([]*InviteApply, 0)
	var count int64
	err := db.Order("id desc").Offset((pageIndex - 1) * pageSize).Find(&res).Limit(-1).Offset(-1).Count(&count).Error
	if err != nil {
		model.Log.Errorf("GetApplyList err:%v", err)
		return nil, 0, err
	}
	return res, count, nil
}
chenweijian's avatar
chenweijian committed
56 57

// 检查被邀请人是否存在
chenweijian's avatar
chenweijian committed
58
func IsInInviteApply(model *domain.Model, userId mysql.ID) (bool, error) {
chenweijian's avatar
chenweijian committed
59
	var apply InviteApply
chenweijian's avatar
chenweijian committed
60
	if err := model.Db.Model(InviteApply{}).Where("new_user_id = ?", userId).First(&apply).Error; err != nil {
chenweijian's avatar
chenweijian committed
61 62 63 64 65 66 67 68
		if err != gorm.ErrRecordNotFound {
			return false, err
		}
		return false, nil
	}
	// err == nil, record exists
	return true, nil
}
chenweijian's avatar
chenweijian committed
69 70

// 检查被邀请人是否存在
chenweijian's avatar
chenweijian committed
71
func GetInviteApplyNumByType(model *domain.Model, gType int, beginTime, endTime time.Time, userIds []uint64) (int, error) {
chenweijian's avatar
chenweijian committed
72
	var count int64
chenweijian's avatar
chenweijian committed
73
	db := model.Db.Model(InviteApply{}).Where("user_id in (?)", userIds).Where("created_time >= ? and created_time <= ?", beginTime, endTime)
chenweijian's avatar
chenweijian committed
74 75 76 77 78 79 80 81 82 83 84 85 86
	switch gType { // 1.已申请2.待审核3.已通过4.已拒绝
	case 2:
		db = db.Where("`status` = ?", 0)
	case 3:
		db = db.Where("`status` = ?", 1)
	case 4:
		db = db.Where("`status` = ?", 2)
	}
	if err := db.Count(&count).Error; err != nil {
		return 0, err
	}
	return int(count), nil
}