cg_sdk.go 4.1 KB

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