chart_extra_config.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. package data
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "hongze/hz_crm_api/models/data_manage"
  6. "hongze/hz_crm_api/services/google"
  7. "strconv"
  8. "strings"
  9. )
  10. func HandleExtraConfig(chartType int, extraConfigStr string) (newExtraConfigStr string, err error, errMsg string) {
  11. newExtraConfigStr = extraConfigStr
  12. switch chartType {
  13. case 10: // 截面散点图
  14. var tmpExtraConfig data_manage.SectionScatterReq
  15. if extraConfigStr == `` {
  16. errMsg = "截面散点图未配置"
  17. err = errors.New(errMsg)
  18. return
  19. }
  20. err = json.Unmarshal([]byte(extraConfigStr), &tmpExtraConfig)
  21. if err != nil {
  22. errMsg = "截面散点配置异常"
  23. err = errors.New(errMsg)
  24. return
  25. }
  26. newExtraConfigStr, err, errMsg = handleSectionScatterChartData(tmpExtraConfig)
  27. }
  28. return
  29. }
  30. // handleSectionScatterChartData 截面散点图的数据处理
  31. func handleSectionScatterChartData(extraConfig data_manage.SectionScatterReq) (extraConfigStr string, err error, errMsg string) {
  32. translateNameList := make([]string, 0)
  33. translateNameMap := make(map[string]bool, 0)
  34. if extraConfig.XNameEn == `` {
  35. if _, ok := translateNameMap[extraConfig.XName]; !ok {
  36. translateNameMap[extraConfig.XName] = true
  37. tmpName := strings.TrimSuffix(extraConfig.XName, " ")
  38. tmpName = strings.TrimPrefix(tmpName, " ")
  39. translateNameList = append(translateNameList, tmpName)
  40. }
  41. }
  42. if extraConfig.XUnitNameEn == `` {
  43. if _, ok := translateNameMap[extraConfig.XUnitName]; !ok {
  44. translateNameMap[extraConfig.XUnitName] = true
  45. tmpName := strings.TrimSuffix(extraConfig.XUnitName, " ")
  46. tmpName = strings.TrimPrefix(tmpName, " ")
  47. translateNameList = append(translateNameList, tmpName)
  48. }
  49. }
  50. if extraConfig.YNameEn == `` {
  51. if _, ok := translateNameMap[extraConfig.YName]; !ok {
  52. translateNameMap[extraConfig.YName] = true
  53. tmpName := strings.TrimSuffix(extraConfig.YName, " ")
  54. tmpName = strings.TrimPrefix(tmpName, " ")
  55. translateNameList = append(translateNameList, tmpName)
  56. }
  57. }
  58. if extraConfig.YUnitNameEn == `` {
  59. if _, ok := translateNameMap[extraConfig.YUnitName]; !ok {
  60. translateNameMap[extraConfig.YUnitName] = true
  61. tmpName := strings.TrimSuffix(extraConfig.YUnitName, " ")
  62. tmpName = strings.TrimPrefix(tmpName, " ")
  63. translateNameList = append(translateNameList, tmpName)
  64. }
  65. }
  66. for _, v := range extraConfig.SeriesList {
  67. if v.NameEn == `` {
  68. if _, ok := translateNameMap[v.Name]; !ok {
  69. translateNameMap[v.Name] = true
  70. tmpName := strings.TrimSuffix(v.Name, " ")
  71. tmpName = strings.TrimPrefix(tmpName, " ")
  72. translateNameList = append(translateNameList, tmpName)
  73. }
  74. }
  75. for _, edbInfo := range v.EdbInfoList {
  76. if edbInfo.NameEn == `` {
  77. if _, ok := translateNameMap[edbInfo.Name]; !ok {
  78. translateNameMap[edbInfo.Name] = true
  79. tmpName := strings.TrimSuffix(edbInfo.Name, " ")
  80. tmpName = strings.TrimPrefix(tmpName, " ")
  81. translateNameList = append(translateNameList, tmpName)
  82. }
  83. }
  84. }
  85. }
  86. // 获取英文名称map
  87. enNameMap, _, _ := GetEnNameMapByCnNameList(translateNameList)
  88. for k, seriesItem := range extraConfig.SeriesList {
  89. if len(seriesItem.EdbInfoList) <= 0 {
  90. errMsg = "指标不能为空"
  91. err = errors.New(errMsg)
  92. return
  93. }
  94. for edbIndex, edbConf := range seriesItem.EdbInfoList {
  95. if edbConf.NameEn == `` { //标签英文名称
  96. if tmpNameEn, ok := enNameMap[edbConf.Name]; ok {
  97. edbConf.NameEn = tmpNameEn
  98. }
  99. }
  100. seriesItem.EdbInfoList[edbIndex] = edbConf
  101. }
  102. if seriesItem.NameEn == `` { //系列英文名称
  103. if tmpNameEn, ok := enNameMap[seriesItem.Name]; ok {
  104. seriesItem.NameEn = tmpNameEn
  105. }
  106. }
  107. extraConfig.SeriesList[k] = seriesItem
  108. }
  109. if extraConfig.XNameEn == `` {
  110. if tmpNameEn, ok := enNameMap[extraConfig.XName]; ok {
  111. extraConfig.XNameEn = tmpNameEn
  112. }
  113. }
  114. if extraConfig.XUnitNameEn == `` {
  115. if tmpNameEn, ok := enNameMap[extraConfig.XUnitName]; ok {
  116. extraConfig.XUnitNameEn = tmpNameEn
  117. }
  118. }
  119. if extraConfig.YNameEn == `` {
  120. if tmpNameEn, ok := enNameMap[extraConfig.YName]; ok {
  121. extraConfig.YNameEn = tmpNameEn
  122. }
  123. }
  124. if extraConfig.YUnitNameEn == `` {
  125. if tmpNameEn, ok := enNameMap[extraConfig.YUnitName]; ok {
  126. extraConfig.YUnitNameEn = tmpNameEn
  127. }
  128. }
  129. extraConfigByte, err := json.Marshal(extraConfig)
  130. if err != nil {
  131. return
  132. }
  133. extraConfigStr = string(extraConfigByte)
  134. return
  135. }
  136. // GetEnNameMapByCnNameList 根据中文名称列表获取英文名称map
  137. func GetEnNameMapByCnNameList(cnNameList []string) (contentEnMap map[string]string, err error, errMsg string) {
  138. // 返回参初始化
  139. contentEnMap = make(map[string]string)
  140. // 英文临时赋值变量
  141. tmpContentEnMapAll := make(map[string]string)
  142. count := 0
  143. contentMap := make(map[string]string, 0)
  144. for k, v := range cnNameList {
  145. //如果单条翻译的字符数超过1000,则直接翻译,否则批量翻译
  146. if count >= 50 { //待翻译的条数不能超过50; 单条翻译字符数不能超过1000字符
  147. tmpContentEnMap, tmpErr, tmpErrMsg := google.BatchTranslateHandlerByGoogle(contentMap)
  148. if tmpErr != nil {
  149. err = tmpErr
  150. errMsg = tmpErrMsg
  151. return
  152. }
  153. for tmpK, contentEn := range tmpContentEnMap {
  154. tmpContentEnMapAll[tmpK] = contentEn
  155. }
  156. contentMap = make(map[string]string, 0)
  157. count = 0
  158. }
  159. contentMap[strconv.Itoa(k)] = v
  160. count += 1
  161. }
  162. //剩余不满50条的content
  163. if count > 0 {
  164. tmpContentEnMap, tmpErr, tmpErrMsg := google.BatchTranslateHandlerByGoogle(contentMap)
  165. if tmpErr != nil {
  166. err = tmpErr
  167. errMsg = tmpErrMsg
  168. return
  169. }
  170. for tmpK, contentEn := range tmpContentEnMap {
  171. tmpContentEnMapAll[tmpK] = contentEn
  172. }
  173. }
  174. // 重新组装拼接返回
  175. lenCnNameList := len(cnNameList)
  176. for k, v := range tmpContentEnMapAll {
  177. tmpIndex, _ := strconv.Atoi(k)
  178. if tmpIndex < lenCnNameList {
  179. contentEnMap[cnNameList[tmpIndex]] = v
  180. }
  181. }
  182. return
  183. }