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