business.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. package business
  2. import (
  3. "fmt"
  4. "reflect"
  5. "strconv"
  6. "strings"
  7. "go-common/app/admin/main/aegis/model"
  8. "go-common/app/admin/main/aegis/model/common"
  9. "go-common/library/log"
  10. xtime "go-common/library/time"
  11. )
  12. const (
  13. // StateEnable 正常
  14. StateEnable = int8(0)
  15. // StateDisable 删除
  16. StateDisable = int8(1)
  17. // TypeIframe .
  18. TypeIframe = int8(0)
  19. // TypeSubmit 提交接口配置
  20. TypeSubmit = int8(1)
  21. // TypeAction 预留给前端组件配置
  22. TypeAction = int8(2)
  23. // TypeManagerBID 角色配置
  24. TypeManagerBID = int8(3)
  25. // TypeReverse 保留字配置
  26. TypeReverse = int8(4)
  27. // TypeAttribute 属性位配置
  28. TypeAttribute = int8(5)
  29. // TypeDeleteState 删除状态配置
  30. TypeDeleteState = int8(6)
  31. // TypeFiler 敏感词过滤
  32. TypeFiler = int8(7)
  33. // TypeBizBID 业务级别用户角色
  34. TypeBizBID = int8(8)
  35. // TypeAdapter 适配器配置
  36. TypeAdapter = int8(9)
  37. // TypeRscListAdapter 资源列表适配器
  38. TypeRscListAdapter = int8(10)
  39. // 中间件配置,用于前后端交互的自定义数据逻辑处理
  40. TypeMiddleware = int8(11)
  41. // TypeTempCodes 临时错误码
  42. TypeTempCodes = int8(12)
  43. // TypeCallback TODO 提交接口v2版本,为了兼容线上的TypeSubmit。待v2上线后,再去掉该配置,改为TypeSubmit
  44. TypeCallback = int8(13)
  45. // MngBIDMID 任务用户角色: {"manager_id":1, "flow_id":12}
  46. MngBIDMID = "manager_id"
  47. // MngBIDFlow .
  48. MngBIDFlow = "flow_id"
  49. // BizBIDMngID 业务用户角色: {"manager_id":1, "admin":12}
  50. BizBIDMngID = "manager_id"
  51. // BizBIDAdmin .
  52. BizBIDAdmin = "admin"
  53. // AccessBiz .
  54. AccessBiz = "access_biz"
  55. // AccessFlow .
  56. AccessFlow = "access_flow"
  57. )
  58. // Business .
  59. type Business struct {
  60. ID int64 `json:"id" gorm:"primary_key" form:"id"`
  61. TP int64 `json:"type" gorm:"column:type" form:"type"`
  62. Name string `json:"name" gorm:"column:name" form:"name" validate:"max=10"`
  63. Desc string `json:"desc" gorm:"column:desc" form:"desc" validate:"max=160"`
  64. Developer string `json:"developer" gorm:"column:developer" form:"developer"`
  65. UID int64 `json:"uid" gorm:"column:uid"`
  66. Attribute int64 `json:"attribute" gorm:"column:attribute" form:"attribute"`
  67. State int64 `json:"state" gorm:"column:state" form:"state"`
  68. UserName string `json:"username" gorm:"-"`
  69. Ctime xtime.Time `json:"ctime" gorm:"column:ctime"`
  70. Mtime xtime.Time `json:"mtime" gorm:"column:mtime"`
  71. }
  72. // IAdapter .
  73. type IAdapter interface {
  74. AdaptString(val string) string
  75. AdaptInt64(val int64) int64
  76. }
  77. // Adapter 业务适配器
  78. type Adapter struct {
  79. Name string `json:"name"`
  80. Rule string `json:"rule"`
  81. Value string `json:"value"`
  82. Compare string `json:"compare"`
  83. }
  84. // AdaptString .
  85. func (a Adapter) AdaptString(val string) string {
  86. return val
  87. }
  88. // AdaptInt .
  89. func (a Adapter) AdaptInt(val int64) int64 {
  90. switch a.Rule {
  91. case "not":
  92. if strconv.FormatInt(val, 10) != a.Compare {
  93. if v, e := strconv.ParseInt(a.Value, 10, 64); e != nil {
  94. log.Error("AdaptInt strconv.ParseInt(%s)", a.Value)
  95. } else {
  96. val = v
  97. }
  98. }
  99. default:
  100. log.Error("AdaptInt unsupported rule(%s)", a.Rule)
  101. }
  102. return val
  103. }
  104. // AdaptAddOpt ..
  105. func AdaptAddOpt(opt *model.AddOption, adps []*Adapter) {
  106. if len(adps) == 0 || opt == nil {
  107. return
  108. }
  109. log.Info("AdaptAddOpt before opt(%+v)", opt)
  110. v := reflect.ValueOf(opt).Elem()
  111. for _, adp := range adps {
  112. val := v.FieldByName(adp.Name)
  113. if !val.CanSet() {
  114. log.Error("AdaptAddOpt field(%s) can't set", adp.Name)
  115. return
  116. }
  117. switch val.Kind() {
  118. case reflect.String:
  119. val.SetString(adp.AdaptString(val.String()))
  120. case reflect.Int8, reflect.Int64, reflect.Int:
  121. val.SetInt(adp.AdaptInt(val.Int()))
  122. default:
  123. log.Error("AdaptAddOpt unsupported adp(%+v) type(%v)", adp, val.Kind())
  124. }
  125. }
  126. log.Info("AdaptAddOpt after opt(%+v)", opt)
  127. }
  128. // AdaptUpdateOpt ..
  129. func AdaptUpdateOpt(opt *model.UpdateOption, adps []*Adapter) {
  130. if len(adps) == 0 || opt == nil {
  131. return
  132. }
  133. log.Info("AdaptUpdateOpt before opt(%+v)", opt)
  134. for _, adp := range adps {
  135. key := strings.ToLower(adp.Name)
  136. if iv, ok := opt.Update[key]; ok {
  137. switch reflect.TypeOf(iv).Kind() {
  138. case reflect.String:
  139. opt.Update[key] = adp.AdaptString(iv.(string))
  140. case reflect.Int8, reflect.Int64, reflect.Int:
  141. intv, err := strconv.Atoi(fmt.Sprint(iv))
  142. if err != nil {
  143. log.Error("AdaptUpdateOpt unsupported adp(%+v)", adp)
  144. continue
  145. }
  146. opt.Update[key] = adp.AdaptInt(int64(intv))
  147. default:
  148. log.Error("AdaptUpdateOpt unsupported adp(%+v)", adp)
  149. }
  150. }
  151. }
  152. log.Info("AdaptUpdateOpt after opt(%+v)", opt)
  153. }
  154. // TableName .
  155. func (t *Business) TableName() string {
  156. return "business"
  157. }
  158. // OptList .
  159. type OptList struct {
  160. common.Pager
  161. TP int8 `form:"type"`
  162. }
  163. // ListBusiness .
  164. type ListBusiness struct {
  165. common.Pager
  166. Business []*Business `json:"business"`
  167. }
  168. // BizItem .
  169. type BizItem struct {
  170. BizID int64 `json:"business_id"`
  171. BizName string `json:"business_name"`
  172. BizType int64 `json:"biz_type"`
  173. Flows map[int64]string `json:"flows"`
  174. }
  175. // FlowItem .
  176. type FlowItem struct {
  177. FlowID int64 `json:"flow_id"`
  178. NetID int64 `json:"net_id"`
  179. Name string `json:"name"`
  180. ChName string `json:"ch_name"`
  181. }
  182. // BizItemArr .
  183. type BizItemArr []*BizItem
  184. func (a BizItemArr) Len() int {
  185. return len(a)
  186. }
  187. func (a BizItemArr) Less(i, j int) bool {
  188. return a[i].BizID < a[j].BizID
  189. }
  190. func (a BizItemArr) Swap(i, j int) {
  191. a[i], a[j] = a[j], a[i]
  192. }