offline_task.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. "encoding/json"
  6. "time"
  7. "git.getensh.com/common/gopkgsv2/logger"
  8. "go.uber.org/zap"
  9. "gorm.io/gorm"
  10. )
  11. type OfflineTask struct {
  12. ID int64 `gorm:"primary_key" json:"-"`
  13. TaskName string `gorm:"column:task_name" json:"task_name" form:"task_name"`
  14. Type int64 `gorm:"column:type" json:"type" form:"type"`
  15. Source string `gorm:"column:source" json:"source" form:"source"`
  16. Sql string `gorm:"column:sql" json:"sql" form:"sql"`
  17. TaskIds string `gorm:"column:task_ids" json:"task_ids" form:"task_ids"`
  18. Total int64 `gorm:"column:total" json:"total" form:"total"`
  19. FinishCount int64 `gorm:"column:finish_count" json:"finish_count" form:"finish_count"`
  20. IsFinish int64 `gorm:"column:is_finish" json:"is_finish" form:"is_finish"`
  21. CreatedAt int64 `gorm:"column:created_at" json:"created_at" form:"created_at"`
  22. UpdatedAt int64 `gorm:"column:updated_at" json:"updated_at" form:"updated_at"`
  23. }
  24. func (OfflineTask) TableName() string {
  25. return "t_adm_offline_task"
  26. }
  27. func MarshalJsonString(args interface{}) (result string) {
  28. if r, err := json.Marshal(args); err == nil {
  29. result = string(r)
  30. }
  31. return
  32. }
  33. // Insert 插入一条记录
  34. func (p *OfflineTask) Insert(db *gorm.DB) error {
  35. timeNow := time.Now().Unix()
  36. p.UpdatedAt = timeNow
  37. p.CreatedAt = timeNow
  38. err := db.Create(p).Error
  39. if err != nil {
  40. logger.Error("mysql",
  41. zap.String("sql", "insert into "+p.TableName()),
  42. zap.String("fields", MarshalJsonString(*p)),
  43. zap.String("error", err.Error()))
  44. }
  45. return err
  46. }
  47. func (p *OfflineTask) InsertByMap(db *gorm.DB, data map[string]interface{}) error {
  48. data["updated_at"] = time.Now().Unix()
  49. err := db.Model(p).Create(data).Error
  50. if err != nil {
  51. logger.Error("mysql",
  52. zap.String("sql", "insert into "+p.TableName()),
  53. zap.String("fields", MarshalJsonString(data)),
  54. zap.String("error", err.Error()))
  55. }
  56. return err
  57. }
  58. func (p *OfflineTask) Delete(db *gorm.DB, filter map[string]interface{}) error {
  59. err := db.Where(filter).Delete(p).Error
  60. if err != nil {
  61. logger.Error("mysql",
  62. zap.String("sql", "delete from "+p.TableName()),
  63. zap.String("fields", MarshalJsonString(filter)),
  64. zap.String("error", err.Error()))
  65. }
  66. return err
  67. }
  68. func (p *OfflineTask) Save(db *gorm.DB) error {
  69. timeNow := time.Now().Unix()
  70. p.UpdatedAt = timeNow
  71. p.CreatedAt = timeNow
  72. //db.Model().Update()
  73. err := db.Save(p).Error
  74. if err != nil {
  75. logger.Error("mysql",
  76. zap.String("sql", "save "+p.TableName()),
  77. zap.String("fields", MarshalJsonString(*p)),
  78. zap.String("error", err.Error()))
  79. }
  80. return err
  81. }
  82. func (p *OfflineTask) Update(db *gorm.DB) error {
  83. timeNow := time.Now().Unix()
  84. p.CreatedAt = 0
  85. p.UpdatedAt = timeNow
  86. err := db.Model(p).Updates(p).Error
  87. if err != nil {
  88. logger.Error("mysql",
  89. zap.String("sql", "update "+p.TableName()),
  90. zap.String("fields", MarshalJsonString(*p)),
  91. zap.String("error", err.Error()))
  92. }
  93. return err
  94. }
  95. func (p *OfflineTask) UpdateByMap(db *gorm.DB, data map[string]interface{}) error {
  96. data["updated_at"] = time.Now().Unix()
  97. err := db.Model(p).Updates(data).Error
  98. if err != nil {
  99. logger.Error("mysql",
  100. zap.String("sql", "update "+p.TableName()),
  101. zap.String("fields", MarshalJsonString(data)),
  102. zap.String("error", err.Error()))
  103. }
  104. return err
  105. }
  106. // 通过结构体变量更新字段值, gorm库会忽略零值字段。就是字段值等于0, nil, "", false这些值会被忽略掉,不会更新。如果想更新零值,可以使用map类型替代结构体。
  107. func (p *OfflineTask) UpdateSome(db *gorm.DB, filed map[string]interface{}) error {
  108. /*if filed == nil {
  109. return errors.ParamsError
  110. }*/
  111. timeNow := time.Now().Unix()
  112. filed["updated_at"] = timeNow
  113. err := db.Model(p).Updates(filed).Error
  114. if err != nil {
  115. logger.Error("mysql",
  116. zap.String("sql", "update "+p.TableName()),
  117. zap.String("fields", MarshalJsonString(filed)),
  118. zap.String("error", err.Error()))
  119. }
  120. return err
  121. }
  122. func (p *OfflineTask) Query(db *gorm.DB, filter map[string]interface{}) error {
  123. err := db.Where(filter).Find(p).Error
  124. if err != nil {
  125. logger.Error("mysql",
  126. zap.String("sql", "select from "+p.TableName()),
  127. zap.String("fields", MarshalJsonString(filter)),
  128. zap.String("error", err.Error()))
  129. }
  130. if p.ID == 0 {
  131. return gorm.ErrRecordNotFound
  132. }
  133. return err
  134. }
  135. func (p *OfflineTask) QueryAll(db *gorm.DB, filter map[string]interface{}, out interface{}) error {
  136. err := db.Where(filter).Find(out).Error
  137. if err != nil {
  138. logger.Error("mysql",
  139. zap.String("sql", "select from "+p.TableName()),
  140. zap.String("fields", MarshalJsonString(filter)),
  141. zap.String("error", err.Error()))
  142. }
  143. return err
  144. }