72 lines
2.0 KiB
Go
72 lines
2.0 KiB
Go
package model
|
|
|
|
import (
|
|
"f5"
|
|
"errors"
|
|
"main/constant"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
type Bag struct {
|
|
Idx int64 `gorm:"column:idx;AUTO_INCREMENT;primaryKey"`
|
|
AccountId string `gorm:"column:account_id"`
|
|
ItemId int32 `gorm:"column:item_id"`
|
|
ItemNum int32 `gorm:"column:item_num"`
|
|
CreateTime int32 `gorm:"column:createtime;<-:create"`
|
|
ModifyTime int32 `gorm:"column:modifytime"`
|
|
}
|
|
|
|
func (this *Bag) TableName() string {
|
|
return "t_bag"
|
|
}
|
|
|
|
func (this *Bag) Create() error {
|
|
if result := f5.GetApp().GetOrmDb(constant.WHEEL_DB).Create(this); result.Error != nil {
|
|
return result.Error
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (this *Bag) FindByItemId(accountId string, itemId int32) (error, bool) {
|
|
if result := f5.GetApp().GetOrmDb(constant.WHEEL_DB).Table(this.TableName()).Take(
|
|
this, "account_id = ? and item_id = ?", accountId, itemId); result.Error != nil &&
|
|
!errors.Is(result.Error, gorm.ErrRecordNotFound) {
|
|
return result.Error, false
|
|
} else {
|
|
return nil, result.RowsAffected > 0
|
|
}
|
|
}
|
|
|
|
func (this *Bag) FindByItemUniId(accountId string, itemUniId int64) (error, bool) {
|
|
if result := f5.GetApp().GetOrmDb(constant.WHEEL_DB).Table(this.TableName()).Take(
|
|
this, "account_id = ? and idx = ?", accountId, itemUniId); result.Error != nil &&
|
|
!errors.Is(result.Error, gorm.ErrRecordNotFound) {
|
|
return result.Error, false
|
|
} else {
|
|
return nil, result.RowsAffected > 0
|
|
}
|
|
}
|
|
|
|
func (this *Bag) AddItemNum(itemNum int32, nowTime int32) error {
|
|
this.ItemNum += itemNum
|
|
this.ModifyTime = nowTime
|
|
if result := f5.GetApp().GetOrmDb(constant.WHEEL_DB).Model(this).Select(
|
|
"item_num", "modifytime").Updates(this); result.Error != nil {
|
|
return result.Error
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (this *Bag) DecItemNum(itemNum int32, nowTime int32) error {
|
|
this.ItemNum -= itemNum
|
|
if this.ItemNum < 0 {
|
|
this.ItemNum = 0
|
|
}
|
|
this.ModifyTime = nowTime
|
|
if result := f5.GetApp().GetOrmDb(constant.WHEEL_DB).Model(this).Select(
|
|
"item_num", "modifytime").Updates(this); result.Error != nil {
|
|
return result.Error
|
|
}
|
|
return nil
|
|
}
|