1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264 |
- package excel
- import (
- "encoding/json"
- "errors"
- "eta/eta_api/models/data_manage"
- "eta/eta_api/models/data_manage/excel"
- "eta/eta_api/models/data_manage/excel/request"
- "eta/eta_api/models/data_manage/excel/response"
- "eta/eta_api/models/system"
- "eta/eta_api/services/data"
- "eta/eta_api/utils"
- "fmt"
- "github.com/shopspring/decimal"
- "github.com/yidane/formula"
- "sort"
- "strconv"
- "strings"
- "time"
- )
- func GetExcelDetailInfoByExcelInfoId(excelInfoId int) (excelDetail response.ExcelInfoDetail, errMsg string, err error) {
- errMsg = `获取失败`
-
- excelInfo, err := excel.GetExcelInfoById(excelInfoId)
- if err != nil {
- err = errors.New("获取ETA表格信息失败,Err:" + err.Error())
- if err.Error() == utils.ErrNoRow() {
- errMsg = "ETA表格被删除,请刷新页面"
- err = errors.New("ETA表格被删除,请刷新页面,Err:" + err.Error())
- }
- return
- }
- excelDetail = response.ExcelInfoDetail{
- ExcelInfoId: excelInfo.ExcelInfoId,
- Source: excelInfo.Source,
- ExcelType: excelInfo.ExcelType,
- ExcelName: excelInfo.ExcelName,
- UniqueCode: excelInfo.UniqueCode,
- ExcelClassifyId: excelInfo.ExcelClassifyId,
- SysUserId: excelInfo.SysUserId,
- SysUserRealName: excelInfo.SysUserRealName,
- Content: excelInfo.Content,
- ExcelImage: excelInfo.ExcelImage,
- FileUrl: excelInfo.FileUrl,
- Sort: excelInfo.Sort,
- IsDelete: excelInfo.IsDelete,
- ModifyTime: excelInfo.ModifyTime,
- CreateTime: excelInfo.CreateTime,
- TableData: nil,
- }
- switch excelInfo.Source {
- case utils.TIME_TABLE:
- var tableDataConfig TableDataConfig
- err = json.Unmarshal([]byte(excelDetail.Content), &tableDataConfig)
- if err != nil {
- err = errors.New("表格json转结构体失败,Err:" + err.Error())
- return
- }
- result, tmpErr := GetDataByTableDataConfig(tableDataConfig)
- if tmpErr != nil {
- err = errors.New("获取最新的表格数据失败,Err:" + tmpErr.Error())
- return
- }
- excelDetail.TableData = result
- case utils.MIXED_TABLE:
- var result request.MixedTableReq
- err = json.Unmarshal([]byte(excelDetail.Content), &result)
- if err != nil {
- err = errors.New("表格json转结构体失败,Err:" + err.Error())
- return
- }
- newData, tmpErr, tmpErrMsg := GetMixedTableCellData(result)
- if tmpErr != nil {
- errMsg = "获取失败"
- if tmpErrMsg != `` {
- errMsg = tmpErrMsg
- }
- err = errors.New("获取最新的数据失败,Err:" + tmpErr.Error())
- return
- }
- result.Data = newData
- excelDetail.TableData = result
- }
- return
- }
- func GetExcelInfoOpButton(sysUser *system.Admin, belongUserId, source int) (button response.ExcelInfoDetailButton) {
-
- button.RefreshButton = true
- button.CopyButton = true
- button.DownloadButton = true
-
-
-
- if sysUser.RoleTypeCode == utils.ROLE_TYPE_CODE_ADMIN || sysUser.RoleTypeCode == utils.ROLE_TYPE_CODE_FICC_ADMIN || sysUser.AdminId == belongUserId || source == utils.EXCEL_DEFAULT {
- button.OpButton = true
- button.DeleteButton = true
- }
-
- if source == utils.CUSTOM_ANALYSIS_TABLE {
- if sysUser.RoleTypeCode == utils.ROLE_TYPE_CODE_ADMIN || sysUser.RoleTypeCode == utils.ROLE_TYPE_CODE_FICC_ADMIN || sysUser.AdminId == belongUserId {
- button.OpEdbButton = true
- button.RefreshEdbButton = true
- }
- }
- return
- }
- func GetFirstEdbDataList(edbInfo *data_manage.EdbInfo, num int, manualDateList []string) (resultDataList []request.ManualDataReq, err error) {
- var dataList []*data_manage.EdbDataList
- switch edbInfo.EdbInfoType {
- case 0:
- dataList, err = data_manage.GetEdbDataList(edbInfo.Source, edbInfo.SubSource, edbInfo.EdbInfoId, ``, ``)
- case 1:
- _, dataList, _, _, err, _ = data.GetPredictDataListByPredictEdbInfoId(edbInfo.EdbInfoId, ``, ``, false)
- default:
- err = errors.New(fmt.Sprint("获取失败,指标类型异常", edbInfo.EdbInfoType))
- }
- if err != nil {
- return
- }
-
- lenData := len(dataList)
- if lenData <= 0 {
- return
- }
- tmpManualDateNum := 0
- lenManualDate := len(manualDateList)
- if lenManualDate > 0 {
- sortDateList := manualDateList
- baseDateList := utils.StrArr{}
- baseDateList = append(baseDateList, sortDateList...)
- sort.Sort(baseDateList)
- sortDateList = append([]string{}, baseDateList...)
- lastData := dataList[lenData-1]
- lastDataDate, tmpErr := time.ParseInLocation(utils.FormatDate, lastData.DataTime, time.Local)
- if tmpErr != nil {
- err = tmpErr
- return
- }
-
- for _, tmpDateStr := range sortDateList {
- tmpDate, tmpErr := time.ParseInLocation(utils.FormatDate, tmpDateStr, time.Local)
- if tmpErr != nil {
- err = tmpErr
- return
- }
- if tmpDate.After(lastDataDate) {
- tmpManualDateNum++
- continue
- }
- break
- }
- }
-
- num = num - tmpManualDateNum
- if num > lenData {
- num = lenData
- }
- latestDateTime, _ := time.ParseInLocation(utils.FormatDate, edbInfo.LatestDate, time.Local)
- for i := 1; i <= num; i++ {
- dataTime, _ := time.ParseInLocation(utils.FormatDate, dataList[lenData-i].DataTime, time.Local)
- dataType := 1
-
- if edbInfo.EdbInfoType == 1 && dataTime.After(latestDateTime) {
- dataType = 5
- }
- resultDataList = append(resultDataList, request.ManualDataReq{
- DataType: dataType,
- DataTime: dataList[lenData-i].DataTime,
- ShowValue: fmt.Sprint(dataList[lenData-i].Value),
- Value: fmt.Sprint(dataList[lenData-i].Value),
- DataTimeType: 1,
- })
- }
- return
- }
- func GetOtherEdbDataList(edbInfo *data_manage.EdbInfo, dateList []string) (resultDataList []request.ManualDataReq, err error) {
- lenDate := len(dateList)
- if lenDate <= 0 {
- return
- }
- sortDateList := dateList
- baseDateList := utils.StrArr{}
- baseDateList = append(baseDateList, sortDateList...)
- sort.Sort(baseDateList)
- sortDateList = append([]string{}, baseDateList...)
- endDateTime, err := time.ParseInLocation(utils.FormatDate, sortDateList[0], time.Local)
- if err != nil {
- return
- }
- firstDateTime, err := time.ParseInLocation(utils.FormatDate, sortDateList[lenDate-1], time.Local)
- if err != nil {
- return
- }
- var dataList []*data_manage.EdbDataList
- switch edbInfo.EdbInfoType {
- case 0:
- dataList, err = data_manage.GetEdbDataList(edbInfo.Source, edbInfo.SubSource, edbInfo.EdbInfoId, ``, ``)
- case 1:
- _, dataList, _, _, err, _ = data.GetPredictDataListByPredictEdbInfoId(edbInfo.EdbInfoId, ``, ``, false)
- default:
- err = errors.New(fmt.Sprint("获取失败,指标类型异常", edbInfo.EdbInfoType))
- }
- if err != nil {
- return
- }
-
- baseDataList := make([]*data_manage.EdbDataList, 0)
- var lastData *data_manage.EdbDataList
- var isInsert bool
- for _, data := range dataList {
- tmpDate := data.DataTime
- tmpDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, tmpDate, time.Local)
- if tmpErr != nil {
- err = tmpErr
- return
- }
- if tmpDateTime.Before(firstDateTime) {
- lastData = data
- continue
- }
-
- if !isInsert && lastData != nil {
- baseDataList = append(baseDataList, lastData)
- }
- if tmpDateTime.After(endDateTime) {
- baseDataList = append(baseDataList, data)
- break
- }
- baseDataList = append(baseDataList, data)
- isInsert = true
- }
-
- realValMap := make(map[string]string)
- for _, v := range baseDataList {
- realValMap[v.DataTime] = v.DataTime
- }
-
- handleDataMap := make(map[string]float64)
- err = data.HandleDataByLinearRegression(baseDataList, handleDataMap)
- if err != nil {
- return
- }
- latestDateTime, _ := time.ParseInLocation(utils.FormatDate, edbInfo.LatestDate, time.Local)
-
- for _, date := range sortDateList {
- dataType := 1
- if _, ok := realValMap[date]; !ok {
- dataType = 2
- } else {
- dataTime, _ := time.ParseInLocation(utils.FormatDate, date, time.Local)
-
- if edbInfo.EdbInfoType == 1 && dataTime.After(latestDateTime) {
- dataType = 5
- }
- }
- var value, showValue string
- if tmpVal, ok := handleDataMap[date]; ok {
- value = fmt.Sprint(tmpVal)
- showValue = value
- } else {
- dataType = 3
- }
- resultDataList = append(resultDataList, request.ManualDataReq{
- DataType: dataType,
- DataTime: date,
- ShowValue: showValue,
- Value: value,
- })
- }
- return
- }
- func GetFirstHistoryEdbDataList(edbInfo *data_manage.EdbInfo, num int, endDate string) (resultDataList []request.ManualDataReq, err error) {
- endDateTime, err := time.ParseInLocation(utils.FormatDate, endDate, time.Local)
- if err != nil {
- return
- }
- var dataList []*data_manage.EdbDataList
- switch edbInfo.EdbInfoType {
- case 0:
- dataList, err = data_manage.GetEdbDataList(edbInfo.Source, edbInfo.SubSource, edbInfo.EdbInfoId, ``, endDate)
- case 1:
- _, dataList, _, _, err, _ = data.GetPredictDataListByPredictEdbInfoId(edbInfo.EdbInfoId, ``, endDate, true)
- default:
- err = errors.New(fmt.Sprint("获取失败,指标类型异常", edbInfo.EdbInfoType))
- }
- if err != nil {
- return
- }
-
- lenData := len(dataList)
- if lenData <= 0 {
- return
- }
- lastData := dataList[lenData-1]
- lastDataDateTime, err := time.ParseInLocation(utils.FormatDate, lastData.DataTime, time.Local)
- if err != nil {
- return
- }
- if endDateTime.Equal(lastDataDateTime) || lastDataDateTime.After(endDateTime) {
- dataList = dataList[:lenData-1]
- lenData = len(dataList)
- }
- if num > lenData {
- num = lenData
- }
- latestDateTime, _ := time.ParseInLocation(utils.FormatDate, edbInfo.LatestDate, time.Local)
- for i := 1; i <= num; i++ {
- dataTime, _ := time.ParseInLocation(utils.FormatDate, dataList[lenData-i].DataTime, time.Local)
- dataType := 1
-
- if edbInfo.EdbInfoType == 1 && dataTime.After(latestDateTime) {
- dataType = 5
- }
- resultDataList = append(resultDataList, request.ManualDataReq{
- DataType: dataType,
- DataTime: dataList[lenData-i].DataTime,
- ShowValue: fmt.Sprint(dataList[lenData-i].Value),
- Value: fmt.Sprint(dataList[lenData-i].Value),
- })
- }
- return
- }
- type TableDataConfig struct {
- EdbInfoIdList []int `description:"指标id列表,从左至右,从上到下的顺序"`
- Sort int `description:"日期排序,0:倒序,1:正序"`
- Data []ManualData `description:"数据列表"`
- Num int `description:"实际数据需要列出来的期数"`
- RemoveDate []string `description:"不展示的日期"`
- ManualDate []string `description:"手动配置的日期(未来的日期)"`
- TableEdbInfoList []TableEdbInfo `description:"表格内指标信息"`
- TextRowData [][]request.ManualDataReq `description:"文本列表"`
- }
- type TableEdbInfo struct {
- EdbInfoId int `description:"指标ID"`
- Tag string `description:"标签"`
- EdbName string `description:"指标名称"`
- EdbAliasName string `description:"指标别名"`
- Frequency string `description:"频度"`
- Unit string `description:"单位"`
- }
- type ManualData struct {
- DataType int `description:"数据类型,1:普通的,2:插值法,3:手动输入,4:公式计算"`
- DataTime string `description:"所属日期"`
- DataTimeType int `description:"日期类型,1:实际日期;2:未来日期"`
- ShowValue string `description:"展示值"`
- Value string `description:"实际值(计算公式)"`
- EdbInfoId int `description:"指标id"`
- Tag string `description:"下标"`
- RelationEdbInfoList []request.RelationEdbInfo `description:"关联指标(计算公式中关联的指标,用于计算的时候去匹配)"`
- }
- func GetTableDataConfig(reqData request.TableDataReq) (tableDataConfig TableDataConfig, err error) {
-
- tableDataConfig.EdbInfoIdList = reqData.EdbInfoIdList
- tableDataConfig.Sort = reqData.Sort
- if len(reqData.Data) <= 0 {
- err = errors.New("数据不能为空")
- return
- }
-
- var startDate string
-
- var firstEdbInfoId int
-
- manualDataList := make([]ManualData, 0)
-
- tableEdbInfoList := make([]TableEdbInfo, 0)
-
- firstDateMap := make(map[string]string)
- manualDateMap := make(map[string]string)
- for _, v := range reqData.Data {
-
- tmpTableEdbInfo := TableEdbInfo{
- EdbInfoId: v.EdbInfoId,
- Tag: v.Tag,
- EdbName: v.EdbName,
- EdbAliasName: v.EdbAliasName,
- Frequency: v.Frequency,
- Unit: v.Unit,
- }
- tableEdbInfoList = append(tableEdbInfoList, tmpTableEdbInfo)
-
- if v.Tag == "A" {
- firstEdbInfoId = v.EdbInfoId
- lenData := len(v.Data)
- if lenData <= 0 {
- err = errors.New("A列不能为空")
- return
- }
- index := 0
- if reqData.Sort == 1 {
-
- index = lenData - 1
- }
- startDate = v.Data[index].DataTime
-
- for _, data := range v.Data {
- firstDateMap[data.DataTime] = data.DataTime
- if data.DataTimeType == 2 {
- manualDateMap[data.DataTime] = data.DataTime
- }
- }
- }
- for _, data := range v.Data {
- if data.DataType == 3 || data.DataType == 4 {
- tmpManualData := ManualData{
- DataType: data.DataType,
- DataTime: data.DataTime,
- DataTimeType: data.DataTimeType,
- ShowValue: data.ShowValue,
- Value: data.Value,
- EdbInfoId: v.EdbInfoId,
- Tag: v.Tag,
- RelationEdbInfoList: data.RelationEdbInfoList,
- }
- if data.DataType == 4 {
- tmpManualData.ShowValue = ``
- }
- manualDataList = append(manualDataList, tmpManualData)
- }
- }
- }
-
- num := len(reqData.Data[0].Data)
- removeDate := make([]string, 0)
-
- {
- firstDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, startDate, time.Local)
- if tmpErr != nil {
- err = tmpErr
- return
- }
- edbInfo, tmpErr := data_manage.GetEdbInfoById(firstEdbInfoId)
- if tmpErr != nil {
- err = tmpErr
- return
- }
- var firstDataList []*data_manage.EdbDataList
- switch edbInfo.EdbInfoType {
- case 0:
- firstDataList, err = data_manage.GetEdbDataList(edbInfo.Source, edbInfo.SubSource, edbInfo.EdbInfoId, ``, ``)
- case 1:
- _, firstDataList, _, _, err, _ = data.GetPredictDataListByPredictEdbInfoId(edbInfo.EdbInfoId, ``, ``, false)
- default:
- err = errors.New(fmt.Sprint("获取失败,指标类型异常", edbInfo.EdbInfoType))
- }
- if err != nil {
- return
- }
-
- baseDataList := make([]*data_manage.EdbDataList, 0)
- for _, data := range firstDataList {
- tmpDate := data.DataTime
- tmpDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, tmpDate, time.Local)
- if tmpErr != nil {
- err = tmpErr
- return
- }
- if tmpDateTime.Before(firstDateTime) {
- continue
- }
- baseDataList = append(baseDataList, data)
- }
-
- for _, tmpData := range baseDataList {
-
- if _, ok := firstDateMap[tmpData.DataTime]; !ok {
- removeDate = append(removeDate, tmpData.DataTime)
- }
- }
- }
- tableDataConfig.Num = num
- tableDataConfig.RemoveDate = removeDate
- tableDataConfig.Data = manualDataList
- tableDataConfig.TableEdbInfoList = tableEdbInfoList
- tableDataConfig.TextRowData = reqData.TextRowData
- return
- }
- func GetDataByTableDataConfig(tableDataConfig TableDataConfig) (resultResp request.TableDataReq, err error) {
-
- if len(tableDataConfig.EdbInfoIdList) <= 0 {
- return
- }
-
- if tableDataConfig.Num <= 0 {
- return
- }
-
- edbInfoMap := make(map[int]*data_manage.EdbInfo)
- edbInfoIdList := make([]int, 0)
-
- tagEdbInfoIdMap := make(map[string]int)
- {
- for _, tableEdbInfo := range tableDataConfig.TableEdbInfoList {
- edbInfoIdList = append(edbInfoIdList, tableEdbInfo.EdbInfoId)
- tagEdbInfoIdMap[tableEdbInfo.Tag] = tableEdbInfo.EdbInfoId
- }
- edbInfoList, tmpErr := data_manage.GetEdbInfoByIdList(edbInfoIdList)
- if tmpErr != nil {
- err = tmpErr
- return
- }
- for _, v := range edbInfoList {
- edbInfoMap[v.EdbInfoId] = v
- }
- }
- manualDateMap := make(map[string]string, 0)
- manualDateList := make([]string, 0)
- for _, v := range tableDataConfig.Data {
- if _, ok := manualDateMap[v.DataTime]; !ok {
- manualDateMap[v.DataTime] = v.DataTime
- manualDateList = append(manualDateList, v.DataTime)
- }
- }
-
- firstEdbInfo, ok := edbInfoMap[tableDataConfig.TableEdbInfoList[0].EdbInfoId]
- if !ok {
- err = errors.New("找不到A列指标")
- return
- }
- baseFirstEdbInfoDataList, err := GetFirstEdbDataList(firstEdbInfo, tableDataConfig.Num, manualDateList)
- if err != nil {
- return
- }
-
- if len(baseFirstEdbInfoDataList) <= 0 {
- return
- }
- firstEdbInfoDataList := make([]request.ManualDataReq, 0)
- if tableDataConfig.RemoveDate != nil && len(tableDataConfig.RemoveDate) > 0 {
- for _, v := range baseFirstEdbInfoDataList {
- if utils.InArrayByStr(tableDataConfig.RemoveDate, v.DataTime) {
- continue
- }
- firstEdbInfoDataList = append(firstEdbInfoDataList, v)
- }
- } else {
- firstEdbInfoDataList = baseFirstEdbInfoDataList
- }
- if len(firstEdbInfoDataList) <= 0 {
- return
- }
-
- lastRealDateTime, err := time.ParseInLocation(utils.FormatDate, firstEdbInfoDataList[0].DataTime, time.Local)
- if err != nil {
- return
- }
- dateMap := make(map[string]string)
- dateList := make([]string, 0)
- edbInfoIdDateDataMap := make(map[int]map[string]request.ManualDataReq)
- firstDateDataMap := make(map[string]request.ManualDataReq)
- for _, v := range firstEdbInfoDataList {
- dateList = append(dateList, v.DataTime)
- dateMap[v.DataTime] = v.DataTime
- firstDateDataMap[v.DataTime] = v
- }
-
- for _, manualData := range tableDataConfig.Data {
- if !utils.InArrayByStr(dateList, manualData.DataTime) {
- tmpDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, manualData.DataTime, time.Local)
- if tmpErr != nil {
- err = tmpErr
- return
- }
- if tmpDateTime.After(lastRealDateTime) {
- dateList = append(dateList, manualData.DataTime)
- }
- }
- }
- edbInfoIdDateDataMap[firstEdbInfo.EdbInfoId] = firstDateDataMap
- for k, edbInfoId := range tableDataConfig.EdbInfoIdList {
- if k == 0 {
- continue
- }
- tmpEdbInfo, ok := edbInfoMap[edbInfoId]
- if !ok {
- err = errors.New("找不到A列指标")
- return
- }
- otherDataList, tmpErr := GetOtherEdbDataList(tmpEdbInfo, dateList)
- if tmpErr != nil {
- err = tmpErr
- return
- }
- tmpDateDataMap := make(map[string]request.ManualDataReq)
- for _, v := range otherDataList {
- tmpDateDataMap[v.DataTime] = v
- }
- edbInfoIdDateDataMap[tmpEdbInfo.EdbInfoId] = tmpDateDataMap
- }
- for _, v := range tableDataConfig.Data {
- tmpDate := v.DataTime
- if _, ok := dateMap[tmpDate]; !ok {
- dateMap[v.DataTime] = tmpDate
- }
- edbInfoIdDateData, ok := edbInfoIdDateDataMap[v.EdbInfoId]
- if !ok {
- edbInfoIdDateData = make(map[string]request.ManualDataReq)
- }
-
- tmpManualData, ok := edbInfoIdDateData[tmpDate]
- if !ok {
- edbInfoIdDateData[tmpDate] = request.ManualDataReq{
- DataType: v.DataType,
- DataTime: v.DataTime,
- ShowValue: v.ShowValue,
- Value: v.Value,
- }
- } else {
- if (tmpManualData.DataType == 3 || tmpManualData.DataType == 4) && tmpManualData.ShowValue == `` {
- tmpManualData.DataType = v.DataType
- tmpManualData.ShowValue = v.ShowValue
- tmpManualData.Value = v.Value
- tmpManualData.RelationEdbInfoList = v.RelationEdbInfoList
- edbInfoIdDateData[tmpDate] = tmpManualData
- }
- }
- edbInfoIdDateDataMap[v.EdbInfoId] = edbInfoIdDateData
- }
-
- sortDateTimeList := make([]time.Time, 0)
- {
- sortDateList := dateList
- if tableDataConfig.Sort == 1 {
- baseDateList := utils.StrArr{}
- baseDateList = append(baseDateList, sortDateList...)
- sort.Sort(baseDateList)
- sortDateList = append([]string{}, baseDateList...)
- } else {
- sort.Strings(sortDateList)
- }
- for _, v := range sortDateList {
- tmpDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, v, time.Local)
- if tmpErr != nil {
- err = tmpErr
- return
- }
- sortDateTimeList = append(sortDateTimeList, tmpDateTime)
- }
- }
-
- tableDataMap, textRowListDataResp := handleTable(tagEdbInfoIdMap, lastRealDateTime, sortDateTimeList, edbInfoIdDateDataMap, tableDataConfig.Data, tableDataConfig.TextRowData)
- data := make([]request.EdbInfoData, 0)
- for _, tableEdbInfo := range tableDataConfig.TableEdbInfoList {
- tagEdbInfoIdMap[tableEdbInfo.Tag] = tableEdbInfo.EdbInfoId
- manualDataReqList := make([]request.ManualDataReq, 0)
- tmpEdbInfoData := request.EdbInfoData{
- EdbInfoId: tableEdbInfo.EdbInfoId,
- Tag: tableEdbInfo.Tag,
- EdbName: tableEdbInfo.EdbName,
- EdbAliasName: tableEdbInfo.EdbAliasName,
- Frequency: tableEdbInfo.Frequency,
- Unit: tableEdbInfo.Unit,
- Data: manualDataReqList,
- }
- edbInfo, ok := edbInfoMap[tableEdbInfo.EdbInfoId]
- if ok {
- tmpEdbInfoData.EdbName = edbInfo.EdbName
- tmpEdbInfoData.Frequency = edbInfo.Frequency
- tmpEdbInfoData.Unit = edbInfo.Unit
- }
- for index, dateTime := range sortDateTimeList {
- dataTimeType := 1
- if dateTime.After(lastRealDateTime) {
- dataTimeType = 2
- }
- tmpDateTimeStr := dateTime.Format(utils.FormatDate)
- rowData, ok := tableDataMap[index+1]
- if !ok {
- manualDataReqList = append(manualDataReqList, request.ManualDataReq{
- DataType: 3,
- DataTime: tmpDateTimeStr,
- DataTimeType: dataTimeType,
- ShowValue: "",
- Value: "",
- RelationEdbInfoList: nil,
- })
- continue
- }
- tmpData, ok := rowData[tableEdbInfo.Tag]
- if !ok {
- manualDataReqList = append(manualDataReqList, request.ManualDataReq{
- DataType: 3,
- DataTime: tmpDateTimeStr,
- DataTimeType: dataTimeType,
- ShowValue: "",
- Value: "",
- RelationEdbInfoList: nil,
- })
- continue
- }
- tmpData.DataTimeType = dataTimeType
- manualDataReqList = append(manualDataReqList, tmpData)
- }
- tmpEdbInfoData.Data = manualDataReqList
- data = append(data, tmpEdbInfoData)
- }
-
- for _, d := range data {
- for k2, d2 := range d.Data {
-
- vf, e := strconv.ParseFloat(d2.ShowValue, 64)
- if e != nil {
- continue
- }
- d.Data[k2].ShowValue = utils.FormatTableDataShowValue(vf)
- }
- }
- for _, d := range textRowListDataResp {
- for k2, d2 := range d {
-
- vf, e := strconv.ParseFloat(d2.ShowValue, 64)
- if e != nil {
- continue
- }
- d[k2].ShowValue = utils.FormatTableDataShowValue(vf)
- }
- }
- resultResp = request.TableDataReq{
- EdbInfoIdList: edbInfoIdList,
- Sort: tableDataConfig.Sort,
- TextRowData: textRowListDataResp,
- Data: data,
- }
- return
- }
- func handleTable(tagEdbInfoIdMap map[string]int, lastRealDateTime time.Time, sortDateTimeList []time.Time, edbInfoIdDateDataMap map[int]map[string]request.ManualDataReq, manualDataList []ManualData, textRowData [][]request.ManualDataReq) (tableDataMap map[int]map[string]request.ManualDataReq, textRowListDataResp [][]request.ManualDataReq) {
- tagList := make([]string, 0)
- for tag, _ := range tagEdbInfoIdMap {
- tagList = append(tagList, tag)
- }
- sort.Strings(tagList)
- tableDataMap = make(map[int]map[string]request.ManualDataReq)
-
- dateIndexMap := make(map[string]int)
- for k, dateTime := range sortDateTimeList {
- rowDataMap := make(map[string]request.ManualDataReq)
- dataTimeType := 1
- if dateTime.After(lastRealDateTime) {
- dataTimeType = 2
- }
- tmpDateTimeStr := dateTime.Format(utils.FormatDate)
- dateIndexMap[tmpDateTimeStr] = k + 1
- for _, tag := range tagList {
- edbInfoId, ok := tagEdbInfoIdMap[tag]
- if !ok {
- rowDataMap[tag] = request.ManualDataReq{
- DataType: 3,
- DataTime: tmpDateTimeStr,
- DataTimeType: dataTimeType,
- ShowValue: "",
- Value: "",
- RelationEdbInfoList: nil,
- }
- continue
- }
-
- dateDataMap, ok := edbInfoIdDateDataMap[edbInfoId]
- if !ok {
- rowDataMap[tag] = request.ManualDataReq{
- DataType: 3,
- DataTime: tmpDateTimeStr,
- DataTimeType: dataTimeType,
- ShowValue: "",
- Value: "",
- RelationEdbInfoList: nil,
- }
- continue
- }
-
- tmpData, ok := dateDataMap[tmpDateTimeStr]
- if !ok {
- rowDataMap[tag] = request.ManualDataReq{
- DataType: 3,
- DataTime: tmpDateTimeStr,
- DataTimeType: dataTimeType,
- ShowValue: "",
- Value: "",
- RelationEdbInfoList: nil,
- }
- continue
- }
- tmpData.DataTimeType = dataTimeType
- rowDataMap[tag] = tmpData
- }
- tableDataMap[k+1] = rowDataMap
- }
-
- for _, manualData := range manualDataList {
-
- index, ok := dateIndexMap[manualData.DataTime]
- if !ok {
- continue
- }
-
- rowDataMap, ok := tableDataMap[index]
- if !ok {
- continue
- }
-
- tmpData, ok := rowDataMap[manualData.Tag]
- if !ok {
- continue
- }
-
- if utils.InArrayByInt([]int{1, 2, 5}, tmpData.DataType) {
- continue
- }
-
- if tmpData.DataType == 3 {
- tmpData.ShowValue = manualData.ShowValue
- tmpData.Value = manualData.Value
- tableDataMap[index][manualData.Tag] = tmpData
-
- continue
- }
-
- tmpData.DataType = manualData.DataType
- tmpData.ShowValue = ``
- tmpData.Value = manualData.Value
- tmpData.RelationEdbInfoList = manualData.RelationEdbInfoList
- tableDataMap[index][manualData.Tag] = tmpData
- }
-
- lenTableData := len(tableDataMap)
-
- firstColTextRowList := make([]request.ManualDataReq, 0)
-
- tmpTextRowList := make([][]request.ManualDataReq, 0)
- for k, textRowList := range textRowData {
-
- if len(tagList)+1 != len(textRowList) {
- continue
- }
- rowDataMap := make(map[string]request.ManualDataReq)
- tmpTextRow := make([]request.ManualDataReq, 0)
- for index, textRow := range textRowList {
-
- if index == 0 {
- firstColTextRowList = append(firstColTextRowList, textRow)
- continue
- }
- rowDataMap[tagList[index-1]] = textRow
- tmpTextRow = append(tmpTextRow, textRow)
- }
- tableDataMap[lenTableData+k+1] = rowDataMap
- tmpTextRowList = append(tmpTextRowList, tmpTextRow)
- }
-
- calculateCellMap := make(map[string]string)
-
- for _, manualData := range manualDataList {
-
- index, ok := dateIndexMap[manualData.DataTime]
- if !ok {
- continue
- }
-
- rowDataMap, ok := tableDataMap[index]
- if !ok {
- continue
- }
-
- colData, ok := rowDataMap[manualData.Tag]
- if !ok {
- continue
- }
-
- if colData.DataType != 4 {
- continue
- }
- tagMap := make(map[string]float64)
- lenRelation := len(colData.RelationEdbInfoList)
- replaceNum := 0
- for _, relation := range colData.RelationEdbInfoList {
- relationCellTagName := strings.ToUpper(relation.Tag) + relation.Row
- valStr, tmpErr := getCalculateValue(tableDataMap, relation.Tag, relation.Row, calculateCellMap)
- if tmpErr != nil {
- continue
- }
- tmpValDecimal, tmpErr := decimal.NewFromString(valStr)
- if tmpErr != nil {
- continue
- }
- tagMap[relationCellTagName], _ = tmpValDecimal.Float64()
- replaceNum++
- }
-
- if lenRelation != replaceNum {
- continue
- }
-
- val, _, err := calculate(strings.ToUpper(colData.Value), tagMap)
-
- if err != nil {
- continue
- }
-
- colData.ShowValue = val
- tableDataMap[index][manualData.Tag] = colData
- }
-
- for k, textRow := range tmpTextRowList {
-
- index := lenTableData + k + 1
- rowDataMap, ok := tableDataMap[index]
- if !ok {
- continue
- }
- for colIndex, _ := range textRow {
- currTag := tagList[colIndex]
-
- colData, ok := rowDataMap[currTag]
- if !ok {
- continue
- }
-
- if colData.DataType != 4 {
- continue
- }
- tagMap := make(map[string]float64)
- lenRelation := len(colData.RelationEdbInfoList)
- replaceNum := 0
- for _, relation := range colData.RelationEdbInfoList {
- relationCellTagName := strings.ToUpper(relation.Tag) + relation.Row
- valStr, tmpErr := getCalculateValue(tableDataMap, relation.Tag, relation.Row, calculateCellMap)
- if tmpErr != nil {
- continue
- }
- tmpValDecimal, tmpErr := decimal.NewFromString(valStr)
- if tmpErr != nil {
- continue
- }
- tagMap[relationCellTagName], _ = tmpValDecimal.Float64()
- replaceNum++
- }
-
- if lenRelation != replaceNum {
- continue
- }
-
- val, _, err := calculate(strings.ToUpper(colData.Value), tagMap)
-
- if err != nil {
- continue
- }
-
- colData.ShowValue = val
- tableDataMap[index][currTag] = colData
- }
- }
-
- for k, colData := range firstColTextRowList {
-
- if colData.DataType != 4 {
- continue
- }
- tagMap := make(map[string]float64)
- lenRelation := len(colData.RelationEdbInfoList)
- replaceNum := 0
- for _, relation := range colData.RelationEdbInfoList {
- relationCellTagName := strings.ToUpper(relation.Tag) + relation.Row
- valStr, tmpErr := getCalculateValue(tableDataMap, relation.Tag, relation.Row, calculateCellMap)
- if tmpErr != nil {
- continue
- }
- tmpValDecimal, tmpErr := decimal.NewFromString(valStr)
- if tmpErr != nil {
- continue
- }
- tagMap[relationCellTagName], _ = tmpValDecimal.Float64()
- replaceNum++
- }
-
- if lenRelation != replaceNum {
- continue
- }
-
- val, _, err := calculate(strings.ToUpper(colData.Value), tagMap)
-
- if err != nil {
- continue
- }
-
- colData.ShowValue = val
- firstColTextRowList[k] = colData
- }
- {
-
- textRowListDataResp = make([][]request.ManualDataReq, 0)
- newLenTableDataMap := len(tableDataMap)
-
- firstTextRow := lenTableData + 1
- for i := firstTextRow; i <= newLenTableDataMap; i++ {
- textRowDataResp := make([]request.ManualDataReq, 0)
- textRowDataResp = append(textRowDataResp, firstColTextRowList[i-firstTextRow])
- for _, tmpTag := range tagList {
- textRowDataResp = append(textRowDataResp, tableDataMap[i][tmpTag])
- }
- textRowListDataResp = append(textRowListDataResp, textRowDataResp)
- }
- }
- return
- }
- func getCalculateValue(tableDataMap map[int]map[string]request.ManualDataReq, tag, row string, calculateCellMap map[string]string) (val string, err error) {
- rowInt, err := strconv.Atoi(row)
- if err != nil {
- return
- }
-
- cellTagName := strings.ToUpper(tag) + row
- val, ok := calculateCellMap[cellTagName]
- if ok {
- return
- }
-
- rowData, ok := tableDataMap[rowInt]
- if !ok {
- err = errors.New("查找" + row + "行的数据失败")
- return
- }
-
- colData, ok := rowData[tag]
- if !ok {
- err = errors.New("查找单元格" + tag + row + "的数据失败")
- return
- }
-
- if colData.DataType != 4 {
- val = colData.ShowValue
- return
- }
-
- calculateCellMap[cellTagName] = ``
- tagMap := make(map[string]float64)
- for _, relation := range colData.RelationEdbInfoList {
- relationCellTagName := strings.ToUpper(relation.Tag) + relation.Row
- valStr, tmpErr := getCalculateValue(tableDataMap, relation.Tag, relation.Row, calculateCellMap)
- if tmpErr != nil {
- err = tmpErr
- return
- }
- tmpValDecimal, tmpErr := decimal.NewFromString(valStr)
- if tmpErr != nil {
- err = tmpErr
- return
- }
- tagMap[relationCellTagName], _ = tmpValDecimal.Float64()
- }
-
- val, _, err = calculate(strings.ToUpper(colData.Value), tagMap)
- if err != nil {
- return
- }
-
- colData.ShowValue = val
- tableDataMap[rowInt][tag] = colData
- calculateCellMap[cellTagName] = val
- return
- }
- func calculate(calculateFormula string, TagMap map[string]float64) (calVal, errMsg string, err error) {
- if calculateFormula == "" {
- errMsg = "公式异常"
- err = errors.New(errMsg)
- return
- }
- calculateFormula = strings.TrimPrefix(calculateFormula, "=")
- calculateFormula = strings.Replace(calculateFormula, "(", "(", -1)
- calculateFormula = strings.Replace(calculateFormula, ")", ")", -1)
- calculateFormula = strings.Replace(calculateFormula, ",", ",", -1)
- calculateFormula = strings.Replace(calculateFormula, "。", ".", -1)
- calculateFormula = strings.Replace(calculateFormula, "%", "*0.01", -1)
- formulaFormStr := utils.ReplaceFormula(TagMap, calculateFormula)
-
- if formulaFormStr == `` {
- errMsg = "公式异常"
- err = errors.New(errMsg)
- return
- }
- expression := formula.NewExpression(formulaFormStr)
- calResult, err := expression.Evaluate()
- if err != nil {
- errMsg = "计算失败"
- err = errors.New("计算失败:Err:" + err.Error() + ";formulaStr:" + formulaFormStr)
-
- if strings.Contains(err.Error(), "divide by zero") {
- errMsg = "分母不能为0"
- err = errors.New("分母不能为空,计算公式:" + formulaFormStr)
- }
- return
- }
-
- if calResult.IsNan() {
- errMsg = "计算失败"
- err = errors.New("计算失败:计算结果是:NAN;formulaStr:" + formulaFormStr)
- return
- }
- calVal = calResult.String()
-
- valDecimal, err := decimal.NewFromString(calVal)
- if err != nil {
- errMsg = "计算失败"
- err = errors.New("计算失败,结果转 Decimal 失败:Err:" + err.Error() + ";formulaStr:" + formulaFormStr)
- return
- }
- calVal = valDecimal.Round(4).String()
- return
- }
|