| | |
| | | "aps_crm/pkg/mysqlx" |
| | | "fmt" |
| | | "gorm.io/gorm" |
| | | "time" |
| | | ) |
| | | |
| | | type ( |
| | | Menu struct { |
| | | ID uint `json:"id" gorm:"type:bigint(20);primaryKey"` // 主键ID |
| | | ParentId uint `json:"parentId" gorm:"index;type:bigint(20);comment:父菜单ID"` |
| | | Path string `json:"path" gorm:"type:varchar(255);comment:路由path"` |
| | | Name string `json:"name" gorm:"type:varchar(255);comment:name"` |
| | | Title string `json:"title" gorm:"type:varchar(255);comment:标题"` |
| | | Sort int `json:"sort" gorm:"type:int(11);comment:排序标记"` |
| | | Icon string `json:"icon" gorm:"type:varchar(512);comment:菜单图标"` |
| | | Hidden bool `json:"hidden" gorm:"type:tinyint(1);comment:是否隐藏"` |
| | | Type int `json:"type" gorm:"type:int(11);comment:类型 0-目录 1-菜单 2-按钮"` |
| | | CreateTime int64 `json:"-" gorm:"type:bigint(20);comment:创建时间"` |
| | | UpdateTime int64 `json:"-" gorm:"type:bigint(20);comment:更新时间"` |
| | | Children []*Menu `json:"children" gorm:"-"` |
| | | CreateAt string `json:"createAt" gorm:"-"` // 创建时间 |
| | | UpdateAt string `json:"updateAt" gorm:"-"` // 更新时间 |
| | | ID uint `json:"id" gorm:"type:bigint(20);primaryKey"` // 主键ID |
| | | ParentId uint `json:"parentId" gorm:"index;type:bigint(20);comment:父菜单ID"` |
| | | Path string `json:"path" gorm:"type:varchar(255);comment:路由path"` |
| | | Name string `json:"name" gorm:"type:varchar(255);comment:name"` |
| | | Title string `json:"title" gorm:"type:varchar(255);comment:标题"` |
| | | Children []*Menu `json:"children" gorm:"-"` |
| | | } |
| | | |
| | | MenuSearch struct { |
| | |
| | | |
| | | func (slf Menu) TableName() string { |
| | | return "menu" |
| | | } |
| | | |
| | | func (slf *Menu) BeforeCreate(tx *gorm.DB) error { |
| | | slf.CreateTime = time.Now().Unix() |
| | | slf.UpdateTime = slf.CreateTime |
| | | return nil |
| | | } |
| | | |
| | | func (slf *Menu) BeforeSave(tx *gorm.DB) error { |
| | | slf.UpdateTime = time.Now().Unix() |
| | | return nil |
| | | } |
| | | |
| | | func (slf *Menu) BeforeUpdate(tx *gorm.DB) error { |
| | | slf.UpdateTime = time.Now().Unix() |
| | | return nil |
| | | } |
| | | |
| | | func (slf *Menu) AfterFind(tx *gorm.DB) error { |
| | | slf.CreateAt = time.Unix(slf.CreateTime, 0).Format("2006-01-02 15:04:05") |
| | | slf.UpdateAt = time.Unix(slf.UpdateTime, 0).Format("2006-01-02 15:04:05") |
| | | return nil |
| | | } |
| | | |
| | | func NewMenuSearch(db *gorm.DB) *MenuSearch { |
| | |
| | | func (slf *MenuSearch) CreateBatch(records []*Menu) error { |
| | | var db = slf.build() |
| | | |
| | | if err := db.Create(&records).Error; err != nil { |
| | | if err := db.Save(&records).Error; err != nil { |
| | | return fmt.Errorf("create batch err: %v, records: %+v", err, records) |
| | | } |
| | | |
| | |
| | | |
| | | return records, nil |
| | | } |
| | | |
| | | func (slf *MenuSearch) FindAll() ([]*Menu, error) { |
| | | var ( |
| | | records = make([]*Menu, 0) |
| | | db = slf.build() |
| | | ) |
| | | |
| | | if err := db.Find(&records).Error; err != nil { |
| | | return records, fmt.Errorf("find all err: %v", err) |
| | | } |
| | | |
| | | return records, nil |
| | | } |