package db import ( "applet/app/db/model" "applet/app/enum" "applet/app/utils/logx" "xorm.io/xorm" ) type QrcodeDb struct { Db *xorm.Engine `json:"db"` } func (qrcodeDb *QrcodeDb) Set() { // set方法 qrcodeDb.Db = Db } func (qrcodeDb *QrcodeDb) GetQrcode(id int) (m *model.Qrcode, err error) { m = new(model.Qrcode) has, err := qrcodeDb.Db.Where("id =?", id).Get(m) if err != nil { return nil, logx.Error(err) } if has == false { return nil, nil } return m, nil } func (qrcodeDb *QrcodeDb) GetQrcodeByIndex(index string) (m *model.Qrcode, err error) { m = new(model.Qrcode) has, err := qrcodeDb.Db.Where("index =?", index).Get(m) if err != nil { return nil, logx.Error(err) } if has == false { return nil, nil } return m, nil } func (qrcodeDb *QrcodeDb) GetQrcodeForAllowUse() (m *model.Qrcode, err error) { m = new(model.Qrcode) has, err := qrcodeDb.Db.Where("state =?", enum.QrcodeSateAllowUse).Get(m) if err != nil { return nil, logx.Error(err) } if has == false { return nil, nil } return m, nil } func (qrcodeDb *QrcodeDb) FindQrcodeForAllowUse() (m []*model.Qrcode, total int64, err error) { total, err = qrcodeDb.Db.Where("state =?", enum.QrcodeSateAllowUse).FindAndCount(&m) return } func (qrcodeDb *QrcodeDb) BatchAddQrcode(data []*model.Qrcode) (int64, error) { affected, err := qrcodeDb.Db.Insert(data) if err != nil { return 0, err } return affected, nil } func (qrcodeDb *QrcodeDb) BatchUpdateQrcodeBySession(session *xorm.Session, ids []int, state int32) (int64, error) { m := new(model.Qrcode) m.State = state affected, err := session.In("id", ids).Cols("state").Update(m) if err != nil { return 0, err } return affected, nil } func (qrcodeDb *QrcodeDb) BatchUpdateQrcode(m *model.Qrcode, columns ...string) (int64, error) { affected, err := qrcodeDb.Db.Where("id =?", m.Id).Cols(columns...).Update(m) if err != nil { return 0, err } return affected, nil }