123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125 |
- package Account
- import (
- "git.baozhida.cn/ERP_libs/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_newest(T_uuid string) (r UserBill, err error) {
- o := orm.NewOrm()
- qs := o.QueryTable(new(UserBill))
- cond := orm.NewCondition()
- cond = cond.And("T_uuid", T_uuid)
- 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
- }
- 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
- }
|