UserBill.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. package Account
  2. import (
  3. "git.baozhida.cn/ERP_libs/lib"
  4. "github.com/astaxie/beego/logs"
  5. "github.com/beego/beego/v2/adapter/orm"
  6. orm2 "github.com/beego/beego/v2/client/orm"
  7. "time"
  8. )
  9. const (
  10. FeeDeduction = "扣费"
  11. Pay = "充值"
  12. )
  13. type UserBill struct {
  14. Id int `orm:"column(ID);size(11);auto;pk"`
  15. T_uid int `orm:"index;size(256);"` // User.Id
  16. T_type string `orm:"size(256);"` // 扣费/充值
  17. T_bill string `orm:"type(text);"` // 说明
  18. T_charging float32 `orm:"digits(12);decimals(2)"` // 金额
  19. T_balance float32 `orm:"digits(12);decimals(2)"` // 余额(扣费后)
  20. CreateTime time.Time `orm:"column(create_time);type(timestamp);null;auto_now_add"` //auto_now_add 第一次保存时才设置时间
  21. }
  22. type UserBill_R struct {
  23. Id int
  24. T_uid int // User.Id
  25. T_type string // 扣费/充值
  26. T_bill string // 说明
  27. T_charging float32 // 金额
  28. T_balance float32 // 余额(扣费后)
  29. CreateTime string // 第一次保存时才设置时间
  30. }
  31. func (t *UserBill) TableName() string {
  32. return "user_bill" // 数据库名称 // ************** 替换 DesignDeviceNotice **************
  33. }
  34. func init() {
  35. //注册模型
  36. orm.RegisterModel(new(UserBill))
  37. }
  38. func UserBillToUserBill_R(r UserBill) (m UserBill_R) {
  39. m.Id = r.Id
  40. m.T_uid = r.T_uid
  41. m.T_type = r.T_type
  42. m.T_bill = r.T_bill
  43. m.T_charging = r.T_charging
  44. m.T_balance = r.T_balance
  45. m.CreateTime = r.CreateTime.Format("2006-01-02 15:04:05")
  46. return
  47. }
  48. func Add_UserBill(r UserBill) (id int64, err error) {
  49. o := orm.NewOrm()
  50. id, err = o.Insert(&r)
  51. if err != nil {
  52. logs.Error(lib.FuncName(), err)
  53. return
  54. }
  55. return
  56. }
  57. func Read_UserBill_newest(T_uuid string) (r UserBill, err error) {
  58. o := orm.NewOrm()
  59. qs := o.QueryTable(new(UserBill))
  60. cond := orm.NewCondition()
  61. cond = cond.And("T_uuid", T_uuid)
  62. var maps []UserBill
  63. _, err = qs.Limit(1, 0).SetCond((*orm2.Condition)(cond)).OrderBy("-Id").All(&maps)
  64. if err != nil {
  65. logs.Error(lib.FuncName(), err)
  66. return r, err
  67. }
  68. if len(maps) == 0 {
  69. return
  70. }
  71. return maps[0], nil
  72. }
  73. func Read_UserBill_List(T_uid int, T_month string, T_type, page, page_z int) (r []UserBill_R, cnt int64) {
  74. o := orm.NewOrm()
  75. qs := o.QueryTable(new(UserBill))
  76. var offset int64
  77. if page <= 1 {
  78. offset = 0
  79. } else {
  80. offset = int64((page - 1) * page_z)
  81. }
  82. cond := orm.NewCondition()
  83. cond = cond.And("T_uid", T_uid)
  84. if len(T_month) > 0 {
  85. cond = cond.And("CreateTime__startswith", T_month)
  86. }
  87. if T_type == 1 {
  88. cond = cond.And("T_type", Pay)
  89. }
  90. if T_type == 2 {
  91. cond = cond.And("T_type", FeeDeduction)
  92. }
  93. var maps []UserBill
  94. var err error
  95. if page_z == 9999 {
  96. _, err = qs.SetCond((*orm2.Condition)(cond)).OrderBy("-Id").All(&maps)
  97. } else {
  98. _, err = qs.Limit(page_z, offset).SetCond((*orm2.Condition)(cond)).OrderBy("-Id").All(&maps)
  99. }
  100. if err != nil {
  101. logs.Error(lib.FuncName(), err)
  102. return r, cnt
  103. }
  104. cnt, err = qs.SetCond((*orm2.Condition)(cond)).Count()
  105. if err != nil {
  106. logs.Error(lib.FuncName(), err)
  107. return
  108. }
  109. for _, v := range maps {
  110. r = append(r, UserBillToUserBill_R(v))
  111. }
  112. return r, cnt
  113. }