package db import ( "applet/app/db/model" "applet/app/utils/logx" "reflect" "xorm.io/xorm" ) type EnterpriseDb struct { Db *xorm.Engine `json:"db"` } func (enterpriseDb *EnterpriseDb) Set() { // set方法 enterpriseDb.Db = Db } func (enterpriseDb *EnterpriseDb) GetEnterprise(id int) (m *model.Enterprise, err error) { m = new(model.Enterprise) has, err := enterpriseDb.Db.Where("id =?", id).Get(m) if err != nil { return nil, logx.Error(err) } if has == false { return nil, nil } return m, nil } func (enterpriseDb *EnterpriseDb) EnterpriseDeleteBySession(session *xorm.Session, id interface{}) (int64, error) { if reflect.TypeOf(id).Kind() == reflect.Slice { return session.In("id", id).Delete(model.Enterprise{}) } else { return session.Where("id = ?", id).Delete(model.Enterprise{}) } } func (enterpriseDb *EnterpriseDb) FindEnterprise(limit, start int) (*[]model.Enterprise, error) { var m []model.Enterprise if limit == 0 || start == 0 { if err := enterpriseDb.Db.Desc("id").Find(&m); err != nil { return nil, logx.Error(err) } } else { if err := enterpriseDb.Db.Desc("id").Limit(limit, start).Find(m); err != nil { return nil, logx.Error(err) } } return &m, nil } func (enterpriseDb *EnterpriseDb) GetEnterpriseByName(name string) (m *model.Enterprise, err error) { m = new(model.Enterprise) has, err := enterpriseDb.Db.Where("name =?", name).Get(m) if err != nil { return nil, logx.Error(err) } if has == false { return nil, nil } return m, nil } func (enterpriseDb *EnterpriseDb) EnterpriseInsert(m *model.Enterprise) (int, error) { _, err := enterpriseDb.Db.InsertOne(m) if err != nil { return 0, err } return m.Id, nil } func (enterpriseDb *EnterpriseDb) EnterpriseUpdate(m *model.Enterprise, columns ...string) (int64, error) { affected, err := enterpriseDb.Db.Where("id =?", m.Id).Cols(columns...).Update(m) if err != nil { return 0, err } return affected, nil } func GetEnterpriseMore(eg *xorm.Engine, enterpriseIds []int) map[int]model.Enterprise { enterpriseMap := make(map[int]model.Enterprise) if len(enterpriseIds) > 0 { enterpriseData := make([]model.Enterprise, 0) eg.In("id", enterpriseIds).Find(&enterpriseData) for _, v := range enterpriseData { enterpriseMap[v.Id] = v } } return enterpriseMap } func GetEnterpriseList(eg *xorm.Engine) *[]model.Enterprise { var data []model.Enterprise err := eg.Where("1=1").Find(&data) if err != nil { return nil } return &data }