// 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 TChargeConf struct { ID int64 `gorm:"column:id" json:"id"` ChargeType int32 `gorm:"column:charge_type" json:"charge_type"` ChargeName string `gorm:"column:charge_name" json:"charge_name"` ChargeTimeType int32 `gorm:"column:charge_time_type" json:"charge_time_type"` ChargeBasis int32 `gorm:"column:charge_basis" json:"charge_basis"` FixAmount int64 `gorm:"column:fix_amount" json:"fix_amount"` FixAmountName string `gorm:"column:fix_amount_name" json:"fix_amount_name"` UnitPrice int64 `gorm:"column:unit_price" json:"unit_price"` BillPeriod int32 `gorm:"column:bill_period" json:"bill_period"` BillPeriodType int32 `gorm:"column:bill_period_type" json:"bill_period_type"` ChargeEffectiveTime int64 `gorm:"column:charge_effective_time" json:"charge_effective_time"` LateFeeDay int64 `gorm:"column:late_fee_day" json:"late_fee_day"` LateFeeMaxDays int64 `gorm:"column:late_fee_max_days" json:"late_fee_max_days"` LateFeePercent int64 `gorm:"column:late_fee_percent" json:"late_fee_percent"` LateFeeEnable int32 `gorm:"column:late_fee_enable" json:"late_fee_enable"` BillLastTime int64 `gorm:"column:bill_last_time" json:"bill_last_time"` UpdatedAt time.Time `gorm:"column:updated_at" json:"updated_at"` CreatedAt time.Time `gorm:"column:created_at" json:"created_at"` table string } func (p *TChargeConf) TableName() string { return p.table } func NewChargeConf(database string) *TChargeConf { return &TChargeConf{table: fmt.Sprintf("%s.%s", database, "t_charge_conf")} } func (p *TChargeConf) SetTable(database string) { p.table = fmt.Sprintf("%s.%s", database, "t_charge_conf") } func (p *TChargeConf) CreateTable(db *gorm.DB) error { sql := "CREATE TABLE IF NOT EXISTS " + p.TableName() + "(" + "`id` int(10) NOT NULL AUTO_INCREMENT," + "`charge_type` int(10) NOT NULL COMMENT '费用类型1 物业费 2 水费 3 电费 4 气费 5 车位管理费 6 月租停车费 99 其他'," + "`charge_name` varchar(128) COLLATE utf8mb4_bin NOT NULL," + " `charge_time_type` tinyint(1) NOT NULL COMMENT ' 1.周期收费 2 单次收费'," + "`charge_basis` tinyint(1) NOT NULL COMMENT '1 按房屋面积 2 按使用面积 3 按车位面积 4 按使用量 5 固定费用 6 自定义'," + "`fix_amount` int(10) NOT NULL COMMENT '固定费用 分'," + "`fix_amount_name` varchar(128) COLLATE utf8mb4_bin NOT NULL COMMENT '固定费用别名'," + "`unit_price` int(10) NOT NULL COMMENT '单价'," + "`bill_period` int(10) NOT NULL COMMENT '账单生成周期 1 月 2 季度 3 半年 4 一年'," + "`bill_period_type` tinyint(1) NOT NULL COMMENT '账单周期计算方式1 自然周期 2 按费用生效时间'," + "`charge_effective_time` bigint(11) NOT NULL COMMENT '费用生效时间'," + "`late_fee_day` int(10) NOT NULL COMMENT '账单生成后xx天收滞纳金'," + "`late_fee_max_days` int(10) NOT NULL COMMENT '滞纳金封顶天数'," + "`late_fee_percent` decimal(18,2) NOT NULL COMMENT '滞纳金比例 1 代表 1/1000'," + "`late_fee_enable` tinyint(1) NOT NULL COMMENT '1 滞纳金启用 2 滞纳金禁用'," + "`bill_last_time` bigint(11) NOT NULL COMMENT '账单上次生成时间'," + "`updated_at` datetime NOT NULL," + " `created_at` datetime NOT NULL," + " PRIMARY KEY (`id`) USING BTREE" + ") ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin ROW_FORMAT=COMPACT 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 *TChargeConf) Find(db *gorm.DB, where map[string]interface{}) error { err := db.Table(p.TableName()).Where(where).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 *TChargeConf) 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 *TChargeConf) 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 *TChargeConf) Delete(db *gorm.DB, filter map[string]interface{}) error { err := db.Table(p.TableName()).Where(filter).Delete(p).Error if err != nil { fields, _ := util.MarshalToString(filter) logger.Error("mysql", zap.String("sql", "delete from "+p.TableName()), zap.String("where", fields), zap.String("error", err.Error())) } return err } func (p *TChargeConf) 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 *TChargeConf) UpdateByModel(db *gorm.DB) error { err := db.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 *TChargeConf) 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 *TChargeConf) List(db *gorm.DB, where map[string]interface{}, or map[string]interface{}, page int, pageSize int) (list []TChargeConf, err error) { if page >= 0 { return p.ListByJoin(db, where, or, page, pageSize) } cond, val, err := whereBuildAndOr(where, or) if err != nil { return list, err } 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 } func (p *TChargeConf) ListByJoin(db *gorm.DB, where map[string]interface{}, or map[string]interface{}, page int, pageSize int) (list []TChargeConf, err error) { cond, val, err := whereBuildAndOr(where, or) if err != nil { return list, err } offset := (page - 1) * pageSize sql := fmt.Sprintf("select * from %s t1 join (select id from %s limit %d offset %d) t2 on t1.id=t2.id", p.TableName(), p.TableName(), pageSize, offset) if cond != "" { sql = fmt.Sprintf("select * from %s t1 join (select id from %s where %s limit %d offset %d) t2 on t1.id=t2.id", p.TableName(), p.TableName(), cond, pageSize, offset) } result := db.Raw(sql, val...).Scan(&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 }