chart_info_excel_balance.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  1. package data
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "eta/eta_chart_lib/models"
  6. "eta/eta_chart_lib/models/data_manage"
  7. "eta/eta_chart_lib/models/data_manage/excel"
  8. "eta/eta_chart_lib/utils"
  9. "fmt"
  10. "math"
  11. "strings"
  12. "time"
  13. )
  14. func GetBalanceExcelChartDetail(chartInfo *models.ChartInfo, mappingListTmp []*excel.ExcelChartEdb, dataListMap map[int][]*models.EdbDataList) (resp *models.ChartInfoDetailResp, err error, errMsg string) {
  15. chartInfoId := chartInfo.ChartInfoId
  16. resp = new(models.ChartInfoDetailResp)
  17. // 获取主题样式
  18. chartTheme, err := GetChartThemeConfig(chartInfo.ChartThemeId, 1, chartInfo.ChartType)
  19. if err != nil {
  20. errMsg = "获取失败"
  21. err = fmt.Errorf(" 获取主题信息失败 Err:%s", err.Error())
  22. return
  23. }
  24. chartInfo.ChartThemeStyle = chartTheme.Config
  25. chartInfo.ChartThemeId = chartTheme.ChartThemeId
  26. dateType := chartInfo.DateType
  27. fmt.Println("dateType:", dateType)
  28. chartType := chartInfo.ChartType
  29. startDate := chartInfo.StartDate
  30. endDate := chartInfo.EndDate
  31. seasonStartDate := chartInfo.SeasonStartDate
  32. seasonEndDate := chartInfo.SeasonEndDate
  33. startYear := chartInfo.StartYear
  34. calendar := chartInfo.Calendar
  35. if calendar == "" {
  36. calendar = "公历"
  37. }
  38. mappingList := make([]*models.ChartEdbInfoMapping, 0)
  39. //循环组装映射关系
  40. for _, v := range mappingListTmp {
  41. dataList := make([]*models.EdbDataList, 0)
  42. dataListTmp, ok := dataListMap[v.ExcelChartEdbId]
  43. if ok {
  44. dataList = dataListTmp
  45. } else {
  46. err = errors.New(fmt.Sprint("获取失败,指标类型异常", v.ExcelChartEdbId))
  47. return
  48. }
  49. startDateStr, endDateStr, _, endVal, maxValue, minValue := getBalanceDataListStartDateAndValue(dataList)
  50. mapping := &models.ChartEdbInfoMapping{
  51. EdbInfoId: v.ExcelChartEdbId,
  52. SourceName: "",
  53. Source: 0,
  54. SubSource: 0,
  55. EdbCode: v.EdbCode,
  56. EdbName: v.EdbName,
  57. EdbAliasName: v.EdbName,
  58. EdbNameEn: "",
  59. EdbAliasNameEn: "",
  60. EdbType: 0,
  61. Frequency: "",
  62. FrequencyEn: "",
  63. Unit: "",
  64. UnitEn: "",
  65. StartDate: startDateStr,
  66. EndDate: endDateStr,
  67. ModifyTime: v.ModifyTime.Format(utils.FormatDateTime),
  68. ChartEdbMappingId: v.ExcelChartEdbId,
  69. ChartInfoId: chartInfoId,
  70. MaxData: v.MaxData,
  71. MinData: v.MinData,
  72. IsOrder: v.IsOrder,
  73. IsAxis: v.IsAxis,
  74. //EdbInfoType: 0,
  75. //EdbInfoCategoryType: 0,
  76. LeadValue: v.LeadValue,
  77. LeadUnit: v.LeadUnit,
  78. LeadUnitEn: "",
  79. ChartStyle: "",
  80. ChartColor: "",
  81. PredictChartColor: "",
  82. ChartWidth: 0,
  83. ChartType: chartType,
  84. LatestDate: endDateStr,
  85. LatestValue: endVal,
  86. MoveLatestDate: "",
  87. UniqueCode: "",
  88. MinValue: minValue,
  89. MaxValue: maxValue,
  90. DataList: nil,
  91. IsNullData: false,
  92. MappingSource: 0,
  93. IsConvert: 0,
  94. ConvertType: 0,
  95. ConvertValue: 0,
  96. ConvertUnit: "",
  97. ConvertEnUnit: "",
  98. }
  99. mappingList = append(mappingList, mapping)
  100. }
  101. if chartType == 2 {
  102. startDate = seasonStartDate
  103. endDate = seasonEndDate
  104. if dateType <= 0 {
  105. if startDate != "" {
  106. dateType = 5
  107. } else {
  108. dateType = utils.DateTypeNYears
  109. }
  110. }
  111. } else {
  112. if dateType <= 0 {
  113. dateType = 3
  114. }
  115. }
  116. yearMax := 0
  117. if dateType == utils.DateTypeNYears {
  118. for _, v := range mappingList {
  119. if v.LatestDate != "" {
  120. lastDateT, tErr := time.Parse(utils.FormatDate, v.LatestDate)
  121. if tErr != nil {
  122. errMsg = "获取失败"
  123. err = fmt.Errorf("获取图表日期信息失败,Err:" + tErr.Error())
  124. return
  125. }
  126. if lastDateT.Year() > yearMax {
  127. yearMax = lastDateT.Year()
  128. }
  129. }
  130. }
  131. }
  132. startDate, endDate = utils.GetDateByDateTypeV2(dateType, startDate, endDate, startYear, yearMax)
  133. if chartInfo.ChartType == 2 {
  134. chartInfo.StartDate = startDate
  135. chartInfo.EndDate = endDate
  136. }
  137. // 图表额外数据参数
  138. extraConfigStr := chartInfo.ExtraConfig
  139. // 柱方图的一些配置
  140. var barConfig data_manage.BarChartInfoReq
  141. if chartInfo != nil && chartInfo.ChartType == 7 {
  142. if chartInfo.BarConfig == `` {
  143. err = fmt.Errorf("柱方图未配置")
  144. errMsg = "柱方图未配置"
  145. return
  146. }
  147. err = json.Unmarshal([]byte(chartInfo.BarConfig), &barConfig)
  148. if err != nil {
  149. err = fmt.Errorf("柱方图配置异常 json.Unmarshal Err:%s", err.Error())
  150. errMsg = "柱方图配置异常"
  151. return
  152. }
  153. extraConfigStr = chartInfo.BarConfig
  154. }
  155. // 获取表格数据
  156. excelChartInfoDataShow := new(ExcelChartInfoDataShow)
  157. excelChartInfoDataShow.DataListMap = dataListMap
  158. // 获取图表中的指标数据
  159. edbList, xEdbIdValue, yDataList, dataResp, e, msg := GetChartEdbDataV2(chartInfoId, chartType, calendar, startDate, endDate, mappingList, extraConfigStr, chartInfo.SeasonExtraConfig, excelChartInfoDataShow)
  160. if e != nil {
  161. err = fmt.Errorf("获取图表,指标数据失败,Err:%s", e.Error())
  162. errMsg = msg
  163. return
  164. }
  165. for _, v := range edbList {
  166. // 指标别名
  167. if barConfig.EdbInfoIdList != nil && len(barConfig.EdbInfoIdList) > 0 {
  168. for _, reqEdb := range barConfig.EdbInfoIdList {
  169. if v.EdbInfoId == reqEdb.EdbInfoId {
  170. v.EdbAliasName = reqEdb.Name
  171. }
  172. }
  173. }
  174. }
  175. // 图表的指标来源
  176. sourceNameList, sourceNameEnList := GetEdbSourceByEdbInfoIdList(edbList)
  177. chartInfo.ChartSource = strings.Join(sourceNameList, ",")
  178. chartInfo.ChartSourceEn = strings.Join(sourceNameEnList, ",")
  179. resp.EdbInfoList = edbList
  180. resp.XEdbIdValue = xEdbIdValue
  181. resp.YDataList = yDataList
  182. resp.DataResp = dataResp
  183. resp.ChartInfo = chartInfo
  184. return
  185. }
  186. // GetBalanceExcelEdbDataMapList 获取指标最后的基础数据
  187. func GetBalanceExcelEdbDataMapList(chartInfoId, chartType int, calendar, startDate, endDate string, mappingList []*models.ChartEdbInfoMapping, seasonExtraConfig string, dataListMap map[int][]*models.EdbDataList) (edbDataListMap map[int][]*models.EdbDataList, edbList []*models.ChartEdbInfoMapping, err error) {
  188. // 指标对应的所有数据
  189. edbDataListMap = make(map[int][]*models.EdbDataList)
  190. for _, v := range mappingList {
  191. //fmt.Println("v:", v.EdbInfoId)
  192. item := new(models.ChartEdbInfoMapping)
  193. item.EdbInfoId = v.EdbInfoId
  194. item.SourceName = v.SourceName
  195. item.Source = v.Source
  196. item.EdbCode = v.EdbCode
  197. item.EdbName = v.EdbName
  198. item.EdbNameEn = v.EdbNameEn
  199. item.Frequency = v.Frequency
  200. item.EdbType = v.EdbType
  201. item.FrequencyEn = GetFrequencyEn(v.Frequency)
  202. if v.Unit != `无` {
  203. item.Unit = v.Unit
  204. }
  205. item.UnitEn = v.UnitEn
  206. item.StartDate = v.StartDate
  207. item.EndDate = v.EndDate
  208. item.ModifyTime = v.ModifyTime
  209. item.EdbInfoCategoryType = v.EdbInfoCategoryType
  210. item.PredictChartColor = v.PredictChartColor
  211. if chartInfoId <= 0 {
  212. item.IsAxis = 1
  213. item.LeadValue = 0
  214. item.LeadUnit = ""
  215. item.ChartEdbMappingId = 0
  216. item.ChartInfoId = 0
  217. item.IsOrder = false
  218. item.EdbInfoType = 1
  219. item.ChartStyle = ""
  220. item.ChartColor = ""
  221. item.ChartWidth = 0
  222. item.MaxData = v.MaxValue
  223. item.MinData = v.MinValue
  224. } else {
  225. item.IsAxis = v.IsAxis
  226. item.EdbInfoType = v.EdbInfoType
  227. item.LeadValue = v.LeadValue
  228. item.LeadUnit = v.LeadUnit
  229. item.LeadUnitEn = GetLeadUnitEn(v.LeadUnit)
  230. item.ChartEdbMappingId = v.ChartEdbMappingId
  231. item.ChartInfoId = v.ChartInfoId
  232. item.ChartStyle = v.ChartStyle
  233. item.ChartColor = v.ChartColor
  234. item.ChartWidth = v.ChartWidth
  235. item.IsOrder = v.IsOrder
  236. item.MaxData = v.MaxData
  237. item.MinData = v.MinData
  238. }
  239. item.LatestValue = v.LatestValue
  240. item.LatestDate = v.LatestDate
  241. item.UniqueCode = v.UniqueCode
  242. item.MoveLatestDate = v.LatestDate
  243. item.EdbAliasName = v.EdbAliasName
  244. item.IsConvert = v.IsConvert
  245. item.ConvertType = v.ConvertType
  246. item.ConvertValue = v.ConvertValue
  247. item.ConvertUnit = v.ConvertUnit
  248. item.ConvertEnUnit = v.ConvertEnUnit
  249. var startDateReal string
  250. var diffSeconds int64
  251. if chartType == 2 { //季节性图
  252. startDateReal = startDate
  253. } else {
  254. if v.EdbInfoType == 0 && v.LeadUnit != "" && v.LeadValue > 0 { //领先指标
  255. var startTimeRealTemp time.Time
  256. startDateParse, _ := time.Parse(utils.FormatDate, startDate)
  257. switch v.LeadUnit {
  258. case "天":
  259. startTimeRealTemp = startDateParse.AddDate(0, 0, -v.LeadValue)
  260. case "月":
  261. startTimeRealTemp = startDateParse.AddDate(0, -v.LeadValue, 0)
  262. case "季":
  263. startTimeRealTemp = startDateParse.AddDate(0, -3*v.LeadValue, 0)
  264. case "周":
  265. startTimeRealTemp = startDateParse.AddDate(0, 0, -7*v.LeadValue)
  266. case "年":
  267. startTimeRealTemp = startDateParse.AddDate(-v.LeadValue, 0, 0)
  268. }
  269. if startTimeRealTemp.Before(startDateParse) {
  270. startDateReal = startTimeRealTemp.Format(utils.FormatDate)
  271. diffSeconds = (int64(startTimeRealTemp.UnixNano()) - int64(startDateParse.UnixNano())) / 1e6
  272. } else {
  273. startDateReal = startDate
  274. diffSeconds = 0
  275. }
  276. // 预测指标的开始日期也要偏移
  277. {
  278. day, tmpErr := utils.GetDaysBetween2Date(utils.FormatDate, startDate, startDateReal)
  279. if tmpErr != nil {
  280. err = tmpErr
  281. return
  282. }
  283. moveLatestDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, item.MoveLatestDate, time.Local)
  284. if tmpErr != nil {
  285. err = tmpErr
  286. return
  287. }
  288. item.MoveLatestDate = moveLatestDateTime.AddDate(0, 0, day).Format(utils.FormatDate)
  289. }
  290. } else {
  291. startDateReal = startDate
  292. }
  293. }
  294. //fmt.Println("line 1011 chart:", v.Source, v.EdbInfoId, startDateReal, endDate)
  295. calendarPreYear := 0
  296. if calendar == "农历" {
  297. newStartDateReal, e := time.Parse(utils.FormatDate, startDateReal)
  298. if e != nil {
  299. err = fmt.Errorf("时间解析 time.Parse(%s, %s) error: %v", utils.FormatDate, startDateReal, e)
  300. return
  301. }
  302. calendarPreYear = newStartDateReal.Year() - 1
  303. newStartDateReal = newStartDateReal.AddDate(-1, 0, 0)
  304. startDateReal = newStartDateReal.Format(utils.FormatDate)
  305. }
  306. dataList := make([]*models.EdbDataList, 0)
  307. dataListTmp, ok := dataListMap[v.EdbInfoId]
  308. if ok {
  309. dataList = dataListTmp
  310. } else {
  311. err = errors.New(fmt.Sprint("获取失败,指标类型异常", v.EdbInfoId))
  312. return
  313. }
  314. if v.IsConvert == 1 {
  315. switch v.ConvertType {
  316. case 1:
  317. for i, data := range dataList {
  318. dataList[i].Value = data.Value * v.ConvertValue
  319. }
  320. //item.MaxData = item.MaxData * v.ConvertValue
  321. //item.MinData = item.MinData * v.ConvertValue
  322. case 2:
  323. for i, data := range dataList {
  324. dataList[i].Value = data.Value / v.ConvertValue
  325. }
  326. //item.MaxData = item.MaxData / v.ConvertValue
  327. //item.MinData = item.MinData / v.ConvertValue
  328. case 3:
  329. for i, data := range dataList {
  330. if data.Value <= 0 {
  331. err = errors.New("数据中含有负数或0,无法对数运算")
  332. return
  333. }
  334. dataList[i].Value = math.Log(data.Value) / math.Log(v.ConvertValue)
  335. }
  336. //item.MaxData = math.Log(item.MaxData) / math.Log(v.ConvertValue)
  337. //item.MinData = math.Log(item.MinData) / math.Log(v.ConvertValue)
  338. }
  339. }
  340. edbDataListMap[v.EdbInfoId] = dataList
  341. if diffSeconds != 0 && v.EdbInfoType == 0 {
  342. dataListLen := len(dataList)
  343. for i := 0; i < dataListLen; i++ {
  344. dataList[i].DataTimestamp = dataList[i].DataTimestamp - diffSeconds
  345. }
  346. }
  347. if chartType == 2 {
  348. latestDate, tmpErr := time.Parse(utils.FormatDate, v.LatestDate)
  349. if tmpErr != nil {
  350. //item.DataList = dataList
  351. item.IsNullData = true
  352. edbList = append(edbList, item)
  353. continue
  354. }
  355. if calendar == "农历" {
  356. if len(dataList) <= 0 {
  357. result := new(models.EdbDataResult)
  358. item.DataList = result
  359. } else {
  360. result, tmpErr := models.AddCalculateQuarterV6(dataList)
  361. if tmpErr != nil {
  362. err = errors.New("获取农历数据失败,Err:" + tmpErr.Error())
  363. return
  364. }
  365. quarterDataList, tErr := GetSeasonEdbInfoDataListByXDateNong(result, latestDate, seasonExtraConfig, calendarPreYear)
  366. if tErr != nil {
  367. err = errors.New("获取季节性图表数据失败,Err:" + tErr.Error())
  368. return
  369. }
  370. item.DataList = quarterDataList
  371. }
  372. } else {
  373. quarterDataList, tErr := GetSeasonEdbInfoDataListByXDate(dataList, latestDate, seasonExtraConfig)
  374. if tErr != nil {
  375. err = errors.New("获取季节性图表数据失败,Err:" + tErr.Error())
  376. return
  377. }
  378. item.DataList = quarterDataList
  379. }
  380. } else if chartType == 7 || chartType == utils.CHART_TYPE_RADAR { //柱方图
  381. //item.DataList = dataList
  382. } else {
  383. item.DataList = dataList
  384. }
  385. edbList = append(edbList, item)
  386. }
  387. return
  388. }
  389. func getBalanceDataListStartDateAndValue(dataList []*models.EdbDataList) (startDate, endDate string, startVal, endVal, maxVal, minVal float64) {
  390. if len(dataList) == 0 {
  391. return
  392. }
  393. startDate = dataList[0].DataTime
  394. startVal = dataList[0].Value
  395. maxVal = dataList[0].Value
  396. minVal = dataList[0].Value
  397. endDate = dataList[len(dataList)-1].DataTime
  398. endVal = dataList[len(dataList)-1].Value
  399. for _, v := range dataList {
  400. if v.DataTime < startDate {
  401. startDate = v.DataTime
  402. startVal = v.Value
  403. }
  404. if v.DataTime > endDate {
  405. endDate = v.DataTime
  406. endVal = v.Value
  407. }
  408. if v.Value > maxVal {
  409. maxVal = v.Value
  410. }
  411. if v.Value < minVal {
  412. minVal = v.Value
  413. }
  414. }
  415. return
  416. }