package db import ( "applet/app/admin/md" "applet/app/db/model" "applet/app/utils/logx" "reflect" "xorm.io/xorm" ) type ExportRecordsDb struct { Db *xorm.Engine `json:"db"` } func (exportRecordsDb *ExportRecordsDb) Set() { // set方法 exportRecordsDb.Db = Db } func (exportRecordsDb *ExportRecordsDb) GetExportRecordsExportRecords(id int) (m *model.ExportRecords, err error) { m = new(model.ExportRecords) has, err := exportRecordsDb.Db.Where("id =?", id).Get(m) if err != nil { return nil, logx.Error(err) } if has == false { return nil, nil } return m, nil } func (exportRecordsDb *ExportRecordsDb) GetExportRecordsExportRecordsByMonth(year, month string) (m *model.ExportRecords, err error) { m = new(model.ExportRecords) has, err := exportRecordsDb.Db.Where("year =? and month =?", year, month).Get(m) if err != nil { return nil, logx.Error(err) } if has == false { return nil, nil } return m, nil } func (exportRecordsDb *ExportRecordsDb) FindExportRecordsExportRecords() (*[]model.ExportRecords, error) { var m []model.ExportRecords if err := exportRecordsDb.Db.Desc("id").Find(&m); err != nil { return nil, logx.Error(err) } return &m, nil } func (exportRecordsDb *ExportRecordsDb) ExportRecordsExportRecordsInsert(m *model.ExportRecords) (int, error) { _, err := exportRecordsDb.Db.InsertOne(m) if err != nil { return 0, err } return m.Id, nil } func (exportRecordsDb *ExportRecordsDb) ExportRecordsExportRecordsInsertBySession(session *xorm.Session, m *model.ExportRecords) (int, error) { _, err := session.InsertOne(m) if err != nil { return 0, err } return m.Id, nil } func (exportRecordsDb *ExportRecordsDb) ExportRecordsExportRecordsDelete(id interface{}) (int64, error) { if reflect.TypeOf(id).Kind() == reflect.Slice { return Db.In("id", id).Delete(model.ExportRecords{}) } else { return Db.Where("id = ?", id).Delete(model.ExportRecords{}) } } func (exportRecordsDb *ExportRecordsDb) ExportRecordsExportRecordsUpdateBySession(session *xorm.Session, id interface{}, m *model.ExportRecords, forceColums ...string) (int64, error) { var ( affected int64 err error ) if forceColums != nil { affected, err = session.Where("id=?", id).Cols(forceColums...).Update(m) } else { affected, err = session.Where("id=?", id).Update(m) } if err != nil { return 0, err } return affected, nil } func (exportRecordsDb *ExportRecordsDb) ExportRecordsExportRecordsList(req md.ExportRecordsListReq) (m []model.ExportRecords, total int64, err error) { sess := exportRecordsDb.Db.Desc("id") if req.Kind != 0 { sess.And("kind =?", req.Kind) } if req.StartDate != "" { sess.And("create_at >=?", req.StartDate) } if req.EndDate != "" { sess.And("create_at <=?", req.EndDate) } total, err = sess.Limit(req.Limit, (req.Page-1)*req.Limit).FindAndCount(&m) if err != nil { return } return }