123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291 |
- // Copyright 2019 getensh.com. All rights reserved.
- // Use of this source code is governed by getensh.com.
- package model
- import (
- "fmt"
- "git.getensh.com/common/gopkgs/logger"
- "git.getensh.com/common/gopkgs/util"
- "go.uber.org/zap"
- "gorm.io/gorm"
- "time"
- )
- type THouseRent struct {
- ID int64 `gorm:"column:id;PRIMARY_KEY" json:"id"`
- GardenId int64 `gorm:"column:garden_id" json:"garden_id"`
- HouseName string `gorm:"column:house_name" json:"house_name"`
- Layer int64 `gorm:"column:layer" json:"layer"`
- HouseArea float64 `gorm:"column:house_area" json:"house_area"`
- Diretion int32 `gorm:"column:diretion" json:"diretion"`
- RoomCount int64 `gorm:"column:room_count" json:"room_count"`
- HallCount int64 `gorm:"column:hall_count" json:"hall_count"`
- WcCount int64 `gorm:"column:wc_count" json:"wc_count"`
- Decorating int32 `gorm:"column:decorating" json:"decorating"`
- Contacter string `gorm:"column:contacter" json:"contacter"`
- ContactPhone string `gorm:"column:contact_phone" json:"contact_phone"`
- PayTimeType int32 `gorm:"column:pay_time_type" json:"pay_time_type"`
- RentType int32 `gorm:"column:rent_type" json:"rent_type"`
- RoomType int32 `gorm:"column:room_type" json:"room_type"`
- RoomArea float64 `gorm:"column:room_area" json:"room_area"`
- RentPrice int64 `gorm:"column:rent_price" json:"rent_price"`
- Desposit int64 `gorm:"column:desposit" json:"desposit"`
- InTime int64 `gorm:"column:in_time" json:"in_time"`
- ServicePrice int64 `gorm:"column:service_price" json:"service_price"`
- IntermediaryPrice int64 `gorm:"column:intermediary_price" json:"intermediary_price"`
- BaseConf int64 `gorm:"column:base_conf" json:"base_conf"`
- SpecialConf int64 `gorm:"column:special_conf" json:"special_conf"`
- HousePic string `gorm:"column:house_pic" json:"house_pic"`
- CertPic string `gorm:"column:cert_pic" json:"cert_pic"`
- ProvinceCode string `gorm:"column:province_code" json:"province_code"`
- CityCode string `gorm:"column:city_code" json:"city_code"`
- AreaCode string `gorm:"column:area_code" json:"area_code"`
- StreetCode string `gorm:"column:street_code" json:"street_code"`
- GardenName string `gorm:"column:garden_name" json:"garden_name"`
- HouseholdUid int64 `gorm:"column:household_uid" json:"household_uid"`
- ApproveStatus int32 `gorm:"column:approve_status" json:"approve_status"`
- ApprovedAt time.Time `gorm:"column:approved_at" json:"approved_at"`
- HasLift int64 `gorm:"column:has_lift" json:"has_lift"`
- Feedback string `gorm:"column:feedback" json:"feedback"`
- CreatedAt time.Time `gorm:"column:created_at" json:"created_at"`
- UpdatedAt time.Time `gorm:"column:updated_at" json:"updated_at"`
- Desc string `gorm:"column:desc" json:"desc"`
- Lat float64 `gorm:"column:lat" json:"lat"`
- Lnt float64 `gorm:"column:lnt" json:"lnt"`
- HouseId int64 `gorm:"column:house_id" json:"house_id"`
- UnitId int64 `gorm:"column:unit_id" json:"unit_id"`
- table string
- }
- func (p *THouseRent) TableName() string {
- return p.table
- }
- func NewHouseRent(database string) *THouseRent {
- return &THouseRent{table: fmt.Sprintf("%s.%s", database, "t_house_rent")}
- }
- func (p *THouseRent) SetTable(database string) {
- p.table = fmt.Sprintf("%s.%s", database, "t_house_rent")
- }
- func (p *THouseRent) CreateTable(db *gorm.DB) error {
- sql := "CREATE TABLE IF NOT EXISTS " + p.TableName() + "(" +
- "`id` bigint(11) NOT NULL AUTO_INCREMENT," +
- "`garden_id` int(10) NOT NULL COMMENT '小区id'," +
- "`house_name` varchar(32) COLLATE utf8mb4_bin NOT NULL COMMENT '房屋全称1-1-906'," +
- "`layer` int(10) NOT NULL COMMENT '楼层'," +
- "`house_area` decimal(18,2) NOT NULL COMMENT '房屋面积'," +
- "`diretion` tinyint(1) NOT NULL COMMENT '朝向 1 东 2 东南 3 南 4 西南 5 西 6 西北 7 北 8 东北'," +
- "`room_count` int(10) NOT NULL COMMENT '几室'," +
- "`hall_count` int(10) NOT NULL COMMENT '几厅'," +
- "`wc_count` int(10) NOT NULL COMMENT '几卫'," +
- "`decorating` tinyint(1) NOT NULL COMMENT '1 精装 2 简装 3 清水'," +
- "`contacter` varchar(64) COLLATE utf8mb4_bin NOT NULL COMMENT '联系人'," +
- "`contact_phone` varchar(16) COLLATE utf8mb4_bin NOT NULL COMMENT '联系人电话'," +
- "`pay_time_type` tinyint(1) NOT NULL COMMENT '1 月付 2 季付 3 半年付 4 年付'," +
- "`rent_type` tinyint(1) NOT NULL COMMENT '1 整租 2 合租 3 转租'," +
- "`room_type` tinyint(1) NOT NULL COMMENT '1 全部 2 主卧 3 次卧'," +
- "`room_area` decimal(18,2) NOT NULL COMMENT '房间面积'," +
- "`rent_price` int(10) NOT NULL COMMENT '租金'," +
- "`desposit` int(10) NOT NULL COMMENT '押金'," +
- "`in_time` bigint(11) NOT NULL COMMENT '可入住时间'," +
- "`service_price` int(10) NOT NULL COMMENT '服务费'," +
- "`intermediary_price` int(10) NOT NULL COMMENT '中介费'," +
- "`base_conf` int(10) NOT NULL COMMENT 'bit表示 从低到高分别表示床 天然气 宽带 冰箱 衣柜 沙发 空调 电视机 热水器 洗衣机'," +
- "`special_conf` int(10) NOT NULL COMMENT 'bit 表示 从低到高分别表示 智能门锁 wifi 近地铁 停车位 独卫 私人阳台 首次出租'," +
- "`house_pic` text COLLATE utf8mb4_bin NOT NULL COMMENT '房屋照片'," +
- "`cert_pic` text COLLATE utf8mb4_bin NOT NULL COMMENT '证件照片'," +
- "`province_code` varchar(32) COLLATE utf8mb4_bin NOT NULL," +
- " `city_code` varchar(32) COLLATE utf8mb4_bin NOT NULL," +
- " `area_code` varchar(32) COLLATE utf8mb4_bin NOT NULL," +
- " `street_code` varchar(32) COLLATE utf8mb4_bin NOT NULL," +
- " `garden_name` varchar(128) COLLATE utf8mb4_bin NOT NULL," +
- " `household_uid` bigint(11) NOT NULL," +
- " `approve_status` tinyint(1) NOT NULL COMMENT '1 待审核 2 通过 3 未通过 4 下架'," +
- "`approved_at` datetime NOT NULL COMMENT '审核时间'," +
- "`has_lift` tinyint(1) DEFAULT NULL COMMENT '1 有电梯 2 无电梯'," +
- "`feedback` varchar(255) COLLATE utf8mb4_bin NOT NULL COMMENT '审核意见'," +
- "`desc` text COLLATE utf8mb4_bin," +
- "`lat` decimal(18,6) NOT NULL COMMENT '纬度'," +
- "`lnt` decimal(18,6) NOT NULL COMMENT '经度'," +
- "`house_id` bigint(20) NOT NULL," +
- "`unit_id` int(10) NOT NULL DEFAULT '0' COMMENT '单元id'," +
- "`created_at` datetime NOT NULL," +
- " `updated_at` datetime NOT NULL," +
- "PRIMARY KEY (`id`)," +
- "KEY `garden_id` (`garden_id`,`house_id`) USING BTREE," +
- "KEY `province` (`province_code`) USING BTREE," +
- "KEY `city` (`city_code`) USING BTREE," +
- "KEY `area` (`area_code`) USING BTREE," +
- "KEY `street` (`street_code`) USING BTREE," +
- "KEY `rent_price` (`rent_price`) USING BTREE" +
- ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;"
- err := db.Exec(sql).Error
- if err != nil {
- logger.Error("mysql",
- zap.String("sql", "create table "+p.TableName()),
- zap.String("fields", ""),
- zap.String("error", err.Error()))
- }
- return err
- }
- func (p *THouseRent) Find(db *gorm.DB, where map[string]interface{}) error {
- err := db.Table(p.TableName()).Where(where).Find(p).Error
- if err != nil {
- fields, _ := util.MarshalToString(where)
- logger.Error("mysql",
- zap.String("sql", "select from "+p.TableName()),
- zap.String("fields", fields),
- zap.String("error", err.Error()))
- }
- return err
- }
- func (p *THouseRent) Last(db *gorm.DB) error {
- err := db.Table(p.TableName()).Last(p).Error
- if err != nil {
- logger.Error("mysql",
- zap.String("sql", "select last from "+p.TableName()),
- zap.String("fields", ""),
- zap.String("error", err.Error()))
- }
- return err
- }
- // Insert 插入一条记录
- func (p *THouseRent) Insert(db *gorm.DB) error {
- err := db.Table(p.TableName()).Create(p).Error
- if err != nil {
- fields, _ := util.MarshalToString(*p)
- logger.Error("mysql",
- zap.String("sql", "insert into "+p.TableName()),
- zap.String("fields", fields),
- zap.String("error", err.Error()))
- }
- return err
- }
- // Insert 插入多条记录
- func (p *THouseRent) InsertMulti(db *gorm.DB, values interface{}) error {
- err := db.Table(p.TableName()).Create(values).Error
- if err != nil {
- fields, _ := util.MarshalToString(*p)
- logger.Error("mysql",
- zap.String("sql", "insert into "+p.TableName()),
- zap.String("fields", fields),
- zap.String("error", err.Error()))
- }
- return err
- }
- func (p *THouseRent) Delete(db *gorm.DB, filter map[string]interface{}) error {
- cond, val, err := whereBuild(filter)
- if err != nil {
- return err
- }
- return db.Table(p.TableName()).Where(cond, val...).Delete(p).Error
- }
- func (p *THouseRent) Update(db *gorm.DB, where map[string]interface{}, values map[string]interface{}) error {
- cond, val, err := whereBuild(where)
- if err != nil {
- if err != nil {
- fields, _ := util.MarshalToString(values)
- logger.Error("mysql",
- zap.String("sql", "update "+p.TableName()),
- zap.String("fields", fields),
- zap.String("error", err.Error()))
- }
- return err
- }
- return db.Table(p.TableName()).Where(cond, val...).Updates(values).Error
- }
- func (p *THouseRent) UpdateByModel(db *gorm.DB) error {
- err := db.Table(p.TableName()).Model(p).Updates(p).Error
- if err != nil {
- fields, _ := util.MarshalToString(*p)
- logger.Error("mysql",
- zap.String("sql", "update "+p.TableName()),
- zap.String("fields", fields),
- zap.String("error", err.Error()))
- }
- return err
- }
- func (p *THouseRent) Count(db *gorm.DB, where map[string]interface{}, or map[string]interface{}) (int64, error) {
- cond, val, err := whereBuildAndOr(where, or)
- if err != nil {
- return 0, err
- }
- ret := int64(0)
- err = db.Table(p.TableName()).Where(cond, val...).Count(&ret).Error
- if err != nil {
- fields, _ := util.MarshalToString(where)
- logger.Error("mysql",
- zap.String("sql", "select count "+p.TableName()),
- zap.String("fields", fields),
- zap.String("error", err.Error()))
- }
- return ret, err
- }
- func (p *THouseRent) List(db *gorm.DB, where map[string]interface{}, or map[string]interface{}, page int, pageSize int) (list []THouseRent, err error) {
- cond, val, err := whereBuildAndOr(where, or)
- if err != nil {
- return list, err
- }
- if pageSize < 0 {
- result := db.Table(p.TableName()).Where(cond, val...).Order("created_at desc").Find(&list)
- if result.Error != nil {
- wherefields, _ := util.MarshalToString(where)
- logger.Error("mysql",
- zap.String("sql", "select * from "+p.TableName()),
- zap.String("where", wherefields),
- zap.String("error", result.Error.Error()))
- }
- return list, result.Error
- }
- offset := (page - 1) * pageSize
- result := db.Table(p.TableName()).Where(cond, val...).Limit(pageSize).Offset(offset).Order("created_at desc").Find(&list)
- if result.Error != nil {
- wherefields, _ := util.MarshalToString(where)
- logger.Error("mysql",
- zap.String("sql", "select * from "+p.TableName()),
- zap.String("where", wherefields),
- zap.String("error", result.Error.Error()))
- }
- return list, result.Error
- }
- func (p *THouseRent) Select(db *gorm.DB, where map[string]interface{}, or map[string]interface{}, selectStr string, result interface{}) (err error) {
- cond, val, err := whereBuildAndOr(where, or)
- if err != nil {
- return err
- }
- r := db.Table(p.TableName()).Select(selectStr).Where(cond, val...).Find(result)
- if r.Error != nil {
- wherefields, _ := util.MarshalToString(where)
- logger.Error("mysql",
- zap.String("sql", "select "+selectStr+" from "+p.TableName()),
- zap.String("where", wherefields),
- zap.String("error", r.Error.Error()))
- }
- return r.Error
- }
|