gate_command.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. // Copyright 2019 getensh.com. All rights reserved.
  2. // Use of this source code is governed by getensh.com.
  3. package model
  4. import (
  5. "git.getensh.com/common/gopkgs/logger"
  6. "git.getensh.com/common/gopkgs/util"
  7. "go.uber.org/zap"
  8. "gorm.io/gorm"
  9. "time"
  10. )
  11. type TGateCommand struct {
  12. ID int64 `gorm:"column:id;PRIMARY_KEY" json:"id"`
  13. DeviceId int64 `gorm:"column:device_id" json:"device_id"`
  14. Param string `gorm:"column:param" json:"param"`
  15. CreatedAt time.Time `gorm:"column:created_at" json:"created_at"`
  16. UpdatedAt time.Time `gorm:"column:updated_at" json:"updated_at"`
  17. Desc string `gorm:"column:desc" json:"desc"`
  18. Status int32 `gorm:"column:status" json:"status"`
  19. ResultStatus int32 `gorm:"column:result_status" json:"result_status"`
  20. ResultStatusDesc string `gorm:"column:result_status_desc" json:"result_status_desc"`
  21. Code int32 `gorm:"column:code" json:"code"`
  22. }
  23. func (p *TGateCommand) TableName() string {
  24. return "t_gate_command"
  25. }
  26. func (p *TGateCommand) Find(db *gorm.DB, where [][2]interface{}) error {
  27. cond, val, err := whereBuild(where)
  28. if err != nil {
  29. if err != nil {
  30. fields, _ := util.MarshalToString(where)
  31. logger.Error("mysql",
  32. zap.String("sql", "find "+p.TableName()),
  33. zap.String("fields", fields),
  34. zap.String("error", err.Error()))
  35. }
  36. return err
  37. }
  38. err = db.Table(p.TableName()).Where(cond, val...).First(p).Error
  39. if err != nil {
  40. fields, _ := util.MarshalToString(where)
  41. logger.Error("mysql",
  42. zap.String("sql", "select from "+p.TableName()),
  43. zap.String("fields", fields),
  44. zap.String("error", err.Error()))
  45. }
  46. return err
  47. }
  48. func (p *TGateCommand) Last(db *gorm.DB) error {
  49. err := db.Table(p.TableName()).Last(p).Error
  50. if err != nil {
  51. logger.Error("mysql",
  52. zap.String("sql", "select last from "+p.TableName()),
  53. zap.String("fields", ""),
  54. zap.String("error", err.Error()))
  55. }
  56. return err
  57. }
  58. // Insert 插入一条记录
  59. func (p *TGateCommand) Insert(db *gorm.DB) error {
  60. err := db.Create(p).Error
  61. if err != nil {
  62. fields, _ := util.MarshalToString(*p)
  63. logger.Error("mysql",
  64. zap.String("sql", "insert into "+p.TableName()),
  65. zap.String("fields", fields),
  66. zap.String("error", err.Error()))
  67. }
  68. return err
  69. }
  70. func (p *TGateCommand) InsertMulti(db *gorm.DB, values interface{}) error {
  71. err := db.Table(p.TableName()).Create(values).Error
  72. if err != nil {
  73. fields, _ := util.MarshalToString(*p)
  74. logger.Error("mysql",
  75. zap.String("sql", "insert into "+p.TableName()),
  76. zap.String("fields", fields),
  77. zap.String("error", err.Error()))
  78. }
  79. return err
  80. }
  81. func (p *TGateCommand) Delete(db *gorm.DB, filter [][2]interface{}) error {
  82. cond, val, err := whereBuild(filter)
  83. if err != nil {
  84. return err
  85. }
  86. return db.Table(p.TableName()).Where(cond, val...).Delete(p).Error
  87. }
  88. func (p *TGateCommand) Update(db *gorm.DB, where [][2]interface{}, values map[string]interface{}) error {
  89. cond, val, err := whereBuild(where)
  90. if err != nil {
  91. if err != nil {
  92. fields, _ := util.MarshalToString(values)
  93. logger.Error("mysql",
  94. zap.String("sql", "update "+p.TableName()),
  95. zap.String("fields", fields),
  96. zap.String("error", err.Error()))
  97. }
  98. return err
  99. }
  100. return db.Table(p.TableName()).Where(cond, val...).Updates(values).Error
  101. }
  102. func (p *TGateCommand) Count(db *gorm.DB, where [][2]interface{}, or [][2]interface{}) (int64, error) {
  103. cond, val, err := whereBuildAndOr(where, or)
  104. if err != nil {
  105. return 0, err
  106. }
  107. ret := int64(0)
  108. err = db.Table(p.TableName()).Where(cond, val...).Count(&ret).Error
  109. if err != nil {
  110. fields, _ := util.MarshalToString(where)
  111. logger.Error("mysql",
  112. zap.String("sql", "select count "+p.TableName()),
  113. zap.String("fields", fields),
  114. zap.String("error", err.Error()))
  115. }
  116. return ret, err
  117. }
  118. func (p *TGateCommand) List(db *gorm.DB, where [][2]interface{}, or [][2]interface{}, page int, pageSize int) (list []TGateCommand, err error) {
  119. cond, val, err := whereBuildAndOr(where, or)
  120. if err != nil {
  121. return list, err
  122. }
  123. if pageSize < 0 {
  124. result := db.Table(p.TableName()).Where(cond, val...).Order("created_at desc").Find(&list)
  125. if result.Error != nil {
  126. wherefields, _ := util.MarshalToString(where)
  127. logger.Error("mysql",
  128. zap.String("sql", "select * from "+p.TableName()),
  129. zap.String("where", wherefields),
  130. zap.String("error", result.Error.Error()))
  131. }
  132. return list, result.Error
  133. }
  134. offset := (page - 1) * pageSize
  135. result := db.Table(p.TableName()).Where(cond, val...).Order("created_at desc").Limit(pageSize).Offset(offset).Find(&list)
  136. if result.Error != nil {
  137. wherefields, _ := util.MarshalToString(where)
  138. logger.Error("mysql",
  139. zap.String("sql", "select * from "+p.TableName()),
  140. zap.String("where", wherefields),
  141. zap.String("error", result.Error.Error()))
  142. }
  143. return list, result.Error
  144. }