package Account import ( "FollowUp_Notice/lib" "github.com/astaxie/beego/logs" "github.com/beego/beego/v2/adapter/orm" orm2 "github.com/beego/beego/v2/client/orm" "time" ) const ( FeeDeduction = "扣费" Pay = "充值" ) type UserBill struct { Id int `orm:"column(ID);size(11);auto;pk"` T_uid int `orm:"index;size(256);"` // User.Id T_type string `orm:"size(256);"` // 扣费/充值 T_bill string `orm:"type(text);"` // 说明 T_charging float32 `orm:"digits(12);decimals(2)"` // 金额 T_balance float32 `orm:"digits(12);decimals(2)"` // 余额(扣费后) CreateTime time.Time `orm:"column(create_time);type(timestamp);null;auto_now_add"` //auto_now_add 第一次保存时才设置时间 } type UserBill_R struct { Id int T_uid int // User.Id T_type string // 扣费/充值 T_bill string // 说明 T_charging float32 // 金额 T_balance float32 // 余额(扣费后) CreateTime string // 第一次保存时才设置时间 } func (t *UserBill) TableName() string { return "user_bill" // 数据库名称 // ************** 替换 DesignDeviceNotice ************** } func init() { //注册模型 orm.RegisterModel(new(UserBill)) } func UserBillToUserBill_R(r UserBill) (m UserBill_R) { m.Id = r.Id m.T_uid = r.T_uid m.T_type = r.T_type m.T_bill = r.T_bill m.T_charging = r.T_charging m.T_balance = r.T_balance m.CreateTime = r.CreateTime.Format("2006-01-02 15:04:05") return } func Add_UserBill(r UserBill) (id int64, err error) { o := orm.NewOrm() id, err = o.Insert(&r) if err != nil { logs.Error(lib.FuncName(), err) return } return } // 获取用户充值金额 func Read_UserBill_Pay(T_uid int) (r UserBill, err error) { o := orm.NewOrm() qs := o.QueryTable(new(UserBill)) cond := orm.NewCondition() cond = cond.And("T_uid", T_uid).And("T_type", "充值") var maps []UserBill _, err = qs.Limit(1, 0).SetCond((*orm2.Condition)(cond)).OrderBy("-Id").All(&maps) if err != nil { logs.Error(lib.FuncName(), err) return r, err } if len(maps) == 0 { return r, orm.ErrNoRows } return maps[0], nil } func Read_UserBill_List(T_uid int, T_month string, T_type, page, page_z int) (r []UserBill_R, cnt int64) { o := orm.NewOrm() qs := o.QueryTable(new(UserBill)) var offset int64 if page <= 1 { offset = 0 } else { offset = int64((page - 1) * page_z) } cond := orm.NewCondition() cond = cond.And("T_uid", T_uid) if len(T_month) > 0 { cond = cond.And("CreateTime__startswith", T_month) } if T_type == 1 { cond = cond.And("T_type", Pay) } if T_type == 2 { cond = cond.And("T_type", FeeDeduction) } var maps []UserBill var err error if page_z == 9999 { _, err = qs.SetCond((*orm2.Condition)(cond)).OrderBy("-Id").All(&maps) } else { _, err = qs.Limit(page_z, offset).SetCond((*orm2.Condition)(cond)).OrderBy("-Id").All(&maps) } if err != nil { logs.Error(lib.FuncName(), err) return r, cnt } cnt, err = qs.SetCond((*orm2.Condition)(cond)).Count() if err != nil { logs.Error(lib.FuncName(), err) return } for _, v := range maps { r = append(r, UserBillToUserBill_R(v)) } return r, cnt }