// 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 TChargeOrder struct { OrderId string `gorm:"column:order_id;PRIMARY_KEY" json:"order_id"` Comment string `gorm:"column:comment" json:"comment"` Amount int64 `gorm:"column:amount" json:"amount"` PayAmount int64 `gorm:"column:pay_amount" json:"pay_amount"` PayType int32 `gorm:"column:pay_type" json:"pay_type"` CreatedAt time.Time `gorm:"column:created_at" json:"created_at"` Uid int64 `gorm:"column:uid" json:"uid"` PackageId int64 `gorm:"column:package_id" json:"package_id"` PrepayId string `gorm:"column:prepay_id" json:"prepay_id"` TransactionId string `gorm:"column:transaction_id" json:"transaction_id"` Status int64 `gorm:"column:status" json:"status"` PayingDesc string `gorm:"column:paying_desc" json:"paying_desc"` PayTime int64 `gorm:"column:pay_time" json:"pay_time"` BillIds string `gorm:"column:bill_ids" json:"bill_ids"` ObjName string `gorm:"column:obj_name" json:"obj_name"` OnlinePay int32 `gorm:"column:online_pay" json:"online_pay"` OnlinePayBill string `gorm:"column:online_pay_bill" json:"online_pay_bill"` Deleted int32 `gorm:"column:deleted" json:"deleted"` table string } func (p *TChargeOrder) TableName() string { return p.table } func NewChargeOrder(database string) *TChargeOrder { return &TChargeOrder{table: fmt.Sprintf("%s.%s", database, "t_charge_order")} } func (p *TChargeOrder) SetTable(database string) { p.table = fmt.Sprintf("%s.%s", database, "t_charge_order") } func (p *TChargeOrder) CreateTable(db *gorm.DB) error { sql :="CREATE TABLE IF NOT EXISTS "+p.TableName()+"("+ " `order_id` varchar(64) COLLATE utf8mb4_bin NOT NULL COMMENT '订单号',"+ "`comment` varchar(512) COLLATE utf8mb4_bin NOT NULL COMMENT '备注',"+ "`amount` int(10) NOT NULL COMMENT '总应收金额',"+ "`pay_amount` int(10) NOT NULL COMMENT '实收金额',"+ "`pay_type` tinyint(1) NOT NULL COMMENT '1 现金 2 微信 3 支付宝 4 pos 5 转账 6 小程序微信支付 7 小程序支付报支付 8 app 微信支付 9 app 支付宝支付',"+ "`created_at` datetime NOT NULL,"+ " `uid` bigint(11) NOT NULL COMMENT 'app或小程序用户id,线下收款该字段0',"+ "`package_id` int(11) NOT NULL COMMENT '套餐id, app或小程序购买套餐时该字段不为0',"+ "`prepay_id` varchar(255) COLLATE utf8mb4_bin NOT NULL COMMENT '线上支付三方预支付(如微信)标识',"+ "`transaction_id` varchar(255) COLLATE utf8mb4_bin NOT NULL COMMENT '线上支付三方交易标识(如微信)',"+ "`status` tinyint(1) NOT NULL COMMENT '1 待支付 2 已支付',"+ "`paying_desc` text COLLATE utf8mb4_bin NOT NULL COMMENT '订单描述',"+ "`pay_time` bigint(11) NOT NULL COMMENT '支付时间',"+ "`bill_ids` text COLLATE utf8mb4_bin NOT NULL COMMENT '账单列表',"+ "`online_pay` tinyint(1) NOT NULL DEFAULT '0' COMMENT '0 非线上预存 1 线上预存',"+ "`online_pay_bill` text COLLATE utf8mb4_bin NOT NULL COMMENT '线上预存费用信息',"+ "`obj_name` varchar(16) COLLATE utf8mb4_bin NOT NULL COMMENT '房屋号/车位/车牌',"+ "`deleted` tinyint(4) NOT NULL COMMENT '1 已删除',"+ "PRIMARY KEY (`order_id`) USING BTREE,"+ " KEY `uid` (`uid`) USING BTREE"+ ") ENGINE=InnoDB 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 *TChargeOrder) 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 *TChargeOrder) 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 *TChargeOrder) 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 *TChargeOrder) 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 *TChargeOrder) 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 *TChargeOrder) 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 *TChargeOrder) 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 *TChargeOrder) 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 *TChargeOrder) List(db *gorm.DB, where map[string]interface{}, or map[string]interface{}, page int, pageSize int) (list []TChargeOrder, 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 }