package db import ( model2 "applet/app/db/model" "applet/app/utils/logx" "reflect" "xorm.io/xorm" ) type ClassWithUser struct { model2.ClassWithUser `xorm:"extends"` model2.UserIdentity `xorm:"extends"` model2.Class `xorm:"extends"` model2.Grade `xorm:"extends"` model2.Enterprise `xorm:"extends"` } func (ClassWithUser) TableName() string { return "class_with_user" } type ClassWithUserDb struct { Db *xorm.Engine `json:"db"` } func (classWithUserDb *ClassWithUserDb) Set() { // set方法 classWithUserDb.Db = Db } func (classWithUserDb *ClassWithUserDb) GetInfoByUserIdentityId(userIdentityId int) (m *ClassWithUser, err error) { m = new(ClassWithUser) has, err := classWithUserDb.Db.Where("class_with_user.user_identity_id =?", userIdentityId). Join("LEFT", "user_identity", "class_with_user.user_identity_id = user_identity.id"). Join("LEFT", "class", "class_with_user.class_id = class.id"). Join("LEFT", "grade", "class.grade_id = grade.id"). Join("LEFT", "enterprise", "grade.enterprise_id = enterprise.id"). Get(m) if err != nil { return nil, logx.Error(err) } if has == false { return nil, nil } return m, nil } func (classWithUserDb *ClassWithUserDb) GetClassWithUserByUserIdentityId(userIdentityId int) (m *model2.ClassWithUser, err error) { m = new(model2.ClassWithUser) has, err := classWithUserDb.Db.Where("user_identity_id =?", userIdentityId).Get(m) if err != nil { return nil, logx.Error(err) } if has == false { return nil, nil } return m, nil } func (classWithUserDb *ClassWithUserDb) ClassWithUserInsert(m *model2.ClassWithUser) (int, error) { _, err := classWithUserDb.Db.InsertOne(m) if err != nil { return 0, err } return m.Id, nil } func (classWithUserDb *ClassWithUserDb) ClassWithUserInsertBySession(session *xorm.Session, m *model2.ClassWithUser) (int, error) { _, err := session.InsertOne(m) if err != nil { return 0, err } return m.Id, nil } func (classWithUserDb *ClassWithUserDb) FindUserIdentity(classId interface{}) (*[]model2.ClassWithUser, error) { var m []model2.ClassWithUser if reflect.TypeOf(classId).Kind() == reflect.Slice { if err := classWithUserDb.Db.In("class_id", classId).Desc("id").Find(&m); err != nil { return nil, logx.Error(err) } } else { if err := classWithUserDb.Db.Where("class_id =?", classId).Desc("id").Find(&m); err != nil { return nil, logx.Error(err) } } return &m, nil } func (classWithUserDb *ClassWithUserDb) ClassWithUserUpdateByUserIdentity(userIdentityId interface{}, m *model2.ClassWithUser, forceColums ...string) (int64, error) { var ( affected int64 err error ) if forceColums != nil { affected, err = classWithUserDb.Db.Where("user_identity_id=?", userIdentityId).Cols(forceColums...).Update(m) } else { affected, err = classWithUserDb.Db.Where("user_identity_id=?", userIdentityId).Update(m) } if err != nil { return 0, err } return affected, nil }