headwear.go 3.45 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 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
package res_m

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

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

//矛盾,项目负责人(产品不接受 商品 同上架分开两个数据结构的模式),又在页面上要求头饰同价格在一起展示,修改。
//目前违背了,ResHeadwearDiamond 作为资源数据,不允许修改的特点。后果在于,对账错误。
type ResHeadwearDiamond struct {
	mysql.Entity
	*domain.Model `gorm:"-"`
	ResHeadwearId mysql.ID
	DiamondNum    mysql.Num
	Second        mysql.Num
	Status        mysql.UserYesNo
}

func InitResHeadwearDiamond(model *domain.Model, resHeadwearId mysql.ID, diamondNum mysql.Num, second mysql.Num) *ResHeadwearDiamond {
	return &ResHeadwearDiamond{
		Model:         model,
		ResHeadwearId: resHeadwearId,
		DiamondNum:    diamondNum,
		Second:        second,
		Status:        mysql.NOUSER,
	}
}

func GetResHeadwearDiamondByHeadwearIdOrNil(model *domain.Model, resHeadwearId mysql.ID) (*ResHeadwearDiamond, error) {
	resHeadwearDiamond := ResHeadwearDiamond{}
	if err := model.Db.Model(&ResHeadwearDiamond{}).Where(&ResHeadwearDiamond{
		ResHeadwearId: resHeadwearId,
	}).First(&resHeadwearDiamond).Error; err != nil {
		if err == gorm.ErrRecordNotFound {
			return nil, nil
		} else {
			return nil, myerr.WrapErr(err)
		}
	}
	resHeadwearDiamond.Model = model
	return &resHeadwearDiamond, nil
}

func (resHeadwearDiamond *ResHeadwearDiamond) SetDiamondNum(diamondNum uint32) *ResHeadwearDiamond {
	resHeadwearDiamond.DiamondNum = diamondNum
	return resHeadwearDiamond
}

func (resHeadwearDiamond *ResHeadwearDiamond) SetSecond(second uint32) *ResHeadwearDiamond {
	resHeadwearDiamond.Second = second
	return resHeadwearDiamond
}

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

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

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

//初始化头饰,
func InitHeadwear(model *domain.Model, name string, picUrl string, effectUrl string) *ResHeadwear {
	return &ResHeadwear{
		Model:     model,
		Name:      name,
		PicUrl:    picUrl,
		EffectUrl: effectUrl,
	}
}

//修改头饰
func (resHeadwear *ResHeadwear) EditName(name string) *ResHeadwear {
	resHeadwear.Name = name
	return resHeadwear
}

func (resHeadwear *ResHeadwear) EditPicUrl(picUrl string) *ResHeadwear {
	resHeadwear.PicUrl = picUrl
	return resHeadwear
}

func (resHeadwear *ResHeadwear) EditEffectUrl(effectUrl string) *ResHeadwear {
	resHeadwear.EffectUrl = effectUrl
	return resHeadwear
}

func CheckHeadwearValidById(model *domain.Model, id mysql.ID) (bool, error) {
	resHeadwear := ResHeadwear{}
	if err := model.Db.Model(&ResHeadwear{}).First(&resHeadwear, id).Error; err != nil {
		if err == gorm.ErrRecordNotFound {
			return false, nil
		} else {
			return false, myerr.WrapErr(err)
		}

	} else {
		return true, nil
	}
}