// 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 TChargeBillDel struct { ID int64 `gorm:"column:id;PRIMARY_KEY" json:"id"` ObjType int32 `gorm:"column:obj_type" json:"obj_type"` ObjId int64 `gorm:"column:obj_id" json:"obj_id"` ChargeId int64 `gorm:"column:charge_id" json:"charge_id"` ChargeBindId int64 `gorm:"column:charge_bind_id" json:"charge_bind_id"` ChargeTimeType int32 `gorm:"column:charge_time_type" json:"charge_time_type"` ChargeDesc string `gorm:"column:charge_desc" json:"charge_desc"` Comment string `gorm:"column:comment" json:"comment"` Amount int64 `gorm:"column:amount" json:"amount"` ChargePowerId int64 `gorm:"column:charge_power_id" json:"charge_power_id"` ChargeType int32 `gorm:"column:charge_type" json:"charge_type"` ChargeName string `gorm:"column:charge_name" json:"charge_name"` Uid int64 `gorm:"column:uid" json:"uid"` PayMode int64 `gorm:"column:pay_mode" json:"pay_mode"` PackageId int64 `gorm:"column:package_id" json:"package_id"` ChargeEnd int64 `gorm:"column:charge_end" json:"charge_end"` ChargeStart int64 `gorm:"column:charge_start" json:"charge_start"` OrderId string `gorm:"column:order_id" json:"order_id"` PayTime int64 `gorm:"column:pay_time" json:"pay_time"` HouseId int64 `gorm:"column:house_id" json:"house_id"` ObjName string `gorm:"column:obj_name" json:"obj_name"` LateFee int64 `gorm:"column:late_fee" json:"late_fee"` CreatedAt time.Time `gorm:"column:created_at" json:"created_at"` UpdatedAt time.Time `gorm:"column:updated_at" json:"updated_at"` PayType int32 `gorm:"column:pay_type" json:"pay_type"` Status int32 `gorm:"column:status" json:"status"` DelAt time.Time `gorm:"column:del_at" json:"del_at"` DelReason string `gorm:"column:del_reason" json:"del_reason"` table string } func (p *TChargeBillDel) TableName() string { return p.table } func NewChargeBillDel(database string) *TChargeBillDel { return &TChargeBillDel{table: fmt.Sprintf("%s.%s", database, "t_charge_bill_del")} } func (p *TChargeBillDel) SetTable(database string) { p.table = fmt.Sprintf("%s.%s", database, "t_charge_bill_del") } func (p *TChargeBillDel) CreateTable(db *gorm.DB) error { sql :="CREATE TABLE IF NOT EXISTS "+p.TableName()+"("+ "`id` bigint(11) NOT NULL AUTO_INCREMENT,"+ "`obj_type` tinyint(1) NOT NULL COMMENT '1 房屋 2 车位 3 车辆',"+ "`obj_id` bigint(11) NOT NULL COMMENT '房屋或车位或车辆',"+ "`charge_id` int(10) NOT NULL COMMENT '费用项目id',"+ "`charge_bind_id` bigint(11) NOT NULL COMMENT '费用绑定关系id',"+ "`charge_time_type` tinyint(1) NOT NULL COMMENT '1 周期 2 一次性',"+ "`charge_desc` varchar(255) COLLATE utf8mb4_bin NOT NULL COMMENT '费用描述',"+ "`comment` varchar(512) COLLATE utf8mb4_bin NOT NULL COMMENT '备注',"+ "`amount` int(10) NOT NULL COMMENT '应收金额(不包含滞纳金)',"+ "`charge_power_id` bigint(11) NOT NULL COMMENT '抄表记录id',"+ "`charge_type` int(10) NOT NULL COMMENT '费用类型',"+ "`pay_type` tinyint(1) NOT NULL COMMENT '1 现金 2 微信 3 支付宝 4 pos 5 转账 6 小程序微信支付 7 小程序支付报支付 8 app 微信支付 9 app 支付宝支付',"+ "`charge_name` varchar(128) COLLATE utf8mb4_bin NOT NULL COMMENT '费用名',"+ "`uid` bigint(11) NOT NULL COMMENT 'app或小程序用户id,线下收款该字段0',"+ "`pay_mode` tinyint(1) NOT NULL COMMENT '1 欠费缴费 2 预存',"+ "`package_id` int(11) NOT NULL COMMENT '套餐id app或小程序购买套餐时该字段不为0',"+ "`charge_end` bigint(11) NOT NULL COMMENT '账单结束时间戳',"+ "`charge_start` bigint(11) NOT NULL COMMENT '账单开始时间戳',"+ "`order_id` varchar(64) COLLATE utf8mb4_bin NOT NULL COMMENT '订单号',"+ "`pay_time` bigint(11) NOT NULL COMMENT '支付时间',"+ "`house_id` bigint(11) NOT NULL COMMENT '房屋',"+ "`obj_name` varchar(16) COLLATE utf8mb4_bin NOT NULL COMMENT '房屋号/车位/车牌',"+ "`late_fee` int(10) NOT NULL COMMENT '滞纳金',"+ "`created_at` datetime NOT NULL,"+ " `updated_at` datetime NOT NULL,"+ "`status` tinyint(1) NOT NULL COMMENT '1 待支付 2 已支付',"+ "`del_at` datetime NOT NULL COMMENT '作废时间',"+ "`del_reason` text COLLATE utf8mb4_bin NOT NULL COMMENT '作废原因',"+ "PRIMARY KEY (`id`) USING BTREE,"+ " UNIQUE KEY `charge_bind_id` (`charge_bind_id`,`charge_end`) USING BTREE,"+ " KEY `house_id` (`obj_id`) USING BTREE,"+ " KEY `charge_id` (`charge_id`) USING BTREE,"+ " KEY `uid` (`uid`) USING BTREE,"+ " KEY `order` (`order_id`) USING BTREE,"+ "KEY `charge_name` (`charge_name`) USING BTREE,"+ "KEY `obj_name` (`obj_name`) USING BTREE"+ ") ENGINE=InnoDB 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 *TChargeBillDel) 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 *TChargeBillDel) 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 *TChargeBillDel) 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 *TChargeBillDel) 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 *TChargeBillDel) 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 *TChargeBillDel) 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 *TChargeBillDel) 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 *TChargeBillDel) 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 *TChargeBillDel) List(db *gorm.DB, where map[string]interface{}, or map[string]interface{}, page int, pageSize int) (list []TChargeBillDel, 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 } func (p *TChargeBillDel) SelectGroup(db *gorm.DB, where map[string]interface{}, or map[string]interface{}, selectStr string, group string, page int, pageSize int, result interface{}) (err error) { cond, val, err := whereBuildAndOr(where, or) if err != nil { return err } if pageSize < 0 { result := db.Table(p.TableName()).Select(selectStr).Where(cond, val...).Group(group).Find(result) if result.Error != nil { wherefields, _ := util.MarshalToString(where) logger.Error("mysql", zap.String("sql", "select "+selectStr+" from "+p.TableName()), zap.String("where", wherefields), zap.String("error", result.Error.Error())) } return result.Error } offset := (page - 1) * pageSize r := db.Table(p.TableName()).Select(selectStr).Where(cond, val...).Group(group).Limit(pageSize).Offset(offset).Find(result) if r.Error != nil { wherefields, _ := util.MarshalToString(where) logger.Error("mysql", zap.String("sql", "select * from "+p.TableName()), zap.String("where", wherefields), zap.String("error", r.Error.Error())) } return r.Error } func (p *TChargeBillDel) CountGroup(db *gorm.DB, where map[string]interface{}, or map[string]interface{}, group string) (int64, error) { cond, val, err := whereBuildAndOr(where, or) if err != nil { return 0, err } sql := fmt.Sprintf("select count(*) as count from (select count(*) from %s group by %s) t1", p.TableName(), group) if len(cond) > 0 { sql = fmt.Sprintf("select count(*) as count from (select count(*) from %s where %s group by %s) t1", p.TableName(), cond, group) } type Result struct { Count int64 } array := []Result{} err = db.Raw(sql, val...).Scan(&array).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 0, err } if len(array) == 0 { return 0, nil } return array[0].Count, err } func (p *TChargeBillDel) Select(db *gorm.DB, where map[string]interface{}, or map[string]interface{}, selectStr string, page int, pageSize int, result interface{}) (err error) { cond, val, err := whereBuildAndOr(where, or) if err != nil { return err } if pageSize < 0 { result := db.Table(p.TableName()).Select(selectStr).Where(cond, val...).Find(result) if result.Error != nil { wherefields, _ := util.MarshalToString(where) logger.Error("mysql", zap.String("sql", "select "+selectStr+" from "+p.TableName()), zap.String("where", wherefields), zap.String("error", result.Error.Error())) } return result.Error } offset := (page - 1) * pageSize r := db.Table(p.TableName()).Select(selectStr).Where(cond, val...).Limit(pageSize).Offset(offset).Find(result) if r.Error != nil { wherefields, _ := util.MarshalToString(where) logger.Error("mysql", zap.String("sql", "select * from "+p.TableName()), zap.String("where", wherefields), zap.String("error", r.Error.Error())) } return r.Error }