house.go 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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 THouse struct {
  13. ID int64 `gorm:"column:id;PRIMARY_KEY" json:"id"`
  14. BuildingId int64 `gorm:"column:building_id" json:"building_id"`
  15. UnitId int64 `gorm:"column:unit_id" json:"unit_id"`
  16. HouseNumber string `gorm:"column:house_number" json:"house_number"`
  17. Layer int64 `gorm:"column:layer" json:"layer"`
  18. RoomCount int64 `gorm:"column:room_count" json:"room_count"`
  19. HallCount int64 `gorm:"column:hall_count" json:"hall_count"`
  20. HouseType int64 `gorm:"column:house_type" json:"house_type"`
  21. HouseArea float64 `gorm:"column:house_area" json:"house_area"`
  22. HouseUsedArea float64 `gorm:"column:house_used_area" json:"house_used_area"`
  23. HasLift int64 `gorm:"column:has_lift" json:"has_lift"`
  24. CreatedAt time.Time `gorm:"column:created_at" json:"created_at"`
  25. UpdatedAt time.Time `gorm:"column:updated_at" json:"updated_at"`
  26. Status int `gorm:"column:status" json:"status"`
  27. table string
  28. }
  29. func (p *THouse) TableName() string {
  30. return p.table
  31. }
  32. func NewHouse(database string) *THouse {
  33. return &THouse{table: fmt.Sprintf("%s.%s", database, "t_house")}
  34. }
  35. func (p *THouse) SetTable(database string) {
  36. p.table = fmt.Sprintf("%s.%s", database, "t_house")
  37. }
  38. func (p *THouse) CreateTable(db *gorm.DB) error {
  39. sql :="CREATE TABLE IF NOT EXISTS "+p.TableName()+"("+
  40. "`id` bigint(11) NOT NULL AUTO_INCREMENT,"+
  41. "`building_id` int(10) NOT NULL COMMENT '楼栋id',"+
  42. "`unit_id` int(10) NOT NULL DEFAULT '0' COMMENT '单元id',"+
  43. "`house_number` varchar(32) COLLATE utf8mb4_bin NOT NULL COMMENT '门牌号',"+
  44. "`layer` int(10) NOT NULL COMMENT '第几层',"+
  45. "`room_count` int(10) NOT NULL COMMENT '几室',"+
  46. "`hall_count` int(10) NOT NULL COMMENT '几厅',"+
  47. "`house_type` int(10) NOT NULL COMMENT '1 住宅 2 商铺 3 办公',"+
  48. "`house_area` decimal(18,2) NOT NULL COMMENT '建筑面积',"+
  49. "`house_used_area` decimal(18,2) NOT NULL COMMENT '使用面积',"+
  50. "`has_lift` tinyint(1) NOT NULL COMMENT '是否有电梯 1 是 2 否',"+
  51. "`created_at` datetime NOT NULL,"+
  52. " `updated_at` datetime NOT NULL,"+
  53. " `status` tinyint(1) NOT NULL DEFAULT '1' COMMENT '1 未入住 2 已入住 3已出租',"+
  54. "PRIMARY KEY (`id`) USING BTREE,"+
  55. " UNIQUE KEY `house_index` (`building_id`,`unit_id`,`house_number`) USING BTREE"+
  56. ") ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin ROW_FORMAT=COMPACT;"
  57. err := db.Exec(sql).Error
  58. if err != nil {
  59. logger.Error("mysql",
  60. zap.String("sql", "create table "+p.TableName()),
  61. zap.String("fields", ""),
  62. zap.String("error", err.Error()))
  63. }
  64. return err
  65. }
  66. func (p *THouse) Find(db *gorm.DB, where map[string]interface{}) error {
  67. err := db.Table(p.TableName()).Where(where).Find(p).Error
  68. if err != nil {
  69. fields, _ := util.MarshalToString(where)
  70. logger.Error("mysql",
  71. zap.String("sql", "select from "+p.TableName()),
  72. zap.String("fields", fields),
  73. zap.String("error", err.Error()))
  74. }
  75. return err
  76. }
  77. func (p *THouse) Last(db *gorm.DB) error {
  78. err := db.Table(p.TableName()).Last(p).Error
  79. if err != nil {
  80. logger.Error("mysql",
  81. zap.String("sql", "select last from "+p.TableName()),
  82. zap.String("fields", ""),
  83. zap.String("error", err.Error()))
  84. }
  85. return err
  86. }
  87. // Insert 插入一条记录
  88. func (p *THouse) Insert(db *gorm.DB) error {
  89. err := db.Table(p.TableName()).Create(p).Error
  90. if err != nil {
  91. fields, _ := util.MarshalToString(*p)
  92. logger.Error("mysql",
  93. zap.String("sql", "insert into "+p.TableName()),
  94. zap.String("fields", fields),
  95. zap.String("error", err.Error()))
  96. }
  97. return err
  98. }
  99. func (p *THouse) InsertMulti(db *gorm.DB, values interface{}) error {
  100. err := db.Table(p.TableName()).Create(values).Error
  101. if err != nil {
  102. fields, _ := util.MarshalToString(*p)
  103. logger.Error("mysql",
  104. zap.String("sql", "insert into "+p.TableName()),
  105. zap.String("fields", fields),
  106. zap.String("error", err.Error()))
  107. }
  108. return err
  109. }
  110. func (p *THouse) Delete(db *gorm.DB, filter map[string]interface{}) error {
  111. cond, val, err := whereBuild(filter)
  112. if err != nil {
  113. return err
  114. }
  115. return db.Table(p.TableName()).Where(cond, val...).Delete(p).Error
  116. }
  117. func (p *THouse) Update(db *gorm.DB, where map[string]interface{}, values map[string]interface{}) error {
  118. cond, val, err := whereBuild(where)
  119. if err != nil {
  120. if err != nil {
  121. fields, _ := util.MarshalToString(values)
  122. logger.Error("mysql",
  123. zap.String("sql", "update "+p.TableName()),
  124. zap.String("fields", fields),
  125. zap.String("error", err.Error()))
  126. }
  127. return err
  128. }
  129. return db.Table(p.TableName()).Where(cond, val...).Updates(values).Error
  130. }
  131. func (p *THouse) UpdateByModel(db *gorm.DB) error {
  132. err := db.Table(p.TableName()).Model(p).Updates(p).Error
  133. if err != nil {
  134. fields, _ := util.MarshalToString(*p)
  135. logger.Error("mysql",
  136. zap.String("sql", "update "+p.TableName()),
  137. zap.String("fields", fields),
  138. zap.String("error", err.Error()))
  139. }
  140. return err
  141. }
  142. func (p *THouse) Count(db *gorm.DB, where map[string]interface{}, or map[string]interface{}) (int64, error) {
  143. cond, val, err := whereBuildAndOr(where, or)
  144. if err != nil {
  145. return 0, err
  146. }
  147. ret := int64(0)
  148. err = db.Table(p.TableName()).Where(cond, val...).Count(&ret).Error
  149. if err != nil {
  150. fields, _ := util.MarshalToString(where)
  151. logger.Error("mysql",
  152. zap.String("sql", "select count "+p.TableName()),
  153. zap.String("fields", fields),
  154. zap.String("error", err.Error()))
  155. }
  156. return ret, err
  157. }
  158. func (p *THouse) List(db *gorm.DB, where map[string]interface{}, or map[string]interface{}, page int, pageSize int) (list []THouse, err error) {
  159. cond, val, err := whereBuildAndOr(where, or)
  160. if err != nil {
  161. return list, err
  162. }
  163. if pageSize < 0 {
  164. result := db.Table(p.TableName()).Where(cond, val...).Order("created_at desc").Find(&list)
  165. if result.Error != nil {
  166. wherefields, _ := util.MarshalToString(where)
  167. logger.Error("mysql",
  168. zap.String("sql", "select * from "+p.TableName()),
  169. zap.String("where", wherefields),
  170. zap.String("error", result.Error.Error()))
  171. }
  172. return list, result.Error
  173. }
  174. offset := (page - 1) * pageSize
  175. result := db.Table(p.TableName()).Where(cond, val...).Limit(pageSize).Offset(offset).Order("created_at desc").Find(&list)
  176. if result.Error != nil {
  177. wherefields, _ := util.MarshalToString(where)
  178. logger.Error("mysql",
  179. zap.String("sql", "select * from "+p.TableName()),
  180. zap.String("where", wherefields),
  181. zap.String("error", result.Error.Error()))
  182. }
  183. return list, result.Error
  184. }
  185. func (p *THouse) GetIds(db *gorm.DB, where map[string]interface{}) ([]int64, error) {
  186. cond, val, err := whereBuildAndOr(where, nil)
  187. if err != nil {
  188. return nil, err
  189. }
  190. type Result struct {
  191. Id int64
  192. }
  193. array := []Result{}
  194. err = db.Table(p.TableName()).Select("id").Where(cond, val...).Find(&array).Error
  195. if err != nil {
  196. wherefields, _ := util.MarshalToString(where)
  197. logger.Error("mysql",
  198. zap.String("sql", "select id from "+p.TableName()),
  199. zap.String("where", wherefields),
  200. zap.String("error", err.Error()))
  201. }
  202. ret := make([]int64, len(array))
  203. for i, v := range array {
  204. ret[i] = v.Id
  205. }
  206. return ret, err
  207. }