headwear.go 4.91 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 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
package res_m

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

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

func GetResHeadwearById(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)
	}
	resHeadwear.Model = model
	return &resHeadwear, nil
}

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

//矛盾,项目负责人(产品不接受 商品 同上架分开两个数据结构的模式),又在页面上要求头饰同价格在一起展示,修改。
//目前违背了,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 GetResHeadwearDiamond(model *domain.Model, resHeadwearDiamondId mysql.ID) (*ResHeadwearDiamond, error) {
	resHeadwearDiamond := ResHeadwearDiamond{}
	if err := model.Db.Model(&ResHeadwearDiamond{}).First(&resHeadwearDiamond, resHeadwearDiamondId).Error; err != nil {
		return nil, myerr.WrapErr(err)
	}
	if resHeadwearDiamond.Status == mysql.NOUSER {
		return nil, bizerr.ResHeadwearDiamondNoUse
	}
	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
}

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
	}
}

//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 BatchGetHeadwearByIds(model *domain.Model, ids []mysql.ID) (map[uint64]ResHeadwear, error) {
	resHeadwears := []ResHeadwear{}
	if err := model.Db.Model(&ResHeadwear{}).Where("id in (?)", ids).Find(&resHeadwears).Error; err != nil {
		return nil, myerr.WrapErr(err)
	}
	resHeadwearMap := map[uint64]ResHeadwear{}
	for i, r := range resHeadwears {
		resHeadwearMap[r.ID] = resHeadwears[i]
	}
	return resHeadwearMap, 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
}