GoodsOrder.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. package Function
  2. import (
  3. "Cold_GoodsOrder/Nats/NatsServer"
  4. "Cold_GoodsOrder/conf"
  5. "encoding/json"
  6. "fmt"
  7. "github.com/astaxie/beego/cache"
  8. "github.com/beego/beego/v2/adapter/orm"
  9. orm2 "github.com/beego/beego/v2/client/orm"
  10. _ "github.com/go-sql-driver/mysql"
  11. "strconv"
  12. "time"
  13. )
  14. // 模板
  15. type GoodsOrder struct {
  16. Id int `orm:"column(ID);size(11);auto;pk"`
  17. //T_id string `orm:"size(50);null"` // 订单唯一ID
  18. T_pid int `orm:"index;size(256);null"` // Account.Company 绑定公司
  19. T_orderid string `orm:"size(256);null"` // 订单号
  20. T_outorderid string `orm:"size(256);null"` // 出库订单号
  21. T_sn string `orm:"size(256);null"` // 设备sn
  22. //T_snid int `orm:"size(256);0"` // 设备id
  23. T_receiving string `orm:"size(256);null"` // 收货单位
  24. T_start_Ut time.Time `orm:"type(timestamp);null;"` // 起运时间
  25. T_start_Ut_T float32 `orm:"size(10);default(0)"` // 起运温度
  26. T_end_Ut time.Time `orm:"type(timestamp);null;"` // 到达时间
  27. T_end_Ut_T float32 `orm:"size(10);default(0);"` // 到达温度
  28. T_text string `orm:"type(text);null"` // 详情
  29. T_State int `orm:"size(2);default(1)"` // 0 删除 1 正常
  30. CreateTime time.Time `orm:"column(create_time);type(timestamp);null;auto_now_add"` //auto_now_add 第一次保存时才设置时间
  31. UpdateTime time.Time `orm:"column(update_time);type(timestamp);null;auto_now"` //auto_now 每次 model 保存时都会对时间自动更新
  32. }
  33. type GoodsOrderR struct {
  34. Id int // ID
  35. T_orderid string // 订单号
  36. T_outorderid string // 出库订单号
  37. T_name string // 设备名称
  38. T_sn string // 设备sn
  39. T_receiving string // 收货单位
  40. T_start_Ut string // 起运时间
  41. T_start_Ut_T float32 // 起运温度
  42. T_end_Ut string // 到达时间
  43. T_end_Ut_T float32 // 到达时间
  44. T_text string // 详情
  45. }
  46. func GoodsOrderToGoodsOrderR(t GoodsOrder) (r GoodsOrderR) {
  47. r.Id = t.Id
  48. r.T_orderid = t.T_orderid
  49. r.T_outorderid = t.T_outorderid
  50. Device_r, err := NatsServer.ReadDeviceByT_sn(t.T_sn)
  51. r.T_name = t.T_sn
  52. if err == nil {
  53. r.T_name = Device_r.T_devName
  54. }
  55. r.T_sn = t.T_sn
  56. r.T_receiving = t.T_receiving
  57. r.T_start_Ut = t.T_start_Ut.Format("2006-01-02 15:04:05")
  58. r.T_start_Ut_T = t.T_start_Ut_T
  59. r.T_end_Ut = t.T_end_Ut.Format("2006-01-02 15:04:05")
  60. r.T_end_Ut_T = t.T_end_Ut_T
  61. r.T_text = t.T_text
  62. return
  63. }
  64. func (t *GoodsOrder) TableName() string {
  65. return "goods_order" // 数据库名称 // ************** 替换 FormulaList **************
  66. }
  67. var redisCache_GoodsOrder cache.Cache
  68. func init() {
  69. //注册模型
  70. orm.RegisterModel(new(GoodsOrder))
  71. config := fmt.Sprintf(`{"key":"%s","conn":"%s","dbNum":"%s","password":"%s"}`,
  72. "redis_GoodsOrder", conf.Redis_address, conf.Redis_dbNum, conf.Redis_password)
  73. fmt.Println(config)
  74. var err error
  75. redisCache_GoodsOrder, err = cache.NewCache("redis", config)
  76. if err != nil || redisCache_GoodsOrder == nil {
  77. errMsg := "failed to init redis"
  78. fmt.Println(errMsg, err)
  79. }
  80. }
  81. // ---------------- Redis -------------------
  82. func Redis_GoodsOrder_Set(r GoodsOrder) (err error) {
  83. //json序列化
  84. str, err := json.Marshal(r)
  85. if err != nil {
  86. fmt.Print(err)
  87. return
  88. }
  89. err = redisCache_GoodsOrder.Put(strconv.Itoa(r.Id), str, 24*time.Hour)
  90. if err != nil {
  91. fmt.Println("set key:", strconv.Itoa(r.Id), ",value:", str, err)
  92. }
  93. return
  94. }
  95. func Redis_GoodsOrder_Get(key string) (r GoodsOrder, is bool) {
  96. if redisCache_GoodsOrder.IsExist(key) {
  97. //println("找到key:",key)
  98. v := redisCache_GoodsOrder.Get(key)
  99. json.Unmarshal(v.([]byte), &r)
  100. return r, true
  101. }
  102. //println("没有 找到key:",key)
  103. return GoodsOrder{}, false
  104. }
  105. func Redis_GoodsOrder_DelK(key string) (err error) {
  106. err = redisCache_GoodsOrder.Delete(key)
  107. return
  108. }
  109. // ---------------- 特殊方法 -------------------
  110. // 获取 ById
  111. func Read_GoodsOrder_ById(id int) (r GoodsOrder) {
  112. if r, is := Redis_GoodsOrder_Get(strconv.Itoa(id)); is {
  113. return r
  114. }
  115. o := orm.NewOrm()
  116. r = GoodsOrder{Id: id, T_State: 1}
  117. err := o.Read(&r)
  118. if err != nil {
  119. fmt.Println(err)
  120. }
  121. return r
  122. }
  123. // 添加
  124. func Add_GoodsOrder(m GoodsOrder) (id int64, err error) {
  125. o := orm.NewOrm()
  126. id, err = o.Insert(&m)
  127. if err != nil {
  128. fmt.Println(err)
  129. }
  130. Redis_GoodsOrder_Set(m)
  131. return id, err
  132. }
  133. // 修改
  134. func Update_GoodsOrder(r GoodsOrder, cols ...string) bool {
  135. o := orm.NewOrm()
  136. if num, err := o.Update(&r, cols...); err == nil {
  137. fmt.Println("Number of records updated in database:", num)
  138. Redis_GoodsOrder_Set(r)
  139. return true
  140. }
  141. return false
  142. }
  143. // 删除
  144. func Delete_GoodsOrder(m GoodsOrder) bool {
  145. o := orm.NewOrm()
  146. m.T_State = 0
  147. if num, err := o.Delete(&m); err == nil {
  148. fmt.Println("Number of records deleted in database:", num)
  149. Redis_GoodsOrder_DelK(strconv.Itoa(m.Id))
  150. return true
  151. }
  152. return false
  153. }
  154. // 获取列表
  155. func Read_GoodsOrder_List(T_pid int, page int, page_z int, Name string) (t []GoodsOrderR, cnt int64) {
  156. o := orm.NewOrm()
  157. // 也可以直接使用 Model 结构体作为表名
  158. qs := o.QueryTable(new(GoodsOrder))
  159. var offset int64
  160. if page_z == 0 {
  161. page_z = conf.Page_size
  162. }
  163. var r []GoodsOrder
  164. if page <= 1 {
  165. offset = 0
  166. } else {
  167. offset = int64((page - 1) * page_z)
  168. }
  169. cond := orm.NewCondition()
  170. cond1 := cond.And("T_pid", T_pid).And("T_State", 1)
  171. if len(Name) > 0 {
  172. cond1 = cond1.AndCond(cond.Or("T_sn__icontains", Name).Or("T_orderid__icontains", Name).Or("T_outorderid__icontains", Name))
  173. }
  174. qs.Limit(page_z, offset).SetCond((*orm2.Condition)(cond1)).OrderBy("-Id").All(&r)
  175. cnt, _ = qs.SetCond((*orm2.Condition)(cond1)).Count()
  176. for _, v := range r {
  177. t = append(t, GoodsOrderToGoodsOrderR(v))
  178. }
  179. return t, cnt
  180. }