charge_urge.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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. )
  11. type TChargeUrge struct {
  12. ID int64 `gorm:"column:id;PRIMARY_KEY" json:"id"`
  13. //BillDay int64 `gorm:"column:bill_day" json:"bill_day"`
  14. UrgeDay int64 `gorm:"column:urge_day" json:"urge_day"`
  15. UrgeTarget int32 `gorm:"column:urge_target" json:"urge_target"`
  16. UrgeMsgType int32 `gorm:"column:urge_msg_type" json:"urge_msg_type"`
  17. table string
  18. }
  19. func (p *TChargeUrge) TableName() string {
  20. return p.table
  21. }
  22. func NewChargeUrge(database string) *TChargeUrge {
  23. return &TChargeUrge{table: fmt.Sprintf("%s.%s", database, "t_charge_urge")}
  24. }
  25. func (p *TChargeUrge) SetTable(database string) {
  26. p.table = fmt.Sprintf("%s.%s", database, "t_charge_urge")
  27. }
  28. func (p *TChargeUrge) CreateTable(db *gorm.DB) error {
  29. sql :="CREATE TABLE IF NOT EXISTS "+p.TableName()+"("+
  30. " `id` int(11) NOT NULL AUTO_INCREMENT,"+
  31. //" `bill_day` int(10) NOT NULL COMMENT '账单生成日期',"+
  32. "`urge_day` int(10) NOT NULL DEFAULT '0' COMMENT '自动催缴日期:0 不催缴',"+
  33. "`urge_target` tinyint(1) NOT NULL COMMENT '1 仅业主 2 业主和家人 3 业主家人租客',"+
  34. "`urge_msg_type` tinyint(1) NOT NULL COMMENT '1 仅短信 2 仅微信 3 短信和微信',"+
  35. "PRIMARY KEY (`id`)"+
  36. ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;"
  37. err := db.Exec(sql).Error
  38. if err != nil {
  39. logger.Error("mysql",
  40. zap.String("sql", "create table "+p.TableName()),
  41. zap.String("fields", ""),
  42. zap.String("error", err.Error()))
  43. }
  44. return err
  45. }
  46. func (p *TChargeUrge) Find(db *gorm.DB, where map[string]interface{}) error {
  47. err := db.Table(p.TableName()).Where(where).Find(p).Error
  48. if err != nil {
  49. fields, _ := util.MarshalToString(where)
  50. logger.Error("mysql",
  51. zap.String("sql", "select from "+p.TableName()),
  52. zap.String("fields", fields),
  53. zap.String("error", err.Error()))
  54. }
  55. return err
  56. }
  57. func (p *TChargeUrge) Last(db *gorm.DB) error {
  58. err := db.Table(p.TableName()).Last(p).Error
  59. if err != nil {
  60. logger.Error("mysql",
  61. zap.String("sql", "select last from "+p.TableName()),
  62. zap.String("fields", ""),
  63. zap.String("error", err.Error()))
  64. }
  65. return err
  66. }
  67. // Insert 插入一条记录
  68. func (p *TChargeUrge) Insert(db *gorm.DB) error {
  69. err := db.Table(p.TableName()).Create(p).Error
  70. if err != nil {
  71. fields, _ := util.MarshalToString(*p)
  72. logger.Error("mysql",
  73. zap.String("sql", "insert into "+p.TableName()),
  74. zap.String("fields", fields),
  75. zap.String("error", err.Error()))
  76. }
  77. return err
  78. }
  79. // Insert 插入多条记录
  80. func (p *TChargeUrge) InsertMulti(db *gorm.DB, values interface{}) error {
  81. err := db.Table(p.TableName()).Create(values).Error
  82. if err != nil {
  83. fields, _ := util.MarshalToString(*p)
  84. logger.Error("mysql",
  85. zap.String("sql", "insert into "+p.TableName()),
  86. zap.String("fields", fields),
  87. zap.String("error", err.Error()))
  88. }
  89. return err
  90. }
  91. func (p *TChargeUrge) Delete(db *gorm.DB, filter map[string]interface{}) error {
  92. cond, val, err := whereBuild(filter)
  93. if err != nil {
  94. return err
  95. }
  96. return db.Table(p.TableName()).Where(cond, val...).Delete(p).Error
  97. }
  98. func (p *TChargeUrge) Update(db *gorm.DB, where map[string]interface{}, values map[string]interface{}) error {
  99. cond, val, err := whereBuild(where)
  100. if err != nil {
  101. if err != nil {
  102. fields, _ := util.MarshalToString(values)
  103. logger.Error("mysql",
  104. zap.String("sql", "update "+p.TableName()),
  105. zap.String("fields", fields),
  106. zap.String("error", err.Error()))
  107. }
  108. return err
  109. }
  110. return db.Table(p.TableName()).Where(cond, val...).Updates(values).Error
  111. }
  112. func (p *TChargeUrge) UpdateByModel(db *gorm.DB) error {
  113. err := db.Table(p.TableName()).Model(p).Updates(p).Error
  114. if err != nil {
  115. fields, _ := util.MarshalToString(*p)
  116. logger.Error("mysql",
  117. zap.String("sql", "update "+p.TableName()),
  118. zap.String("fields", fields),
  119. zap.String("error", err.Error()))
  120. }
  121. return err
  122. }
  123. func (p *TChargeUrge) Count(db *gorm.DB, where map[string]interface{}, or map[string]interface{}) (int64, error) {
  124. cond, val, err := whereBuildAndOr(where, or)
  125. if err != nil {
  126. return 0, err
  127. }
  128. ret := int64(0)
  129. err = db.Table(p.TableName()).Where(cond, val...).Count(&ret).Error
  130. if err != nil {
  131. fields, _ := util.MarshalToString(where)
  132. logger.Error("mysql",
  133. zap.String("sql", "select count "+p.TableName()),
  134. zap.String("fields", fields),
  135. zap.String("error", err.Error()))
  136. }
  137. return ret, err
  138. }
  139. func (p *TChargeUrge) List(db *gorm.DB, where map[string]interface{}, or map[string]interface{}, page int, pageSize int) (list []TChargeUrge, err error) {
  140. cond, val, err := whereBuildAndOr(where, or)
  141. if err != nil {
  142. return list, err
  143. }
  144. if pageSize < 0 {
  145. result := db.Table(p.TableName()).Where(cond, val...).Find(&list)
  146. if result.Error != nil {
  147. wherefields, _ := util.MarshalToString(where)
  148. logger.Error("mysql",
  149. zap.String("sql", "select * from "+p.TableName()),
  150. zap.String("where", wherefields),
  151. zap.String("error", result.Error.Error()))
  152. }
  153. return list, result.Error
  154. }
  155. offset := (page - 1) * pageSize
  156. result := db.Table(p.TableName()).Where(cond, val...).Limit(pageSize).Offset(offset).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. }