package db import ( "applet/app/db/model" "applet/app/utils/logx" "reflect" "xorm.io/xorm" ) type MerchantDb struct { Db *xorm.Engine `json:"db"` } func (merchantDb *MerchantDb) Set() { // set方法 merchantDb.Db = Db } func (merchantDb *MerchantDb) GetMerchant(id int) (m *model.Merchant, err error) { m = new(model.Merchant) has, err := merchantDb.Db.Where("id =?", id).Get(m) if err != nil { return nil, logx.Error(err) } if has == false { return nil, nil } return m, nil } func (merchantDb *MerchantDb) GetPopToCentralKitchen() (m *model.Merchant, err error) { m = new(model.Merchant) has, err := merchantDb.Db.Where("is_pop_to_central_kitchen =?", 1).Get(m) if err != nil { return nil, logx.Error(err) } if has == false { return nil, nil } return m, nil } func (merchantDb *MerchantDb) FindMerchantById(ids interface{}) (*[]model.Merchant, error) { var m []model.Merchant if err := merchantDb.Db.In("id", ids).Desc("sort").Find(&m); err != nil { return nil, logx.Error(err) } return &m, nil } func (merchantDb *MerchantDb) FindMerchant(limit, start int) (*[]model.Merchant, error) { var m []model.Merchant if limit == 0 || start == 0 { if err := merchantDb.Db.Asc("sort").Find(&m); err != nil { return nil, logx.Error(err) } } else { if err := merchantDb.Db.Asc("sort").Limit(limit, start).Find(m); err != nil { return nil, logx.Error(err) } } return &m, nil } func (merchantDb *MerchantDb) GetMerchantByName(name string) (m *model.Merchant, err error) { m = new(model.Merchant) has, err := merchantDb.Db.Where("name =?", name).Get(m) if err != nil { return nil, logx.Error(err) } if has == false { return nil, nil } return m, nil } func (merchantDb *MerchantDb) MerchantInsert(m *model.Merchant) (int, error) { _, err := merchantDb.Db.InsertOne(m) if err != nil { return 0, err } return m.Id, nil } func (merchantDb *MerchantDb) MerchantUpdate(m *model.Merchant, columns ...string) (int64, error) { affected, err := merchantDb.Db.Where("id =?", m.Id).Cols(columns...).Update(m) if err != nil { return 0, err } return affected, nil } func (merchantDb *MerchantDb) MerchantDelete(id interface{}) (int64, error) { if reflect.TypeOf(id).Kind() == reflect.Slice { return Db.In("id", id).Delete(model.Merchant{}) } else { return Db.Where("id = ?", id).Delete(model.Merchant{}) } } type MerchantWithEnterpriseInfo struct { model.Merchant `xorm:"extends"` model.Enterprise `xorm:"extends"` } func (MerchantWithEnterpriseInfo) TableName() string { return "merchant" }