cg_sdk.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. package cg_sdk
  2. import (
  3. "git.sxidc.com/go-framework/baize/framework/core/api/request"
  4. "git.sxidc.com/go-framework/baize/framework/core/api/response"
  5. "github.com/pkg/errors"
  6. )
  7. const (
  8. delimiter = "_"
  9. )
  10. const (
  11. encodeSegmentFormType = "form"
  12. encodeSegmentDynamicType = "dynamic"
  13. )
  14. type LibraryCodeInfo struct {
  15. ID string `json:"id" sqlresult:"column:id;"`
  16. ParentID string `json:"parentId" sqlresult:"column:parent_id;"`
  17. Name string `json:"name" sqlresult:"column:name;"`
  18. Code string `json:"code" sqlresult:"column:content;"`
  19. }
  20. type ConfigEncode struct {
  21. InputStruct any
  22. ConfigNamespace string
  23. ConfigName string
  24. EncodeStatus uint8
  25. CreateUserID string
  26. TenantID string
  27. OperatorUserName string
  28. inputMap map[string]any
  29. segmentValueMap map[string]LibraryCodeInfo
  30. encodeConfigInfo *EncodeConfigInfo
  31. codeNamespace string
  32. Code string
  33. }
  34. // 创建配置编码
  35. func (configEncode *ConfigEncode) CreateConfigEncode() (*Code, error) {
  36. var err error
  37. configEncode.encodeConfigInfo, err = getEncodeConfigInfo(configEncode.TenantID, configEncode.ConfigNamespace, configEncode.ConfigName)
  38. if err != nil {
  39. return nil, err
  40. }
  41. configEncode.GenCodeNamespace()
  42. configEncode.inputMap = response.StructToMap(configEncode.InputStruct)
  43. err = configEncode.genSegmentValueMap()
  44. if err != nil {
  45. return nil, err
  46. }
  47. code := &Code{
  48. Namespace: configEncode.codeNamespace,
  49. EncodeRuleID: configEncode.encodeConfigInfo.EncodeRuleID,
  50. EncodeStatus: configEncode.EncodeStatus,
  51. TenantID: configEncode.TenantID,
  52. UpdateUserID: configEncode.CreateUserID,
  53. OperatorUserName: configEncode.OperatorUserName,
  54. }
  55. code.Code, err = createEncode(configEncode.createCreateJsonBody())
  56. if err != nil {
  57. return nil, err
  58. }
  59. return code, nil
  60. }
  61. func (configEncode *ConfigEncode) UpdateConfigEncodeStatue() (*Code, error) {
  62. var err error
  63. configEncode.encodeConfigInfo, err = getEncodeConfigInfo(configEncode.TenantID, configEncode.ConfigNamespace, configEncode.ConfigName)
  64. if err != nil {
  65. return nil, err
  66. }
  67. configEncode.GenCodeNamespace()
  68. code := &Code{
  69. Code: configEncode.Code,
  70. Namespace: configEncode.codeNamespace,
  71. EncodeRuleID: configEncode.encodeConfigInfo.EncodeRuleID,
  72. TenantID: configEncode.TenantID,
  73. UpdateUserID: configEncode.CreateUserID,
  74. OperatorUserName: configEncode.OperatorUserName,
  75. }
  76. return code, nil
  77. }
  78. func (configEncode *ConfigEncode) genSegmentValueMap() error {
  79. if configEncode.segmentValueMap == nil {
  80. configEncode.segmentValueMap = make(map[string]LibraryCodeInfo)
  81. }
  82. for _, encodeSegment := range configEncode.encodeConfigInfo.ConfigContent {
  83. if inputFieldValueAny, ok := configEncode.inputMap[encodeSegment.FieldValue]; ok {
  84. switch encodeSegment.EncodeSegmentType {
  85. case encodeSegmentFormType:
  86. fallthrough
  87. case encodeSegmentDynamicType:
  88. switch inputFieldValueAny.(type) {
  89. case LibraryCodeInfo:
  90. configEncode.segmentValueMap[encodeSegment.EncodeSegmentID] = inputFieldValueAny.(LibraryCodeInfo)
  91. case string:
  92. configEncode.segmentValueMap[encodeSegment.EncodeSegmentID] = LibraryCodeInfo{Name: inputFieldValueAny.(string)}
  93. default:
  94. return errors.New("转化业务参数失败,未知类型:" + encodeSegment.EncodeSegmentType)
  95. }
  96. default:
  97. return errors.New("未知类型:" + encodeSegment.EncodeSegmentType)
  98. }
  99. }
  100. }
  101. return nil
  102. }
  103. func (configEncode *ConfigEncode) createCreateJsonBody() CreateEncodeJsonBody {
  104. return CreateEncodeJsonBody{
  105. Namespace: configEncode.codeNamespace,
  106. EncodeRuleID: configEncode.encodeConfigInfo.EncodeRuleID,
  107. SegmentValueMap: configEncode.segmentValueMap,
  108. EncodeStatus: configEncode.EncodeStatus,
  109. CreateUserIDJsonBody: request.CreateUserIDJsonBody{CreateUserID: configEncode.CreateUserID},
  110. TenantIDJsonBody: request.TenantIDJsonBody{TenantID: configEncode.TenantID},
  111. OperatorUserNameJsonBody: request.OperatorUserNameJsonBody{OperatorUserName: configEncode.OperatorUserName},
  112. }
  113. }
  114. func (code *Code) FormUpdateEncodeJsonBody() *UpdateEncodeJsonBody {
  115. return &UpdateEncodeJsonBody{
  116. Namespace: code.Namespace,
  117. EncodeRuleID: code.EncodeRuleID,
  118. Content: code.Code,
  119. UpdateUserIDJsonBody: request.UpdateUserIDJsonBody{UpdateUserID: code.UpdateUserID},
  120. TenantIDJsonBody: request.TenantIDJsonBody{TenantID: code.TenantID},
  121. OperatorUserNameJsonBody: request.OperatorUserNameJsonBody{OperatorUserName: code.OperatorUserName},
  122. }
  123. }
  124. func GetFreeStatus() uint8 {
  125. return codeStatusFree
  126. }
  127. func GetUsingStatus() uint8 {
  128. return codeStatusUsing
  129. }
  130. func GenCodeNamespace(configNamespace string, configName string) string {
  131. return configNamespace + delimiter + configName
  132. }
  133. func (configEncode *ConfigEncode) GenCodeNamespace() {
  134. configEncode.codeNamespace = GenCodeNamespace(configEncode.ConfigNamespace, configEncode.ConfigName)
  135. }