Distributor.go 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. package Distributor
  2. import (
  3. "ColdVerify_server/conf"
  4. "ColdVerify_server/lib"
  5. "ColdVerify_server/logs"
  6. "encoding/json"
  7. "fmt"
  8. "github.com/astaxie/beego/cache"
  9. "github.com/beego/beego/v2/adapter/orm"
  10. orm2 "github.com/beego/beego/v2/client/orm"
  11. _ "github.com/go-sql-driver/mysql"
  12. "time"
  13. )
  14. // 模版
  15. type Distributor struct {
  16. Id int `orm:"column(ID);size(11);auto;pk"`
  17. T_name string `orm:"size(256);null"` // 名称
  18. T_Distributor_id string `orm:"size(256);null"` // 分销商id
  19. T_State int `orm:"size(200);default(1)"` // 0删除 1 正常
  20. CreateTime time.Time `orm:"column(create_time);type(timestamp);null;auto_now_add"` //auto_now 每次 model 保存时都会对时间自动更新
  21. UpdateTime time.Time `orm:"column(update_time);type(timestamp);null;auto_now"` //auto_now_add 第一次保存时才设置时间
  22. }
  23. type Distributor_R struct {
  24. T_name string
  25. T_Distributor_id string
  26. }
  27. func (t *Distributor) TableName() string {
  28. return "distributor" // 数据库名称 // ************** 替换 FormulaList **************
  29. }
  30. var redisCache_Distributor cache.Cache
  31. func init() {
  32. //注册模型
  33. orm.RegisterModel(new(Distributor))
  34. config := fmt.Sprintf(`{"key":"%s","conn":"%s","dbNum":"%s","password":"%s"}`,
  35. "redis_"+"distributor", conf.Redis_address, conf.Redis_dbNum, conf.Redis_password)
  36. logs.Println(config)
  37. var err error
  38. redisCache_Distributor, err = cache.NewCache("redis", config)
  39. if err != nil || redisCache_Distributor == nil {
  40. errMsg := "failed to init redis"
  41. logs.Println(errMsg, err)
  42. }
  43. }
  44. // -------------------------------------------------------------
  45. func DistributorToDistributor_R(T Distributor) (T_r Distributor_R) {
  46. T_r.T_name = T.T_name
  47. T_r.T_Distributor_id = T.T_Distributor_id
  48. //......
  49. return T_r
  50. }
  51. // ---------------- Redis -------------------
  52. // Redis_Set(m.T_sn,m) // Redis 更新缓存
  53. func Redis_Distributor_Set(key string, r Distributor) (err error) {
  54. //json序列化
  55. str, err := json.Marshal(r)
  56. if err != nil {
  57. logs.Error(lib.FuncName(), err)
  58. return
  59. }
  60. err = redisCache_Distributor.Put(key, str, 24*time.Hour)
  61. if err != nil {
  62. logs.Println("set key:", key, ",value:", str, err)
  63. }
  64. return
  65. }
  66. // if r,is :=Redis_Get(T_sn);is{
  67. // return r,nil
  68. // }
  69. func Redis_Distributor_Get(key string) (r Distributor, is bool) {
  70. if redisCache_Distributor.IsExist(key) {
  71. logs.Println("找到key:", key)
  72. v := redisCache_Distributor.Get(key)
  73. json.Unmarshal(v.([]byte), &r)
  74. return r, true
  75. }
  76. logs.Println("没有 找到key:", key)
  77. return Distributor{}, false
  78. }
  79. func Redis_Distributor_DelK(key string) (err error) {
  80. err = redisCache_Distributor.Delete(key)
  81. return
  82. }
  83. // ---------------- 特殊方法 -------------------
  84. // 获取 ById
  85. func Read_Distributor_ById(id int) (r Distributor, is bool) {
  86. o := orm.NewOrm()
  87. r = Distributor{Id: id}
  88. err := o.Read(&r) // o.Read(&r,"Tokey") 如果不是 主键 就得指定字段名
  89. if err != nil {
  90. logs.Error(lib.FuncName(), err)
  91. return r, false
  92. }
  93. return r, true
  94. }
  95. // 获取 By
  96. func Read_Distributor(T_Distributor_id string) (r Distributor, is bool) {
  97. o := orm.NewOrm()
  98. qs := o.QueryTable(new(Distributor))
  99. err := qs.Filter("T_Distributor_id", T_Distributor_id).One(&r)
  100. if err != nil {
  101. return r, false
  102. }
  103. return r, true
  104. }
  105. // 添加
  106. func Add_Distributor(r Distributor) (id int64, is bool) {
  107. o := orm.NewOrm()
  108. // 生成编号
  109. rand_x := 0
  110. for true {
  111. r.T_Distributor_id = lib.GetRandstring(4, "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789", int64(rand_x)) // 1,336,336
  112. err := o.Read(&r, "T_Distributor_id") // o.Read(&r,"Tokey") 如果不是 主键 就得指定字段名
  113. if err != nil {
  114. break
  115. }
  116. rand_x += 1
  117. }
  118. id, err := o.Insert(&r)
  119. if err != nil {
  120. logs.Error(lib.FuncName(), err)
  121. return 0, false
  122. }
  123. Redis_Distributor_Set(r.T_name, r)
  124. return id, true
  125. }
  126. // 删除
  127. func Delete_Distributor(v Distributor) bool {
  128. o := orm.NewOrm()
  129. if num, err := o.Delete(&v); err == nil {
  130. logs.Println("Number of records deleted in database:", num)
  131. } else {
  132. return false
  133. }
  134. Redis_Distributor_DelK(v.T_name)
  135. return true
  136. }
  137. // 删除
  138. func Delete_Distributor_(v Distributor) bool {
  139. o := orm.NewOrm()
  140. v.T_State = 0
  141. if num, err := o.Update(&v, "T_State"); err == nil {
  142. logs.Println("Number of records updated in database:", num)
  143. } else {
  144. return false
  145. }
  146. Redis_Distributor_DelK(v.T_name)
  147. return true
  148. }
  149. // 修改
  150. func Update_Distributor(m Distributor, cols ...string) bool {
  151. o := orm.NewOrm()
  152. if num, err := o.Update(&m, cols...); err == nil {
  153. logs.Println("Number of records updated in database:", num)
  154. Redis_Distributor_Set(m.T_name, m)
  155. return true
  156. }
  157. return false
  158. }
  159. // 获取列表
  160. func Read_Distributor_List(T_name string, page int, page_z int) ([]Distributor_R, int64) {
  161. o := orm.NewOrm()
  162. // 也可以直接使用 Model 结构体作为表名
  163. var r []Distributor
  164. qs := o.QueryTable(new(Distributor))
  165. var offset int64
  166. if page <= 1 {
  167. offset = 0
  168. } else {
  169. offset = int64((page - 1) * page_z)
  170. }
  171. cond := orm.NewCondition()
  172. cond1 := cond.And("T_name__icontains", T_name).And("T_State", 1) // .AndNot("status__in", 1).Or("profile__age__gt", 2000)
  173. if page_z == 9999 {
  174. qs.SetCond((*orm2.Condition)(cond1)).OrderBy("-Id").All(&r)
  175. } else {
  176. qs.Limit(page_z, offset).SetCond((*orm2.Condition)(cond1)).OrderBy("-Id").All(&r)
  177. }
  178. cnt, _ := qs.SetCond((*orm2.Condition)(cond1)).Count()
  179. // 转换
  180. var Distributor_r []Distributor_R
  181. for _, v := range r {
  182. Distributor_r = append(Distributor_r, DistributorToDistributor_R(v))
  183. }
  184. return Distributor_r, cnt
  185. }
  186. // 获取列表
  187. func Read_Distributor_List_ALL(T_name string) (maps []Distributor) {
  188. o := orm.NewOrm()
  189. qs := o.QueryTable(new(Distributor))
  190. if len(T_name) > 0 {
  191. qs = qs.Filter("T_name__icontains", T_name)
  192. }
  193. qs.Filter("T_State", 1).OrderBy("Id").All(&maps)
  194. return maps
  195. }
  196. func DistributorListToMap(T []Distributor) map[string]string {
  197. maps := make(map[string]string, len(T))
  198. for _, v := range T {
  199. maps[v.T_Distributor_id] = v.T_name
  200. }
  201. return maps
  202. }