package data import ( "encoding/json" "errors" "eta/eta_api/models/data_manage" "eta/eta_api/services/google" "strconv" "strings" ) func HandleExtraConfig(chartType int, extraConfigStr string) (newExtraConfigStr string, err error, errMsg string) { newExtraConfigStr = extraConfigStr switch chartType { case 10: // 截面散点图 var tmpExtraConfig data_manage.SectionScatterReq if extraConfigStr == `` { errMsg = "截面散点图未配置" err = errors.New(errMsg) return } err = json.Unmarshal([]byte(extraConfigStr), &tmpExtraConfig) if err != nil { errMsg = "截面散点配置异常" err = errors.New(errMsg) return } newExtraConfigStr, err, errMsg = handleSectionScatterChartData(tmpExtraConfig) } return } // handleSectionScatterChartData 截面散点图的数据处理 func handleSectionScatterChartData(extraConfig data_manage.SectionScatterReq) (extraConfigStr string, err error, errMsg string) { translateNameList := make([]string, 0) translateNameMap := make(map[string]bool, 0) if extraConfig.XNameEn == `` { if _, ok := translateNameMap[extraConfig.XName]; !ok { translateNameMap[extraConfig.XName] = true tmpName := strings.TrimSuffix(extraConfig.XName, " ") tmpName = strings.TrimPrefix(tmpName, " ") translateNameList = append(translateNameList, tmpName) } } if extraConfig.XUnitNameEn == `` { if _, ok := translateNameMap[extraConfig.XUnitName]; !ok { translateNameMap[extraConfig.XUnitName] = true tmpName := strings.TrimSuffix(extraConfig.XUnitName, " ") tmpName = strings.TrimPrefix(tmpName, " ") translateNameList = append(translateNameList, tmpName) } } if extraConfig.YNameEn == `` { if _, ok := translateNameMap[extraConfig.YName]; !ok { translateNameMap[extraConfig.YName] = true tmpName := strings.TrimSuffix(extraConfig.YName, " ") tmpName = strings.TrimPrefix(tmpName, " ") translateNameList = append(translateNameList, tmpName) } } if extraConfig.YUnitNameEn == `` { if _, ok := translateNameMap[extraConfig.YUnitName]; !ok { translateNameMap[extraConfig.YUnitName] = true tmpName := strings.TrimSuffix(extraConfig.YUnitName, " ") tmpName = strings.TrimPrefix(tmpName, " ") translateNameList = append(translateNameList, tmpName) } } for _, v := range extraConfig.SeriesList { if v.NameEn == `` { if _, ok := translateNameMap[v.Name]; !ok { translateNameMap[v.Name] = true tmpName := strings.TrimSuffix(v.Name, " ") tmpName = strings.TrimPrefix(tmpName, " ") translateNameList = append(translateNameList, tmpName) } } for _, edbInfo := range v.EdbInfoList { if edbInfo.NameEn == `` { if _, ok := translateNameMap[edbInfo.Name]; !ok { translateNameMap[edbInfo.Name] = true tmpName := strings.TrimSuffix(edbInfo.Name, " ") tmpName = strings.TrimPrefix(tmpName, " ") translateNameList = append(translateNameList, tmpName) } } } } // 获取英文名称map enNameMap, _, _ := GetEnNameMapByCnNameList(translateNameList) for k, seriesItem := range extraConfig.SeriesList { if len(seriesItem.EdbInfoList) <= 0 { errMsg = "指标不能为空" err = errors.New(errMsg) return } for edbIndex, edbConf := range seriesItem.EdbInfoList { if edbConf.NameEn == `` { //标签英文名称 if tmpNameEn, ok := enNameMap[edbConf.Name]; ok { edbConf.NameEn = tmpNameEn } } seriesItem.EdbInfoList[edbIndex] = edbConf } if seriesItem.NameEn == `` { //系列英文名称 if tmpNameEn, ok := enNameMap[seriesItem.Name]; ok { seriesItem.NameEn = tmpNameEn } } extraConfig.SeriesList[k] = seriesItem } if extraConfig.XNameEn == `` { if tmpNameEn, ok := enNameMap[extraConfig.XName]; ok { extraConfig.XNameEn = tmpNameEn } } if extraConfig.XUnitNameEn == `` { if tmpNameEn, ok := enNameMap[extraConfig.XUnitName]; ok { extraConfig.XUnitNameEn = tmpNameEn } } if extraConfig.YNameEn == `` { if tmpNameEn, ok := enNameMap[extraConfig.YName]; ok { extraConfig.YNameEn = tmpNameEn } } if extraConfig.YUnitNameEn == `` { if tmpNameEn, ok := enNameMap[extraConfig.YUnitName]; ok { extraConfig.YUnitNameEn = tmpNameEn } } extraConfigByte, err := json.Marshal(extraConfig) if err != nil { return } extraConfigStr = string(extraConfigByte) return } // GetEnNameMapByCnNameList 根据中文名称列表获取英文名称map func GetEnNameMapByCnNameList(cnNameList []string) (contentEnMap map[string]string, err error, errMsg string) { // 返回参初始化 contentEnMap = make(map[string]string) // 英文临时赋值变量 tmpContentEnMapAll := make(map[string]string) count := 0 contentMap := make(map[string]string, 0) for k, v := range cnNameList { //如果单条翻译的字符数超过1000,则直接翻译,否则批量翻译 if count >= 50 { //待翻译的条数不能超过50; 单条翻译字符数不能超过1000字符 tmpContentEnMap, tmpErr, tmpErrMsg := google.BatchTranslateHandlerByGoogle(contentMap) if tmpErr != nil { err = tmpErr errMsg = tmpErrMsg return } for tmpK, contentEn := range tmpContentEnMap { tmpContentEnMapAll[tmpK] = contentEn } contentMap = make(map[string]string, 0) count = 0 } contentMap[strconv.Itoa(k)] = v count += 1 } //剩余不满50条的content if count > 0 { tmpContentEnMap, tmpErr, tmpErrMsg := google.BatchTranslateHandlerByGoogle(contentMap) if tmpErr != nil { err = tmpErr errMsg = tmpErrMsg return } for tmpK, contentEn := range tmpContentEnMap { tmpContentEnMapAll[tmpK] = contentEn } } // 重新组装拼接返回 lenCnNameList := len(cnNameList) for k, v := range tmpContentEnMapAll { tmpIndex, _ := strconv.Atoi(k) if tmpIndex < lenCnNameList { contentEnMap[cnNameList[tmpIndex]] = v } } return }