Order.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. package Function
  2. import (
  3. "Cold_Api/conf"
  4. "Cold_Api/controllers/lib"
  5. "fmt"
  6. "github.com/beego/beego/v2/adapter/orm"
  7. orm2 "github.com/beego/beego/v2/client/orm"
  8. _ "github.com/go-sql-driver/mysql"
  9. "time"
  10. )
  11. // 模板
  12. type GoodsOrder struct {
  13. Id int `orm:"column(ID);size(11);auto;pk"`
  14. T_id string `orm:"size(50);null"` // 订单唯一ID
  15. T_uuid string `orm:"size(50);null"` // 用户
  16. T_orderid string `orm:"size(256);null"` // 订单号
  17. T_outorderid string `orm:"size(256);null"` // 出库订单号
  18. T_sn string `orm:"size(256);null"` // 出库订单号
  19. T_receiving string `orm:"size(256);null"` // 收货单位
  20. T_start_Ut time.Time `orm:"type(timestamp);null;"` // 起运时间
  21. T_start_Ut_T float32 `orm:"size(10);0;"` // 起运温度
  22. T_end_Ut time.Time `orm:"type(timestamp);null;"` // 到达时间
  23. T_end_Ut_T float32 `orm:"size(10);0;"` // 到达时间
  24. T_text string `orm:"type(text);null"` // 详情
  25. T_State int `orm:"size(2);1"` // 0 删除 1 正常
  26. CreateTime time.Time `orm:"column(create_time);type(timestamp);null;auto_now_add"` //auto_now_add 第一次保存时才设置时间
  27. UpdateTime time.Time `orm:"column(update_time);type(timestamp);null;auto_now"` //auto_now 每次 model 保存时都会对时间自动更新
  28. }
  29. func (t *GoodsOrder) TableName() string {
  30. return "GoodsOrder" // 数据库名称 // ************** 替换 FormulaList **************
  31. }
  32. func init() {
  33. //注册模型
  34. orm.RegisterModel(new(GoodsOrder))
  35. }
  36. // ---------------- 特殊方法 -------------------
  37. // 获取 ById
  38. func Read_GoodsOrder_ByT_id(T_id string) (r GoodsOrder) {
  39. o := orm.NewOrm()
  40. r = GoodsOrder{T_id: T_id}
  41. err := o.Read(&r, "T_id") // o.Read(&r,"Tokey") 如果不是 主键 就得指定字段名
  42. if err != nil {
  43. fmt.Println(err)
  44. }
  45. return r
  46. }
  47. // 添加
  48. func Add_GoodsOrder(m GoodsOrder) (id int64, err error) {
  49. o := orm.NewOrm()
  50. T_id := ""
  51. var rand_x int64
  52. for true {
  53. var rx GoodsOrder
  54. rx.T_id = lib.GetRandstring(8, "", rand_x)
  55. err = o.Read(&rx, "T_id") // o.Read(&r,"Tokey") 如果不是 主键 就得指定字段名
  56. if err == nil {
  57. T_id = rx.T_id
  58. break
  59. }
  60. rand_x++
  61. }
  62. m.T_id = T_id
  63. id, err = o.Insert(&m)
  64. if err != nil {
  65. fmt.Println(err)
  66. }
  67. return id, err
  68. }
  69. // 修改
  70. func Update_TGoodsOrder(r GoodsOrder, cols ...string) bool {
  71. o := orm.NewOrm()
  72. if num, err := o.Update(&r, cols...); err == nil {
  73. fmt.Println("Number of records updated in database:", num)
  74. return true
  75. }
  76. return false
  77. }
  78. // 删除
  79. func Delete_GoodsOrder_ById(r GoodsOrder) (err error) {
  80. r.T_State = 0
  81. Update_TGoodsOrder(r, "T_State")
  82. return
  83. }
  84. // 删除
  85. func Delete_GoodsOrder(m GoodsOrder) (err error) {
  86. o := orm.NewOrm()
  87. if num, err := o.Delete(&m); err == nil {
  88. fmt.Println("Number of records deleted in database:", num)
  89. }
  90. return
  91. }
  92. // 获取列表
  93. func Read_GoodsOrder_List(page int, page_z int, Name string) (r []GoodsOrder, cnt int64) {
  94. o := orm.NewOrm()
  95. // 也可以直接使用 Model 结构体作为表名
  96. qs := o.QueryTable(new(GoodsOrder))
  97. var offset int64
  98. if page_z == 0 {
  99. page_z = conf.Page_size
  100. }
  101. if page <= 1 {
  102. offset = 0
  103. } else {
  104. offset = int64((page - 1) * page_z)
  105. }
  106. cond := orm.NewCondition()
  107. cond1 := cond.And("T_State", 1)
  108. if len(Name) > 0 {
  109. cond1 = cond.AndCond(cond1).AndCond(cond.Or("T_sn__icontains", Name).Or("T_orderid__icontains", Name).Or("T_outorderid__icontains", Name))
  110. }
  111. qs.Limit(page_z, offset).SetCond((*orm2.Condition)(cond1)).OrderBy("Id").All(&r)
  112. cnt, _ = qs.SetCond((*orm2.Condition)(cond1)).Count()
  113. return r, cnt
  114. }