t_house_rent_manager.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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 THouseRentManager struct {
  12. ID int64 `gorm:"column:id" json:"id"`
  13. ManagerUid int64 `gorm:"column:manager_uid" json:"manager_uid"`
  14. RentId int64 `gorm:"column:rent_id" json:"rent_id"`
  15. table string
  16. }
  17. func (p *THouseRentManager) TableName() string {
  18. return p.table
  19. }
  20. func NewHouseRentManager(database string) *THouseRentManager {
  21. return &THouseRentManager{table: fmt.Sprintf("%s.%s", database, "t_house_rent_manager")}
  22. }
  23. func (p *THouseRentManager) SetTable(database string) {
  24. p.table = fmt.Sprintf("%s.%s", database, "t_house_rent_manager")
  25. }
  26. func (p *THouseRentManager) CreateTable(db *gorm.DB) error {
  27. sql :="CREATE TABLE IF NOT EXISTS "+p.TableName()+"("+
  28. "`id` bigint(11) NOT NULL AUTO_INCREMENT,"+
  29. "`manager_uid` int(10) NOT NULL,"+
  30. "`rent_id` bigint(11) NOT NULL,"+
  31. "PRIMARY KEY (`id`),"+
  32. "KEY `rent_id` (`rent_id`,`manager_uid`) USING BTREE,"+
  33. "KEY `uid` (`manager_uid`) USING BTREE"+
  34. ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;"
  35. err := db.Exec(sql).Error
  36. if err != nil {
  37. logger.Error("mysql",
  38. zap.String("sql", "create table "+p.TableName()),
  39. zap.String("fields", ""),
  40. zap.String("error", err.Error()))
  41. }
  42. return err
  43. }
  44. func (p *THouseRentManager) Find(db *gorm.DB, where map[string]interface{}) error {
  45. err := db.Table(p.TableName()).Where(where).Find(p).Error
  46. if err != nil {
  47. fields, _ := util.MarshalToString(where)
  48. logger.Error("mysql",
  49. zap.String("sql", "select from "+p.TableName()),
  50. zap.String("fields", fields),
  51. zap.String("error", err.Error()))
  52. }
  53. return err
  54. }
  55. func (p *THouseRentManager) Last(db *gorm.DB) error {
  56. err := db.Table(p.TableName()).Last(p).Error
  57. if err != nil {
  58. logger.Error("mysql",
  59. zap.String("sql", "select last from "+p.TableName()),
  60. zap.String("fields", ""),
  61. zap.String("error", err.Error()))
  62. }
  63. return err
  64. }
  65. // Insert 插入一条记录
  66. func (p *THouseRentManager) Insert(db *gorm.DB) error {
  67. err := db.Table(p.TableName()).Create(p).Error
  68. if err != nil {
  69. fields, _ := util.MarshalToString(*p)
  70. logger.Error("mysql",
  71. zap.String("sql", "insert into "+p.TableName()),
  72. zap.String("fields", fields),
  73. zap.String("error", err.Error()))
  74. }
  75. return err
  76. }
  77. // Insert 插入多条记录
  78. func (p *THouseRentManager) InsertMulti(db *gorm.DB, values interface{}) error {
  79. err := db.Table(p.TableName()).Create(values).Error
  80. if err != nil {
  81. fields, _ := util.MarshalToString(*p)
  82. logger.Error("mysql",
  83. zap.String("sql", "insert into "+p.TableName()),
  84. zap.String("fields", fields),
  85. zap.String("error", err.Error()))
  86. }
  87. return err
  88. }
  89. func (p *THouseRentManager) Delete(db *gorm.DB, filter map[string]interface{}) error {
  90. cond, val, err := whereBuild(filter)
  91. if err != nil {
  92. return err
  93. }
  94. return db.Table(p.TableName()).Where(cond, val...).Delete(p).Error
  95. }
  96. func (p *THouseRentManager) Update(db *gorm.DB, where map[string]interface{}, values map[string]interface{}) error {
  97. cond, val, err := whereBuild(where)
  98. if err != nil {
  99. if err != nil {
  100. fields, _ := util.MarshalToString(values)
  101. logger.Error("mysql",
  102. zap.String("sql", "update "+p.TableName()),
  103. zap.String("fields", fields),
  104. zap.String("error", err.Error()))
  105. }
  106. return err
  107. }
  108. return db.Table(p.TableName()).Where(cond, val...).Updates(values).Error
  109. }
  110. func (p *THouseRentManager) UpdateByModel(db *gorm.DB) error {
  111. err := db.Table(p.TableName()).Model(p).Updates(p).Error
  112. if err != nil {
  113. fields, _ := util.MarshalToString(*p)
  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. func (p *THouseRentManager) Count(db *gorm.DB, where map[string]interface{}, or map[string]interface{}) (int64, error) {
  122. cond, val, err := whereBuildAndOr(where, or)
  123. if err != nil {
  124. return 0, err
  125. }
  126. ret := int64(0)
  127. err = db.Table(p.TableName()).Where(cond, val...).Count(&ret).Error
  128. if err != nil {
  129. fields, _ := util.MarshalToString(where)
  130. logger.Error("mysql",
  131. zap.String("sql", "select count "+p.TableName()),
  132. zap.String("fields", fields),
  133. zap.String("error", err.Error()))
  134. }
  135. return ret, err
  136. }
  137. func (p *THouseRentManager) List(db *gorm.DB, where map[string]interface{}, or map[string]interface{}, page int, pageSize int) (list []THouseRentManager, err error) {
  138. cond, val, err := whereBuildAndOr(where, or)
  139. if err != nil {
  140. return list, err
  141. }
  142. if pageSize < 0 {
  143. result := db.Table(p.TableName()).Where(cond, val...).Find(&list)
  144. if result.Error != nil {
  145. wherefields, _ := util.MarshalToString(where)
  146. logger.Error("mysql",
  147. zap.String("sql", "select * from "+p.TableName()),
  148. zap.String("where", wherefields),
  149. zap.String("error", result.Error.Error()))
  150. }
  151. return list, result.Error
  152. }
  153. offset := (page - 1) * pageSize
  154. result := db.Table(p.TableName()).Where(cond, val...).Limit(pageSize).Offset(offset).Find(&list)
  155. if result.Error != nil {
  156. wherefields, _ := util.MarshalToString(where)
  157. logger.Error("mysql",
  158. zap.String("sql", "select * from "+p.TableName()),
  159. zap.String("where", wherefields),
  160. zap.String("error", result.Error.Error()))
  161. }
  162. return list, result.Error
  163. }