t_house_rent.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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 THouseRent struct {
  13. ID int64 `gorm:"column:id;PRIMARY_KEY" json:"id"`
  14. GardenId int64 `gorm:"column:garden_id" json:"garden_id"`
  15. HouseName string `gorm:"column:house_name" json:"house_name"`
  16. Layer int64 `gorm:"column:layer" json:"layer"`
  17. HouseArea float64 `gorm:"column:house_area" json:"house_area"`
  18. Diretion int32 `gorm:"column:diretion" json:"diretion"`
  19. RoomCount int64 `gorm:"column:room_count" json:"room_count"`
  20. HallCount int64 `gorm:"column:hall_count" json:"hall_count"`
  21. WcCount int64 `gorm:"column:wc_count" json:"wc_count"`
  22. Decorating int32 `gorm:"column:decorating" json:"decorating"`
  23. Contacter string `gorm:"column:contacter" json:"contacter"`
  24. ContactPhone string `gorm:"column:contact_phone" json:"contact_phone"`
  25. PayTimeType int32 `gorm:"column:pay_time_type" json:"pay_time_type"`
  26. RentType int32 `gorm:"column:rent_type" json:"rent_type"`
  27. RoomType int32 `gorm:"column:room_type" json:"room_type"`
  28. RoomArea float64 `gorm:"column:room_area" json:"room_area"`
  29. RentPrice int64 `gorm:"column:rent_price" json:"rent_price"`
  30. Desposit int64 `gorm:"column:desposit" json:"desposit"`
  31. InTime int64 `gorm:"column:in_time" json:"in_time"`
  32. ServicePrice int64 `gorm:"column:service_price" json:"service_price"`
  33. IntermediaryPrice int64 `gorm:"column:intermediary_price" json:"intermediary_price"`
  34. BaseConf int64 `gorm:"column:base_conf" json:"base_conf"`
  35. SpecialConf int64 `gorm:"column:special_conf" json:"special_conf"`
  36. HousePic string `gorm:"column:house_pic" json:"house_pic"`
  37. CertPic string `gorm:"column:cert_pic" json:"cert_pic"`
  38. ProvinceCode string `gorm:"column:province_code" json:"province_code"`
  39. CityCode string `gorm:"column:city_code" json:"city_code"`
  40. AreaCode string `gorm:"column:area_code" json:"area_code"`
  41. StreetCode string `gorm:"column:street_code" json:"street_code"`
  42. GardenName string `gorm:"column:garden_name" json:"garden_name"`
  43. HouseholdUid int64 `gorm:"column:household_uid" json:"household_uid"`
  44. ApproveStatus int32 `gorm:"column:approve_status" json:"approve_status"`
  45. ApprovedAt time.Time `gorm:"column:approved_at" json:"approved_at"`
  46. HasLift int64 `gorm:"column:has_lift" json:"has_lift"`
  47. Feedback string `gorm:"column:feedback" json:"feedback"`
  48. CreatedAt time.Time `gorm:"column:created_at" json:"created_at"`
  49. UpdatedAt time.Time `gorm:"column:updated_at" json:"updated_at"`
  50. Desc string `gorm:"column:desc" json:"desc"`
  51. Lat float64 `gorm:"column:lat" json:"lat"`
  52. Lnt float64 `gorm:"column:lnt" json:"lnt"`
  53. HouseId int64 `gorm:"column:house_id" json:"house_id"`
  54. UnitId int64 `gorm:"column:unit_id" json:"unit_id"`
  55. table string
  56. }
  57. func (p *THouseRent) TableName() string {
  58. return p.table
  59. }
  60. func NewHouseRent(database string) *THouseRent {
  61. return &THouseRent{table: fmt.Sprintf("%s.%s", database, "t_house_rent")}
  62. }
  63. func (p *THouseRent) SetTable(database string) {
  64. p.table = fmt.Sprintf("%s.%s", database, "t_house_rent")
  65. }
  66. func (p *THouseRent) CreateTable(db *gorm.DB) error {
  67. sql := "CREATE TABLE IF NOT EXISTS " + p.TableName() + "(" +
  68. "`id` bigint(11) NOT NULL AUTO_INCREMENT," +
  69. "`garden_id` int(10) NOT NULL COMMENT '小区id'," +
  70. "`house_name` varchar(32) COLLATE utf8mb4_bin NOT NULL COMMENT '房屋全称1-1-906'," +
  71. "`layer` int(10) NOT NULL COMMENT '楼层'," +
  72. "`house_area` decimal(18,2) NOT NULL COMMENT '房屋面积'," +
  73. "`diretion` tinyint(1) NOT NULL COMMENT '朝向 1 东 2 东南 3 南 4 西南 5 西 6 西北 7 北 8 东北'," +
  74. "`room_count` int(10) NOT NULL COMMENT '几室'," +
  75. "`hall_count` int(10) NOT NULL COMMENT '几厅'," +
  76. "`wc_count` int(10) NOT NULL COMMENT '几卫'," +
  77. "`decorating` tinyint(1) NOT NULL COMMENT '1 精装 2 简装 3 清水'," +
  78. "`contacter` varchar(64) COLLATE utf8mb4_bin NOT NULL COMMENT '联系人'," +
  79. "`contact_phone` varchar(16) COLLATE utf8mb4_bin NOT NULL COMMENT '联系人电话'," +
  80. "`pay_time_type` tinyint(1) NOT NULL COMMENT '1 月付 2 季付 3 半年付 4 年付'," +
  81. "`rent_type` tinyint(1) NOT NULL COMMENT '1 整租 2 合租 3 转租'," +
  82. "`room_type` tinyint(1) NOT NULL COMMENT '1 全部 2 主卧 3 次卧'," +
  83. "`room_area` decimal(18,2) NOT NULL COMMENT '房间面积'," +
  84. "`rent_price` int(10) NOT NULL COMMENT '租金'," +
  85. "`desposit` int(10) NOT NULL COMMENT '押金'," +
  86. "`in_time` bigint(11) NOT NULL COMMENT '可入住时间'," +
  87. "`service_price` int(10) NOT NULL COMMENT '服务费'," +
  88. "`intermediary_price` int(10) NOT NULL COMMENT '中介费'," +
  89. "`base_conf` int(10) NOT NULL COMMENT 'bit表示 从低到高分别表示床 天然气 宽带 冰箱 衣柜 沙发 空调 电视机 热水器 洗衣机'," +
  90. "`special_conf` int(10) NOT NULL COMMENT 'bit 表示 从低到高分别表示 智能门锁 wifi 近地铁 停车位 独卫 私人阳台 首次出租'," +
  91. "`house_pic` text COLLATE utf8mb4_bin NOT NULL COMMENT '房屋照片'," +
  92. "`cert_pic` text COLLATE utf8mb4_bin NOT NULL COMMENT '证件照片'," +
  93. "`province_code` varchar(32) COLLATE utf8mb4_bin NOT NULL," +
  94. " `city_code` varchar(32) COLLATE utf8mb4_bin NOT NULL," +
  95. " `area_code` varchar(32) COLLATE utf8mb4_bin NOT NULL," +
  96. " `street_code` varchar(32) COLLATE utf8mb4_bin NOT NULL," +
  97. " `garden_name` varchar(128) COLLATE utf8mb4_bin NOT NULL," +
  98. " `household_uid` bigint(11) NOT NULL," +
  99. " `approve_status` tinyint(1) NOT NULL COMMENT '1 待审核 2 通过 3 未通过 4 下架'," +
  100. "`approved_at` datetime NOT NULL COMMENT '审核时间'," +
  101. "`has_lift` tinyint(1) DEFAULT NULL COMMENT '1 有电梯 2 无电梯'," +
  102. "`feedback` varchar(255) COLLATE utf8mb4_bin NOT NULL COMMENT '审核意见'," +
  103. "`desc` text COLLATE utf8mb4_bin," +
  104. "`lat` decimal(18,6) NOT NULL COMMENT '纬度'," +
  105. "`lnt` decimal(18,6) NOT NULL COMMENT '经度'," +
  106. "`house_id` bigint(20) NOT NULL," +
  107. "`unit_id` int(10) NOT NULL DEFAULT '0' COMMENT '单元id'," +
  108. "`created_at` datetime NOT NULL," +
  109. " `updated_at` datetime NOT NULL," +
  110. "PRIMARY KEY (`id`)," +
  111. "KEY `garden_id` (`garden_id`,`house_id`) USING BTREE," +
  112. "KEY `province` (`province_code`) USING BTREE," +
  113. "KEY `city` (`city_code`) USING BTREE," +
  114. "KEY `area` (`area_code`) USING BTREE," +
  115. "KEY `street` (`street_code`) USING BTREE," +
  116. "KEY `rent_price` (`rent_price`) USING BTREE" +
  117. ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;"
  118. err := db.Exec(sql).Error
  119. if err != nil {
  120. logger.Error("mysql",
  121. zap.String("sql", "create table "+p.TableName()),
  122. zap.String("fields", ""),
  123. zap.String("error", err.Error()))
  124. }
  125. return err
  126. }
  127. func (p *THouseRent) Find(db *gorm.DB, where map[string]interface{}) error {
  128. err := db.Table(p.TableName()).Where(where).Find(p).Error
  129. if err != nil {
  130. fields, _ := util.MarshalToString(where)
  131. logger.Error("mysql",
  132. zap.String("sql", "select from "+p.TableName()),
  133. zap.String("fields", fields),
  134. zap.String("error", err.Error()))
  135. }
  136. return err
  137. }
  138. func (p *THouseRent) Last(db *gorm.DB) error {
  139. err := db.Table(p.TableName()).Last(p).Error
  140. if err != nil {
  141. logger.Error("mysql",
  142. zap.String("sql", "select last from "+p.TableName()),
  143. zap.String("fields", ""),
  144. zap.String("error", err.Error()))
  145. }
  146. return err
  147. }
  148. // Insert 插入一条记录
  149. func (p *THouseRent) Insert(db *gorm.DB) error {
  150. err := db.Table(p.TableName()).Create(p).Error
  151. if err != nil {
  152. fields, _ := util.MarshalToString(*p)
  153. logger.Error("mysql",
  154. zap.String("sql", "insert into "+p.TableName()),
  155. zap.String("fields", fields),
  156. zap.String("error", err.Error()))
  157. }
  158. return err
  159. }
  160. // Insert 插入多条记录
  161. func (p *THouseRent) InsertMulti(db *gorm.DB, values interface{}) error {
  162. err := db.Table(p.TableName()).Create(values).Error
  163. if err != nil {
  164. fields, _ := util.MarshalToString(*p)
  165. logger.Error("mysql",
  166. zap.String("sql", "insert into "+p.TableName()),
  167. zap.String("fields", fields),
  168. zap.String("error", err.Error()))
  169. }
  170. return err
  171. }
  172. func (p *THouseRent) Delete(db *gorm.DB, filter map[string]interface{}) error {
  173. cond, val, err := whereBuild(filter)
  174. if err != nil {
  175. return err
  176. }
  177. return db.Table(p.TableName()).Where(cond, val...).Delete(p).Error
  178. }
  179. func (p *THouseRent) Update(db *gorm.DB, where map[string]interface{}, values map[string]interface{}) error {
  180. cond, val, err := whereBuild(where)
  181. if err != nil {
  182. if err != nil {
  183. fields, _ := util.MarshalToString(values)
  184. logger.Error("mysql",
  185. zap.String("sql", "update "+p.TableName()),
  186. zap.String("fields", fields),
  187. zap.String("error", err.Error()))
  188. }
  189. return err
  190. }
  191. return db.Table(p.TableName()).Where(cond, val...).Updates(values).Error
  192. }
  193. func (p *THouseRent) UpdateByModel(db *gorm.DB) error {
  194. err := db.Table(p.TableName()).Model(p).Updates(p).Error
  195. if err != nil {
  196. fields, _ := util.MarshalToString(*p)
  197. logger.Error("mysql",
  198. zap.String("sql", "update "+p.TableName()),
  199. zap.String("fields", fields),
  200. zap.String("error", err.Error()))
  201. }
  202. return err
  203. }
  204. func (p *THouseRent) Count(db *gorm.DB, where map[string]interface{}, or map[string]interface{}) (int64, error) {
  205. cond, val, err := whereBuildAndOr(where, or)
  206. if err != nil {
  207. return 0, err
  208. }
  209. ret := int64(0)
  210. err = db.Table(p.TableName()).Where(cond, val...).Count(&ret).Error
  211. if err != nil {
  212. fields, _ := util.MarshalToString(where)
  213. logger.Error("mysql",
  214. zap.String("sql", "select count "+p.TableName()),
  215. zap.String("fields", fields),
  216. zap.String("error", err.Error()))
  217. }
  218. return ret, err
  219. }
  220. func (p *THouseRent) List(db *gorm.DB, where map[string]interface{}, or map[string]interface{}, page int, pageSize int) (list []THouseRent, err error) {
  221. cond, val, err := whereBuildAndOr(where, or)
  222. if err != nil {
  223. return list, err
  224. }
  225. if pageSize < 0 {
  226. result := db.Table(p.TableName()).Where(cond, val...).Order("created_at desc").Find(&list)
  227. if result.Error != nil {
  228. wherefields, _ := util.MarshalToString(where)
  229. logger.Error("mysql",
  230. zap.String("sql", "select * from "+p.TableName()),
  231. zap.String("where", wherefields),
  232. zap.String("error", result.Error.Error()))
  233. }
  234. return list, result.Error
  235. }
  236. offset := (page - 1) * pageSize
  237. result := db.Table(p.TableName()).Where(cond, val...).Limit(pageSize).Offset(offset).Order("created_at desc").Find(&list)
  238. if result.Error != nil {
  239. wherefields, _ := util.MarshalToString(where)
  240. logger.Error("mysql",
  241. zap.String("sql", "select * from "+p.TableName()),
  242. zap.String("where", wherefields),
  243. zap.String("error", result.Error.Error()))
  244. }
  245. return list, result.Error
  246. }
  247. func (p *THouseRent) Select(db *gorm.DB, where map[string]interface{}, or map[string]interface{}, selectStr string, result interface{}) (err error) {
  248. cond, val, err := whereBuildAndOr(where, or)
  249. if err != nil {
  250. return err
  251. }
  252. r := db.Table(p.TableName()).Select(selectStr).Where(cond, val...).Find(result)
  253. if r.Error != nil {
  254. wherefields, _ := util.MarshalToString(where)
  255. logger.Error("mysql",
  256. zap.String("sql", "select "+selectStr+" from "+p.TableName()),
  257. zap.String("where", wherefields),
  258. zap.String("error", r.Error.Error()))
  259. }
  260. return r.Error
  261. }