project_job.go 3.0 KB

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