|
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586 |
- 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 (adminDb *AdminDb) FindEnterpriseGradeClass(enterpriseId int) (list []*AdminRolePermission, total int64, err error) {
- total, err = adminDb.Db.Where("enterprise.id =?", enterpriseId).
- Join("LEFT", "grade", "enterprise.id = grade.enterprise_id").
- Join("LEFT", "class", "class.grade_id = grade.id").
- FindAndCount(&list)
- return
- }
|