|
- package db
-
- import (
- "applet/app/db/model"
- "applet/app/utils/logx"
- "reflect"
- "xorm.io/xorm"
- )
-
- type StaffDb struct {
- Db *xorm.Engine `json:"db"`
- }
-
- func (staffDb *StaffDb) Set() { // set方法
- staffDb.Db = Db
- }
-
- func (staffDb *StaffDb) GetStaff(id int) (m *model.Staff, err error) {
- m = new(model.Staff)
- has, err := staffDb.Db.Where("id =?", id).Get(m)
- if err != nil {
- return nil, logx.Error(err)
- }
- if has == false {
- return nil, nil
- }
- return m, nil
- }
-
- func (staffDb *StaffDb) FindStaff(limit, start int) (*[]model.Staff, error) {
- var m []model.Staff
- if limit == 0 || start == 0 {
- if err := staffDb.Db.Desc("id").Find(&m); err != nil {
- return nil, logx.Error(err)
- }
- } else {
- if err := staffDb.Db.Desc("id").Limit(limit, start).Find(m); err != nil {
- return nil, logx.Error(err)
- }
- }
- return &m, nil
- }
-
- func (staffDb *StaffDb) GetStaffByName(name string) (m *model.Staff, err error) {
- m = new(model.Staff)
- has, err := staffDb.Db.Where("name =?", name).Get(m)
- if err != nil {
- return nil, logx.Error(err)
- }
- if has == false {
- return nil, nil
- }
- return m, nil
- }
-
- func (staffDb *StaffDb) StaffInsert(m *model.Staff) (int, error) {
- _, err := staffDb.Db.InsertOne(m)
- if err != nil {
- return 0, err
- }
- return m.Id, nil
- }
-
- func (staffDb *StaffDb) StaffUpdate(m *model.Staff, columns ...string) (int64, error) {
- affected, err := staffDb.Db.Where("id =?", m.Id).Cols(columns...).Update(m)
- if err != nil {
- return 0, err
- }
- return affected, nil
- }
-
- func (staffDb *StaffDb) StaffDelete(id interface{}) (int64, error) {
- if reflect.TypeOf(id).Kind() == reflect.Slice {
- return Db.In("id", id).Delete(model.Staff{})
- } else {
- return Db.Where("id = ?", id).Delete(model.Staff{})
- }
- }
- func GetStaff(eg *xorm.Engine) *[]model.Staff {
- var data []model.Staff
- err := eg.Where("1=1").Find(&data)
- if err != nil {
- return nil
- }
- return &data
- }
- func GetStaffMore(eg *xorm.Engine, ids []int) map[int]model.Staff {
-
- data := make(map[int]model.Staff)
- if len(ids) > 0 {
- list := make([]model.Staff, 0)
- eg.In("id", ids).Find(&list)
- for _, v := range list {
- data[v.Id] = v
- }
- }
- return data
- }
|