123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205 |
- // 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 TChargePower struct {
- ID int64 `gorm:"column:id;PRIMARY_KEY" json:"id"`
- HouseId int64 `gorm:"column:house_id" json:"house_id"`
- LastAmount float64 `gorm:"column:last_amount" json:"last_amount"`
- CurrentAmount float64 `gorm:"column:current_amount" json:"current_amount"`
- Start int64 `gorm:"column:start" json:"start"`
- End int64 `gorm:"column:end" json:"end"`
- CreatedAt time.Time `gorm:"column:created_at" json:"created_at"`
- UpdatedAt time.Time `gorm:"column:updated_at" json:"updated_at"`
- Comment string `gorm:"column:comment" json:"comment"`
- ChargeType int32 `gorm:"column:charge_type" json:"charge_type"`
- BillId int64 `gorm:"column:bill_id" json:"bill_id"`
- table string
- }
- func (p *TChargePower) TableName() string {
- return p.table
- }
- func NewChargePower(database string) *TChargePower {
- return &TChargePower{table: fmt.Sprintf("%s.%s", database, "t_charge_power")}
- }
- func (p *TChargePower) SetTable(database string) {
- p.table = fmt.Sprintf("%s.%s", database, "t_charge_power")
- }
- func (p *TChargePower) CreateTable(db *gorm.DB) error {
- sql := "CREATE TABLE IF NOT EXISTS " + p.TableName() + "(" +
- " `id` bigint(11) NOT NULL AUTO_INCREMENT," +
- " `house_id` bigint(11) NOT NULL COMMENT '房屋id'," +
- "`last_amount` decimal(18,6) NOT NULL COMMENT '上期使用量'," +
- "`current_amount` decimal(18,6) NOT NULL COMMENT '本期使用量'," +
- "`start` bigint(11) NOT NULL COMMENT '开始时间'," +
- "`end` bigint(11) NOT NULL COMMENT '结束时间'," +
- "`created_at` datetime NOT NULL," +
- " `updated_at` datetime NOT NULL," +
- " `comment` varchar(255) COLLATE utf8mb4_bin NOT NULL COMMENT '备注'," +
- "`charge_type` tinyint(1) NOT NULL COMMENT ' 2 水费 3 电费 4 气费'," +
- "`bill_id` bigint(11) NOT NULL COMMENT '账单id'," +
- "PRIMARY KEY (`id`) USING BTREE," +
- " UNIQUE KEY `house_id` (`house_id`,`charge_type`,`start`,`end`) USING BTREE," +
- " KEY `bill_id` (`bill_id`) USING BTREE" +
- ") ENGINE=InnoDB AUTO_INCREMENT=6 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 *TChargePower) 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 *TChargePower) 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 *TChargePower) 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 *TChargePower) 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 *TChargePower) 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 *TChargePower) 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 *TChargePower) 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 *TChargePower) 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 *TChargePower) List(db *gorm.DB, where map[string]interface{}, or map[string]interface{}, page int, pageSize int) (list []TChargePower, 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...).Order("created_at desc").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
- }
|