|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124 |
- 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) FindClassByEnterprise(enterpriseId interface{}) (*[]model.Class, error) {
- var m []model.Class
- if err := classDb.Db.Desc("id").Where("enterprise_id =?", enterpriseId).Find(&m); err != nil {
- return nil, logx.Error(err)
- }
- return &m, nil
- }
-
- func (classDb *ClassDb) FindClassAscByEnterprise(enterpriseId interface{}) (*[]model.Class, error) {
- var m []model.Class
- if err := classDb.Db.Asc("id").Where("enterprise_id =?", enterpriseId).Find(&m); err != nil {
- return nil, logx.Error(err)
- }
- return &m, nil
- }
-
- func (classDb *ClassDb) FindClassSortAscByEnterprise(enterpriseId interface{}) (*[]model.Class, error) {
- var m []model.Class
- if err := classDb.Db.Asc("sort").Where("enterprise_id =?", enterpriseId).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) FindClassById(ids interface{}) (*[]model.Class, error) {
- var m []model.Class
- if err := classDb.Db.In("id", ids).Find(&m); err != nil {
- return nil, logx.Error(err)
- }
- return &m, nil
- }
-
- 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
- }
|