CompanyBill.go 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. package Company
  2. import (
  3. "Cold_Api/controllers/lib"
  4. "github.com/beego/beego/v2/adapter/orm"
  5. orm2 "github.com/beego/beego/v2/client/orm"
  6. "github.com/beego/beego/v2/core/logs"
  7. "time"
  8. )
  9. type CompanyBill struct {
  10. Id int `orm:"column(ID);size(11);auto;pk"`
  11. T_pid int `orm:"index;size(256);"` // Account.Company 绑定公司
  12. T_type string `orm:"size(256);"` // 扣费/充值
  13. T_project string `orm:"size(256);"` // 项目
  14. T_bill string `orm:"type(text);"` // 说明
  15. T_charging float32 `orm:"digits(12);decimals(2)"` // 金额
  16. T_balance float32 `orm:"digits(12);decimals(2)"` // 余额(扣费后)
  17. CreateTime time.Time `orm:"column(create_time);type(timestamp);null;auto_now_add"` //auto_now_add 第一次保存时才设置时间
  18. }
  19. func (t *CompanyBill) TableName() string {
  20. return "company_bill" // 数据库名称 // ************** 替换 DesignDeviceNotice **************
  21. }
  22. func init() {
  23. //注册模型
  24. orm.RegisterModel(new(CompanyBill))
  25. }
  26. func Add_CompanyBill(r CompanyBill) (id int64, err error) {
  27. o := orm.NewOrm()
  28. id, err = o.Insert(&r)
  29. if err != nil {
  30. logs.Error(lib.FuncName(), err)
  31. return
  32. }
  33. return
  34. }
  35. func Read_CompanyBill_newest(T_pid int) (r CompanyBill, err error) {
  36. o := orm.NewOrm()
  37. qs := o.QueryTable(new(CompanyBill))
  38. cond := orm.NewCondition()
  39. cond = cond.And("T_pid", T_pid)
  40. var maps []CompanyBill
  41. _, err = qs.Limit(1, 0).SetCond((*orm2.Condition)(cond)).OrderBy("-Id").All(&maps)
  42. if err != nil {
  43. logs.Error(lib.FuncName(), err)
  44. return r, err
  45. }
  46. if len(maps) == 0 {
  47. return
  48. }
  49. return maps[0], nil
  50. }
  51. func Read_CompanyBill_List(T_pid int, T_month string) (r []CompanyBill, err error) {
  52. o := orm.NewOrm()
  53. qs := o.QueryTable(new(CompanyBill))
  54. cond := orm.NewCondition()
  55. cond = cond.And("T_pid", T_pid).And("CreateTime__startswith", T_month)
  56. var maps []CompanyBill
  57. _, err = qs.SetCond((*orm2.Condition)(cond)).OrderBy("-Id").All(&maps)
  58. if err != nil {
  59. logs.Error(lib.FuncName(), err)
  60. return r, err
  61. }
  62. if len(maps) == 0 {
  63. return
  64. }
  65. return maps, nil
  66. }