banner.go 4.93 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
package mgr_m

import (
	"database/sql/driver"
	"git.hilo.cn/hilo-common/domain"
	"git.hilo.cn/hilo-common/resource/mysql"
	"gorm.io/gorm"
	"hilo-group/myerr"
	"strings"
	"time"
)

type Strs []string

func (this *Strs) Scan(val interface{}) error {
	s := val.([]uint8)
	ss := strings.Split(string(s), ",")
	*this = ss
	return nil
}

func (this Strs) Value() (driver.Value, error) {
	str := strings.Join(this, ",")
	return str, nil
}

//群组Banner
type GroupBanner struct {
	mysql.Entity
	*domain.Model `gorm:"-"`
	Title         mysql.Str
	Image         mysql.Str
	Url           mysql.Str
	GroupCode     mysql.Str //群组code
	N             mysql.Num
	Status        mysql.UserYesNo
	Area          int   // 上线区域:0全区;1阿语区;2非阿语区;3指定国家
	CountryList   Strs  // 指定区域的国家列表
	StartAt       int64 // 上架时间戳
	EndAt         int64 // 下架时间戳
}

func GetGroupBanner(model *domain.Model, id mysql.ID) (*GroupBanner, error) {
	groupBanner := GroupBanner{}
	if err := model.Db.Model(&GroupBanner{}).First(&groupBanner, id).Error; err != nil {
		return &groupBanner, myerr.WrapErr(err)
	}
	groupBanner.Model = model
	return &groupBanner, nil
}

//加入
func AddGroupBanner(model *domain.Model, title mysql.Str, image mysql.Str, url mysql.Str, groupCode mysql.Str, n mysql.Num) *GroupBanner {
	return &GroupBanner{
		Model:     model,
		Title:     title,
		Image:     image,
		Url:       url,
		GroupCode: groupCode,
		N:         n,
		Status:    mysql.USER,
	}
}

//更新
func (groupBanner *GroupBanner) Update(title mysql.Str, image mysql.Str, url mysql.Str, groupCode mysql.Str, n mysql.Num) *GroupBanner {
	groupBanner.Title = title
	groupBanner.Image = image
	groupBanner.Url = url
	groupBanner.GroupCode = groupCode
	groupBanner.N = n
	return groupBanner
}

//上架
func (groupBanner *GroupBanner) User() *GroupBanner {
	groupBanner.Status = mysql.USER
	return groupBanner
}

//下架
func (groupBanner *GroupBanner) NoUser() *GroupBanner {
	groupBanner.Status = mysql.NOUSER
	return groupBanner
}

// 房间Banner
type RoomBanner struct {
	mysql.Entity
	Title       mysql.Str
	Image       mysql.Str
	Url         mysql.Str
	N           mysql.Num
	Status      mysql.UserYesNo
	Area        int  // 上线区域:0全区;1阿语区;2非阿语区;3指定国家
	CountryList Strs // 指定区域的国家列表
}

func (rb *RoomBanner) GetRoomBanners(db *gorm.DB, vcAllow bool) ([]RoomBanner, error) {
	rows := make([]RoomBanner, 0)
	if err := db.Model(&RoomBanner{}).Where(rb).Order("n DESC").Find(&rows).Error; err != nil {
		return nil, err
	}
	res := make([]RoomBanner, 0, len(rows))
	for _, v := range rows {
hujiebin's avatar
hujiebin committed
106 107
		// 版本控制
		if !vcAllow && (v.ID == 241 || v.ID == 781 || v.ID == 771 || v.ID == 911 || v.ID == 841 || v.ID == 851 || v.ID == 1001 || v.ID == 981) {
108 109 110 111 112 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 176 177 178 179 180 181 182 183
			continue
		}
		res = append(res, v)
	}
	return res, nil
}

func (rb *RoomBanner) Create(db *gorm.DB) error {
	return db.Save(rb).Error
}

func (rb *RoomBanner) Save(db *gorm.DB) error {
	return db.Omit("status").Save(rb).Error
}

func (rb *RoomBanner) SetStatus(db *gorm.DB, status uint8) error {
	return db.Model(&RoomBanner{}).Where(rb).Update("status", status).Error
}

type DiscoveryBanner struct {
	mysql.Entity
	*domain.Model `gorm:"-"`
	Title         mysql.Str
	Image         mysql.Str
	Url           mysql.Str
	GroupCode     mysql.Str
	N             mysql.Num
	BeginTime     time.Time
	EndTime       time.Time
	Area          int  // 上线区域:0全区;1阿语区;2非阿语区;3指定国家
	CountryList   Strs // 指定区域的国家列表
}

func GetDiscoveryBannerOrErr(model *domain.Model, id mysql.ID) (*DiscoveryBanner, error) {
	discoveryBanner := DiscoveryBanner{}
	if err := model.Db.Model(&DiscoveryBanner{}).First(&discoveryBanner, id).Error; err != nil {
		return &discoveryBanner, myerr.WrapErr(err)
	}
	discoveryBanner.Model = model
	return &discoveryBanner, nil
}

func InitDiscoveryBanner(model *domain.Model, title mysql.Str, image mysql.Str, url mysql.Str, groupCode mysql.Str, n mysql.Num, beginTime time.Time, endTime time.Time) *DiscoveryBanner {
	return &DiscoveryBanner{
		Model:     model,
		Title:     title,
		Image:     image,
		Url:       url,
		GroupCode: groupCode,
		N:         n,
		BeginTime: beginTime,
		EndTime:   endTime,
	}
}

//更新
func (discoveryBanner *DiscoveryBanner) Update(title mysql.Str, image mysql.Str, url mysql.Str, groupCode mysql.Str, n mysql.Num, beginTime time.Time, endTime time.Time) *DiscoveryBanner {
	discoveryBanner.Title = title
	discoveryBanner.Image = image
	discoveryBanner.Url = url
	discoveryBanner.GroupCode = groupCode
	discoveryBanner.N = n
	discoveryBanner.BeginTime = beginTime
	discoveryBanner.EndTime = endTime
	return discoveryBanner
}

func (discoveryBanner *DiscoveryBanner) EditEndTime(time time.Time) *DiscoveryBanner {
	discoveryBanner.EndTime = time
	return discoveryBanner
}

func (discoveryBanner *DiscoveryBanner) EditBeginTime(time time.Time) *DiscoveryBanner {
	discoveryBanner.BeginTime = time
	return discoveryBanner
}