123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367 |
- // 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 TChargeBill 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"`
- table string
- }
- type ChargeDesc struct {
- FixAmount int64 `json:"fix_amount"`
- FixAmountName string `json:"fix_amount_name"`
- UnitPrice int64 `json:"unit_price"`
- ObjArea float64 `json:"obj_area"`
- ChargeBasis int32 `json:"charge_basis"`
- PowerLastUsed float64 `json:"power_last_used"`
- PowerCurrentUsed float64 `json:"power_current_used"`
- CustomFee int64 `json:"custom_fee"`
- }
- func (p *TChargeBill) TableName() string {
- return p.table
- }
- func NewChargeBill(database string) *TChargeBill {
- return &TChargeBill{table: fmt.Sprintf("%s.%s", database, "t_charge_bill")}
- }
- func (p *TChargeBill) SetTable(database string) {
- p.table = fmt.Sprintf("%s.%s", database, "t_charge_bill")
- }
- func NewChargeBillPayed(database string) *TChargeBill {
- return &TChargeBill{table: fmt.Sprintf("%s.%s", database, "t_charge_bill_payed")}
- }
- func (p *TChargeBill) SetTablePayed(database string) {
- p.table = fmt.Sprintf("%s.%s", database, "t_charge_bill_payed")
- }
- func (p *TChargeBill) 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 '房屋id'," +
- "`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 已支付'," +
- "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_id` (`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 *TChargeBill) Find(db *gorm.DB, where map[string]interface{}) error {
- cond, val, err := whereBuild(where)
- 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
- }
- 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 *TChargeBill) 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 *TChargeBill) 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 *TChargeBill) 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 *TChargeBill) 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 *TChargeBill) 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 *TChargeBill) 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 *TChargeBill) 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 *TChargeBill) List(db *gorm.DB, where map[string]interface{}, or map[string]interface{}, page int, pageSize int, order string) (list []TChargeBill, err error) {
- cond, val, err := whereBuildAndOr(where, or)
- if order == "" {
- order = "updated_at desc"
- }
- if err != nil {
- return list, err
- }
- if pageSize < 0 {
- result := db.Table(p.TableName()).Where(cond, val...).Order(order).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(order).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 *TChargeBill) 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 *TChargeBill) 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 *TChargeBill) 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
- }
|