business.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. package service
  2. import (
  3. "context"
  4. "encoding/json"
  5. "fmt"
  6. "go-common/app/admin/main/aegis/model/business"
  7. "go-common/app/admin/main/aegis/model/middleware"
  8. "go-common/library/ecode"
  9. "go-common/library/log"
  10. )
  11. // AddBusiness .
  12. func (s *Service) AddBusiness(c context.Context, b *business.Business) (id int64, err error) {
  13. if id, err = s.gorm.AddBusiness(c, b); err != nil {
  14. log.Error("s.gorm.AddBusiness error(%v)", err)
  15. }
  16. return
  17. }
  18. // UpdateBusiness .
  19. func (s *Service) UpdateBusiness(c context.Context, b *business.Business) (err error) {
  20. if err = s.gorm.UpdateBusiness(c, b); err != nil {
  21. log.Error("s.gorm.UpdateBusiness error(%v)", err)
  22. }
  23. return
  24. }
  25. // SetBusinessState .
  26. func (s *Service) SetBusinessState(c context.Context, b *business.Business) (err error) {
  27. if int8(b.State) == business.StateEnable {
  28. if err = s.gorm.EnableBusiness(c, b.ID); err != nil {
  29. log.Error("s.gorm.EnableBusiness error(%v)", err)
  30. }
  31. } else {
  32. if err = s.gorm.DisableBusiness(c, b.ID); err != nil {
  33. log.Error("s.gorm.EnableBusiness error(%v)", err)
  34. }
  35. }
  36. return
  37. }
  38. // Business .
  39. func (s *Service) Business(c context.Context, b *business.Business) (res *business.Business, err error) {
  40. if res, err = s.gorm.Business(c, b.ID); err != nil {
  41. log.Error("s.gorm.Business error(%v)", err)
  42. }
  43. return
  44. }
  45. // BusinessList .
  46. func (s *Service) BusinessList(c context.Context, businessID []int64, onlyEnable bool) (res []*business.Business, err error) {
  47. if res, err = s.gorm.BusinessList(c, 0, businessID, onlyEnable); err != nil {
  48. log.Error("s.gorm.BusinessList error(%v) ids(%+v)", err, businessID)
  49. }
  50. s.mulIDtoName(c, res, s.transUnames, "UID", "UserName")
  51. return
  52. }
  53. func (s *Service) transUnames(c context.Context, uids []int64) (unames map[int64][]interface{}, err error) {
  54. unames = make(map[int64][]interface{})
  55. uns, err := s.http.GetUnames(c, uids)
  56. if err != nil {
  57. return
  58. }
  59. for uid, uname := range uns {
  60. unames[uid] = []interface{}{uname}
  61. }
  62. return
  63. }
  64. // AddBizCFG .
  65. func (s *Service) AddBizCFG(c context.Context, b *business.BizCFG) (lastid int64, err error) {
  66. return s.gorm.AddBizConfig(c, b)
  67. }
  68. // UpdateBizCFG .
  69. func (s *Service) UpdateBizCFG(c context.Context, b *business.BizCFG) (err error) {
  70. if err = s.gorm.EditBizConfig(c, b); err != nil {
  71. log.Error("s.gorm.UpdateBizCFG error(%v)", err)
  72. return
  73. }
  74. delete(s.bizCfgCache, fmt.Sprintf("%d_%d", b.BusinessID, b.TP))
  75. delete(s.bizRoleCache, b.BusinessID)
  76. delete(s.taskRoleCache, b.BusinessID)
  77. delete(s.bizMiddlewareCache, b.BusinessID)
  78. return
  79. }
  80. // ListBizCFGs .
  81. func (s *Service) ListBizCFGs(c context.Context, bizid int64) (res []*business.BizCFG, err error) {
  82. if res, err = s.gorm.GetConfigs(c, bizid); err != nil {
  83. log.Error("s.gorm.ListBizCFGs error(%v)", err)
  84. }
  85. return
  86. }
  87. // ReserveCFG 保留字配置
  88. func (s *Service) ReserveCFG(c context.Context, bizid int64) (rsvcfg map[string]interface{}, err error) {
  89. var rsv string
  90. if rsv = s.getConfig(c, bizid, business.TypeReverse); len(rsv) == 0 {
  91. return
  92. }
  93. rsvcfg = make(map[string]interface{})
  94. if err = json.Unmarshal([]byte(rsv), &rsvcfg); err != nil {
  95. log.Error("json.Unmarshal error(%v)", err)
  96. err = ecode.AegisReservedCfgErr
  97. }
  98. return
  99. }
  100. // AttributeCFG 属性位配置
  101. func (s *Service) AttributeCFG(c context.Context, bizid int64) (attrcfg map[string]uint, err error) {
  102. var attr string
  103. if attr = s.getConfig(c, bizid, business.TypeAttribute); len(attr) == 0 {
  104. return
  105. }
  106. attrcfg = make(map[string]uint)
  107. if err = json.Unmarshal([]byte(attr), &attrcfg); err != nil {
  108. log.Error("json.Unmarshal error(%v)", err)
  109. }
  110. return
  111. }
  112. func (s *Service) syncBizCache(c context.Context) (err error) {
  113. var (
  114. bizcfg []*business.BizCFG
  115. )
  116. if bizcfg, err = s.gorm.ActiveConfigs(c); err != nil {
  117. return
  118. }
  119. bizTpMap := map[string]string{}
  120. bizRoleMap := map[int64]map[string]int64{}
  121. taskRoleMap := map[int64]map[int64][]int64{}
  122. bizMiddleware := map[int64][]*middleware.Aggregate{}
  123. for _, item := range bizcfg {
  124. bizTpMap[fmt.Sprintf("%d_%d", item.BusinessID, item.TP)] = item.Config
  125. biz, taskRole, err := item.FormatMngBID()
  126. if err == nil && len(taskRole) > 0 {
  127. taskRoleMap[biz] = taskRole
  128. }
  129. bizID, bizRoles, err := item.FormatBizBID()
  130. if err == nil && len(bizRoles) > 0 {
  131. bizRoleMap[bizID] = bizRoles
  132. }
  133. mws, err := item.FormatAggregate()
  134. if err == nil && len(mws) > 0 {
  135. bizMiddleware[item.BusinessID] = mws
  136. }
  137. }
  138. s.bizCfgCache = bizTpMap
  139. s.bizRoleCache = bizRoleMap
  140. s.taskRoleCache = taskRoleMap
  141. s.bizMiddlewareCache = bizMiddleware
  142. return
  143. }
  144. func (s *Service) getConfig(c context.Context, bizid int64, tp int8) (cfg string) {
  145. var ok bool
  146. if cfg, ok = s.bizCfgCache[fmt.Sprintf("%d_%d", bizid, tp)]; ok {
  147. return cfg
  148. }
  149. s.syncBizCache(c)
  150. cfg = s.bizCfgCache[fmt.Sprintf("%d_%d", bizid, tp)]
  151. return
  152. }
  153. func (s *Service) getTaskRole(c context.Context, bizid int64) (cfgs map[int64][]int64) {
  154. var exist bool
  155. if cfgs, exist = s.taskRoleCache[bizid]; exist {
  156. return cfgs
  157. }
  158. s.syncBizCache(c)
  159. cfgs = s.taskRoleCache[bizid]
  160. return
  161. }
  162. func (s *Service) getTaskBiz(c context.Context, bids []int64) (bizs []int64, flows []int64) {
  163. bizs = []int64{}
  164. flows = []int64{}
  165. s.syncBizCache(c)
  166. for _, bid := range bids {
  167. for biz, item := range s.taskRoleCache {
  168. if v, exist := item[bid]; exist && len(v) > 0 {
  169. bizs = append(bizs, biz)
  170. flows = append(flows, v...)
  171. }
  172. }
  173. }
  174. return
  175. }
  176. func (s *Service) getBizRole(c context.Context, mngID int64) (bizRole map[string]int64, bizID int64) {
  177. for biz, item := range s.bizRoleCache {
  178. if item[business.BizBIDMngID] == mngID {
  179. bizID = biz
  180. bizRole = item
  181. return
  182. }
  183. }
  184. s.syncBizCache(c)
  185. for biz, item := range s.bizRoleCache {
  186. if item[business.BizBIDMngID] == mngID {
  187. bizID = biz
  188. bizRole = item
  189. break
  190. }
  191. }
  192. return
  193. }