// 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 THouse struct { ID int64 `gorm:"column:id;PRIMARY_KEY" json:"id"` BuildingId int64 `gorm:"column:building_id" json:"building_id"` UnitId int64 `gorm:"column:unit_id" json:"unit_id"` HouseNumber string `gorm:"column:house_number" json:"house_number"` Layer int64 `gorm:"column:layer" json:"layer"` RoomCount int64 `gorm:"column:room_count" json:"room_count"` HallCount int64 `gorm:"column:hall_count" json:"hall_count"` HouseType int64 `gorm:"column:house_type" json:"house_type"` HouseArea float64 `gorm:"column:house_area" json:"house_area"` HouseUsedArea float64 `gorm:"column:house_used_area" json:"house_used_area"` HasLift int64 `gorm:"column:has_lift" json:"has_lift"` CreatedAt time.Time `gorm:"column:created_at" json:"created_at"` UpdatedAt time.Time `gorm:"column:updated_at" json:"updated_at"` Status int `gorm:"column:status" json:"status"` table string } func (p *THouse) TableName() string { return p.table } func NewHouse(database string) *THouse { return &THouse{table: fmt.Sprintf("%s.%s", database, "t_house")} } func (p *THouse) SetTable(database string) { p.table = fmt.Sprintf("%s.%s", database, "t_house") } func (p *THouse) CreateTable(db *gorm.DB) error { sql :="CREATE TABLE IF NOT EXISTS "+p.TableName()+"("+ "`id` bigint(11) NOT NULL AUTO_INCREMENT,"+ "`building_id` int(10) NOT NULL COMMENT '楼栋id',"+ "`unit_id` int(10) NOT NULL DEFAULT '0' COMMENT '单元id',"+ "`house_number` varchar(32) COLLATE utf8mb4_bin NOT NULL COMMENT '门牌号',"+ "`layer` int(10) NOT NULL COMMENT '第几层',"+ "`room_count` int(10) NOT NULL COMMENT '几室',"+ "`hall_count` int(10) NOT NULL COMMENT '几厅',"+ "`house_type` int(10) NOT NULL COMMENT '1 住宅 2 商铺 3 办公',"+ "`house_area` decimal(18,2) NOT NULL COMMENT '建筑面积',"+ "`house_used_area` decimal(18,2) NOT NULL COMMENT '使用面积',"+ "`has_lift` tinyint(1) NOT NULL COMMENT '是否有电梯 1 是 2 否',"+ "`created_at` datetime NOT NULL,"+ " `updated_at` datetime NOT NULL,"+ " `status` tinyint(1) NOT NULL DEFAULT '1' COMMENT '1 未入住 2 已入住 3已出租',"+ "PRIMARY KEY (`id`) USING BTREE,"+ " UNIQUE KEY `house_index` (`building_id`,`unit_id`,`house_number`) USING BTREE"+ ") ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin ROW_FORMAT=COMPACT;" 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 *THouse) 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 *THouse) 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 *THouse) 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 } func (p *THouse) 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 *THouse) 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 *THouse) 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 *THouse) 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 *THouse) 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 *THouse) List(db *gorm.DB, where map[string]interface{}, or map[string]interface{}, page int, pageSize int) (list []THouse, 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 *THouse) GetIds(db *gorm.DB, where map[string]interface{}) ([]int64, error) { cond, val, err := whereBuildAndOr(where, nil) if err != nil { return nil, err } type Result struct { Id int64 } array := []Result{} err = db.Table(p.TableName()).Select("id").Where(cond, val...).Find(&array).Error if err != nil { wherefields, _ := util.MarshalToString(where) logger.Error("mysql", zap.String("sql", "select id from "+p.TableName()), zap.String("where", wherefields), zap.String("error", err.Error())) } ret := make([]int64, len(array)) for i, v := range array { ret[i] = v.Id } return ret, err }