package_application.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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 TPackageApplication struct {
  12. ID int64 `gorm:"column:id;PrimaryKey" json:"id"`
  13. ApplicationId int64 `gorm:"column:application_id" json:"application_id"`
  14. PackageId int64 `gorm:"column:package_id" json:"package_id"`
  15. CreatedAt time.Time `gorm:"column:created_at" json:"created_at"`
  16. UpdatedAt time.Time `gorm:"column:updated_at" json:"updated_at"`
  17. }
  18. func (p *TPackageApplication) TableName() string {
  19. return "t_package_application"
  20. }
  21. func (p *TPackageApplication) Find(db *gorm.DB, where map[string]interface{}) error {
  22. err := db.Table(p.TableName()).Where(where).Find(p).Error
  23. if err != nil {
  24. fields, _ := util.MarshalToString(where)
  25. logger.Error("mysql",
  26. zap.String("sql", "select from "+p.TableName()),
  27. zap.String("fields", fields),
  28. zap.String("error", err.Error()))
  29. }
  30. return err
  31. }
  32. func (p *TPackageApplication) Last(db *gorm.DB) error {
  33. err := db.Table(p.TableName()).Last(p).Error
  34. if err != nil {
  35. logger.Error("mysql",
  36. zap.String("sql", "select last from "+p.TableName()),
  37. zap.String("fields", ""),
  38. zap.String("error", err.Error()))
  39. }
  40. return err
  41. }
  42. // Insert 插入一条记录
  43. func (p *TPackageApplication) Insert(db *gorm.DB) error {
  44. err := db.Create(p).Error
  45. if err != nil {
  46. fields, _ := util.MarshalToString(*p)
  47. logger.Error("mysql",
  48. zap.String("sql", "insert into "+p.TableName()),
  49. zap.String("fields", fields),
  50. zap.String("error", err.Error()))
  51. }
  52. return err
  53. }
  54. func (p *TPackageApplication) InsertMulti(db *gorm.DB, values interface{}) error {
  55. err := db.Table(p.TableName()).Create(values).Error
  56. if err != nil {
  57. fields, _ := util.MarshalToString(*p)
  58. logger.Error("mysql",
  59. zap.String("sql", "insert into "+p.TableName()),
  60. zap.String("fields", fields),
  61. zap.String("error", err.Error()))
  62. }
  63. return err
  64. }
  65. func (p *TPackageApplication) Delete(db *gorm.DB, filter map[string]interface{}) error {
  66. cond, val, err := whereBuild(filter)
  67. if err != nil {
  68. return err
  69. }
  70. return db.Table(p.TableName()).Where(cond, val...).Delete(p).Error
  71. }
  72. func (p *TPackageApplication) Update(db *gorm.DB, where map[string]interface{}, values map[string]interface{}) error {
  73. cond, val, err := whereBuild(where)
  74. if err != nil {
  75. if err != nil {
  76. fields, _ := util.MarshalToString(values)
  77. logger.Error("mysql",
  78. zap.String("sql", "update "+p.TableName()),
  79. zap.String("fields", fields),
  80. zap.String("error", err.Error()))
  81. }
  82. return err
  83. }
  84. return db.Table(p.TableName()).Where(cond, val...).Updates(values).Error
  85. }
  86. func (p *TPackageApplication) Count(db *gorm.DB, where map[string]interface{}, or map[string]interface{}) (int64, error) {
  87. cond, val, err := whereBuildAndOr(where, or)
  88. if err != nil {
  89. return 0, err
  90. }
  91. ret := int64(0)
  92. err = db.Table(p.TableName()).Where(cond, val...).Count(&ret).Error
  93. if err != nil {
  94. fields, _ := util.MarshalToString(where)
  95. logger.Error("mysql",
  96. zap.String("sql", "select count "+p.TableName()),
  97. zap.String("fields", fields),
  98. zap.String("error", err.Error()))
  99. }
  100. return ret, err
  101. }
  102. func (p *TPackageApplication) List(db *gorm.DB, where map[string]interface{}, or map[string]interface{}, page int, pageSize int) (list []TPackageApplication, err error) {
  103. cond, val, err := whereBuildAndOr(where, or)
  104. if err != nil {
  105. return list, err
  106. }
  107. if pageSize < 0 {
  108. result := db.Table(p.TableName()).Where(cond, val...).Find(&list)
  109. if result.Error != nil {
  110. wherefields, _ := util.MarshalToString(where)
  111. logger.Error("mysql",
  112. zap.String("sql", "select * from "+p.TableName()),
  113. zap.String("where", wherefields),
  114. zap.String("error", result.Error.Error()))
  115. }
  116. return list, result.Error
  117. }
  118. offset := (page - 1) * pageSize
  119. result := db.Table(p.TableName()).Where(cond, val...).Limit(pageSize).Offset(offset).Find(&list)
  120. if result.Error != nil {
  121. wherefields, _ := util.MarshalToString(where)
  122. logger.Error("mysql",
  123. zap.String("sql", "select * from "+p.TableName()),
  124. zap.String("where", wherefields),
  125. zap.String("error", result.Error.Error()))
  126. }
  127. return list, result.Error
  128. }