|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778 |
- 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) FindGradeByPeriodId(periodId int) (*[]model.Grade, error) {
- var m []model.Grade
- if err := gradeDb.Db.Desc("id").Where("enterprise_id =?", gradeDb.EnterpriseId).And("period_id =?", periodId).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
- }
|