|
- package db
-
- import (
- "applet/app/db/model"
- "applet/app/utils/logx"
- "reflect"
- "xorm.io/xorm"
- )
-
- type CompanyDb struct {
- Db *xorm.Engine `json:"db"`
- }
-
- func (companyDb *CompanyDb) Set() { // set方法
- companyDb.Db = Db
- }
-
- func (companyDb *CompanyDb) GetCompany(id int) (m *model.Company, err error) {
- m = new(model.Company)
- has, err := companyDb.Db.Where("id =?", id).Get(m)
- if err != nil {
- return nil, logx.Error(err)
- }
- if has == false {
- return nil, nil
- }
- return m, nil
- }
-
- func (companyDb *CompanyDb) FindCompany(limit, start int) (*[]model.Company, error) {
- var m []model.Company
- if limit == 0 || start == 0 {
- if err := companyDb.Db.Desc("id").Find(&m); err != nil {
- return nil, logx.Error(err)
- }
- } else {
- if err := companyDb.Db.Desc("id").Limit(limit, start).Find(m); err != nil {
- return nil, logx.Error(err)
- }
- }
- return &m, nil
- }
-
- func (companyDb *CompanyDb) GetCompanyByName(name string) (m *model.Company, err error) {
- m = new(model.Company)
- has, err := companyDb.Db.Where("name =?", name).Get(m)
- if err != nil {
- return nil, logx.Error(err)
- }
- if has == false {
- return nil, nil
- }
- return m, nil
- }
-
- func (companyDb *CompanyDb) CompanyInsert(m *model.Company) (int, error) {
- _, err := companyDb.Db.InsertOne(m)
- if err != nil {
- return 0, err
- }
- return m.Id, nil
- }
-
- func (companyDb *CompanyDb) CompanyUpdate(m *model.Company, columns ...string) (int64, error) {
- affected, err := companyDb.Db.Where("id =?", m.Id).Cols(columns...).Update(m)
- if err != nil {
- return 0, err
- }
- return affected, nil
- }
-
- func (companyDb *CompanyDb) CompanyDelete(id interface{}) (int64, error) {
- if reflect.TypeOf(id).Kind() == reflect.Slice {
- return Db.In("id", id).Delete(model.Company{})
- } else {
- return Db.Where("id = ?", id).Delete(model.Company{})
- }
- }
-
- type EnterpriseWithCompany struct {
- model.Enterprise `xorm:"extends"`
- model.Company `xorm:"extends"`
- }
-
- func (EnterpriseWithCompany) TableName() string {
- return "enterprise"
- }
|