123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517 |
- package data
- import (
- "encoding/json"
- "errors"
- "eta/eta_api/models/data_manage"
- "eta/eta_api/services/google"
- "eta/eta_api/utils"
- "fmt"
- "strconv"
- "strings"
- "time"
- )
- 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
- }
- // GetChartSectionCombineData 截面组合图的数据处理
- func GetChartSectionCombineData(chartInfoId int, mappingList []*data_manage.ChartEdbInfoMapping, edbDataListMap map[int][]*data_manage.EdbDataList, extraConfig data_manage.ChartSectionAllExtraConf) (edbIdList []int, chartDataResp data_manage.SectionScatterInfoResp, err error) {
- // 指标数据数组(10086:{"2022-12-02":100.01,"2022-12-01":102.3})
- edbDataMap := make(map[int]map[string]float64)
- for edbInfoId, edbDataList := range edbDataListMap {
- edbDateData := make(map[string]float64)
- for _, edbData := range edbDataList {
- edbDateData[edbData.DataTime] = edbData.Value
- }
- edbDataMap[edbInfoId] = edbDateData
- }
- // edbIdList 指标展示顺序;x轴的指标顺序
- edbIdList = make([]int, 0)
- edbMappingMap := make(map[int]*data_manage.ChartEdbInfoMapping)
- for _, v := range mappingList {
- edbIdList = append(edbIdList, v.EdbInfoId)
- edbMappingMap[v.EdbInfoId] = v
- }
- // 确定好截面散点图返回的数据格式
- // 获取所有的引用日期设置
- DateConfListMap := make(map[string]*data_manage.ChartSectionDateConfItem)
- for _, v := range extraConfig.DateConfList {
- DateConfListMap[v.DateConfName] = v
- }
- // 遍历每个系列
- // 遍历每个指标,根据选中的日期,进行日期变换得到最终的日期,根据最终的日期获取对应的值
- // 组装数据
- dataListResp := new(data_manage.ChartSectionCombineDataResp) //y轴的数据列表
- for _, seriesItem := range extraConfig.SeriesList {
- var maxDate time.Time
- var minVal, maxVal float64
- noDataEdbIdList := make([]int, 0)
- dataList := make([]float64, len(seriesItem.EdbInfoList))
- for index, edbConf := range seriesItem.EdbInfoList {
- edbInfoId := edbConf.EdbInfoId //X轴的指标
- _, ok := edbMappingMap[edbInfoId]
- if !ok {
- continue
- }
- edbDataList, ok3 := edbDataListMap[edbInfoId]
- if !ok3 {
- err = fmt.Errorf("指标%d的日期数据不存在", edbInfoId)
- return
- }
- /*dataList := edbDataListMap[edbInfoId] //指标的所有数据值
- if len(dataList) <= 0 {
- // 没有数据的指标id
- //findDataList = append(findDataList, 0)
- continue
- }*/
- //日期变换处理,判断用指标的最新日期还是,直接获取引用日期
- var findDate string
- if edbConf.DateConfName == "" {
- findDate, err = GetChartSectionSeriesDateByDateChange(edbInfoId, edbDataList, edbConf.DateConf.DateChange, edbConf.DateConf.MoveForward)
- } else {
- // 获取日期配置
- dateConfItem, ok1 := DateConfListMap[edbConf.DateConfName]
- if !ok1 {
- err = fmt.Errorf("引用日期配置不存在")
- return
- }
- // todo 根据日期变换得到最终日期
- edbDataListTmp := make([]*data_manage.EdbDataList, 0)
- if dateConfItem.EdbInfoId > 0 {
- edbDataListTmp, ok1 = edbDataListMap[dateConfItem.EdbInfoId]
- if !ok1 {
- err = fmt.Errorf("指标%d的日期数据不存在", dateConfItem.EdbInfoId)
- return
- }
- }
- findDate, err = GetChartSectionSeriesDateByDateChange(dateConfItem.EdbInfoId, edbDataListTmp, dateConfItem.DateChange, dateConfItem.MoveForward)
- }
- findDateTime, _ := time.ParseInLocation(utils.FormatDate, findDate, time.Local)
- if maxDate.IsZero() {
- maxDate = findDateTime
- } else {
- if findDateTime.After(maxDate) {
- maxDate = findDateTime
- }
- }
- if tmpValue, ok := edbDataMap[edbInfoId][findDate]; ok {
- dataList[index] = tmpValue
- if index == 0 {
- minVal = tmpValue
- maxVal = tmpValue
- } else {
- if tmpValue < minVal {
- minVal = tmpValue
- }
- if tmpValue > maxVal {
- maxVal = tmpValue
- }
- }
- } else {
- noDataEdbIdList = append(noDataEdbIdList, edbInfoId)
- continue
- }
- }
- seriesItem.DataList = dataList
- seriesItem.MinData = minVal
- seriesItem.MaxData = maxVal
- seriesItem.NoDataEdbIdList = noDataEdbIdList
- }
- dataListResp.XDataList = extraConfig.XDataList
- dataListResp.SeriesList = extraConfig.SeriesList
- dataListResp.DateConfList = extraConfig.DateConfList
- dataListResp.BaseChartSeriesName = extraConfig.BaseChartSeriesName
- dataListResp.UnitList = extraConfig.UnitList
- dataListResp.IsHeap = extraConfig.IsHeap
- dataListResp.SortType = extraConfig.SortType
- return
- }
- // GetChartSectionSeriesDateByDateChange 获取日期变换后的日期edbInfoId 1指标日期,2 系统日期
- func GetChartSectionSeriesDateByDateChange(edbInfoId int, dataList []*data_manage.EdbDataList, dateChange []*data_manage.ChartSectionDateChange, moveForward int) (newDate string, err error) {
- if edbInfoId > 0 { //指标日期
- newDate = GetEdbDateByMoveForward(moveForward, dataList)
- } else {
- //系统日期
- newDate = time.Now().Format(utils.FormatDate)
- }
- if newDate != "" && len(dateChange) > 0 {
- newDate, err = HandleChartSectionSeriesDateChange(newDate, dateChange)
- }
- return
- }
- func GetEdbDateByMoveForward(moveForward int, edbDataList []*data_manage.EdbDataList) (date string) {
- dateList := make([]string, 0)
- for _, v := range edbDataList {
- dateList = append(dateList, v.DataTime)
- }
- date = GetEdbDateByMoveForwardByDateList(moveForward, dateList)
- return
- }
- func GetEdbDateByMoveForwardByDateList(moveForward int, dateList []string) (date string) {
- // 根据日期进行排序
- index := len(dateList) - 1 - moveForward
- for k, v := range dateList {
- if k == index {
- date = v
- return
- }
- }
- return
- }
- // HandleChartSectionSeriesDateChange 处理日期变换
- func HandleChartSectionSeriesDateChange(date string, dateChange []*data_manage.ChartSectionDateChange) (newDate string, err error) {
- newDate = date
- if newDate != "" {
- if len(dateChange) > 0 {
- var dateTime time.Time
- dateTime, err = time.ParseInLocation(utils.FormatDate, newDate, time.Local)
- if err != nil {
- err = fmt.Errorf("日期解析失败: %s", err.Error())
- return
- }
- for _, v := range dateChange {
- if v.ChangeType == 1 {
- dateTime = dateTime.AddDate(v.Year, v.Month, v.Day)
- newDate = dateTime.Format(utils.FormatDate)
- } else if v.ChangeType == 2 {
- newDate, err, _ = handleSystemAppointDateT(dateTime, v.FrequencyDay, v.Frequency)
- if err != nil {
- return
- }
- dateTime, err = time.ParseInLocation(utils.FormatDate, newDate, time.Local)
- if err != nil {
- err = fmt.Errorf("日期解析失败: %s", err.Error())
- return
- }
- }
- }
- }
- }
- return
- }
- // handleSystemAppointDateT
- // @Description: 处理系统日期相关的指定频率(所在周/旬/月/季/半年/年的最后/最早一天)
- // @author: Roc
- // @datetime2023-10-27 09:31:35
- // @param Frequency string
- // @param Day string
- // @return date string
- // @return err error
- // @return errMsg string
- func handleSystemAppointDateT(currDate time.Time, appointDay, frequency string) (date string, err error, errMsg string) {
- //currDate := time.Now()
- switch frequency {
- case "本周":
- day := int(currDate.Weekday())
- if day == 0 { // 周日
- day = 7
- }
- num := 0
- switch appointDay {
- case "周一":
- num = 1
- case "周二":
- num = 2
- case "周三":
- num = 3
- case "周四":
- num = 4
- case "周五":
- num = 5
- case "周六":
- num = 6
- case "周日":
- num = 7
- }
- day = num - day
- date = currDate.AddDate(0, 0, day).Format(utils.FormatDate)
- case "本旬":
- day := currDate.Day()
- var tmpDate time.Time
- switch appointDay {
- case "第一天":
- if day <= 10 {
- tmpDate = time.Date(currDate.Year(), currDate.Month(), 1, 0, 0, 0, 0, currDate.Location())
- } else if day <= 20 {
- tmpDate = time.Date(currDate.Year(), currDate.Month(), 11, 0, 0, 0, 0, currDate.Location())
- } else {
- tmpDate = time.Date(currDate.Year(), currDate.Month(), 21, 0, 0, 0, 0, currDate.Location())
- }
- case "最后一天":
- if day <= 10 {
- tmpDate = time.Date(currDate.Year(), currDate.Month(), 10, 0, 0, 0, 0, currDate.Location())
- } else if day <= 20 {
- tmpDate = time.Date(currDate.Year(), currDate.Month(), 20, 0, 0, 0, 0, currDate.Location())
- } else {
- tmpDate = time.Date(currDate.Year(), currDate.Month()+1, 1, 0, 0, 0, 0, currDate.Location()).AddDate(0, 0, -1)
- }
- }
- date = tmpDate.Format(utils.FormatDate)
- case "本月":
- var tmpDate time.Time
- switch appointDay {
- case "第一天":
- tmpDate = time.Date(currDate.Year(), currDate.Month(), 1, 0, 0, 0, 0, currDate.Location())
- case "最后一天":
- tmpDate = time.Date(currDate.Year(), currDate.Month()+1, 1, 0, 0, 0, 0, currDate.Location()).AddDate(0, 0, -1)
- }
- date = tmpDate.Format(utils.FormatDate)
- case "本季":
- month := currDate.Month()
- var tmpDate time.Time
- switch appointDay {
- case "第一天":
- if month <= 3 {
- tmpDate = time.Date(currDate.Year(), 1, 1, 0, 0, 0, 0, currDate.Location())
- } else if month <= 6 {
- tmpDate = time.Date(currDate.Year(), 4, 1, 0, 0, 0, 0, currDate.Location())
- } else if month <= 9 {
- tmpDate = time.Date(currDate.Year(), 7, 1, 0, 0, 0, 0, currDate.Location())
- } else {
- tmpDate = time.Date(currDate.Year(), 10, 1, 0, 0, 0, 0, currDate.Location())
- }
- case "最后一天":
- if month <= 3 {
- tmpDate = time.Date(currDate.Year(), 3, 31, 0, 0, 0, 0, currDate.Location())
- } else if month <= 6 {
- tmpDate = time.Date(currDate.Year(), 6, 30, 0, 0, 0, 0, currDate.Location())
- } else if month <= 9 {
- tmpDate = time.Date(currDate.Year(), 9, 30, 0, 0, 0, 0, currDate.Location())
- } else {
- tmpDate = time.Date(currDate.Year(), 12, 31, 0, 0, 0, 0, currDate.Location())
- }
- }
- date = tmpDate.Format(utils.FormatDate)
- case "本半年":
- month := currDate.Month()
- var tmpDate time.Time
- switch appointDay {
- case "第一天":
- if month <= 6 {
- tmpDate = time.Date(currDate.Year(), 1, 1, 0, 0, 0, 0, currDate.Location())
- } else {
- tmpDate = time.Date(currDate.Year(), 7, 1, 0, 0, 0, 0, currDate.Location())
- }
- case "最后一天":
- if month <= 6 {
- tmpDate = time.Date(currDate.Year(), 6, 30, 0, 0, 0, 0, currDate.Location())
- } else {
- tmpDate = time.Date(currDate.Year(), 12, 31, 0, 0, 0, 0, currDate.Location())
- }
- }
- date = tmpDate.Format(utils.FormatDate)
- case "本年":
- var tmpDate time.Time
- switch appointDay {
- case "第一天":
- tmpDate = time.Date(currDate.Year(), 1, 1, 0, 0, 0, 0, currDate.Location())
- case "最后一天":
- tmpDate = time.Date(currDate.Year(), 12, 31, 0, 0, 0, 0, currDate.Location())
- }
- date = tmpDate.Format(utils.FormatDate)
- default:
- errMsg = "错误的日期频度:" + frequency
- err = errors.New(errMsg)
- return
- }
- return
- }
|