package db import ( "applet/app/db/model" "applet/app/utils/logx" "reflect" "xorm.io/xorm" ) type FactoryIdentityDb struct { Db *xorm.Engine `json:"db"` EnterpriseId int `json:"enterprise_id"` } func (factoryIdentityDb *FactoryIdentityDb) Set(enterpriseId int) { // set方法 factoryIdentityDb.Db = Db factoryIdentityDb.EnterpriseId = enterpriseId } func (factoryIdentityDb *FactoryIdentityDb) FindFactoryIdentity(page, limit int) (list *[]model.FactoryIdentity, count int64, err error) { var m []model.FactoryIdentity if count, err = factoryIdentityDb.Db.Where("enterprise_id = ?", factoryIdentityDb.EnterpriseId).Desc("id").Limit(limit, (page-1)*limit).FindAndCount(&m); err != nil { return nil, 0, logx.Error(err) } return &m, count, nil } func (factoryIdentityDb *FactoryIdentityDb) GetFactoryIdentity(id int) (m *model.FactoryIdentity, err error) { m = new(model.FactoryIdentity) has, err := factoryIdentityDb.Db.Where("id = ?", id).Get(m) if err != nil { return nil, logx.Error(err) } if has == false { return nil, nil } return m, nil } func (factoryIdentityDb *FactoryIdentityDb) GetFactoryIdentityByName(name string) (m *model.FactoryIdentity, err error) { m = new(model.FactoryIdentity) has, err := factoryIdentityDb.Db.Where("name = ?", name).Get(m) if err != nil { return nil, logx.Error(err) } if has == false { return nil, nil } return m, nil } func (factoryIdentityDb *FactoryIdentityDb) UpdateFactoryIdentity(m *model.FactoryIdentity, columns ...string) (int64, error) { affected, err := factoryIdentityDb.Db.Where("id =?", m.Id).Cols(columns...).Update(m) if err != nil { return 0, err } return affected, nil } func (factoryIdentityDb *FactoryIdentityDb) FactoryIdentityDeleteBySession(session *xorm.Session, id interface{}) (int64, error) { if reflect.TypeOf(id).Kind() == reflect.Slice { return session.In("id", id).Delete(model.FactoryIdentity{}) } else { return session.Where("id = ?", id).Delete(model.FactoryIdentity{}) } } func (factoryIdentityDb *FactoryIdentityDb) FactoryIdentityInsert(m *model.FactoryIdentity) (int, error) { _, err := factoryIdentityDb.Db.InsertOne(m) if err != nil { return 0, err } return m.Id, nil } func (factoryIdentityDb *FactoryIdentityDb) FindUserIdentityWithFactoryIdentity(userIdentityId int) (list []*UserIdentityWithFactoryIdentity, total int64, err error) { total, err = factoryIdentityDb.Db.Where("user_identity.id =?", userIdentityId). Join("LEFT", "factory_identity_with_user_identity", "factory_identity_with_user_identity.user_identity_id = user_identity.id"). Join("LEFT", "factory_identity", "factory_identity.id = factory_identity_with_user_identity.factory_identity_id"). FindAndCount(&list) return } type UserIdentityWithFactoryIdentity struct { model.UserIdentity `xorm:"extends"` model.User `xorm:"extends"` model.FactoryIdentityWithUserIdentity `xorm:"extends"` model.FactoryIdentity `xorm:"extends"` } func (UserIdentityWithFactoryIdentity) TableName() string { return "user_identity" } func (factoryIdentityDb *FactoryIdentityDb) FindFactoryIdentityWithUserIdentity(factoryIdentityId int) (list []*FactoryIdentityWithUserIdentity, total int64, err error) { total, err = factoryIdentityDb.Db.Where("factory_identity.id =?", factoryIdentityId). Join("LEFT", "factory_identity_with_user_identity", "factory_identity_with_user_identity.factory_identity_id = factory_identity.id"). Join("LEFT", "user_identity", "user_identity.id = factory_identity_with_user_identity.user_identity_id"). FindAndCount(&list) return } type FactoryIdentityWithUserIdentity struct { model.FactoryIdentity `xorm:"extends"` model.UserIdentity `xorm:"extends"` model.FactoryIdentityWithUserIdentity `xorm:"extends"` } func (FactoryIdentityWithUserIdentity) TableName() string { return "factory_identity" }