property_package.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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. "fmt"
  6. "git.getensh.com/common/gopkgs/logger"
  7. "git.getensh.com/common/gopkgs/util"
  8. "go.uber.org/zap"
  9. "gorm.io/gorm"
  10. "time"
  11. )
  12. type TPropertyPackage struct {
  13. ID int64 `gorm:"column:id;PRIMARY_KEY" json:"id"`
  14. PackageType int32 `gorm:"column:package_type" json:"package_type"`
  15. GiveMonths int64 `gorm:"column:give_months" json:"give_months"`
  16. GiveContent string `gorm:"column:give_content" json:"give_content"`
  17. Enable int32 `gorm:"column:enable" json:"enable"`
  18. PayMonths int64 `gorm:"column:pay_months" json:"pay_months"`
  19. CreatedAt time.Time `gorm:"column:created_at" json:"created_at"`
  20. UpdatedAt time.Time `gorm:"column:updated_at" json:"updated_at"`
  21. table string
  22. }
  23. func (p *TPropertyPackage) TableName() string {
  24. return p.table
  25. }
  26. func NewPropertyPackage(database string) *TPropertyPackage {
  27. return &TPropertyPackage{table: fmt.Sprintf("%s.%s", database, "t_property_package")}
  28. }
  29. func (p *TPropertyPackage) SetTable(database string) {
  30. p.table = fmt.Sprintf("%s.%s", database, "t_property_package")
  31. }
  32. func (p *TPropertyPackage) CreateTable(db *gorm.DB) error {
  33. sql :="CREATE TABLE IF NOT EXISTS "+p.TableName()+"("+
  34. "`id` int(10) NOT NULL AUTO_INCREMENT,"+
  35. "`package_type` tinyint(1) NOT NULL COMMENT '1 按月 2 文本',"+
  36. "`give_months` int(10) NOT NULL COMMENT '赠送月数,按月有效',"+
  37. "`give_content` text COLLATE utf8mb4_bin NOT NULL COMMENT '赠送内容文本',"+
  38. "`enable` tinyint(1) NOT NULL COMMENT '1 启用 2 停用',"+
  39. "`pay_months` int(10) NOT NULL COMMENT '缴费月数',"+
  40. "`created_at` datetime NOT NULL,"+
  41. " `updated_at` datetime NOT NULL,"+
  42. " PRIMARY KEY (`id`)"+
  43. ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin COMMENT='物业费套餐';"
  44. err := db.Exec(sql).Error
  45. if err != nil {
  46. logger.Error("mysql",
  47. zap.String("sql", "create table "+p.TableName()),
  48. zap.String("fields", ""),
  49. zap.String("error", err.Error()))
  50. }
  51. return err
  52. }
  53. func (p *TPropertyPackage) Find(db *gorm.DB, where map[string]interface{}) error {
  54. cond, val, err := whereBuild(where)
  55. if err != nil {
  56. return err
  57. }
  58. err = db.Table(p.TableName()).Where(cond, val...).First(p).Error
  59. if err != nil {
  60. fields, _ := util.MarshalToString(where)
  61. logger.Error("mysql",
  62. zap.String("sql", "select from "+p.TableName()),
  63. zap.String("fields", fields),
  64. zap.String("error", err.Error()))
  65. }
  66. return err
  67. }
  68. func (p *TPropertyPackage) Last(db *gorm.DB) error {
  69. err := db.Table(p.TableName()).Last(p).Error
  70. if err != nil {
  71. logger.Error("mysql",
  72. zap.String("sql", "select last from "+p.TableName()),
  73. zap.String("fields", ""),
  74. zap.String("error", err.Error()))
  75. }
  76. return err
  77. }
  78. // Insert 插入一条记录
  79. func (p *TPropertyPackage) Insert(db *gorm.DB) error {
  80. err := db.Table(p.TableName()).Create(p).Error
  81. if err != nil {
  82. fields, _ := util.MarshalToString(*p)
  83. logger.Error("mysql",
  84. zap.String("sql", "insert into "+p.TableName()),
  85. zap.String("fields", fields),
  86. zap.String("error", err.Error()))
  87. }
  88. return err
  89. }
  90. // Insert 插入多条记录
  91. func (p *TPropertyPackage) InsertMulti(db *gorm.DB, values interface{}) error {
  92. err := db.Table(p.TableName()).Create(values).Error
  93. if err != nil {
  94. fields, _ := util.MarshalToString(*p)
  95. logger.Error("mysql",
  96. zap.String("sql", "insert into "+p.TableName()),
  97. zap.String("fields", fields),
  98. zap.String("error", err.Error()))
  99. }
  100. return err
  101. }
  102. func (p *TPropertyPackage) Delete(db *gorm.DB, filter map[string]interface{}) error {
  103. cond, val, err := whereBuild(filter)
  104. if err != nil {
  105. return err
  106. }
  107. return db.Table(p.TableName()).Where(cond, val...).Delete(p).Error
  108. }
  109. func (p *TPropertyPackage) Update(db *gorm.DB, where map[string]interface{}, values map[string]interface{}) error {
  110. cond, val, err := whereBuild(where)
  111. if err != nil {
  112. if err != nil {
  113. fields, _ := util.MarshalToString(values)
  114. logger.Error("mysql",
  115. zap.String("sql", "update "+p.TableName()),
  116. zap.String("fields", fields),
  117. zap.String("error", err.Error()))
  118. }
  119. return err
  120. }
  121. return db.Table(p.TableName()).Where(cond, val...).Updates(values).Error
  122. }
  123. func (p *TPropertyPackage) UpdateByModel(db *gorm.DB) error {
  124. err := db.Table(p.TableName()).Model(p).Updates(p).Error
  125. if err != nil {
  126. fields, _ := util.MarshalToString(*p)
  127. logger.Error("mysql",
  128. zap.String("sql", "update "+p.TableName()),
  129. zap.String("fields", fields),
  130. zap.String("error", err.Error()))
  131. }
  132. return err
  133. }
  134. func (p *TPropertyPackage) Count(db *gorm.DB, where map[string]interface{}, or map[string]interface{}) (int64, error) {
  135. cond, val, err := whereBuildAndOr(where, or)
  136. if err != nil {
  137. return 0, err
  138. }
  139. ret := int64(0)
  140. err = db.Table(p.TableName()).Where(cond, val...).Count(&ret).Error
  141. if err != nil {
  142. fields, _ := util.MarshalToString(where)
  143. logger.Error("mysql",
  144. zap.String("sql", "select count "+p.TableName()),
  145. zap.String("fields", fields),
  146. zap.String("error", err.Error()))
  147. }
  148. return ret, err
  149. }
  150. func (p *TPropertyPackage) List(db *gorm.DB, where map[string]interface{}, or map[string]interface{}, page int, pageSize int) (list []TPropertyPackage, err error) {
  151. cond, val, err := whereBuildAndOr(where, or)
  152. if err != nil {
  153. return list, err
  154. }
  155. if pageSize < 0 {
  156. result := db.Table(p.TableName()).Where(cond, val...).Find(&list)
  157. if result.Error != nil {
  158. wherefields, _ := util.MarshalToString(where)
  159. logger.Error("mysql",
  160. zap.String("sql", "select * from "+p.TableName()),
  161. zap.String("where", wherefields),
  162. zap.String("error", result.Error.Error()))
  163. }
  164. return list, result.Error
  165. }
  166. offset := (page - 1) * pageSize
  167. result := db.Table(p.TableName()).Where(cond, val...).Limit(pageSize).Offset(offset).Find(&list)
  168. if result.Error != nil {
  169. wherefields, _ := util.MarshalToString(where)
  170. logger.Error("mysql",
  171. zap.String("sql", "select * from "+p.TableName()),
  172. zap.String("where", wherefields),
  173. zap.String("error", result.Error.Error()))
  174. }
  175. return list, result.Error
  176. }