|
- package db
-
- import (
- "applet/app/db/model"
- "applet/app/utils/logx"
- "reflect"
- "xorm.io/xorm"
- )
-
- type BannerDb struct {
- Db *xorm.Engine `json:"db"`
- }
-
- func (bannerDb *BannerDb) Set() { // set方法
- bannerDb.Db = Db
- }
-
- func (bannerDb *BannerDb) GetBanner(id int) (m *model.Banner, err error) {
- m = new(model.Banner)
- has, err := bannerDb.Db.Where("id =?", id).Get(m)
- if err != nil {
- return nil, logx.Error(err)
- }
- if has == false {
- return nil, nil
- }
- return m, nil
- }
-
- func (bannerDb *BannerDb) FindBannerById(ids interface{}) (*[]model.Banner, error) {
- var m []model.Banner
- if err := bannerDb.Db.In("id", ids).Desc("sort").Find(&m); err != nil {
- return nil, logx.Error(err)
- }
- return &m, nil
- }
-
- func (bannerDb *BannerDb) FindBanner(limit, start int) (*[]model.Banner, error) {
- var m []model.Banner
- if limit == 0 || start == 0 {
- if err := bannerDb.Db.Desc("sort").Find(&m); err != nil {
- return nil, logx.Error(err)
- }
- } else {
- if err := bannerDb.Db.Desc("sort").Limit(limit, start).Find(m); err != nil {
- return nil, logx.Error(err)
- }
- }
- return &m, nil
- }
-
- func (bannerDb *BannerDb) GetBannerByName(name string) (m *model.Banner, err error) {
- m = new(model.Banner)
- has, err := bannerDb.Db.Where("name =?", name).Get(m)
- if err != nil {
- return nil, logx.Error(err)
- }
- if has == false {
- return nil, nil
- }
- return m, nil
- }
-
- func (bannerDb *BannerDb) BannerInsert(m *model.Banner) (int, error) {
- _, err := bannerDb.Db.InsertOne(m)
- if err != nil {
- return 0, err
- }
- return m.Id, nil
- }
-
- func (bannerDb *BannerDb) BannerUpdate(m *model.Banner, columns ...string) (int64, error) {
- affected, err := bannerDb.Db.Where("id =?", m.Id).Cols(columns...).Update(m)
- if err != nil {
- return 0, err
- }
- return affected, nil
- }
-
- func (bannerDb *BannerDb) BannerDelete(id interface{}) (int64, error) {
- if reflect.TypeOf(id).Kind() == reflect.Slice {
- return Db.In("id", id).Delete(model.Banner{})
- } else {
- return Db.Where("id = ?", id).Delete(model.Banner{})
- }
- }
|