package models import ( "github.com/beego/beego/v2/client/orm" "time" ) func (i *ImageSource) TableName() string { return "image_sources" } type ImageSource struct { Id int ` description:"Id"` ImgName string `description:"图片名称"` SrcUrl string `description:"图片资源"` PermissionId int `description:"品种"` Deleted int `description:"是否删除"` CreatedTime time.Time `description:"创建时间"` UpdatedTime time.Time `description:"更新时间"` } type ImageSourceView struct { Id int ` description:"Id"` ImgName string `description:"图片名称"` SrcUrl string `description:"图片资源"` PermissionId int `description:"品种名称"` PermissionName string `description:"品种名称"` CreatedTime string `description:"创建时间"` } func (i *ImageSource) ToView() (item *ImageSourceView, err error) { item = &ImageSourceView{ Id: i.Id, ImgName: i.ImgName, SrcUrl: i.SrcUrl, PermissionId: i.PermissionId, CreatedTime: i.CreatedTime.Format(time.DateTime), } name, err := GetPermissionNameById(i.PermissionId) if err != nil { return } item.PermissionName = name return } func (i *ImageSource) Insert() (insertId int64, err error) { o := orm.NewOrm() insertId, err = o.Insert(i) return } func (i *ImageSource) Update() (err error) { o := orm.NewOrm() _, err = o.Update(i, "img_name", "src_url", "permission_id") return } func (i *ImageSource) Delete() (err error) { o := orm.NewOrm() _, err = o.Update(i, "deleted") return } func GetImageByPermissionId(permissionId int) (items []ImageSource, err error) { o := orm.NewOrm() sql := "select id,src_url from image_sources where permission_id = ? " _, err = o.Raw(sql, permissionId).QueryRows(&items) return } func GetImageById(Id int) (item *ImageSource, err error) { o := orm.NewOrm() sql := "select id,src_url from image_sources where id = ?" err = o.Raw(sql, Id).QueryRow(&item) return } func GetImageCountByCondition(condition string, pars []interface{}) (count int, err error) { o := orm.NewOrm() sql := `SELECT COUNT(*) AS count FROM image_sources WHERE 1 =1 AND deleted=0 ` if condition != "" { sql += condition } err = o.Raw(sql, pars).QueryRow(&count) return } func GetImageByCondition(condition string, sortCondition string, pars []interface{}, startPage, pageSize int) (items []*ImageSource, err error) { o := orm.NewOrm() sql := `SELECT * FROM image_sources WHERE 1 = 1 AND deleted=0` if condition != "" { sql += condition } if sortCondition != "" { sql += sortCondition // 排序 } sql += ` LIMIT ?, ?` _, err = o.Raw(sql, pars, startPage, pageSize).QueryRows(&items) return }