property.go 4.12 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
package res_m

import (
	"git.hilo.cn/hilo-common/domain"
	"git.hilo.cn/hilo-common/resource/mysql"
	"gorm.io/gorm"
	"hilo-user/_const/enum/res_e"
	"hilo-user/myerr"
	"hilo-user/myerr/bizerr"
)

type ResProperty struct {
	mysql.Entity
	*domain.Model `gorm:"-"`
	Name          mysql.Str
	PicUrl        mysql.Str
	EffectUrl     mysql.Str
}

type ResPropertyDiamond struct {
	mysql.Entity
	*domain.Model `gorm:"-"`
	ResPropertyId mysql.ID
	DiamondNum    mysql.Num
	Second        mysql.Num
	Status        mysql.UserYesNo
}

type ResPropertyAvatar struct {
	mysql.Entity
	*domain.Model  `gorm:"-"`
	ResPropertyId  mysql.ID
	Type           res_e.ResPropertyAvatarType
	SendUserId     mysql.ID
	ReceiverUserId mysql.ID
chenweijian's avatar
chenweijian committed
36
	Area           int
hujiebin's avatar
hujiebin committed
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 106 107 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
}

func InitResPropertyDiamond(model *domain.Model, resPropertyId mysql.ID, diamondNum mysql.Num, second mysql.Num) *ResPropertyDiamond {
	return &ResPropertyDiamond{
		Model:         model,
		ResPropertyId: resPropertyId,
		DiamondNum:    diamondNum,
		Second:        second,
		Status:        mysql.NOUSER,
	}
}

//id获取头饰,不存在则抛异常
func GetPropertyById(model *domain.Model, id mysql.ID) (*ResProperty, error) {
	resProperty := ResProperty{}
	if err := model.Db.Model(&ResProperty{}).First(&resProperty, id).Error; err != nil {
		return nil, myerr.WrapErr(err)
	} else {
		resProperty.Model = model
		return &resProperty, nil
	}
}

func GetResPropertyDiamond(model *domain.Model, resPropertyDiamondId mysql.ID) (*ResPropertyDiamond, error) {
	resPropertyDiamond := ResPropertyDiamond{}
	if err := model.Db.Model(&ResPropertyDiamond{}).First(&resPropertyDiamond, resPropertyDiamondId).Error; err != nil {
		return nil, myerr.WrapErr(err)
	}
	if resPropertyDiamond.Status == mysql.NOUSER {
		return nil, bizerr.ResPropertyDiamondNoUse
	}
	return &resPropertyDiamond, nil
}

func GetResPropertyDiamondByPropertyIdOrNil(model *domain.Model, resPropertyId mysql.ID) (*ResPropertyDiamond, error) {
	resPropertyDiamond := ResPropertyDiamond{}
	if err := model.Db.Model(&ResPropertyDiamond{}).Where(&ResPropertyDiamond{
		ResPropertyId: resPropertyId,
	}).First(&resPropertyDiamond).Error; err != nil {
		if err == gorm.ErrRecordNotFound {
			return nil, nil
		} else {
			return nil, myerr.WrapErr(err)
		}
	}
	resPropertyDiamond.Model = model
	return &resPropertyDiamond, nil
}

//设置成未使用
func (resPropertyDiamond *ResPropertyDiamond) SetUser() *ResPropertyDiamond {
	resPropertyDiamond.Status = mysql.USER
	return resPropertyDiamond
}

//设置成未使用
func (resPropertyDiamond *ResPropertyDiamond) SetNoUser() *ResPropertyDiamond {
	resPropertyDiamond.Status = mysql.NOUSER
	return resPropertyDiamond
}

func (resPropertyDiamond *ResPropertyDiamond) SetDiamondNum(diamondNum uint32) *ResPropertyDiamond {
	resPropertyDiamond.DiamondNum = diamondNum
	return resPropertyDiamond
}

func (resPropertyDiamond *ResPropertyDiamond) SetSecond(second uint32) *ResPropertyDiamond {
	resPropertyDiamond.Second = second
	return resPropertyDiamond
}

func AddProperty(model *domain.Model, p *ResProperty) error {
	return model.Db.Create(p).Error
}

func EditProperty(model *domain.Model, propertyId uint64, name, picUrl, effectUrl string) error {
	activityConfig := ResProperty{Entity: mysql.Entity{ID: propertyId}}
	err := model.Db.First(&activityConfig).Error
	if err != nil {
		return err
	}
	activityConfig.Name = name
	activityConfig.PicUrl = picUrl
	activityConfig.EffectUrl = effectUrl
	return model.Db.Save(&activityConfig).Error
}

func (p *ResProperty) Get(db *gorm.DB) error {
	return db.Where(p).First(p).Error
}

func (p *ResProperty) GetAll(db *gorm.DB) (map[uint64]ResProperty, error) {
	rows := make([]ResProperty, 0)
	if err := db.Where(p).Find(&rows).Error; err != nil {
		return nil, err
	}
	result := make(map[uint64]ResProperty, 0)
	for _, i := range rows {
		result[i.ID] = i
	}
	return result, nil
}

func (p *ResPropertyAvatar) GetAll(db *gorm.DB) (map[uint64]ResPropertyAvatar, error) {
	rows := make([]ResPropertyAvatar, 0)
	if err := db.Where(p).Find(&rows).Error; err != nil {
		return nil, myerr.WrapErr(err)
	}
	result := make(map[uint64]ResPropertyAvatar, 0)
	for _, i := range rows {
		result[i.ResPropertyId] = i
	}
	return result, nil
}