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