chart_info_excel_balance.go 14 KB

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