package db import ( "applet/app/db/model" "applet/app/utils/logx" "reflect" "xorm.io/xorm" ) type NoticeDb struct { Db *xorm.Engine `json:"db"` } func (noticeDb *NoticeDb) Set() { // set方法 noticeDb.Db = Db } func (noticeDb *NoticeDb) GetNotice(id int) (m *model.Notice, err error) { m = new(model.Notice) has, err := noticeDb.Db.Where("id =?", id).Get(m) if err != nil { return nil, logx.Error(err) } if has == false { return nil, nil } return m, nil } func (noticeDb *NoticeDb) FindNoticeById(ids interface{}) (*[]model.Notice, error) { var m []model.Notice if err := noticeDb.Db.In("id", ids).Desc("sort").Find(&m); err != nil { return nil, logx.Error(err) } return &m, nil } func (noticeDb *NoticeDb) FindNotice(limit, start int) (*[]model.Notice, error) { var m []model.Notice if limit == 0 || start == 0 { if err := noticeDb.Db.Asc("sort").Find(&m); err != nil { return nil, logx.Error(err) } } else { if err := noticeDb.Db.Asc("sort").Limit(limit, start).Find(m); err != nil { return nil, logx.Error(err) } } return &m, nil } func (noticeDb *NoticeDb) GetNoticeByName(name string) (m *model.Notice, err error) { m = new(model.Notice) has, err := noticeDb.Db.Where("name =?", name).Get(m) if err != nil { return nil, logx.Error(err) } if has == false { return nil, nil } return m, nil } func (noticeDb *NoticeDb) NoticeInsert(m *model.Notice) (int, error) { _, err := noticeDb.Db.InsertOne(m) if err != nil { return 0, err } return m.Id, nil } func (noticeDb *NoticeDb) NoticeUpdate(m *model.Notice, columns ...string) (int64, error) { affected, err := noticeDb.Db.Where("id =?", m.Id).Cols(columns...).Update(m) if err != nil { return 0, err } return affected, nil } func (noticeDb *NoticeDb) NoticeDelete(id interface{}) (int64, error) { if reflect.TypeOf(id).Kind() == reflect.Slice { return Db.In("id", id).Delete(model.Notice{}) } else { return Db.Where("id = ?", id).Delete(model.Notice{}) } }