groupCustomTheme.go 3.28 KB
Newer Older
hujiebin's avatar
hujiebin committed
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
package group_m

import (
	"git.hilo.cn/hilo-common/domain"
	"git.hilo.cn/hilo-common/resource/config"
	"git.hilo.cn/hilo-common/resource/mysql"
	"git.hilo.cn/hilo-common/utils"
	"gorm.io/gorm"
	"hilo-group/_const/enum/group_e"
	"hilo-group/myerr"
	"hilo-group/myerr/bizerr"
	"time"
)

type GroupCustomTheme struct {
	mysql.Entity
	*domain.Model `gorm:"-"`
	ImGroupId     string
	PicUrl        mysql.Str
	ExpireTime    time.Time
	Using         mysql.YesNo
}

func GetGroupCustomThemeById(model *domain.Model, imGroupId string, id mysql.ID) (*GroupCustomTheme, error) {
	groupCustomTheme := GroupCustomTheme{}
	if err := model.Db.Model(&GroupCustomTheme{
		ImGroupId: imGroupId,
	}).First(&groupCustomTheme, id).Error; err != nil {
		return nil, myerr.WrapErr(err)
	}
	groupCustomTheme.Model = model
	return &groupCustomTheme, nil
}

func (groupCustomTheme *GroupCustomTheme) SetUsing(userId mysql.ID) (*GroupCustomTheme, error) {
	// 判断有没有权限修改资料
	role, err := GetRoleInGroup(groupCustomTheme.Model, userId, groupCustomTheme.ImGroupId)
	if err != nil {
		return nil, err
	}
	if role != group_e.GROUP_OWNER && role != group_e.GROUP_MANAGER {
		//return nil, myerr.NewSysError("No privileges")
		return nil, bizerr.NoPrivileges
	}

	//将其它的赋值为未使用
	if err := groupCustomTheme.Db.Model(&GroupCustomTheme{}).Where(&GroupCustomTheme{ImGroupId: groupCustomTheme.ImGroupId}).UpdateColumn("using", mysql.NO).Error; err != nil {
		return nil, myerr.WrapErr(err)
	}
	groupCustomTheme.Using = mysql.YES
	return groupCustomTheme, nil
}

func AddGroupCustomTheme(model *domain.Model, userId mysql.ID, imGroupId string, picUrl string) (*GroupCustomTheme, error) {
	// 判断有没有权限修改资料
	role, err := GetRoleInGroup(model, userId, imGroupId)
	if err != nil {
		return nil, err
	}
	if role != group_e.GROUP_OWNER && role != group_e.GROUP_MANAGER {
		//return nil, myerr.NewSysError("No privileges")
		return nil, bizerr.NoPrivileges
	}

	var n int64 = 0
	if err := model.Db.Model(&GroupCustomTheme{}).Where(&GroupCustomTheme{ImGroupId: imGroupId}).Where("expire_time > ?", time.Now()).Count(&n).Error; err != nil {
		return nil, myerr.WrapErr(err)
	}
	if n >= int64(config.GetGroupCustomThemeConfig().PIC_LIMIT) {
		return nil, bizerr.GroupCustomThemeLimit
	}
	//校验权限,产品没说,应该有

	//将其它的赋值为未使用
	if err := model.Db.Model(&GroupCustomTheme{}).Where(&GroupCustomTheme{ImGroupId: imGroupId}).UpdateColumn("using", mysql.NO).Error; err != nil {
		return nil, myerr.WrapErr(err)
	}
	picUrl = utils.MakeFullUrl(picUrl)

	return &GroupCustomTheme{
		Model:      model,
		ImGroupId:  imGroupId,
		PicUrl:     picUrl,
		ExpireTime: time.Now().AddDate(0, 0, config.GetGroupCustomThemeConfig().DAY),
		Using:      mysql.YES,
	}, nil
}

//获取最新展示的主题
func GetShowCustomTheme(model *domain.Model, imGroupId string) (uint64, string, error) {
	groupCustomTheme := GroupCustomTheme{}
	if err := model.Db.Model(&GroupCustomTheme{}).Where(&GroupCustomTheme{ImGroupId: imGroupId}).Where("expire_time > ?", time.Now()).Order("`using` asc, expire_time asc").First(&groupCustomTheme).Error; err != nil {
		if err == gorm.ErrRecordNotFound {
			return 0, "", nil
		} else {
			return 0, "", myerr.WrapErr(err)
		}
	} else {
		return groupCustomTheme.ID, groupCustomTheme.PicUrl, nil
	}
}