package db import ( "applet/app/db/model" "applet/app/utils/logx" "reflect" "xorm.io/xorm" ) type ClassDb struct { Db *xorm.Engine `json:"db"` GradeId int `json:"enterprise_id"` } func (classDb *ClassDb) Set(gradeId int) { // set方法 classDb.Db = Db classDb.GradeId = gradeId } func (classDb *ClassDb) FindClass() (*[]model.Class, error) { var m []model.Class if classDb.GradeId == 0 { if err := classDb.Db.Desc("id").Find(&m); err != nil { return nil, logx.Error(err) } } else { if err := classDb.Db.Desc("id").Where("grade_id =?", classDb.GradeId).Find(&m); err != nil { return nil, logx.Error(err) } } return &m, nil } func (classDb *ClassDb) ClassDeleteBySession(id interface{}) (int64, error) { if reflect.TypeOf(id).Kind() == reflect.Slice { return classDb.Db.In("id", id).Delete(model.Class{}) } else { return classDb.Db.Where("id = ?", id).Delete(model.Class{}) } } func (classDb *ClassDb) ClassDeleteBySessionForGrade(session *xorm.Session, gradeId interface{}) (int64, error) { if reflect.TypeOf(gradeId).Kind() == reflect.Slice { return session.In("grade_id", gradeId).Delete(model.Class{}) } else { return session.Where("grade_id = ?", gradeId).Delete(model.Class{}) } } func (classDb *ClassDb) ClassDeleteBySessionForEnterprise(session *xorm.Session, enterpriseId interface{}) (int64, error) { if reflect.TypeOf(enterpriseId).Kind() == reflect.Slice { return session.In("enterprise_id", enterpriseId).Delete(model.Class{}) } else { return session.Where("enterprise_id = ?", enterpriseId).Delete(model.Class{}) } } func (classDb *ClassDb) BatchAddClass(mm []*model.Class) (int64, error) { affected, err := classDb.Db.Insert(mm) if err != nil { return 0, err } return affected, nil } func (classDb *ClassDb) CountClassForEnterprise(enterpriseId int) (total int64, err error) { var m model.Class total, err = classDb.Db.Where("enterprise_id =?", enterpriseId).Count(&m) if err != nil { return } return } func (classDb *ClassDb) ClassUpdate(m *model.Class, columns ...string) (int64, error) { affected, err := classDb.Db.Where("id =?", m.Id).Cols(columns...).Update(m) if err != nil { return 0, err } return affected, nil } func (classDb *ClassDb) GetClass(id int) (m *model.Class, err error) { m = new(model.Class) has, err := classDb.Db.Where("id =?", id).Get(m) if err != nil { return nil, logx.Error(err) } if has == false { return nil, nil } return m, nil }