gate_command.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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 string `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 map[string]interface{}) error {
  27. err := db.Table(p.TableName()).Where(where).First(p).Error
  28. if err != nil {
  29. fields, _ := util.MarshalToString(where)
  30. logger.Error("mysql",
  31. zap.String("sql", "select from "+p.TableName()),
  32. zap.String("fields", fields),
  33. zap.String("error", err.Error()))
  34. }
  35. return err
  36. }
  37. func (p *TGateCommand) Last(db *gorm.DB) error {
  38. err := db.Table(p.TableName()).Last(p).Error
  39. if err != nil {
  40. logger.Error("mysql",
  41. zap.String("sql", "select last from "+p.TableName()),
  42. zap.String("fields", ""),
  43. zap.String("error", err.Error()))
  44. }
  45. return err
  46. }
  47. // Insert 插入一条记录
  48. func (p *TGateCommand) Insert(db *gorm.DB) error {
  49. err := db.Create(p).Error
  50. if err != nil {
  51. fields, _ := util.MarshalToString(*p)
  52. logger.Error("mysql",
  53. zap.String("sql", "insert into "+p.TableName()),
  54. zap.String("fields", fields),
  55. zap.String("error", err.Error()))
  56. }
  57. return err
  58. }
  59. func (p *TGateCommand) InsertMulti(db *gorm.DB, values interface{}) error {
  60. err := db.Table(p.TableName()).Create(values).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) Delete(db *gorm.DB, filter map[string]interface{}) error {
  71. cond, val, err := whereBuild(filter)
  72. if err != nil {
  73. return err
  74. }
  75. return db.Table(p.TableName()).Where(cond, val...).Delete(p).Error
  76. }
  77. func (p *TGateCommand) Update(db *gorm.DB, where map[string]interface{}, values map[string]interface{}) error {
  78. cond, val, err := whereBuild(where)
  79. if err != nil {
  80. if err != nil {
  81. fields, _ := util.MarshalToString(values)
  82. logger.Error("mysql",
  83. zap.String("sql", "update "+p.TableName()),
  84. zap.String("fields", fields),
  85. zap.String("error", err.Error()))
  86. }
  87. return err
  88. }
  89. return db.Table(p.TableName()).Where(cond, val...).Updates(values).Error
  90. }
  91. func (p *TGateCommand) Count(db *gorm.DB, where map[string]interface{}, or map[string]interface{}) (int64, error) {
  92. cond, val, err := whereBuildAndOr(where, or)
  93. if err != nil {
  94. return 0, err
  95. }
  96. ret := int64(0)
  97. err = db.Table(p.TableName()).Where(cond, val...).Count(&ret).Error
  98. if err != nil {
  99. fields, _ := util.MarshalToString(where)
  100. logger.Error("mysql",
  101. zap.String("sql", "select count "+p.TableName()),
  102. zap.String("fields", fields),
  103. zap.String("error", err.Error()))
  104. }
  105. return ret, err
  106. }
  107. func (p *TGateCommand) List(db *gorm.DB, where map[string]interface{}, or map[string]interface{}, page int, pageSize int) (list []TGateCommand, err error) {
  108. cond, val, err := whereBuildAndOr(where, or)
  109. if err != nil {
  110. return list, err
  111. }
  112. if pageSize < 0 {
  113. result := db.Table(p.TableName()).Where(cond, val...).Order("created_at desc").Find(&list)
  114. if result.Error != nil {
  115. wherefields, _ := util.MarshalToString(where)
  116. logger.Error("mysql",
  117. zap.String("sql", "select * from "+p.TableName()),
  118. zap.String("where", wherefields),
  119. zap.String("error", result.Error.Error()))
  120. }
  121. return list, result.Error
  122. }
  123. offset := (page - 1) * pageSize
  124. result := db.Table(p.TableName()).Where(cond, val...).Order("created_at desc").Limit(pageSize).Offset(offset).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. }