project_job.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. ProviderId int64 `gorm:"column:provider_id" json:"provider_id" form:"provider_id"`
  15. Status int64 `gorm:"column:status" json:"status" form:"status"`
  16. UpdatedAt time.Time `gorm:"column:updated_at" json:"updated_at" form:"updated_at"`
  17. ProjectId int64 `gorm:"column:project_id" json:"project_id" form:"project_id"`
  18. Feedback string `json:"feedback"`
  19. Reason string `json:"reason"`
  20. }
  21. func (TProjectJob) TableName() string {
  22. return "t_project_job"
  23. }
  24. func (p *TProjectJob) Insert(db *gorm.DB) error {
  25. return db.Create(p).Error
  26. }
  27. func (p *TProjectJob) Del(db *gorm.DB, where map[string]interface{}) error {
  28. cond, val, err := whereBuild(where)
  29. if err != nil {
  30. return err
  31. }
  32. return db.Table(p.TableName()).Where(cond, val...).Delete(p).Error
  33. }
  34. func (p *TProjectJob) Find(db *gorm.DB, where map[string]interface{}) error {
  35. cond, val, err := whereBuild(where)
  36. if err != nil {
  37. return err
  38. }
  39. return db.Table(p.TableName()).Where(cond, val...).First(p).Error
  40. }
  41. func (p *TProjectJob) Update(db *gorm.DB, where map[string]interface{}, values map[string]interface{}) error {
  42. cond, val, err := whereBuild(where)
  43. if err != nil {
  44. return err
  45. }
  46. return db.Table(p.TableName()).Where(cond, val...).Updates(values).Error
  47. }
  48. func (p *TProjectJob) FindSort(db *gorm.DB, where map[string]interface{}, sort string) error {
  49. cond, val, err := whereBuild(where)
  50. if err != nil {
  51. return err
  52. }
  53. ps := []TProjectJob{}
  54. err = db.Table(p.TableName()).Where(cond, val...).Order(sort).Limit(1).Find(&ps).Error
  55. if err != nil {
  56. return err
  57. }
  58. if len(ps) > 0 {
  59. *p = ps[0]
  60. }
  61. return nil
  62. }
  63. func (p *TProjectJob) Save(db *gorm.DB) error {
  64. return db.Save(p).Error
  65. }
  66. func (p *TProjectJob) Count(db *gorm.DB, where map[string]interface{}) (int64, error) {
  67. if len(where) > 0 {
  68. cond, val, err := whereBuild(where)
  69. if err != nil {
  70. return 0, err
  71. }
  72. ret := int64(0)
  73. err = db.Table(p.TableName()).Where(cond, val...).Count(&ret).Error
  74. return ret, err
  75. }
  76. ret := int64(0)
  77. err := db.Table(p.TableName()).Count(&ret).Error
  78. return ret, err
  79. }
  80. func (p *TProjectJob) List(db *gorm.DB, where map[string]interface{}, page int) (list []TProjectJob, err error) {
  81. if len(where) > 0 {
  82. cond, val, err := whereBuild(where)
  83. if err != nil {
  84. return list, err
  85. }
  86. result := db.Table(p.TableName()).Where(cond, val...).Limit(PageSize).Offset(page).Find(&list)
  87. return list, result.Error
  88. }
  89. result := db.Table(p.TableName()).Limit(10).Offset(page).Find(&list)
  90. return list, result.Error
  91. }
  92. func (p *TProjectJob) All(db *gorm.DB) (list []TDevice, err error) {
  93. result := db.Table(p.TableName()).Find(&list)
  94. return list, result.Error
  95. }