chart_info_excel_balance.go 14 KB

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