Company.go 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. package Account
  2. import (
  3. "Cold_Api/conf"
  4. "Cold_Api/models"
  5. "encoding/json"
  6. "fmt"
  7. "github.com/astaxie/beego/cache"
  8. _ "github.com/astaxie/beego/cache/redis"
  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. "strconv"
  13. "time"
  14. )
  15. type Company struct {
  16. Id int `orm:"column(ID);size(11);auto;pk"`
  17. T_mid int `orm:"size(200);null"` // 上一级 ID
  18. T_name string `orm:"size(256);null"` // 公司名称
  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. Children []Company `orm:"-"`
  23. }
  24. type Company_R struct {
  25. Id int
  26. T_mid int // 上一级 ID
  27. T_name string // 公司名称
  28. Children []Company_R
  29. }
  30. func CompanyToCompany_R(r Company) (v Company_R) {
  31. v.Id = r.Id
  32. v.T_mid = r.T_mid
  33. v.T_name = r.T_name
  34. return v
  35. }
  36. func (t *Company) TableName() string {
  37. return "company" // 数据库名称 // ************** 替换 FormulaList **************
  38. }
  39. var redisCache_Company cache.Cache
  40. func init() {
  41. //注册模型
  42. orm.RegisterModel(new(Company))
  43. orm.Debug = true
  44. config := fmt.Sprintf(`{"key":"%s","conn":"%s","dbNum":"%s","password":"%s"}`,
  45. "redis_Cold_User_Company", conf.Redis_address, conf.Redis_dbNum, conf.Redis_password)
  46. fmt.Println(config)
  47. var err error
  48. redisCache_Company, err = cache.NewCache("redis", config)
  49. if err != nil || redisCache_Company == nil {
  50. errMsg := "failed to init redis"
  51. fmt.Println(errMsg, err)
  52. }
  53. }
  54. // ---------------- Redis -------------------
  55. // Redis_Set(m.T_sn,m) // Redis 更新缓存
  56. func Redis_Company_Set(r Company) (err error) {
  57. //json序列化
  58. str, err := json.Marshal(r)
  59. if err != nil {
  60. fmt.Print(err)
  61. return
  62. }
  63. err = redisCache_Company.Put(strconv.Itoa(r.Id), str, 24*time.Hour)
  64. if err != nil {
  65. fmt.Println("set key:", strconv.Itoa(r.Id), ",value:", str, err)
  66. }
  67. return
  68. }
  69. // if r,is :=Redis_Get(T_sn);is{
  70. // return r,nil
  71. // }
  72. func Redis_Company_Get(key string) (r Company, is bool) {
  73. if redisCache_Company.IsExist(key) {
  74. //println("找到key:",key)
  75. v := redisCache_Company.Get(key)
  76. json.Unmarshal(v.([]byte), &r)
  77. return r, true
  78. }
  79. //println("没有 找到key:",key)
  80. return Company{}, false
  81. }
  82. func Redis_Company_DelK(key string) (err error) {
  83. err = redisCache_Company.Delete(key)
  84. return
  85. }
  86. // ---------------- 特殊方法 -------------------
  87. // 添加
  88. func Add_Company(m Company) (id int64, err error) {
  89. o := orm.NewOrm()
  90. id, err = o.Insert(&m)
  91. if err != nil {
  92. fmt.Println(err)
  93. }
  94. m.Id = int(id)
  95. Redis_Company_Set(m)
  96. return id, err
  97. }
  98. // 修改
  99. func Update_Company(m Company, cols ...string) bool {
  100. o := orm.NewOrm()
  101. if num, err := o.Update(&m, cols...); err == nil {
  102. fmt.Println("Number of records updated in database:", num)
  103. Redis_Company_Set(m) // Redis 更新缓存
  104. return true
  105. }
  106. return false
  107. }
  108. // 删除
  109. func Delete_Company(m Company) bool {
  110. o := orm.NewOrm()
  111. m.T_State = 0
  112. if num, err := o.Update(&m, "T_State"); err == nil {
  113. fmt.Println("Number of records updated in database:", num)
  114. } else {
  115. return false
  116. }
  117. Redis_Company_DelK(strconv.Itoa(m.Id))
  118. return true
  119. }
  120. // 获取 ById
  121. func Read_Company_ById(Id int) (r Company, e error) {
  122. if r, is := Redis_Company_Get(strconv.Itoa(Id)); is {
  123. //println("Redis_Get OK")
  124. return r, nil
  125. }
  126. o := orm.NewOrm()
  127. qs := o.QueryTable(new(Company))
  128. e = qs.Filter("Id", Id).Filter("T_State", 1).One(&r)
  129. return r, e
  130. }
  131. // 获取列表
  132. func Read_Company_List(admin_r Admin, Company_name string) (CompanyList []Company_R, cnt int64) {
  133. o := orm.NewOrm()
  134. // 也可以直接使用 Model 结构体作为表名
  135. qs := o.QueryTable(new(Company))
  136. var maps []Company
  137. cond := orm.NewCondition()
  138. cond1 := cond.And("T_State", 1)
  139. if admin_r.T_pid > 0 {
  140. cond1 = cond1.And("Id", admin_r.T_pid)
  141. }
  142. if len(admin_r.T_pids) > 0 {
  143. T_pids := models.SplitStringIds(admin_r.T_pids)
  144. cond1 = cond1.AndCond(cond.Or("Id__in", T_pids).Or("T_mid__in", T_pids))
  145. }
  146. if len(Company_name) > 0 {
  147. cond1 = cond1.And("T_name__icontains", Company_name)
  148. }
  149. qs.SetCond((*orm2.Condition)(cond1)).OrderBy("-Id").All(&maps)
  150. cnt, _ = qs.SetCond((*orm2.Condition)(cond1)).Count()
  151. parentMap, flag := getCompanyParent(maps)
  152. if flag {
  153. for i := 0; i < len(maps); i++ {
  154. if parentMap[maps[i].T_mid] {
  155. continue
  156. }
  157. r := Company_R{
  158. Id: maps[i].Id,
  159. T_mid: maps[i].T_mid,
  160. T_name: maps[i].T_name,
  161. Children: nil,
  162. }
  163. info := CompanyCall(maps, r)
  164. CompanyList = append(CompanyList, info)
  165. }
  166. } else {
  167. for i := 0; i < len(maps); i++ {
  168. r := Company_R{
  169. Id: maps[i].Id,
  170. T_mid: maps[i].T_mid,
  171. T_name: maps[i].T_name,
  172. Children: nil,
  173. }
  174. info := CompanyCall(maps, r)
  175. CompanyList = append(CompanyList, info)
  176. }
  177. }
  178. return CompanyList, cnt
  179. }
  180. func CompanyCall(CompanyList []Company, company Company_R) Company_R {
  181. list := CompanyList
  182. min := make([]Company_R, 0)
  183. for j := 0; j < len(list); j++ {
  184. if company.Id != list[j].T_mid {
  185. continue
  186. }
  187. mi := Company_R{}
  188. mi.Id = list[j].Id
  189. mi.T_mid = list[j].T_mid
  190. mi.T_name = list[j].T_name
  191. mi.Children = []Company_R{}
  192. ms := CompanyCall(CompanyList, mi)
  193. min = append(min, ms)
  194. }
  195. company.Children = min
  196. return company
  197. }
  198. func getCompanyParent(CompanyList []Company) (map[int]bool, bool) {
  199. list := CompanyList
  200. var flag = false
  201. var parentMap = map[int]bool{}
  202. for j := 0; j < len(list); j++ {
  203. parentMap[list[j].T_mid] = false
  204. }
  205. for j := 0; j < len(list); j++ {
  206. if _, ok := parentMap[list[j].Id]; !ok {
  207. continue
  208. }
  209. parentMap[list[j].Id] = true
  210. flag = true
  211. }
  212. return parentMap, flag
  213. }
  214. func Read_Company_List_All_ByT_name(T_name string) (maps []Company) {
  215. o := orm.NewOrm()
  216. // 也可以直接使用 Model 结构体作为表名
  217. qs := o.QueryTable(new(Company))
  218. if len(T_name) > 0 {
  219. qs.Filter("T_name", T_name).All(&maps)
  220. return maps
  221. }
  222. qs.All(&maps)
  223. return maps
  224. }