UserBill.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. package Account
  2. import (
  3. "FollowUp_Notice/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. // 获取用户充值金额
  58. func Read_UserBill_Pay(T_uid int) (r UserBill, err error) {
  59. o := orm.NewOrm()
  60. qs := o.QueryTable(new(UserBill))
  61. cond := orm.NewCondition()
  62. cond = cond.And("T_uid", T_uid).And("T_type", "充值")
  63. var maps []UserBill
  64. _, err = qs.Limit(1, 0).SetCond((*orm2.Condition)(cond)).OrderBy("-Id").All(&maps)
  65. if err != nil {
  66. logs.Error(lib.FuncName(), err)
  67. return r, err
  68. }
  69. if len(maps) == 0 {
  70. return r, orm.ErrNoRows
  71. }
  72. return maps[0], nil
  73. }
  74. func Read_UserBill_List(T_uid int, T_month string, T_type, page, page_z int) (r []UserBill_R, cnt int64) {
  75. o := orm.NewOrm()
  76. qs := o.QueryTable(new(UserBill))
  77. var offset int64
  78. if page <= 1 {
  79. offset = 0
  80. } else {
  81. offset = int64((page - 1) * page_z)
  82. }
  83. cond := orm.NewCondition()
  84. cond = cond.And("T_uid", T_uid)
  85. if len(T_month) > 0 {
  86. cond = cond.And("CreateTime__startswith", T_month)
  87. }
  88. if T_type == 1 {
  89. cond = cond.And("T_type", Pay)
  90. }
  91. if T_type == 2 {
  92. cond = cond.And("T_type", FeeDeduction)
  93. }
  94. var maps []UserBill
  95. var err error
  96. if page_z == 9999 {
  97. _, err = qs.SetCond((*orm2.Condition)(cond)).OrderBy("-Id").All(&maps)
  98. } else {
  99. _, err = qs.Limit(page_z, offset).SetCond((*orm2.Condition)(cond)).OrderBy("-Id").All(&maps)
  100. }
  101. if err != nil {
  102. logs.Error(lib.FuncName(), err)
  103. return r, cnt
  104. }
  105. cnt, err = qs.SetCond((*orm2.Condition)(cond)).Count()
  106. if err != nil {
  107. logs.Error(lib.FuncName(), err)
  108. return
  109. }
  110. for _, v := range maps {
  111. r = append(r, UserBillToUserBill_R(v))
  112. }
  113. return r, cnt
  114. }