// 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 TPropertyPackage struct { ID int64 `gorm:"column:id;PRIMARY_KEY" json:"id"` PackageType int32 `gorm:"column:package_type" json:"package_type"` GiveMonths int64 `gorm:"column:give_months" json:"give_months"` GiveContent string `gorm:"column:give_content" json:"give_content"` Enable int32 `gorm:"column:enable" json:"enable"` PayMonths int64 `gorm:"column:pay_months" json:"pay_months"` CreatedAt time.Time `gorm:"column:created_at" json:"created_at"` UpdatedAt time.Time `gorm:"column:updated_at" json:"updated_at"` table string } func (p *TPropertyPackage) TableName() string { return p.table } func NewPropertyPackage(database string) *TPropertyPackage { return &TPropertyPackage{table: fmt.Sprintf("%s.%s", database, "t_property_package")} } func (p *TPropertyPackage) SetTable(database string) { p.table = fmt.Sprintf("%s.%s", database, "t_property_package") } func (p *TPropertyPackage) CreateTable(db *gorm.DB) error { sql :="CREATE TABLE IF NOT EXISTS "+p.TableName()+"("+ "`id` int(10) NOT NULL AUTO_INCREMENT,"+ "`package_type` tinyint(1) NOT NULL COMMENT '1 按月 2 文本',"+ "`give_months` int(10) NOT NULL COMMENT '赠送月数,按月有效',"+ "`give_content` text COLLATE utf8mb4_bin NOT NULL COMMENT '赠送内容文本',"+ "`enable` tinyint(1) NOT NULL COMMENT '1 启用 2 停用',"+ "`pay_months` int(10) NOT NULL COMMENT '缴费月数',"+ "`created_at` datetime NOT NULL,"+ " `updated_at` datetime NOT NULL,"+ " PRIMARY KEY (`id`)"+ ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin COMMENT='物业费套餐';" 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 *TPropertyPackage) Find(db *gorm.DB, where map[string]interface{}) error { cond, val, err := whereBuild(where) if err != nil { return err } err = db.Table(p.TableName()).Where(cond, val...).First(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 *TPropertyPackage) 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 *TPropertyPackage) 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 *TPropertyPackage) 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 *TPropertyPackage) 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 *TPropertyPackage) 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 *TPropertyPackage) 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 *TPropertyPackage) 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 *TPropertyPackage) List(db *gorm.DB, where map[string]interface{}, or map[string]interface{}, page int, pageSize int) (list []TPropertyPackage, 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...).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).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 }