CompanyClass.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. package Company
  2. import (
  3. "ColdP_server/conf"
  4. "encoding/json"
  5. "fmt"
  6. "github.com/astaxie/beego/cache"
  7. _ "github.com/astaxie/beego/cache/redis"
  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. type CompanyClass struct {
  15. Id int `orm:"column(ID);size(11);auto;pk"`
  16. T_pid int `orm:"index;size(256);null"` // Account.Company 绑定公司
  17. T_name string `orm:"size(256);null"` // 分类
  18. T_State int `orm:"size(2);default(1)"` // 0 删除 1 正常
  19. CreateTime time.Time `orm:"column(create_time);type(timestamp);null;auto_now_add"` //auto_now_add 第一次保存时才设置时间
  20. UpdateTime time.Time `orm:"column(update_time);type(timestamp);null;auto_now"` //auto_now 每次 model 保存时都会对时间自动更新
  21. }
  22. type CompanyClass_R struct {
  23. Id int
  24. T_pid int // Account.Company 绑定公司
  25. T_name string // 分类
  26. }
  27. func (t *CompanyClass) TableName() string {
  28. return "company_class" // 数据库名称 // ************** 替换 DesignClass **************
  29. }
  30. var redisCache_CompanyClass cache.Cache
  31. func init() {
  32. //注册模型
  33. orm.RegisterModel(new(CompanyClass))
  34. }
  35. // Redis_Set(m.T_sn,m) // Redis 更新缓存
  36. func Redis_CompanyClass_Set(r CompanyClass) (err error) {
  37. key := strconv.Itoa(r.Id)
  38. //json序列化
  39. str, err := json.Marshal(r)
  40. if err != nil {
  41. fmt.Print(err)
  42. return
  43. }
  44. err = redisCache_CompanyClass.Put(key, str, 2*time.Hour)
  45. if err != nil {
  46. fmt.Println("set key:", key, ",value:", str, err)
  47. }
  48. return
  49. }
  50. // if r,is :=Redis_Get(T_sn);is{
  51. // return r,nil
  52. // }
  53. func Redis_CompanyClass_Get(key string) (CompanyClass, bool) {
  54. println("找到key:", key)
  55. if redisCache_CompanyClass.IsExist(key) {
  56. //println("找到key:",key)
  57. v := redisCache_CompanyClass.Get(key)
  58. var r CompanyClass
  59. json.Unmarshal(v.([]byte), &r)
  60. return r, true
  61. }
  62. return CompanyClass{}, false
  63. }
  64. func Redis_CompanyClass_DelK(key string) (err error) {
  65. err = redisCache_CompanyClass.Delete(key)
  66. return
  67. }
  68. // ---------------- 特殊方法 -------------------
  69. func CompanyClassToCompanyClass_R(t CompanyClass) (r CompanyClass_R) {
  70. r.Id = t.Id
  71. r.T_pid = t.T_pid
  72. r.T_name = t.T_name
  73. return r
  74. }
  75. // 获取 ById
  76. func Read_CompanyClass_ById(id int) (r CompanyClass, err error) {
  77. key := strconv.Itoa(id)
  78. if r, is := Redis_CompanyClass_Get(key); is {
  79. return r, nil
  80. }
  81. o := orm.NewOrm()
  82. r = CompanyClass{Id: id, T_State: 1}
  83. err = o.Read(&r, "Id", "T_pid", "T_State")
  84. if err != nil {
  85. fmt.Println(err)
  86. return r, err
  87. }
  88. Redis_CompanyClass_Set(r)
  89. return r, err
  90. }
  91. // 添加
  92. func Add_CompanyClass(m CompanyClass) (id int64, err error) {
  93. o := orm.NewOrm()
  94. id, err = o.Insert(&m)
  95. if err != nil {
  96. fmt.Println(err)
  97. }
  98. Redis_CompanyClass_Set(m)
  99. return
  100. }
  101. // 修改
  102. func Update_CompanyClass(r CompanyClass, cols ...string) bool {
  103. o := orm.NewOrm()
  104. if num, err := o.Update(&r, cols...); err == nil {
  105. fmt.Println("Number of records updated in database:", num)
  106. Redis_CompanyClass_Set(r)
  107. return true
  108. }
  109. return false
  110. }
  111. // 删除
  112. func Delete_CompanyClass_ById(id int) bool {
  113. o := orm.NewOrm()
  114. v := CompanyClass{Id: id}
  115. // ascertain id exists in the database
  116. if err := o.Read(&v); err == nil {
  117. var num int64
  118. v.T_State = 0
  119. if num, err = o.Update(&v, "T_State"); err == nil {
  120. fmt.Println("Number of records updated in database:", num)
  121. key := strconv.Itoa(v.Id)
  122. Redis_CompanyClass_DelK(key)
  123. return true
  124. }
  125. }
  126. return false
  127. }
  128. // 获取列表
  129. func Read_CompanyClass_List(T_pid int, T_name string, page int, page_z int) (r []CompanyClass_R, cnt int64) {
  130. o := orm.NewOrm()
  131. // 也可以直接使用 Model 结构体作为表名
  132. var map_r []CompanyClass
  133. qs := o.QueryTable(new(CompanyClass))
  134. var offset int64
  135. if page_z == 0 {
  136. page_z = conf.Page_size
  137. }
  138. if page <= 1 {
  139. offset = 0
  140. } else {
  141. offset = int64((page - 1) * page_z)
  142. }
  143. cond := orm.NewCondition()
  144. cond1 := cond.And("T_State", 1)
  145. if T_pid > 0 {
  146. cond1 = cond1.And("T_pid", T_pid)
  147. }
  148. if len(T_name) > 0 {
  149. cond1 = cond1.And("T_name", T_name)
  150. }
  151. qs.Limit(page_z, offset).SetCond((*orm2.Condition)(cond1)).OrderBy("-Id").All(&map_r)
  152. cnt, _ = qs.SetCond((*orm2.Condition)(cond1)).Count()
  153. for _, v := range map_r {
  154. r = append(r, CompanyClassToCompanyClass_R(v))
  155. }
  156. return r, cnt
  157. }
  158. // 获取全部列表
  159. func Read_CompanyClass_All(T_pid int, T_name string) (r []CompanyClass_R) {
  160. o := orm.NewOrm()
  161. // 也可以直接使用 Model 结构体作为表名
  162. var map_r []CompanyClass
  163. qs := o.QueryTable(new(CompanyClass))
  164. cond := orm.NewCondition()
  165. cond1 := cond.And("T_State", 1).And("T_pid", T_pid)
  166. if len(T_name) > 0 {
  167. cond1 = cond1.And("T_name", T_name)
  168. }
  169. qs.SetCond((*orm2.Condition)(cond1)).OrderBy("-Id").All(&map_r)
  170. for _, v := range map_r {
  171. r = append(r, CompanyClassToCompanyClass_R(v))
  172. }
  173. return r
  174. }