123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317 |
- package excel
- import (
- "encoding/json"
- "errors"
- "eta_gn/eta_chart_lib/models"
- "eta_gn/eta_chart_lib/models/data_manage"
- "eta_gn/eta_chart_lib/models/data_manage/excel"
- "eta_gn/eta_chart_lib/models/request"
- "eta_gn/eta_chart_lib/services/data"
- "eta_gn/eta_chart_lib/utils"
- "fmt"
- "sort"
- "time"
- )
- func GetFirstEdbDataList(edbInfo *data_manage.EdbInfo, num int, manualDateList []string) (resultDataList []request.ManualDataReq, err error) {
- var dataList []*models.EdbDataList
- switch edbInfo.EdbInfoType {
- case 0:
- dataList, err = models.GetEdbDataList(edbInfo.Source, edbInfo.SubSource, edbInfo.EdbInfoId, ``, ``)
- case 1:
- _, dataList, _, _, err, _ = data.GetPredictDataListByPredictEdbInfoId(edbInfo.EdbInfoId, ``, ``, false)
- default:
- err = errors.New(fmt.Sprint("获取失败,指标类型异常", edbInfo.EdbInfoType))
- 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 []*models.EdbDataList
- switch edbInfo.EdbInfoType {
- case 0:
- dataList, err = models.GetEdbDataList(edbInfo.Source, edbInfo.SubSource, edbInfo.EdbInfoId, ``, ``)
- case 1:
- _, dataList, _, _, err, _ = data.GetPredictDataListByPredictEdbInfoId(edbInfo.EdbInfoId, ``, ``, false)
- default:
- err = errors.New(fmt.Sprint("获取失败,指标类型异常", edbInfo.EdbInfoType))
- return
- }
- baseDataList := make([]*models.EdbDataList, 0)
- var lastData *models.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 []*models.EdbDataList
- switch edbInfo.EdbInfoType {
- case 0:
- dataList, err = models.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))
- 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
- }
- func GetExcelDetailInfoByExcelInfoId(uniqueCode, lang string) (excelDetail excel.ExcelInfoDetail, errMsg string, err error) {
- errMsg = `获取失败`
- excelInfo, err := excel.GetExcelInfoByUniqueCode(uniqueCode)
- if err != nil {
- err = errors.New("获取ETA表格信息失败,Err:" + err.Error())
- if utils.IsErrNoRow(err) {
- errMsg = "ETA表格被删除,请刷新页面"
- err = errors.New("ETA表格被删除,请刷新页面,Err:" + err.Error())
- }
- return
- }
- return formatExcelInfo2Detail(excelInfo, lang)
- }
- func formatExcelInfo2Detail(excelInfo *excel.ExcelInfo, lang string) (excelDetail excel.ExcelInfoDetail, errMsg string, err error) {
- excelDetail = excel.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, lang)
- if tmpErr != nil {
- errMsg = "获取失败"
- if tmpErrMsg != `` {
- errMsg = tmpErrMsg
- }
- err = errors.New("获取最新的数据失败,Err:" + tmpErr.Error())
- return
- }
- result.Data = newData
- excelDetail.TableData = result
- }
- return
- }
|