gate_card.go 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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 TGateCard struct {
  12. ID int64 `gorm:"column:id;Primary_key" json:"id"`
  13. CreatedAt time.Time `gorm:"column:created_at" json:"created_at"`
  14. UpdatedAt time.Time `gorm:"column:updated_at" json:"updated_at"`
  15. RecordId int64 `gorm:"column:record_id" json:"record_id"`
  16. Status int64 `gorm:"column:status" json:"status"`
  17. DeviceId int64 `gorm:"column:device_id" json:"device_id"`
  18. Sn string `gorm:"column:sn" json:"sn"`
  19. Protocol int32 `gorm:"column:protocol" json:"protocol"`
  20. Msg string `gorm:"column:msg" json:"msg"`
  21. }
  22. func (p *TGateCard) TableName() string {
  23. return "t_gate_card"
  24. }
  25. func (p *TGateCard) Find(db *gorm.DB, where [][2]interface{}) error {
  26. cond, val, err := whereBuild(where)
  27. if err != nil {
  28. if err != nil {
  29. fields, _ := util.MarshalToString(where)
  30. logger.Error("mysql",
  31. zap.String("sql", "find "+p.TableName()),
  32. zap.String("fields", fields),
  33. zap.String("error", err.Error()))
  34. }
  35. return err
  36. }
  37. err = db.Table(p.TableName()).Where(cond, val...).First(p).Error
  38. if err != nil {
  39. fields, _ := util.MarshalToString(where)
  40. logger.Error("mysql",
  41. zap.String("sql", "select from "+p.TableName()),
  42. zap.String("fields", fields),
  43. zap.String("error", err.Error()))
  44. }
  45. return err
  46. }
  47. func (p *TGateCard) Last(db *gorm.DB) error {
  48. err := db.Table(p.TableName()).Last(p).Error
  49. if err != nil {
  50. logger.Error("mysql",
  51. zap.String("sql", "select last from "+p.TableName()),
  52. zap.String("fields", ""),
  53. zap.String("error", err.Error()))
  54. }
  55. return err
  56. }
  57. // Insert 插入一条记录
  58. func (p *TGateCard) Insert(db *gorm.DB) error {
  59. err := db.Create(p).Error
  60. if err != nil {
  61. fields, _ := util.MarshalToString(*p)
  62. logger.Error("mysql",
  63. zap.String("sql", "insert into "+p.TableName()),
  64. zap.String("fields", fields),
  65. zap.String("error", err.Error()))
  66. }
  67. return err
  68. }
  69. // Insert 插入多条记录
  70. func (p *TGateCard) 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 *TGateCard) Delete(db *gorm.DB, filter [][2]interface{}) error {
  82. cond, val, err := whereBuild(filter)
  83. if err != nil {
  84. if err != nil {
  85. fields, _ := util.MarshalToString(filter)
  86. logger.Error("mysql",
  87. zap.String("sql", "delete "+p.TableName()),
  88. zap.String("fields", fields),
  89. zap.String("error", err.Error()))
  90. }
  91. return err
  92. }
  93. err = db.Where(cond, val...).Delete(p).Error
  94. if err != nil {
  95. fields, _ := util.MarshalToString(filter)
  96. logger.Error("mysql",
  97. zap.String("sql", "delete from "+p.TableName()),
  98. zap.String("where", fields),
  99. zap.String("error", err.Error()))
  100. }
  101. return err
  102. }
  103. func (p *TGateCard) Update(db *gorm.DB, where [][2]interface{}, values map[string]interface{}) error {
  104. cond, val, err := whereBuild(where)
  105. if err != nil {
  106. if err != nil {
  107. fields, _ := util.MarshalToString(values)
  108. logger.Error("mysql",
  109. zap.String("sql", "update "+p.TableName()),
  110. zap.String("fields", fields),
  111. zap.String("error", err.Error()))
  112. }
  113. return err
  114. }
  115. return db.Table(p.TableName()).Where(cond, val...).Updates(values).Error
  116. }
  117. func (p *TGateCard) Count(db *gorm.DB, where [][2]interface{}, or [][2]interface{}) (int64, error) {
  118. cond, val, err := whereBuildAndOr(where, or)
  119. if err != nil {
  120. return 0, err
  121. }
  122. ret := int64(0)
  123. err = db.Table(p.TableName()).Where(cond, val...).Count(&ret).Error
  124. if err != nil {
  125. fields, _ := util.MarshalToString(where)
  126. logger.Error("mysql",
  127. zap.String("sql", "select count "+p.TableName()),
  128. zap.String("fields", fields),
  129. zap.String("error", err.Error()))
  130. }
  131. return ret, err
  132. }
  133. func (p *TGateCard) List(db *gorm.DB, where [][2]interface{}, or [][2]interface{}, page int, pageSize int) (list []TGateCard, err error) {
  134. cond, val, err := whereBuildAndOr(where, or)
  135. if err != nil {
  136. return list, err
  137. }
  138. if pageSize < 0 {
  139. result := db.Table(p.TableName()).Where(cond, val...).Order("created_at desc").Find(&list)
  140. if result.Error != nil {
  141. wherefields, _ := util.MarshalToString(where)
  142. logger.Error("mysql",
  143. zap.String("sql", "select * from "+p.TableName()),
  144. zap.String("where", wherefields),
  145. zap.String("error", result.Error.Error()))
  146. }
  147. return list, result.Error
  148. }
  149. offset := (page - 1) * pageSize
  150. result := db.Table(p.TableName()).Where(cond, val...).Limit(pageSize).Offset(offset).Order("created_at desc").Find(&list)
  151. if result.Error != nil {
  152. wherefields, _ := util.MarshalToString(where)
  153. logger.Error("mysql",
  154. zap.String("sql", "select * from "+p.TableName()),
  155. zap.String("where", wherefields),
  156. zap.String("error", result.Error.Error()))
  157. }
  158. return list, result.Error
  159. }
  160. func (p *TGateCard) SelectList(db *gorm.DB, where [][2]interface{}, selectStr string, ret interface{}) (err error) {
  161. cond, val, err := whereBuildAndOr(where, nil)
  162. if err != nil {
  163. return err
  164. }
  165. result := db.Table(p.TableName()).Select(selectStr).Where(cond, val...).Find(ret)
  166. if result.Error != nil {
  167. wherefields, _ := util.MarshalToString(where)
  168. logger.Error("mysql",
  169. zap.String("sql", "select "+selectStr+" from "+p.TableName()),
  170. zap.String("where", wherefields),
  171. zap.String("error", result.Error.Error()))
  172. }
  173. return result.Error
  174. }
  175. func (p *TGateCard) SelectListGroup(db *gorm.DB, where [][2]interface{}, selectStr string, group string, ret interface{}) (err error) {
  176. cond, val, err := whereBuildAndOr(where, nil)
  177. if err != nil {
  178. return err
  179. }
  180. result := db.Table(p.TableName()).Select(selectStr).Where(cond, val...).Group(group).Find(ret)
  181. if result.Error != nil {
  182. wherefields, _ := util.MarshalToString(where)
  183. logger.Error("mysql",
  184. zap.String("sql", "select "+selectStr+" from "+p.TableName()),
  185. zap.String("where", wherefields),
  186. zap.String("error", result.Error.Error()))
  187. }
  188. return result.Error
  189. }
  190. func (p *TGateCard) SelectListGroupPage(db *gorm.DB, where [][2]interface{}, selectStr string, group string, ret interface{}, page int, pageSize int) (err error) {
  191. cond, val, err := whereBuildAndOr(where, nil)
  192. if err != nil {
  193. return err
  194. }
  195. offset := (page - 1) * pageSize
  196. result := db.Table(p.TableName()).Select(selectStr).Where(cond, val...).Group(group).Limit(pageSize).Offset(offset).Find(ret)
  197. if result.Error != nil {
  198. wherefields, _ := util.MarshalToString(where)
  199. logger.Error("mysql",
  200. zap.String("sql", "select "+selectStr+" from "+p.TableName()),
  201. zap.String("where", wherefields),
  202. zap.String("error", result.Error.Error()))
  203. }
  204. return result.Error
  205. }