package db import ( "applet/app/db/model" "applet/app/utils/logx" "reflect" "xorm.io/xorm" ) type PeriodDb struct { Db *xorm.Engine `json:"db"` EnterpriseId int `json:"enterprise_id"` } func (PeriodDb *PeriodDb) Set(enterpriseId int) { // set方法 PeriodDb.Db = Db PeriodDb.EnterpriseId = enterpriseId } func (PeriodDb *PeriodDb) FindPeriod() (*[]model.Period, error) { var m []model.Period if err := PeriodDb.Db.Desc("id").Where("enterprise_id =?", PeriodDb.EnterpriseId).Find(&m); err != nil { return nil, logx.Error(err) } return &m, nil } func (PeriodDb *PeriodDb) PeriodDeleteBySession(id interface{}) (int64, error) { if reflect.TypeOf(id).Kind() == reflect.Slice { return PeriodDb.Db.In("id", id).Delete(model.Period{}) } else { return PeriodDb.Db.Where("id = ?", id).Delete(model.Period{}) } } func (PeriodDb *PeriodDb) ClassDeleteBySessionForEnterprise(session *xorm.Session, enterpriseId interface{}) (int64, error) { if reflect.TypeOf(enterpriseId).Kind() == reflect.Slice { return session.In("enterprise_id", enterpriseId).Delete(model.Period{}) } else { return session.Where("enterprise_id = ?", enterpriseId).Delete(model.Period{}) } } func (PeriodDb *PeriodDb) PeriodInsert(m *model.Period) (int, error) { _, err := PeriodDb.Db.InsertOne(m) if err != nil { return 0, err } return m.Id, nil } func (PeriodDb *PeriodDb) PeriodUpdate(m *model.Period, columns ...string) (int64, error) { affected, err := PeriodDb.Db.Where("id =?", m.Id).Cols(columns...).Update(m) if err != nil { return 0, err } return affected, nil } func (PeriodDb *PeriodDb) GetPeriod(id int) (m *model.Period, err error) { m = new(model.Period) has, err := PeriodDb.Db.Where("id =?", id).Get(m) if err != nil { return nil, logx.Error(err) } if has == false { return nil, nil } return m, nil }