Distributor.go 6.2 KB

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