project_job.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. // Copyright 2019 github.com. All rights reserved.
  2. // Use of this source code is governed by github.com.
  3. package model
  4. import (
  5. "github.com/jinzhu/gorm"
  6. "time"
  7. )
  8. type TProjectJob struct {
  9. ID int64 `gorm:"column:id" json:"id" form:"id"`
  10. Type int64 `gorm:"column:type" json:"type" form:"type"`
  11. Origin string `gorm:"column:origin" json:"origin" form:"origin"`
  12. Content string `gorm:"column:content" json:"content" form:"content"`
  13. CreatedAt time.Time `gorm:"column:created_at" json:"created_at" form:"created_at"`
  14. Status int64 `gorm:"column:status" json:"status" form:"status"`
  15. UpdatedAt time.Time `gorm:"column:updated_at" json:"updated_at" form:"updated_at"`
  16. ProjectId int64 `gorm:"column:project_id" json:"project_id" form:"project_id"`
  17. Feedback string `json:"feedback"`
  18. Reason string `json:"reason"`
  19. ProviderId int64 `json:"provider_id"`
  20. DeviceId int64 `json:"device_id"`
  21. }
  22. func (TProjectJob) TableName() string {
  23. return "t_project_job"
  24. }
  25. func (p *TProjectJob) Insert(db *gorm.DB) error {
  26. return db.Create(p).Error
  27. }
  28. func (p *TProjectJob) Del(db *gorm.DB, where map[string]interface{}) error {
  29. cond, val, err := whereBuild(where)
  30. if err != nil {
  31. return err
  32. }
  33. return db.Table(p.TableName()).Where(cond, val...).Delete(p).Error
  34. }
  35. func (p *TProjectJob) Find(db *gorm.DB, where map[string]interface{}) error {
  36. cond, val, err := whereBuild(where)
  37. if err != nil {
  38. return err
  39. }
  40. return db.Table(p.TableName()).Where(cond, val...).First(p).Error
  41. }
  42. func (p *TProjectJob) Update(db *gorm.DB, where map[string]interface{}, values map[string]interface{}) error {
  43. cond, val, err := whereBuild(where)
  44. if err != nil {
  45. return err
  46. }
  47. return db.Table(p.TableName()).Where(cond, val...).Updates(values).Error
  48. }
  49. func (p *TProjectJob) FindSort(db *gorm.DB, where map[string]interface{}, sort string) error {
  50. cond, val, err := whereBuild(where)
  51. if err != nil {
  52. return err
  53. }
  54. ps := []TProjectJob{}
  55. err = db.Table(p.TableName()).Where(cond, val...).Order(sort).Limit(1).Find(&ps).Error
  56. if err != nil {
  57. return err
  58. }
  59. if len(ps) > 0 {
  60. *p = ps[0]
  61. }
  62. return nil
  63. }
  64. func (p *TProjectJob) Save(db *gorm.DB) error {
  65. return db.Save(p).Error
  66. }
  67. func (p *TProjectJob) Count(db *gorm.DB, where map[string]interface{}) (int64, error) {
  68. if len(where) > 0 {
  69. cond, val, err := whereBuild(where)
  70. if err != nil {
  71. return 0, err
  72. }
  73. ret := int64(0)
  74. err = db.Table(p.TableName()).Where(cond, val...).Count(&ret).Error
  75. return ret, err
  76. }
  77. ret := int64(0)
  78. err := db.Table(p.TableName()).Count(&ret).Error
  79. return ret, err
  80. }
  81. func (p *TProjectJob) List(db *gorm.DB, where map[string]interface{}, page int) (list []TProjectJob, err error) {
  82. if len(where) > 0 {
  83. cond, val, err := whereBuild(where)
  84. if err != nil {
  85. return list, err
  86. }
  87. result := db.Table(p.TableName()).Where(cond, val...).Limit(PageSize).Offset(page).Find(&list)
  88. return list, result.Error
  89. }
  90. result := db.Table(p.TableName()).Limit(10).Offset(page).Find(&list)
  91. return list, result.Error
  92. }
  93. func (p *TProjectJob) All(db *gorm.DB) (list []TDevice, err error) {
  94. result := db.Table(p.TableName()).Find(&list)
  95. return list, result.Error
  96. }