package supply_analysis import ( "errors" "fmt" "hongze/hz_crm_api/models/data_manage" "hongze/hz_crm_api/models/data_manage/supply_analysis" "hongze/hz_crm_api/utils" "time" ) // GetCustomEdbData 通过品种id获取自定义ETA指标库格式的数据列表 func GetCustomEdbData(varietyEdbId int, startDate, endDate string) (dataList []*data_manage.EdbDataList, minVal, maxVal float64, err error) { // 根据品种指标id获取所有的数据 tmpVarietyDataList, err := supply_analysis.GetVarietyEdbData(varietyEdbId, startDate, endDate) if err != nil { return } dataList = make([]*data_manage.EdbDataList, 0) for _, v := range tmpVarietyDataList { dataList = append(dataList, &data_manage.EdbDataList{ EdbDataId: v.StockPlantDataId, EdbInfoId: v.VarietyEdbId, DataTime: v.DataTime.Format(utils.FormatDate), DataTimestamp: v.DataTime.UnixNano() / 1e6, Value: v.Value, }) if v.Value < minVal { minVal = v.Value } if v.Value > maxVal { maxVal = v.Value } } return } // GetChartEdbSeasonalData 获取指标的季节性数据 func GetChartEdbSeasonalData(varietyEdbId int, calendar, startDate, endDate, latestDateStr string) (resultData interface{}, minVal, maxVal float64, err error) { var startDateReal string startDateReal = startDate calendarPreYear := 0 if calendar == "农历" { newStartDateReal, tmpErr := time.Parse(utils.FormatDate, startDateReal) if tmpErr != nil { err = tmpErr fmt.Println("time.Parse:" + err.Error()) return } calendarPreYear = newStartDateReal.Year() - 1 newStartDateReal = newStartDateReal.AddDate(-1, 0, 0) startDateReal = newStartDateReal.Format(utils.FormatDate) } dataList, minVal, maxVal, err := GetCustomEdbData(varietyEdbId, startDateReal, endDate) if err != nil { return } //latestDateStr := edbInfo.LatestDate //实际数据的截止日期 latestDate, tmpErr := time.Parse(utils.FormatDate, latestDateStr) if tmpErr != nil { err = errors.New(fmt.Sprint("获取最后实际数据的日期失败,Err:" + tmpErr.Error() + ";LatestDate:" + latestDateStr)) return } latestDateYear := latestDate.Year() //实际数据截止年份 if calendar == "农历" { if len(dataList) <= 0 { resultData = new(data_manage.EdbDataResult) } else { result, tmpErr := data_manage.AddCalculateQuarterV4(dataList) if tmpErr != nil { err = errors.New("获取农历数据失败,Err:" + tmpErr.Error()) return } // 处理季节图的截止日期 for k, edbDataItems := range result.List { var cuttingDataTimestamp int64 // 切割的日期时间字符串 cuttingDataTimeStr := latestDate.AddDate(0, 0, edbDataItems.BetweenDay).Format(utils.FormatDate) //如果等于最后的实际日期,那么遍历找到该日期对应的时间戳,并将其赋值为 切割时间戳 if edbDataItems.Year >= latestDateYear { for _, tmpData := range edbDataItems.Items { if tmpData.DataTime == cuttingDataTimeStr { cuttingDataTimestamp = tmpData.DataTimestamp break } } } edbDataItems.CuttingDataTimestamp = cuttingDataTimestamp result.List[k] = edbDataItems } if result.List[0].Year != calendarPreYear { itemList := make([]*data_manage.EdbDataList, 0) items := new(data_manage.EdbDataItems) //items.Year = calendarPreYear items.Items = itemList newResult := new(data_manage.EdbDataResult) newResult.List = append(newResult.List, items) newResult.List = append(newResult.List, result.List...) resultData = newResult } else { resultData = result } } } else { currentYear := time.Now().Year() quarterDataList := make([]*data_manage.QuarterData, 0) quarterMap := make(map[int][]*data_manage.EdbDataList) var quarterArr []int for _, v := range dataList { itemDate, tmpErr := time.Parse(utils.FormatDate, v.DataTime) if tmpErr != nil { err = errors.New("季度指标日期转换,Err:" + tmpErr.Error() + ";DataTime:" + v.DataTime) return } year := itemDate.Year() newItemDate := itemDate.AddDate(currentYear-year, 0, 0) timestamp := newItemDate.UnixNano() / 1e6 v.DataTimestamp = timestamp if findVal, ok := quarterMap[year]; !ok { quarterArr = append(quarterArr, year) findVal = append(findVal, v) quarterMap[year] = findVal } else { findVal = append(findVal, v) quarterMap[year] = findVal } } for _, v := range quarterArr { itemList := quarterMap[v] quarterItem := new(data_manage.QuarterData) quarterItem.Year = v quarterItem.DataList = itemList //如果等于最后的实际日期,那么将切割时间戳记录 if v == latestDateYear { var cuttingDataTimestamp int64 for _, tmpData := range itemList { if tmpData.DataTime == latestDateStr { cuttingDataTimestamp = tmpData.DataTimestamp break } } quarterItem.CuttingDataTimestamp = cuttingDataTimestamp } else if v > latestDateYear { //如果大于最后的实际日期,那么第一个点就是切割的时间戳 if len(itemList) > 0 { quarterItem.CuttingDataTimestamp = itemList[0].DataTimestamp - 100 } } quarterDataList = append(quarterDataList, quarterItem) } resultData = quarterDataList } return }