package controllers import ( "encoding/json" "fmt" "hongze/hongze_chart_lib/models" "hongze/hongze_chart_lib/services/data" "hongze/hongze_chart_lib/utils" "strconv" "time" ) //图表 type ChartController struct { BaseAuthController } // ChartInfoDetail // @Title 获取图表详情 // @Description 获取图表详情接口 // @Param UniqueCode query string true "图表唯一编码,如果是管理后台访问,传固定字符串:7c69b590249049942070ae9dcd5bf6dc" // @Success 200 {object} data_manage.ChartInfoDetailResp // @router /detail [get] func (this *ChartController) ChartInfoDetail() { br := new(models.BaseResponse).Init() defer func() { this.Data["json"] = br this.ServeJSON() }() uniqueCode := this.GetString("UniqueCode") if uniqueCode == "" { br.Msg = "参数错误" br.ErrMsg = "参数错误,uniqueCode is empty" return } key := utils.HZ_CHART_LIB_DETAIL + uniqueCode resp := new(models.ChartInfoDetailResp) //判断是否有缓存 if utils.Re == nil { if utils.Re == nil && utils.Rc.IsExist(key) { if data, err1 := utils.Rc.RedisBytes(key); err1 == nil { err := json.Unmarshal(data, &resp) if err == nil && resp != nil { br.Ret = 200 br.Success = true br.Msg = "获取成功" br.Data = resp fmt.Println("source redis") return } } } } chartInfo, err := models.GetChartInfoByUniqueCode(uniqueCode) if err != nil { if err.Error() == utils.ErrNoRow() { br.Msg = "该图已被删除,请刷新页面" br.ErrMsg = "该图已被删除,请刷新页面,Err:" + err.Error() return } br.Msg = "获取失败" br.ErrMsg = "获取图表信息失败,Err:" + err.Error() return } chartInfoId := chartInfo.ChartInfoId dateType := chartInfo.DateType if dateType <= 0 { dateType = 3 } startDate := chartInfo.StartDate endDate := chartInfo.EndDate seasonStartDate := chartInfo.SeasonStartDate seasonEndDate := chartInfo.SeasonEndDate calendar := chartInfo.Calendar chartType := chartInfo.ChartType if calendar == "" { calendar = "公历" } switch dateType { case 1: startDate = "2000-01-01" //endDate = time.Now().Format(utils.FormatDate) endDate = "" case 2: startDate = "2010-01-01" //endDate = time.Now().Format(utils.FormatDate) endDate = "" case 3: startDate = "2015-01-01" //endDate = time.Now().Format(utils.FormatDate) endDate = "" case 4: startDate = strconv.Itoa(time.Now().Year()) + "-01-01" startDate = "2021-01-01" //endDate = time.Now().Format(utils.FormatDate) endDate = "" case 5: startDate = startDate + "-01" endDate = endDate + "-01" case 6: startDate = startDate + "-01" //endDate = time.Now().Format(utils.FormatDate) endDate = "" case 7: startDate = "2018-01-01" //endDate = time.Now().Format(utils.FormatDate) endDate = "" case 8: startDate = "2019-01-01" //endDate = time.Now().Format(utils.FormatDate) endDate = "" case 9: startDate = "2020-01-01" //endDate = time.Now().Format(utils.FormatDate) endDate = "" case 11: startDate = "2022-01-01" endDate = "" } if chartType == 2 { if seasonStartDate != "" { startDate = seasonStartDate + "-01-01" } else { fivePre := time.Now().AddDate(-4, 0, 0).Year() startDate = strconv.Itoa(fivePre) + "-01-01" } if seasonEndDate != "" { endDate = seasonEndDate + "-12-31" } else { //endDate = time.Now().Format(utils.FormatDate) endDate = "" } } mappingList, err := models.GetChartEdbMappingList(chartInfoId) if err != nil { br.Msg = "获取失败" br.ErrMsg = "获取图表,指标信息失败,Err:" + err.Error() return } //edbList := make([]*models.ChartEdbInfoMapping, 0) //for _, v := range mappingList { // //minData, maxData, err := data_manage.GetEdbDataListMinAndMax(v.Source, v.EdbInfoId, startDate, endDate) // item := new(models.ChartEdbInfoMapping) // item.EdbInfoId = v.EdbInfoId // item.SourceName = v.SourceName // item.Source = v.Source // item.EdbCode = v.EdbCode // item.EdbName = v.EdbName // item.EdbNameEn = v.EdbNameEn // item.Frequency = v.Frequency // item.FrequencyEn = GetFrequencyEn(v.Frequency) // //无就是不展示 // if v.Unit != `无` { // item.Unit = v.Unit // } // item.UnitEn = v.UnitEn // item.StartDate = v.StartDate // item.EndDate = v.EndDate // //item.ModifyTime = v.ModifyTime // //item.ChartEdbMappingId = v.ChartEdbMappingId // item.ChartInfoId = v.ChartInfoId // item.MaxData = v.MaxData // item.MinData = v.MinData // item.IsOrder = v.IsOrder // item.IsAxis = v.IsAxis // item.EdbInfoType = v.EdbInfoType // item.EdbType = v.EdbType // item.LeadValue = v.LeadValue // item.LeadUnit = v.LeadUnit // item.LeadUnitEn = GetLeadUnitEn(v.LeadUnit) // item.ChartStyle = v.ChartStyle // item.ChartColor = v.ChartColor // item.ChartWidth = v.ChartWidth // // var startDateReal string // var diffSeconds int64 // if chartType == 2 { //季节性图表 // startDateReal = startDate // } else { // if v.EdbInfoType == 0 && v.LeadUnit != "" && v.LeadValue > 0 { //领先指标 // var startTimeRealTemp time.Time // startDateParse, _ := time.Parse(utils.FormatDate, startDate) // switch v.LeadUnit { // case "天": // startTimeRealTemp = startDateParse.AddDate(0, 0, -v.LeadValue) // case "月": // startTimeRealTemp = startDateParse.AddDate(0, -v.LeadValue, 0) // case "季": // startTimeRealTemp = startDateParse.AddDate(0, -3*v.LeadValue, 0) // case "周": // startTimeRealTemp = startDateParse.AddDate(0, 0, -7*v.LeadValue) // case "年": // startTimeRealTemp = startDateParse.AddDate(-v.LeadValue, 0, 0) // } // if startTimeRealTemp.Before(startDateParse) { // startDateReal = startTimeRealTemp.Format(utils.FormatDate) // diffSeconds = (int64(startTimeRealTemp.UnixNano()) - int64(startDateParse.UnixNano())) / 1e6 // } else { // startDateReal = startDate // diffSeconds = 0 // } // } else { // startDateReal = startDate // } // } // calendarPreYear := 0 // if calendar == "农历" { // newStartDateReal, err := time.Parse(utils.FormatDate, startDateReal) // if err != nil { // fmt.Println("time.Parse:" + err.Error()) // utils.FileLog.Info("startDateReal parse err:" + err.Error()) // } // calendarPreYear = newStartDateReal.Year() - 1 // newStartDateReal = newStartDateReal.AddDate(-1, 0, 0) // startDateReal = newStartDateReal.Format(utils.FormatDate) // } // dataList := make([]*models.EdbDataList, 0) // fmt.Println("chart:", v.Source, v.EdbInfoId, startDateReal, endDate) // dataList, err = models.GetEdbDataList(v.Source, v.EdbInfoId, startDateReal, endDate) // if err != nil { // br.Msg = "获取失败" // br.Msg = "获取失败,Err:" + err.Error() // return // } // if diffSeconds != 0 && v.EdbInfoType == 0 { // dataListLen := len(dataList) // for i := 0; i < dataListLen; i++ { // dataList[i].DataTimestamp = dataList[i].DataTimestamp - diffSeconds // } // } // // if chartType == 2 { // if calendar == "农历" { // result, err := models.AddCalculateQuarterV5(dataList) // if err != nil { // br.Msg = "获取失败" // br.Msg = "获取农历数据失败,Err:" + err.Error() // return // } // if result.List[0].Year != calendarPreYear { // itemList := make([]*models.EdbDataList, 0) // items := new(models.EdbDataItems) // //items.Year = calendarPreYear // items.Items = itemList // // newResult := new(models.EdbDataResult) // newResult.List = append(newResult.List, items) // newResult.List = append(newResult.List, result.List...) // item.DataList = newResult // } else { // item.DataList = result // } // } else { // currentYear := time.Now().Year() // quarterDataList := make([]*models.QuarterData, 0) // quarterMap := make(map[int][]*models.EdbDataList) // var quarterArr []int // for _, v := range dataList { // //v.DataTime // itemDate, err := time.Parse(utils.FormatDate, v.DataTime) // if err != nil { // br.Msg = "获取失败" // br.Msg = "季度指标日期转换,Err:" + err.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(models.QuarterData) // quarterItem.Year = v // quarterItem.DataList = itemList // quarterDataList = append(quarterDataList, quarterItem) // } // item.DataList = quarterDataList // } // } else { // item.DataList = dataList // } // edbList = append(edbList, item) //} // 获取图表中的指标数据 edbList, err := data.GetChartEdbData(chartInfoId, chartType, calendar, startDate, endDate, mappingList) if err != nil { br.Msg = "获取失败" br.ErrMsg = "获取图表,指标信息失败,Err:" + err.Error() return } resp.ChartInfo = chartInfo resp.EdbInfoList = edbList if utils.Re == nil { data, _ := json.Marshal(resp) utils.Rc.Put(key, data, 10*time.Minute) } br.Ret = 200 br.Success = true br.Msg = "获取成功" br.Data = resp } // ChartInfoRefresh // @Title 图表刷新接口 // @Description 图表刷新接口 // @Param UniqueCode query string true "图表唯一编码,如果是管理后台访问,传固定字符串:7c69b590249049942070ae9dcd5bf6dc" // @Success Ret=200 刷新成功 // @router /refresh [get] func (this *ChartController) ChartInfoRefresh() { br := new(models.BaseResponse).Init() chartId := 0 defer func() { // 添加日志 if chartId > 0 { shareChartRefreshLogInfo := &models.ShareChartRefreshLog{ Ip: this.Ctx.Input.IP(), ChartId: chartId, CreateTime: time.Now(), } models.AddShareChartRefreshLog(shareChartRefreshLogInfo) } this.Data["json"] = br this.ServeJSON() }() uniqueCode := this.GetString("UniqueCode") if uniqueCode == "" { br.Msg = "参数错误" br.ErrMsg = "参数错误,uniqueCode is empty" return } chartInfo, err := models.GetChartInfoByUniqueCode(uniqueCode) if err != nil { if err.Error() == utils.ErrNoRow() { br.Msg = "该图已被删除,请刷新页面" br.ErrMsg = "该图已被删除,请刷新页面,Err:" + err.Error() return } br.Msg = "获取失败" br.ErrMsg = "获取图表信息失败,Err:" + err.Error() return } chartId = chartInfo.ChartInfoId //err = data.ChartInfoRefresh(chartInfo.ChartInfoId) //if err != nil { // br.Msg = "刷新失败" // br.ErrMsg = "刷新图表关联指标信息失败,Err:" + err.Error() // return //} err = data.ChartInfoRefreshV1(chartInfo.ChartInfoId) if err != nil { br.Msg = "刷新失败" br.ErrMsg = "刷新图表关联指标信息失败,Err:" + err.Error() return } //清除数据缓存 key := utils.HZ_CHART_LIB_DETAIL + uniqueCode if utils.Re == nil { utils.Rc.Delete(key) } br.Ret = 200 br.Success = true br.Msg = "刷新成功" } // 获取频度的英文版 func GetFrequencyEn(frequency string) (frequencyEn string) { switch frequency { case "日度": frequencyEn = "day" return case "周度": frequencyEn = "week" return case "旬度": frequencyEn = "ten days" return case "月度": frequencyEn = "month" return case "季度": frequencyEn = "quarter" return case "年度": frequencyEn = "year" return } return } func GetLeadUnitEn(unit string) (unitEn string) { switch unit { case "天": unitEn = "day" return case "周": unitEn = "week" return case "月": unitEn = "month" return case "季": unitEn = "quarter" return case "年": unitEn = "year" return } return }