chart_info_excel_balance.go 14 KB

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