package db import ( "applet/app/db/model" "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) (m *model.UserIdentity, err error) { m = new(model.UserIdentity) has, err := userIdentityDb.Db.Where("enterprise_id =?", enterpriseId).And("id_no =?", idNo).Get(m) if err != nil { return nil, logx.Error(err) } if has == false { return nil, nil } return m, 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.uid =?", userIdentityDb.Uid).Desc("user_identity.id").Find(&m); err != nil { return nil, logx.Error(err) } 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"` } 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" }