123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199 |
- 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
- }
|