package db import ( "applet/app/db/model" "applet/app/enum" "applet/app/utils/logx" "reflect" "xorm.io/xorm" ) type UserIdentityDb struct { Db *xorm.Engine `json:"db"` Uid int `json:"uid"` } func (userIdentityDb *UserIdentityDb) Set(uid int) { // set方法 userIdentityDb.Db = Db userIdentityDb.Uid = uid } func (userIdentityDb *UserIdentityDb) GetUserIdentity(id int) (m *model.UserIdentity, err error) { m = new(model.UserIdentity) has, err := userIdentityDb.Db.Where("id =?", id).Get(m) if err != nil { return nil, logx.Error(err) } if has == false { return nil, nil } return m, nil } func (userIdentityDb *UserIdentityDb) UserIdentityExist(enterpriseId int, idNo string, certType int) (m *model.UserIdentity, err error) { m = new(model.UserIdentity) has, err := userIdentityDb.Db.Where("enterprise_id =?", enterpriseId).And("id_no =?", idNo).And("cert_type =?", certType).Get(m) if err != nil { return nil, logx.Error(err) } if has == false { return nil, nil } return m, nil } func (userIdentityDb *UserIdentityDb) UserIdentityExistV1(enterpriseId int, name string) (m *model.UserIdentity, err error) { m = new(model.UserIdentity) has, err := userIdentityDb.Db.Where("enterprise_id =?", enterpriseId).And("uid =?", userIdentityDb.Uid).And("name =?", name).Get(m) if err != nil { return nil, logx.Error(err) } if has == false { return nil, nil } return m, nil } //func (userIdentityDb *UserIdentityDb) UserIdentityExistV2(enterpriseId int, name string, classId int) (m *model.UserIdentity, err error) { // m = new(model.UserIdentity) // var mm []model.UserIdentity // if err = userIdentityDb.Db.Where("enterpriseId =?", enterpriseId).And("uid =?", userIdentityDb.Uid).And("name =?", name).Find(&mm); err != nil { // return nil, logx.Error(err) // } // if len(mm) <= 0 { // return nil, nil // } // classWithUserDb := ClassWithUserDb{} // classWithUserDb.Set() // for _, v := range mm { // //查找 class_with_user // class, err1 := classWithUserDb.GetClassWithUserByUserIdentityId(v.Id) // if err1 != nil { // return nil, err // } // //if class == nil { // // return nil, errors.New("当前学生身份待管理员确认") // //} // if class.ClassId == classId { // return m, nil // } // } // return nil, nil //} func (userIdentityDb *UserIdentityDb) FindUserIdentity() (*[]UserIdentityWithEnterprise, error) { var m []UserIdentityWithEnterprise if err := userIdentityDb.Db. Join("LEFT", "enterprise", "user_identity.enterprise_id = enterprise.id"). Where("user_identity.state =?", enum.UserIdentityStateForNormal). And("user_identity.uid =?", userIdentityDb.Uid).Desc("user_identity.id").Find(&m); err != nil { return nil, logx.Error(err) } return &m, nil } func (userIdentityDb *UserIdentityDb) FindUserIdentityByKind(identity int) (*[]UserIdentityWithEnterprise, error) { var m []UserIdentityWithEnterprise if err := userIdentityDb.Db. Join("LEFT", "enterprise", "user_identity.enterprise_id = enterprise.id"). Where("user_identity.state =?", enum.UserIdentityStateForNormal). And("user_identity.identity =?", identity). And("user_identity.uid =?", userIdentityDb.Uid).Desc("user_identity.id").Find(&m); err != nil { return nil, logx.Error(err) } return &m, nil } func (userIdentityDb *UserIdentityDb) FindUserIdentityWithClass() (*[]UserIdentityWithClass, error) { var m []UserIdentityWithClass if err := userIdentityDb.Db. Join("LEFT", "enterprise", "user_identity.enterprise_id = enterprise.id"). Join("LEFT", "user", "user_identity.uid = user.id"). Join("LEFT", "class_with_user", "user_identity.id = class_with_user.user_identity_id"). Join("LEFT", "class", "class_with_user.class_id = class.id"). Join("LEFT", "grade", "class.grade_id = grade.id"). Where("user_identity.state =?", enum.UserIdentityStateForNormal). And("user_identity.uid =?", userIdentityDb.Uid).Desc("user_identity.id").Find(&m); err != nil { return nil, logx.Error(err) } return &m, nil } func (userIdentityDb *UserIdentityDb) UserIdentity(id int) (m *UserIdentityWithEnterprise, err error) { m = new(UserIdentityWithEnterprise) has, err := userIdentityDb.Db.Join("LEFT", "enterprise", "user_identity.enterprise_id = enterprise.id"). And("user_identity.id =?", id).Desc("user_identity.id").Get(m) if err != nil { return nil, logx.Error(err) } if has == false { return nil, nil } return m, nil } func (userIdentityDb *UserIdentityDb) UserIdentityInsert(m *model.UserIdentity) (int, error) { _, err := userIdentityDb.Db.InsertOne(m) if err != nil { return 0, err } return m.Id, nil } func (userIdentityDb *UserIdentityDb) UserIdentityInsertBySession(session *xorm.Session, m *model.UserIdentity) (int, error) { _, err := session.InsertOne(m) if err != nil { return 0, err } return m.Id, nil } func (userIdentityDb *UserIdentityDb) BatchAddUserIdentities(mm []*model.UserIdentity) (int64, error) { affected, err := userIdentityDb.Db.Insert(mm) if err != nil { return 0, err } return affected, nil } func (userIdentityDb *UserIdentityDb) UserIdentityDelete(id interface{}) (int64, error) { if reflect.TypeOf(id).Kind() == reflect.Slice { return Db.In("id", id).Delete(model.UserIdentity{}) } else { return Db.Where("id = ?", id).Delete(model.UserIdentity{}) } } func (userIdentityDb *UserIdentityDb) UserIdentityUpdate(id interface{}, m *model.UserIdentity, forceColums ...string) (int64, error) { var ( affected int64 err error ) if forceColums != nil { affected, err = userIdentityDb.Db.Where("id=?", id).Cols(forceColums...).Update(m) } else { affected, err = userIdentityDb.Db.Where("id=?", id).Update(m) } if err != nil { return 0, err } return affected, nil } func (userIdentityDb *UserIdentityDb) UserIdentityUpdateBySession(session *xorm.Session, id interface{}, m *model.UserIdentity, forceColums ...string) (int64, error) { var ( affected int64 err error ) if forceColums != nil { affected, err = session.Where("id=?", id).Cols(forceColums...).Update(m) } else { affected, err = session.Where("id=?", id).Update(m) } if err != nil { return 0, err } return affected, nil } func (userIdentityDb *UserIdentityDb) CountUserIdentityForEnterprise(enterpriseId, identity int) (total int64, err error) { var m model.UserIdentity total, err = userIdentityDb.Db.Where("enterprise_id =? AND identity =?", enterpriseId, identity).Count(&m) if err != nil { return } return } func (userIdentityDb *UserIdentityDb) FindUserIdentityForEnterpriseByIdentity(enterpriseId, identity int) (*[]model.UserIdentity, error) { var m []model.UserIdentity err := userIdentityDb.Db.Where("enterprise_id =? AND identity =?", enterpriseId, identity).Find(&m) if err != nil { return nil, err } return &m, nil } func (userIdentityDb *UserIdentityDb) FindUserIdentityForEnterprise(enterpriseId int) (*[]model.UserIdentity, error) { var m []model.UserIdentity err := userIdentityDb.Db.Where("enterprise_id =?", enterpriseId).Find(&m) if err != nil { return nil, err } return &m, nil } type UserIdentityWithUser struct { model.UserIdentity `xorm:"extends"` model.User `xorm:"extends"` model.ClassWithUser `xorm:"extends"` model.Class `xorm:"extends"` model.Grade `xorm:"extends"` model.SelfSupportForUserFaceInfo `xorm:"extends"` model.Enterprise `xorm:"extends"` } func (UserIdentityWithUser) TableName() string { return "user_identity" } type UserIdentityWithEnterprise struct { model.UserIdentity `xorm:"extends"` model.Enterprise `xorm:"extends"` } func (UserIdentityWithEnterprise) TableName() string { return "user_identity" } type UserIdentityWithClass struct { model.UserIdentity `xorm:"extends"` model.User `xorm:"extends"` model.Enterprise `xorm:"extends"` model.ClassWithUser `xorm:"extends"` model.Class `xorm:"extends"` model.Grade `xorm:"extends"` } func (UserIdentityWithClass) TableName() string { return "user_identity" }