Couriers.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. package Function
  2. import (
  3. "Cold_GoodsOrder/conf"
  4. "encoding/json"
  5. "fmt"
  6. "github.com/astaxie/beego/cache"
  7. "github.com/beego/beego/v2/adapter/orm"
  8. orm2 "github.com/beego/beego/v2/client/orm"
  9. _ "github.com/go-sql-driver/mysql"
  10. "strconv"
  11. "time"
  12. )
  13. // Couriers Order 模板
  14. type Couriers struct {
  15. Id int `json:"Id" orm:"column(ID);size(11);auto;pk"`
  16. Name string `json:"T_name" orm:"size(256);null"` // 配送员
  17. Phone string `orm:"size(256);null"` // 电话
  18. UserId int `orm:"index;size(256);null"` // 用户ID
  19. CreateTime time.Time `orm:"column(create_time);type(timestamp);null;auto_now_add"` // 创建时间
  20. UpdateTime time.Time `orm:"column(update_time);type(timestamp);null;auto_now"` // 更新时间
  21. }
  22. type CouriersR struct {
  23. Id int `json:"Id"` // ID
  24. Name string `json:"T_name"` // 配送员
  25. Phone string // 电话
  26. CreateTime string // 创建时间
  27. UpdateTime string // 更新时间
  28. }
  29. func CouriersToCouriersR(t Couriers) (r CouriersR) {
  30. r.Id = t.Id
  31. r.Name = t.Name
  32. r.Phone = t.Phone
  33. r.CreateTime = t.CreateTime.Format("2006-01-02 15:04:05")
  34. r.UpdateTime = t.UpdateTime.Format("2006-01-02 15:04:05")
  35. return
  36. }
  37. func (t *Couriers) TableName() string {
  38. return "couriers" // 数据库名称
  39. }
  40. var redisCache_Couriers cache.Cache
  41. func init() {
  42. //注册模型
  43. orm.RegisterModel(new(Couriers))
  44. config := fmt.Sprintf(`{"key":"%s","conn":"%s","dbNum":"%s","password":"%s"}`,
  45. "redis_Couriers", conf.Redis_address, conf.Redis_dbNum, conf.Redis_password)
  46. fmt.Println(config)
  47. var err error
  48. redisCache_Couriers, err = cache.NewCache("redis", config)
  49. if err != nil || redisCache_Couriers == nil {
  50. errMsg := "failed to init redis"
  51. fmt.Println(errMsg, err)
  52. }
  53. }
  54. // ---------------- Redis -------------------
  55. func Redis_Couriers_Set(r Couriers) (err error) {
  56. //json序列化
  57. str, err := json.Marshal(r)
  58. if err != nil {
  59. fmt.Print(err)
  60. return
  61. }
  62. err = redisCache_Couriers.Put(strconv.Itoa(r.Id), str, 24*time.Hour)
  63. if err != nil {
  64. fmt.Println("set key:", strconv.Itoa(r.Id), ",value:", str, err)
  65. }
  66. return
  67. }
  68. func Redis_Couriers_Get(key string) (r Couriers, is bool) {
  69. if redisCache_Couriers.IsExist(key) {
  70. //println("找到key:",key)
  71. v := redisCache_Couriers.Get(key)
  72. json.Unmarshal(v.([]byte), &r)
  73. return r, true
  74. }
  75. //println("没有 找到key:",key)
  76. return Couriers{}, false
  77. }
  78. func Redis_Couriers_DelK(key string) (err error) {
  79. err = redisCache_Couriers.Delete(key)
  80. return
  81. }
  82. // ---------------- 特殊方法 -------------------
  83. // 获取 ById
  84. func Read_Couriers_ById(id int) (r Couriers) {
  85. if r, is := Redis_Couriers_Get(strconv.Itoa(id)); is {
  86. return r
  87. }
  88. o := orm.NewOrm()
  89. r = Couriers{Id: id}
  90. err := o.Read(&r)
  91. if err != nil {
  92. fmt.Println(err)
  93. }
  94. return r
  95. }
  96. // 添加
  97. func Add_Couriers(m Couriers) (id int64, err error) {
  98. o := orm.NewOrm()
  99. id, err = o.Insert(&m)
  100. if err != nil {
  101. fmt.Println(err)
  102. }
  103. Redis_Couriers_Set(m)
  104. return id, err
  105. }
  106. // 修改
  107. func Update_Couriers(r Couriers, cols ...string) bool {
  108. o := orm.NewOrm()
  109. if num, err := o.Update(&r, cols...); err == nil {
  110. fmt.Println("Number of records updated in database:", num)
  111. Redis_Couriers_Set(r)
  112. return true
  113. }
  114. return false
  115. }
  116. // 删除
  117. func Delete_Couriers(m Couriers) bool {
  118. o := orm.NewOrm()
  119. if num, err := o.Delete(&m); err == nil {
  120. fmt.Println("Number of records deleted in database:", num)
  121. Redis_Couriers_DelK(strconv.Itoa(m.Id))
  122. return true
  123. }
  124. return false
  125. }
  126. // 获取列表
  127. func Read_Couriers_List(userId int, page int, page_z int, name string) (t []CouriersR, cnt int64) {
  128. o := orm.NewOrm()
  129. // 也可以直接使用 Model 结构体作为表名
  130. qs := o.QueryTable(new(Couriers))
  131. var offset int64
  132. if page_z == 0 {
  133. page_z = conf.Page_size
  134. }
  135. var r []Couriers
  136. if page <= 1 {
  137. offset = 0
  138. } else {
  139. offset = int64((page - 1) * page_z)
  140. }
  141. cond := orm.NewCondition()
  142. cond1 := cond.And("UserId", userId)
  143. if len(name) > 0 {
  144. cond1 = cond1.And("name__icontains", name)
  145. }
  146. qs.Limit(page_z, offset).SetCond((*orm2.Condition)(cond1)).OrderBy("-Id").All(&r)
  147. cnt, _ = qs.SetCond((*orm2.Condition)(cond1)).Count()
  148. for _, v := range r {
  149. t = append(t, CouriersToCouriersR(v))
  150. }
  151. return t, cnt
  152. }