CompanyClass.go 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. package Company
  2. import (
  3. "Cold_Api/conf"
  4. "Cold_Api/controllers/lib"
  5. "Cold_Api/models/Account"
  6. "encoding/json"
  7. "fmt"
  8. "github.com/astaxie/beego/cache"
  9. _ "github.com/astaxie/beego/cache/redis"
  10. "github.com/beego/beego/v2/adapter/orm"
  11. orm2 "github.com/beego/beego/v2/client/orm"
  12. "github.com/beego/beego/v2/core/logs"
  13. _ "github.com/go-sql-driver/mysql"
  14. "strconv"
  15. "time"
  16. )
  17. type CompanyClass struct {
  18. Id int `orm:"column(ID);size(11);auto;pk"`
  19. T_pid int `orm:"index;size(256);null"` // Account.Company 绑定公司
  20. T_name string `orm:"size(256);null"` // 分类
  21. T_State int `orm:"size(2);default(1)"` // 0 删除 1 正常
  22. CreateTime time.Time `orm:"column(create_time);type(timestamp);null;auto_now_add"` //auto_now_add 第一次保存时才设置时间
  23. UpdateTime time.Time `orm:"column(update_time);type(timestamp);null;auto_now"` //auto_now 每次 model 保存时都会对时间自动更新
  24. }
  25. type CompanyClass_R struct {
  26. Id int
  27. T_pid int // Account.Company 绑定公司
  28. T_name string // 分类
  29. }
  30. type CompanyClass_Company struct {
  31. Id int
  32. T_pid int // Account.Company 绑定公司
  33. T_pid_name string // Account.Company 绑定公司名称
  34. T_name string // 分类
  35. }
  36. func (t *CompanyClass) TableName() string {
  37. return "company_class" // 数据库名称 // ************** 替换 DesignClass **************
  38. }
  39. var redisCache_CompanyClass cache.Cache
  40. func init() {
  41. //注册模型
  42. orm.RegisterModel(new(CompanyClass))
  43. config := fmt.Sprintf(`{"key":"%s","conn":"%s","dbNum":"%s","password":"%s"}`,
  44. "redis_CompanyClass", conf.Redis_address, conf.Redis_dbNum, conf.Redis_password)
  45. fmt.Println(config)
  46. var err error
  47. redisCache_CompanyClass, err = cache.NewCache("redis", config)
  48. if err != nil || redisCache_CompanyClass == nil {
  49. errMsg := "failed to init redis"
  50. logs.Error(errMsg, err)
  51. panic(errMsg)
  52. }
  53. }
  54. // Redis_Set(m.T_sn,m) // Redis 更新缓存
  55. func Redis_CompanyClass_Set(r CompanyClass) (err error) {
  56. key := strconv.Itoa(r.Id)
  57. //json序列化
  58. str, err := json.Marshal(r)
  59. if err != nil {
  60. logs.Error(lib.FuncName(), err)
  61. return
  62. }
  63. err = redisCache_CompanyClass.Put(key, str, 2*time.Hour)
  64. if err != nil {
  65. logs.Error("set key:", key, ",value:", str, err)
  66. }
  67. return
  68. }
  69. // if r,is :=Redis_Get(T_sn);is{
  70. // return r,nil
  71. // }
  72. func Redis_CompanyClass_Get(key string) (CompanyClass, bool) {
  73. println("找到key:", key)
  74. if redisCache_CompanyClass.IsExist(key) {
  75. //println("找到key:",key)
  76. v := redisCache_CompanyClass.Get(key)
  77. var r CompanyClass
  78. err := json.Unmarshal(v.([]byte), &r)
  79. if err != nil {
  80. logs.Error(lib.FuncName(), err)
  81. return CompanyClass{}, false
  82. }
  83. return r, true
  84. }
  85. return CompanyClass{}, false
  86. }
  87. func Redis_CompanyClass_DelK(key string) (err error) {
  88. err = redisCache_CompanyClass.Delete(key)
  89. if err != nil {
  90. logs.Error(lib.FuncName(), err)
  91. return
  92. }
  93. return
  94. }
  95. // ---------------- 特殊方法 -------------------
  96. func CompanyClassToCompanyClass_R(t CompanyClass) (r CompanyClass_R) {
  97. r.Id = t.Id
  98. r.T_pid = t.T_pid
  99. r.T_name = t.T_name
  100. return r
  101. }
  102. func CompanyClassToCompanyClass_Company(t CompanyClass) (r CompanyClass_Company) {
  103. r.Id = t.Id
  104. r.T_pid = t.T_pid
  105. r.T_pid_name = Account.Read_Company_Get(t.T_pid)
  106. r.T_name = t.T_name
  107. return r
  108. }
  109. // 获取 ById
  110. func Read_CompanyClass_ById(id int) (r CompanyClass, err error) {
  111. key := strconv.Itoa(id)
  112. if r, is := Redis_CompanyClass_Get(key); is {
  113. return r, nil
  114. }
  115. o := orm.NewOrm()
  116. r = CompanyClass{Id: id, T_State: 1}
  117. err = o.Read(&r, "Id", "T_State")
  118. if err != nil {
  119. logs.Error(lib.FuncName(), err)
  120. return r, err
  121. }
  122. Redis_CompanyClass_Set(r)
  123. return r, err
  124. }
  125. // 添加
  126. func Add_CompanyClass(m CompanyClass) (id int64, err error) {
  127. o := orm.NewOrm()
  128. id, err = o.Insert(&m)
  129. if err != nil {
  130. logs.Error(lib.FuncName(), err)
  131. return
  132. }
  133. Redis_CompanyClass_Set(m)
  134. return
  135. }
  136. // 修改
  137. func Update_CompanyClass(r CompanyClass, cols ...string) bool {
  138. o := orm.NewOrm()
  139. num, err := o.Update(&r, cols...)
  140. if err != nil {
  141. logs.Error(lib.FuncName(), err)
  142. return false
  143. }
  144. logs.Info("Number of records updated in database:", num)
  145. Redis_CompanyClass_Set(r)
  146. return true
  147. }
  148. // 删除
  149. func Delete_CompanyClass_ById(id int) bool {
  150. o := orm.NewOrm()
  151. v := CompanyClass{Id: id}
  152. if err := o.Read(&v); err == nil {
  153. var num int64
  154. v.T_State = 0
  155. num, err = o.Update(&v, "T_State")
  156. if err != nil {
  157. logs.Error(lib.FuncName(), err)
  158. return false
  159. }
  160. logs.Info("Number of records updated in database:", num)
  161. key := strconv.Itoa(v.Id)
  162. Redis_CompanyClass_DelK(key)
  163. return true
  164. }
  165. return false
  166. }
  167. // 获取列表
  168. func Read_CompanyClass_List(T_pid int, T_name string, page int, page_z int) (r []CompanyClass_R, cnt int64) {
  169. o := orm.NewOrm()
  170. // 也可以直接使用 Model 结构体作为表名
  171. var map_r []CompanyClass
  172. qs := o.QueryTable(new(CompanyClass))
  173. var offset int64
  174. if page <= 1 {
  175. offset = 0
  176. } else {
  177. offset = int64((page - 1) * page_z)
  178. }
  179. cond := orm.NewCondition()
  180. cond1 := cond.And("T_State", 1)
  181. if T_pid > 0 {
  182. cond1 = cond1.And("T_pid", T_pid)
  183. }
  184. if len(T_name) > 0 {
  185. cond1 = cond1.And("T_name__icontains", T_name)
  186. }
  187. _, err := qs.Limit(page_z, offset).SetCond((*orm2.Condition)(cond1)).OrderBy("-Id").All(&map_r)
  188. if err != nil {
  189. logs.Error(lib.FuncName(), err)
  190. return
  191. }
  192. cnt, err = qs.SetCond((*orm2.Condition)(cond1)).Count()
  193. if err != nil {
  194. logs.Error(lib.FuncName(), err)
  195. return
  196. }
  197. for _, v := range map_r {
  198. r = append(r, CompanyClassToCompanyClass_R(v))
  199. }
  200. return r, cnt
  201. }
  202. // 获取全部列表
  203. func Read_CompanyClass_All(T_pid int, T_name string) (r []CompanyClass_R) {
  204. o := orm.NewOrm()
  205. // 也可以直接使用 Model 结构体作为表名
  206. var map_r []CompanyClass
  207. qs := o.QueryTable(new(CompanyClass))
  208. cond := orm.NewCondition()
  209. cond1 := cond.And("T_State", 1).And("T_pid", T_pid)
  210. if len(T_name) > 0 {
  211. cond1 = cond1.And("T_name__icontains", T_name)
  212. }
  213. _, err := qs.SetCond((*orm2.Condition)(cond1)).OrderBy("-Id").All(&map_r)
  214. if err != nil {
  215. logs.Error(lib.FuncName(), err)
  216. return
  217. }
  218. for _, v := range map_r {
  219. r = append(r, CompanyClassToCompanyClass_R(v))
  220. }
  221. return r
  222. }
  223. func Read_CompanyClass_All_For_Company(T_pids []int, T_name string) (r []CompanyClass_Company) {
  224. o := orm.NewOrm()
  225. // 也可以直接使用 Model 结构体作为表名
  226. var map_r []CompanyClass
  227. qs := o.QueryTable(new(CompanyClass))
  228. cond := orm.NewCondition()
  229. cond1 := cond.And("T_State", 1).And("T_pid__in", T_pids)
  230. if len(T_name) > 0 {
  231. cond1 = cond1.And("T_name__icontains", T_name)
  232. }
  233. _, err := qs.SetCond((*orm2.Condition)(cond1)).OrderBy("T_pid").All(&map_r)
  234. if err != nil {
  235. logs.Error(lib.FuncName(), err)
  236. return
  237. }
  238. for _, v := range map_r {
  239. r = append(r, CompanyClassToCompanyClass_Company(v))
  240. }
  241. return r
  242. }