一物一码
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.
 
 
 
 
 
 

86 rindas
2.0 KiB

  1. package db
  2. import (
  3. "applet/app/db/model"
  4. "applet/app/enum"
  5. "applet/app/utils/logx"
  6. "fmt"
  7. "xorm.io/xorm"
  8. )
  9. type QrcodeDb struct {
  10. Db *xorm.Engine `json:"db"`
  11. }
  12. func (qrcodeDb *QrcodeDb) Set() { // set方法
  13. qrcodeDb.Db = Db
  14. }
  15. func (qrcodeDb *QrcodeDb) GetQrcode(id int) (m *model.Qrcode, err error) {
  16. m = new(model.Qrcode)
  17. has, err := qrcodeDb.Db.Where("id =?", id).Get(m)
  18. if err != nil {
  19. return nil, logx.Error(err)
  20. }
  21. if has == false {
  22. return nil, nil
  23. }
  24. return m, nil
  25. }
  26. func (qrcodeDb *QrcodeDb) GetQrcodeByIndex(index string) (m *model.Qrcode, err error) {
  27. fmt.Println(">>>>>>>>>>>>>>>>>>identity<<<<<<<<<<<<<<", index)
  28. m = new(model.Qrcode)
  29. has, err := qrcodeDb.Db.Where("identity =?", index).Get(m)
  30. if err != nil {
  31. return nil, logx.Error(err)
  32. }
  33. if has == false {
  34. return nil, nil
  35. }
  36. return m, nil
  37. }
  38. func (qrcodeDb *QrcodeDb) GetQrcodeForAllowUse() (m *model.Qrcode, err error) {
  39. m = new(model.Qrcode)
  40. has, err := qrcodeDb.Db.Where("state =?", enum.QrcodeSateAllowUse).Get(m)
  41. if err != nil {
  42. return nil, logx.Error(err)
  43. }
  44. if has == false {
  45. return nil, nil
  46. }
  47. return m, nil
  48. }
  49. func (qrcodeDb *QrcodeDb) FindQrcodeForAllowUse() (m []*model.Qrcode, total int64, err error) {
  50. total, err = qrcodeDb.Db.Where("state =?", enum.QrcodeSateAllowUse).FindAndCount(&m)
  51. return
  52. }
  53. func (qrcodeDb *QrcodeDb) BatchAddQrcode(data []*model.Qrcode) (int64, error) {
  54. affected, err := qrcodeDb.Db.Insert(data)
  55. if err != nil {
  56. return 0, err
  57. }
  58. return affected, nil
  59. }
  60. func (qrcodeDb *QrcodeDb) BatchUpdateQrcodeBySession(session *xorm.Session, ids []int, state int32) (int64, error) {
  61. m := new(model.Qrcode)
  62. m.State = state
  63. affected, err := session.In("id", ids).Cols("state").Update(m)
  64. if err != nil {
  65. return 0, err
  66. }
  67. return affected, nil
  68. }
  69. func (qrcodeDb *QrcodeDb) BatchUpdateQrcode(m *model.Qrcode, columns ...string) (int64, error) {
  70. affected, err := qrcodeDb.Db.Where("id =?", m.Id).Cols(columns...).Update(m)
  71. if err != nil {
  72. return 0, err
  73. }
  74. return affected, nil
  75. }