Company.go 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  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_Tree(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. // 内部用户未绑定公司
  143. if admin_r.T_pid == 0 && len(admin_r.T_pids) == 0 {
  144. return CompanyList, cnt
  145. }
  146. // 内部用户已绑定公司,* 绑定所有公司
  147. if len(admin_r.T_pids) > 0 && admin_r.T_pids != "*" {
  148. T_pids := models.SplitStringIds(admin_r.T_pids, "P")
  149. cond1 = cond1.AndCond(cond.Or("Id__in", T_pids).Or("T_mid__in", T_pids))
  150. }
  151. if len(Company_name) > 0 {
  152. cond1 = cond1.And("T_name__icontains", Company_name)
  153. }
  154. qs.SetCond((*orm2.Condition)(cond1)).OrderBy("-Id").All(&maps)
  155. cnt, _ = qs.SetCond((*orm2.Condition)(cond1)).Count()
  156. parentMap, flag := getCompanyParent(maps)
  157. if flag {
  158. for i := 0; i < len(maps); i++ {
  159. if parentMap[maps[i].T_mid] {
  160. continue
  161. }
  162. r := Company_R{
  163. Id: maps[i].Id,
  164. T_mid: maps[i].T_mid,
  165. T_name: maps[i].T_name,
  166. Children: nil,
  167. }
  168. info := CompanyCall(maps, r)
  169. CompanyList = append(CompanyList, info)
  170. }
  171. } else {
  172. for i := 0; i < len(maps); i++ {
  173. r := Company_R{
  174. Id: maps[i].Id,
  175. T_mid: maps[i].T_mid,
  176. T_name: maps[i].T_name,
  177. Children: nil,
  178. }
  179. info := CompanyCall(maps, r)
  180. CompanyList = append(CompanyList, info)
  181. }
  182. }
  183. return CompanyList, cnt
  184. }
  185. func Read_Company_List(page, page_z int) (CompanyList []Company_R, cnt int64) {
  186. var offset int64
  187. if page <= 1 {
  188. offset = 0
  189. } else {
  190. offset = int64((page - 1) * page_z)
  191. }
  192. o := orm.NewOrm()
  193. // 也可以直接使用 Model 结构体作为表名
  194. qs := o.QueryTable(new(Company))
  195. var maps []Company
  196. cond := orm.NewCondition()
  197. cond1 := cond.And("T_State", 1)
  198. qs.Limit(page_z, offset).SetCond((*orm2.Condition)(cond1)).OrderBy("-Id").All(&maps)
  199. cnt, _ = qs.SetCond((*orm2.Condition)(cond1)).Count()
  200. for _, v := range maps {
  201. CompanyList = append(CompanyList, CompanyToCompany_R(v))
  202. }
  203. return CompanyList, cnt
  204. }
  205. func CompanyTree(list []Company, parentId int) []Company_R {
  206. res := make([]Company_R, 0)
  207. for _, v := range list {
  208. if v.T_mid == parentId {
  209. r := Company_R{
  210. Id: v.Id,
  211. T_mid: v.T_mid,
  212. T_name: v.T_name,
  213. Children: nil,
  214. }
  215. r.Children = CompanyTree(list, v.Id)
  216. res = append(res, r)
  217. }
  218. }
  219. return res
  220. }
  221. func CompanyCall(CompanyList []Company, company Company_R) Company_R {
  222. list := CompanyList
  223. min := make([]Company_R, 0)
  224. for j := 0; j < len(list); j++ {
  225. if company.Id != list[j].T_mid {
  226. continue
  227. }
  228. mi := Company_R{}
  229. mi.Id = list[j].Id
  230. mi.T_mid = list[j].T_mid
  231. mi.T_name = list[j].T_name
  232. mi.Children = []Company_R{}
  233. ms := CompanyCall(CompanyList, mi)
  234. min = append(min, ms)
  235. }
  236. company.Children = min
  237. return company
  238. }
  239. func getCompanyParent(CompanyList []Company) (map[int]bool, bool) {
  240. list := CompanyList
  241. var flag = false
  242. var parentMap = map[int]bool{}
  243. for j := 0; j < len(list); j++ {
  244. parentMap[list[j].T_mid] = false
  245. }
  246. for j := 0; j < len(list); j++ {
  247. if _, ok := parentMap[list[j].Id]; !ok {
  248. continue
  249. }
  250. parentMap[list[j].Id] = true
  251. flag = true
  252. }
  253. return parentMap, flag
  254. }
  255. func Read_Company_List_All_ByT_name(T_name string) (maps []Company) {
  256. o := orm.NewOrm()
  257. // 也可以直接使用 Model 结构体作为表名
  258. qs := o.QueryTable(new(Company))
  259. if len(T_name) > 0 {
  260. qs.Filter("T_name", T_name).All(&maps)
  261. return maps
  262. }
  263. qs.All(&maps)
  264. return maps
  265. }
  266. // 获取列表
  267. func Read_Company_List_ByT_pids(T_pids string, page int, page_z int) (Company_r []Company_R, cnt int64) {
  268. o := orm.NewOrm()
  269. // 也可以直接使用 Model 结构体作为表名
  270. qs := o.QueryTable(new(Company))
  271. var offset int64
  272. if page_z == 0 {
  273. page_z = conf.Page_size
  274. }
  275. if page <= 1 {
  276. offset = 0
  277. } else {
  278. offset = int64((page - 1) * page_z)
  279. }
  280. var r []Company
  281. cond := orm.NewCondition()
  282. cond1 := cond.And("T_State", 1)
  283. if len(T_pids) >= 0 {
  284. list := models.SplitStringIds(T_pids, "P")
  285. cond1 = cond1.And("Id__in", list)
  286. }
  287. qs.Limit(page_z, offset).SetCond((*orm2.Condition)(cond1)).OrderBy("Id").All(&r)
  288. cnt, _ = qs.SetCond((*orm2.Condition)(cond1)).Count()
  289. for _, v := range r {
  290. Company_r = append(Company_r, CompanyToCompany_R(v))
  291. }
  292. return Company_r, cnt
  293. }