|
@@ -0,0 +1,235 @@
|
|
|
+package excel
|
|
|
+
|
|
|
+import (
|
|
|
+ "encoding/json"
|
|
|
+ "errors"
|
|
|
+ "eta/eta_api/models/data_manage"
|
|
|
+ "eta/eta_api/models/data_manage/excel/request"
|
|
|
+ "eta/eta_api/services/data"
|
|
|
+ "eta/eta_api/utils"
|
|
|
+ "fmt"
|
|
|
+ "strings"
|
|
|
+ "time"
|
|
|
+)
|
|
|
+
|
|
|
+// GetMixedTableCellData 获取混合表格数据
|
|
|
+func GetMixedTableCellData(cellRelationConf string, config [][]request.MixedTableCellDataReq) (newMixedTableCellDataList [][]request.MixedTableCellDataReq, err error) {
|
|
|
+ newMixedTableCellDataList = config
|
|
|
+
|
|
|
+ // 单元格关系配置x信息
|
|
|
+ cellRelationConfMap := make(map[string]request.CellRelationConf)
|
|
|
+ cellRelationConfList := make([]request.CellRelationConf, 0)
|
|
|
+ if cellRelationConf != `` {
|
|
|
+ err = json.Unmarshal([]byte(cellRelationConf), &cellRelationConfList)
|
|
|
+ if err != nil {
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ for _, v := range cellRelationConfList {
|
|
|
+ cellRelationConfMap[v.Key] = v
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 找出所有的关联指标id
|
|
|
+ config, edbInfoIdList, _ := handleConfig(config)
|
|
|
+
|
|
|
+ edbInfoList, err := data_manage.GetEdbInfoByIdList(edbInfoIdList)
|
|
|
+ if err != nil {
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ // 指标信息map
|
|
|
+ edbInfoMap := make(map[int]*data_manage.EdbInfo)
|
|
|
+ // 日度指标数据map
|
|
|
+ edbDataListMap := make(map[int]map[string]float64)
|
|
|
+ // 月度指标数据map
|
|
|
+ edbMonthDataListMap := make(map[int]map[string]float64)
|
|
|
+ for _, edbInfo := range edbInfoList {
|
|
|
+ edbInfoMap[edbInfo.EdbInfoId] = edbInfo
|
|
|
+
|
|
|
+ dataList := make([]*data_manage.EdbDataList, 0)
|
|
|
+ switch edbInfo.EdbInfoType {
|
|
|
+ case 0:
|
|
|
+ dataList, _ = data_manage.GetEdbDataList(edbInfo.Source, edbInfo.EdbInfoId, ``, ``)
|
|
|
+ case 1:
|
|
|
+ _, dataList, _, _, _, _ = data.GetPredictDataListByPredictEdbInfoId(edbInfo.EdbInfoId, ``, ``, false)
|
|
|
+ default:
|
|
|
+ err = errors.New(fmt.Sprint("获取失败,指标类型异常", edbInfo.EdbInfoType))
|
|
|
+ }
|
|
|
+
|
|
|
+ dateValMap := make(map[string]float64)
|
|
|
+ monthValMap := make(map[string]float64)
|
|
|
+ for _, data := range dataList {
|
|
|
+ // 日度数据
|
|
|
+ dateValMap[data.DataTime] = data.Value
|
|
|
+ // 月度数据(取该月份的第一个数据)
|
|
|
+ yearMonth := strings.Join(strings.Split(data.DataTime, "-")[0:2], "-")
|
|
|
+ if _, ok := monthValMap[yearMonth]; !ok {
|
|
|
+ monthValMap[yearMonth] = data.Value
|
|
|
+ }
|
|
|
+ }
|
|
|
+ edbDataListMap[edbInfo.EdbInfoId] = dateValMap
|
|
|
+ edbMonthDataListMap[edbInfo.EdbInfoId] = monthValMap
|
|
|
+ }
|
|
|
+
|
|
|
+ for k, row := range newMixedTableCellDataList {
|
|
|
+ for i, cell := range row {
|
|
|
+ if cell.DataType == request.EdbDT {
|
|
|
+ if edbInfo, ok := edbInfoMap[cell.EdbInfoId]; ok {
|
|
|
+ cell.ShowValue = edbInfo.EdbName
|
|
|
+ }
|
|
|
+ } else if utils.InArrayByInt([]int{request.InsertDataDT, request.PopInsertDataDT}, cell.DataType) {
|
|
|
+ tmpDateList := strings.Split(cell.DataTime, "-")
|
|
|
+ tmpDateValMap := make(map[string]float64)
|
|
|
+ if len(tmpDateList) == 2 {
|
|
|
+ //月度数据
|
|
|
+ if dateValMap, ok := edbMonthDataListMap[cell.EdbInfoId]; ok {
|
|
|
+ tmpDateValMap = dateValMap
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ // 日度数据
|
|
|
+ if dateValMap, ok := edbDataListMap[cell.EdbInfoId]; ok {
|
|
|
+ tmpDateValMap = dateValMap
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+ if val, ok2 := tmpDateValMap[cell.DataTime]; ok2 {
|
|
|
+ //cell.ShowValue = fmt.Sprint(val)
|
|
|
+ cell.ShowValue = utils.FormatTableDataShowValue(val)
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ row[i] = cell
|
|
|
+ }
|
|
|
+ newMixedTableCellDataList[k] = row
|
|
|
+ }
|
|
|
+
|
|
|
+ return
|
|
|
+}
|
|
|
+
|
|
|
+func handleConfig(config [][]request.MixedTableCellDataReq) ([][]request.MixedTableCellDataReq, []int, []int) {
|
|
|
+ edbInfoIdList := make([]int, 0)
|
|
|
+ dataEdbInfoIdList := make([]int, 0)
|
|
|
+ for _, row := range config {
|
|
|
+ for _, cell := range row {
|
|
|
+ switch cell.DataType {
|
|
|
+ case request.EdbDT: // 指标信息
|
|
|
+ edbInfoIdList = append(edbInfoIdList, cell.EdbInfoId)
|
|
|
+ case request.InsertDataDT, request.PopInsertDataDT: // 插值、弹框插值
|
|
|
+ dataEdbInfoIdList = append(dataEdbInfoIdList, cell.EdbInfoId)
|
|
|
+ case request.DateDT: // 日期类型
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return config, edbInfoIdList, dataEdbInfoIdList
|
|
|
+}
|
|
|
+
|
|
|
+func HandleDate(dataTimeType int, val string) (date string, err error, errMsg string) {
|
|
|
+ return handleDate(dataTimeType, val)
|
|
|
+}
|
|
|
+
|
|
|
+func handleDate(dataTimeType int, val string) (date string, err error, errMsg string) {
|
|
|
+ if val == `` {
|
|
|
+ errMsg = "错误的日期数据"
|
|
|
+ err = errors.New(errMsg)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ switch dataTimeType {
|
|
|
+ case request.CustomDateT: //手动输入日期
|
|
|
+ date = val
|
|
|
+ case request.SystemDateT: // 系统日期
|
|
|
+ date = time.Now().Format(utils.FormatDate)
|
|
|
+ case request.SystemCalculateDateT: // 系统日期计算后的日期
|
|
|
+ date, err, errMsg = handleSystemCalculateDateT(val)
|
|
|
+ case request.SystemAppointDateT: // 处理系统日期相关的指定频率(所在周/旬/月/季/半年/年的最后/最早一天)
|
|
|
+ date, err, errMsg = handleSystemAppointDateT(val)
|
|
|
+ case request.EdbDateDT: // 导入指标日期(指标库的最新日期)
|
|
|
+ default:
|
|
|
+ errMsg = "错误的日期类型"
|
|
|
+ err = errors.New(errMsg)
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ return
|
|
|
+}
|
|
|
+
|
|
|
+// handleSystemCalculateDateT
|
|
|
+// @Description: 处理系统日期计算后的日期
|
|
|
+// @author: Roc
|
|
|
+// @datetime2023-10-26 11:12:56
|
|
|
+// @param val string
|
|
|
+// @return date string
|
|
|
+// @return err error
|
|
|
+// @return errMsg string
|
|
|
+func handleSystemCalculateDateT(val string) (date string, err error, errMsg string) {
|
|
|
+ type SystemCalculateVal struct {
|
|
|
+ Num int
|
|
|
+ Frequency string
|
|
|
+ }
|
|
|
+ var config SystemCalculateVal
|
|
|
+ err = json.Unmarshal([]byte(val), &config)
|
|
|
+ if err != nil {
|
|
|
+ return
|
|
|
+ }
|
|
|
+ currDate := time.Now()
|
|
|
+ switch config.Frequency {
|
|
|
+ case "", "日":
|
|
|
+ date = currDate.AddDate(0, 0, config.Num).Format(utils.FormatDate)
|
|
|
+ default:
|
|
|
+ errMsg = "错误的日期频度:" + config.Frequency
|
|
|
+ err = errors.New(errMsg)
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ return
|
|
|
+}
|
|
|
+
|
|
|
+// handleSystemAppointDateT
|
|
|
+// @Description: 处理系统日期相关的指定频率(所在周/旬/月/季/半年/年的最后/最早一天)
|
|
|
+// @author: Roc
|
|
|
+// @datetime2023-10-26 11:13:52
|
|
|
+// @param val string
|
|
|
+// @return date string
|
|
|
+// @return err error
|
|
|
+// @return errMsg string
|
|
|
+func handleSystemAppointDateT(val string) (date string, err error, errMsg string) {
|
|
|
+ type SystemCalculateVal struct {
|
|
|
+ Frequency string
|
|
|
+ Day string
|
|
|
+ }
|
|
|
+ var config SystemCalculateVal
|
|
|
+ err = json.Unmarshal([]byte(val), &config)
|
|
|
+ if err != nil {
|
|
|
+ return
|
|
|
+ }
|
|
|
+ currDate := time.Now()
|
|
|
+ switch config.Frequency {
|
|
|
+ case "本周":
|
|
|
+ day := currDate.Day()
|
|
|
+ num := 0
|
|
|
+ switch config.Day {
|
|
|
+ case "周一":
|
|
|
+ num = 1
|
|
|
+ case "周二":
|
|
|
+ num = 2
|
|
|
+ case "周三":
|
|
|
+ num = 3
|
|
|
+ case "周四":
|
|
|
+ num = 4
|
|
|
+ case "周五":
|
|
|
+ num = 5
|
|
|
+ case "周六":
|
|
|
+ num = 6
|
|
|
+ case "周日":
|
|
|
+ num = 0
|
|
|
+ }
|
|
|
+ day = day - num
|
|
|
+ default:
|
|
|
+ errMsg = "错误的日期频度:" + config.Frequency
|
|
|
+ err = errors.New(errMsg)
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ return
|
|
|
+}
|