excel_info.go 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117
  1. package excel
  2. import (
  3. "errors"
  4. "eta/eta_chart_lib/models"
  5. "eta/eta_chart_lib/models/data_manage"
  6. "eta/eta_chart_lib/models/request"
  7. "eta/eta_chart_lib/services/data"
  8. "eta/eta_chart_lib/utils"
  9. "fmt"
  10. "sort"
  11. "time"
  12. )
  13. // GetFirstEdbDataList 获取第一列的数据
  14. func GetFirstEdbDataList(edbInfo *data_manage.EdbInfo, num int, manualDateList []string) (resultDataList []request.ManualDataReq, err error) {
  15. var dataList []*models.EdbDataList
  16. switch edbInfo.EdbInfoType {
  17. case 0:
  18. dataList, err = models.GetEdbDataList(edbInfo.Source, edbInfo.SubSource, edbInfo.EdbInfoId, ``, ``)
  19. case 1:
  20. _, dataList, _, _, err, _ = data.GetPredictDataListByPredictEdbInfoId(edbInfo.EdbInfoId, ``, ``, false)
  21. default:
  22. err = errors.New(fmt.Sprint("获取失败,指标类型异常", edbInfo.EdbInfoType))
  23. return
  24. }
  25. // 获取需要的期数
  26. lenData := len(dataList)
  27. if lenData <= 0 {
  28. return
  29. }
  30. tmpManualDateNum := 0 // 手工数据的期数
  31. lenManualDate := len(manualDateList)
  32. if lenManualDate > 0 {
  33. sortDateList := manualDateList
  34. baseDateList := utils.StrArr{}
  35. baseDateList = append(baseDateList, sortDateList...)
  36. sort.Sort(baseDateList)
  37. sortDateList = append([]string{}, baseDateList...)
  38. lastData := dataList[lenData-1]
  39. lastDataDate, tmpErr := time.ParseInLocation(utils.FormatDate, lastData.DataTime, time.Local)
  40. if tmpErr != nil {
  41. err = tmpErr
  42. return
  43. }
  44. // 遍历倒序后的日期,然后匹配在实际数据之后日期的个数
  45. for _, tmpDateStr := range sortDateList {
  46. tmpDate, tmpErr := time.ParseInLocation(utils.FormatDate, tmpDateStr, time.Local)
  47. if tmpErr != nil {
  48. err = tmpErr
  49. return
  50. }
  51. if tmpDate.After(lastDataDate) {
  52. tmpManualDateNum++
  53. continue
  54. }
  55. break
  56. }
  57. }
  58. // 需要的期数减去手工数据的期数,这才是A列指标需要的数据
  59. num = num - tmpManualDateNum
  60. if num > lenData {
  61. num = lenData
  62. }
  63. latestDateTime, _ := time.ParseInLocation(utils.FormatDate, edbInfo.LatestDate, time.Local)
  64. for i := 1; i <= num; i++ {
  65. dataTime, _ := time.ParseInLocation(utils.FormatDate, dataList[lenData-i].DataTime, time.Local)
  66. dataType := 1
  67. // 如果是预测指标,且当前值的日期,晚于实际日期,那么是预测值
  68. if edbInfo.EdbInfoType == 1 && dataTime.After(latestDateTime) {
  69. dataType = 5
  70. }
  71. resultDataList = append(resultDataList, request.ManualDataReq{
  72. DataType: dataType,
  73. DataTime: dataList[lenData-i].DataTime,
  74. ShowValue: fmt.Sprint(dataList[lenData-i].Value),
  75. Value: fmt.Sprint(dataList[lenData-i].Value),
  76. DataTimeType: 1,
  77. })
  78. }
  79. return
  80. }
  81. // GetOtherEdbDataList 获取其他列的数据
  82. func GetOtherEdbDataList(edbInfo *data_manage.EdbInfo, dateList []string) (resultDataList []request.ManualDataReq, err error) {
  83. lenDate := len(dateList)
  84. if lenDate <= 0 {
  85. return
  86. }
  87. sortDateList := dateList
  88. baseDateList := utils.StrArr{}
  89. baseDateList = append(baseDateList, sortDateList...)
  90. sort.Sort(baseDateList)
  91. sortDateList = append([]string{}, baseDateList...)
  92. endDateTime, err := time.ParseInLocation(utils.FormatDate, sortDateList[0], time.Local)
  93. if err != nil {
  94. return
  95. }
  96. firstDateTime, err := time.ParseInLocation(utils.FormatDate, sortDateList[lenDate-1], time.Local)
  97. if err != nil {
  98. return
  99. }
  100. var dataList []*models.EdbDataList
  101. switch edbInfo.EdbInfoType {
  102. case 0:
  103. dataList, err = models.GetEdbDataList(edbInfo.Source, edbInfo.SubSource, edbInfo.EdbInfoId, ``, ``)
  104. case 1:
  105. _, dataList, _, _, err, _ = data.GetPredictDataListByPredictEdbInfoId(edbInfo.EdbInfoId, ``, ``, false)
  106. default:
  107. err = errors.New(fmt.Sprint("获取失败,指标类型异常", edbInfo.EdbInfoType))
  108. return
  109. }
  110. // 获取日期内的数据(包含开始日期前一个日期,以及 结束日期后一个日期,目的为了做空日期时的 插值法兼容)
  111. baseDataList := make([]*models.EdbDataList, 0)
  112. var lastData *models.EdbDataList
  113. var isInsert bool
  114. for _, data := range dataList {
  115. tmpDate := data.DataTime
  116. tmpDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, tmpDate, time.Local)
  117. if tmpErr != nil {
  118. err = tmpErr
  119. return
  120. }
  121. if tmpDateTime.Before(firstDateTime) {
  122. lastData = data
  123. continue
  124. }
  125. // 如果是第一次写入数据
  126. if !isInsert && lastData != nil {
  127. baseDataList = append(baseDataList, lastData)
  128. }
  129. if tmpDateTime.After(endDateTime) {
  130. baseDataList = append(baseDataList, data)
  131. break
  132. }
  133. baseDataList = append(baseDataList, data)
  134. isInsert = true
  135. }
  136. // 实际数据的日期map
  137. realValMap := make(map[string]string)
  138. for _, v := range baseDataList {
  139. realValMap[v.DataTime] = v.DataTime
  140. }
  141. // 插值法处理
  142. handleDataMap := make(map[string]float64)
  143. err = data.HandleDataByLinearRegression(baseDataList, handleDataMap)
  144. if err != nil {
  145. return
  146. }
  147. latestDateTime, _ := time.ParseInLocation(utils.FormatDate, edbInfo.LatestDate, time.Local)
  148. // 对于不存在的数据做补充
  149. for _, date := range sortDateList {
  150. dataType := 1
  151. if _, ok := realValMap[date]; !ok {
  152. dataType = 2
  153. } else {
  154. dataTime, _ := time.ParseInLocation(utils.FormatDate, date, time.Local)
  155. // 如果是预测指标,且当前值的日期,晚于实际日期,那么是预测值
  156. if edbInfo.EdbInfoType == 1 && dataTime.After(latestDateTime) {
  157. dataType = 5
  158. }
  159. }
  160. var value, showValue string
  161. if tmpVal, ok := handleDataMap[date]; ok {
  162. value = fmt.Sprint(tmpVal)
  163. showValue = value
  164. } else {
  165. dataType = 3
  166. }
  167. resultDataList = append(resultDataList, request.ManualDataReq{
  168. DataType: dataType,
  169. DataTime: date,
  170. ShowValue: showValue,
  171. Value: value,
  172. })
  173. }
  174. return
  175. }
  176. // GetFirstHistoryEdbDataList 获取指标的历史的数据
  177. func GetFirstHistoryEdbDataList(edbInfo *data_manage.EdbInfo, num int, endDate string) (resultDataList []request.ManualDataReq, err error) {
  178. endDateTime, err := time.ParseInLocation(utils.FormatDate, endDate, time.Local)
  179. if err != nil {
  180. return
  181. }
  182. var dataList []*models.EdbDataList
  183. switch edbInfo.EdbInfoType {
  184. case 0:
  185. dataList, err = models.GetEdbDataList(edbInfo.Source, edbInfo.SubSource, edbInfo.EdbInfoId, ``, endDate)
  186. case 1:
  187. _, dataList, _, _, err, _ = data.GetPredictDataListByPredictEdbInfoId(edbInfo.EdbInfoId, ``, endDate, true)
  188. default:
  189. err = errors.New(fmt.Sprint("获取失败,指标类型异常", edbInfo.EdbInfoType))
  190. return
  191. }
  192. // 获取需要的期数
  193. lenData := len(dataList)
  194. if lenData <= 0 {
  195. return
  196. }
  197. lastData := dataList[lenData-1]
  198. lastDataDateTime, err := time.ParseInLocation(utils.FormatDate, lastData.DataTime, time.Local)
  199. if err != nil {
  200. return
  201. }
  202. if endDateTime.Equal(lastDataDateTime) || lastDataDateTime.After(endDateTime) {
  203. dataList = dataList[:lenData-1]
  204. lenData = len(dataList)
  205. }
  206. if num > lenData {
  207. num = lenData
  208. }
  209. latestDateTime, _ := time.ParseInLocation(utils.FormatDate, edbInfo.LatestDate, time.Local)
  210. for i := 1; i <= num; i++ {
  211. dataTime, _ := time.ParseInLocation(utils.FormatDate, dataList[lenData-i].DataTime, time.Local)
  212. dataType := 1
  213. // 如果是预测指标,且当前值的日期,晚于实际日期,那么是预测值
  214. if edbInfo.EdbInfoType == 1 && dataTime.After(latestDateTime) {
  215. dataType = 5
  216. }
  217. resultDataList = append(resultDataList, request.ManualDataReq{
  218. DataType: dataType,
  219. DataTime: dataList[lenData-i].DataTime,
  220. ShowValue: fmt.Sprint(dataList[lenData-i].Value),
  221. Value: fmt.Sprint(dataList[lenData-i].Value),
  222. })
  223. }
  224. return
  225. }
  226. //type TableDataConfig struct {
  227. // EdbInfoIdList []int `description:"指标id列表,从左至右,从上到下的顺序"`
  228. // Sort int `description:"日期排序,0:倒序,1:正序"`
  229. // Data []ManualData `description:"数据列表"`
  230. // Num int `description:"实际数据需要列出来的期数"`
  231. // RemoveDate []string `description:"不展示的日期"`
  232. // ManualDate []string `description:"手动配置的日期(未来的日期)"`
  233. // TableEdbInfoList []TableEdbInfo `description:"表格内指标信息"`
  234. // TextRowData [][]request.ManualDataReq `description:"文本列表"`
  235. //}
  236. //
  237. //type TableEdbInfo struct {
  238. // EdbInfoId int `description:"指标ID"`
  239. // Tag string `description:"标签"`
  240. // EdbName string `description:"指标名称"`
  241. // EdbAliasName string `description:"指标别名"`
  242. // Frequency string `description:"频度"`
  243. // Unit string `description:"单位"`
  244. //}
  245. //
  246. //type ManualData struct {
  247. // DataType int `description:"数据类型,1:普通的,2:插值法,3:手动输入,4:公式计算"`
  248. // DataTime string `description:"所属日期"`
  249. // DataTimeType int `description:"日期类型,1:实际日期;2:未来日期"`
  250. // ShowValue string `description:"展示值"`
  251. // Value string `description:"实际值(计算公式)"`
  252. // EdbInfoId int `description:"指标id"`
  253. // Tag string `description:"下标"`
  254. // RelationEdbInfoList []request.RelationEdbInfo `description:"关联指标(计算公式中关联的指标,用于计算的时候去匹配)"`
  255. //}
  256. //
  257. //func GetTableDataConfig(reqData request.TableDataReq) (tableDataConfig TableDataConfig, err error) {
  258. // // 指标数据
  259. // tableDataConfig.EdbInfoIdList = reqData.EdbInfoIdList
  260. // tableDataConfig.Sort = reqData.Sort
  261. //
  262. // // 开始日期
  263. // var startDate string
  264. // // A列的指标id
  265. // var firstEdbInfoId int
  266. // // 手工操作的数据列
  267. // manualDataList := make([]ManualData, 0)
  268. // // 指标配置列表
  269. // tableEdbInfoList := make([]TableEdbInfo, 0)
  270. //
  271. // // 第一列的日期map
  272. // firstDateMap := make(map[string]string)
  273. // manualDateMap := make(map[string]string)
  274. //
  275. // for _, v := range reqData.Data {
  276. // // 指标信息
  277. // tmpTableEdbInfo := TableEdbInfo{
  278. // EdbInfoId: v.EdbInfoId,
  279. // Tag: v.Tag,
  280. // EdbName: v.EdbName,
  281. // EdbAliasName: v.EdbAliasName,
  282. // Frequency: v.Frequency,
  283. // Unit: v.Unit,
  284. // }
  285. // tableEdbInfoList = append(tableEdbInfoList, tmpTableEdbInfo)
  286. //
  287. // // 确定数据A列
  288. // if v.Tag == "A" {
  289. // firstEdbInfoId = v.EdbInfoId
  290. // lenData := len(v.Data)
  291. // if lenData <= 0 {
  292. // err = errors.New("A列不能为空")
  293. // return
  294. // }
  295. // index := 0
  296. // if reqData.Sort == 1 {
  297. // // 倒序
  298. // index = lenData - 1
  299. // }
  300. //
  301. // startDate = v.Data[index].DataTime
  302. //
  303. // // 存在的日期列表
  304. // for _, data := range v.Data {
  305. // firstDateMap[data.DataTime] = data.DataTime
  306. //
  307. // if data.DataTimeType == 2 {
  308. // manualDateMap[data.DataTime] = data.DataTime
  309. // }
  310. // }
  311. // }
  312. //
  313. // for _, data := range v.Data {
  314. // if data.DataType == 3 || data.DataType == 4 {
  315. // tmpManualData := ManualData{
  316. // DataType: data.DataType,
  317. // DataTime: data.DataTime,
  318. // DataTimeType: data.DataTimeType,
  319. // ShowValue: data.ShowValue,
  320. // Value: data.Value,
  321. // EdbInfoId: v.EdbInfoId,
  322. // Tag: v.Tag,
  323. // RelationEdbInfoList: data.RelationEdbInfoList,
  324. // }
  325. // if data.DataType == 4 {
  326. // tmpManualData.ShowValue = ``
  327. // }
  328. // manualDataList = append(manualDataList, tmpManualData)
  329. // }
  330. // }
  331. // }
  332. //
  333. // // 实际期数
  334. // var num int
  335. // removeDate := make([]string, 0)
  336. //
  337. // // 获取期数
  338. // {
  339. // firstDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, startDate, time.Local)
  340. // if tmpErr != nil {
  341. // err = tmpErr
  342. // return
  343. // }
  344. //
  345. // edbInfo, tmpErr := data_manage.GetEdbInfoById(firstEdbInfoId)
  346. // if tmpErr != nil {
  347. // err = tmpErr
  348. // return
  349. // }
  350. //
  351. // var firstDataList []*models.EdbDataList
  352. // switch edbInfo.EdbInfoType {
  353. // case 0:
  354. // firstDataList, err = models.GetEdbDataList(edbInfo.Source, edbInfo.SubSource, edbInfo.EdbInfoId, ``, ``)
  355. // case 1:
  356. // _, firstDataList, _, _, err, _ = data.GetPredictDataListByPredictEdbInfoId(edbInfo.EdbInfoId, ``, ``, false)
  357. // default:
  358. // err = errors.New(fmt.Sprint("获取失败,指标类型异常", edbInfo.EdbInfoType))
  359. // return
  360. // }
  361. //
  362. // // 获取日期内的数据(包含开始日期前一个日期,以及 结束日期后一个日期,目的为了做空日期时的 插值法兼容)
  363. // baseDataList := make([]*models.EdbDataList, 0)
  364. // for _, data := range firstDataList {
  365. // tmpDate := data.DataTime
  366. // tmpDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, tmpDate, time.Local)
  367. // if tmpErr != nil {
  368. // err = tmpErr
  369. // return
  370. // }
  371. // if tmpDateTime.Before(firstDateTime) {
  372. // continue
  373. // }
  374. // baseDataList = append(baseDataList, data)
  375. // }
  376. //
  377. // // 开始日期到现在的实际期数
  378. // num = len(baseDataList)
  379. //
  380. // // 筛选出需要删除的日期
  381. // for _, tmpData := range baseDataList {
  382. // //firstDateMap{}
  383. // if _, ok := firstDateMap[tmpData.DataTime]; !ok {
  384. // removeDate = append(removeDate, tmpData.DataTime)
  385. // }
  386. // }
  387. // }
  388. //
  389. // tableDataConfig.Num = num
  390. // tableDataConfig.RemoveDate = removeDate
  391. // tableDataConfig.Data = manualDataList
  392. // tableDataConfig.TableEdbInfoList = tableEdbInfoList
  393. // tableDataConfig.TextRowData = reqData.TextRowData
  394. //
  395. // return
  396. //}
  397. //
  398. //func GetDataByTableDataConfig(tableDataConfig TableDataConfig) (resultResp request.TableDataReq, err error) {
  399. // // 没有选择指标的情况下,直接返回吧
  400. // if len(tableDataConfig.EdbInfoIdList) <= 0 {
  401. // return
  402. // }
  403. //
  404. // // 实际期数没有的情况下,直接返回吧
  405. // if tableDataConfig.Num <= 0 {
  406. // return
  407. // }
  408. //
  409. // // 获取所有的指标信息
  410. // edbInfoMap := make(map[int]*data_manage.EdbInfo)
  411. // edbInfoIdList := make([]int, 0)
  412. // // 标签与指标id的map
  413. // tagEdbInfoIdMap := make(map[string]int)
  414. // {
  415. // for _, tableEdbInfo := range tableDataConfig.TableEdbInfoList {
  416. // edbInfoIdList = append(edbInfoIdList, tableEdbInfo.EdbInfoId)
  417. // tagEdbInfoIdMap[tableEdbInfo.Tag] = tableEdbInfo.EdbInfoId
  418. // }
  419. // edbInfoList, tmpErr := data_manage.GetEdbInfoByIdList(edbInfoIdList)
  420. // if tmpErr != nil {
  421. // err = tmpErr
  422. // return
  423. // }
  424. // for _, v := range edbInfoList {
  425. // edbInfoMap[v.EdbInfoId] = v
  426. // }
  427. // }
  428. //
  429. // manualDateMap := make(map[string]string, 0)
  430. // manualDateList := make([]string, 0)
  431. // for _, v := range tableDataConfig.Data {
  432. // if _, ok := manualDateMap[v.DataTime]; !ok {
  433. // manualDateMap[v.DataTime] = v.DataTime
  434. // manualDateList = append(manualDateList, v.DataTime)
  435. // }
  436. // }
  437. //
  438. // // 寻找A列的数据列表
  439. // firstEdbInfo, ok := edbInfoMap[tableDataConfig.TableEdbInfoList[0].EdbInfoId]
  440. // if !ok {
  441. // err = errors.New("找不到A列指标")
  442. // return
  443. // }
  444. // baseFirstEdbInfoDataList, err := GetFirstEdbDataList(firstEdbInfo, tableDataConfig.Num, manualDateList)
  445. // if err != nil {
  446. // return
  447. // }
  448. // // A列找不到数据,那么就直接返回吧
  449. // if len(baseFirstEdbInfoDataList) <= 0 {
  450. // return
  451. // }
  452. //
  453. // firstEdbInfoDataList := make([]request.ManualDataReq, 0)
  454. // if tableDataConfig.RemoveDate != nil && len(tableDataConfig.RemoveDate) > 0 {
  455. // for _, v := range baseFirstEdbInfoDataList {
  456. // if utils.InArrayByStr(tableDataConfig.RemoveDate, v.DataTime) {
  457. // continue
  458. // }
  459. // firstEdbInfoDataList = append(firstEdbInfoDataList, v)
  460. // }
  461. // } else {
  462. // firstEdbInfoDataList = baseFirstEdbInfoDataList
  463. // }
  464. // if len(firstEdbInfoDataList) <= 0 {
  465. // return
  466. // }
  467. // // 实际数据的最后一天
  468. // lastRealDateTime, err := time.ParseInLocation(utils.FormatDate, firstEdbInfoDataList[0].DataTime, time.Local)
  469. // if err != nil {
  470. // return
  471. // }
  472. //
  473. // dateMap := make(map[string]string)
  474. // dateList := make([]string, 0)
  475. // edbInfoIdDateDataMap := make(map[int]map[string]request.ManualDataReq)
  476. // firstDateDataMap := make(map[string]request.ManualDataReq)
  477. // for _, v := range firstEdbInfoDataList {
  478. // dateList = append(dateList, v.DataTime)
  479. // dateMap[v.DataTime] = v.DataTime
  480. // firstDateDataMap[v.DataTime] = v
  481. // }
  482. //
  483. // // 将手工数据的日期填补进去(未来的日期,过去的就不管了)
  484. // for _, manualData := range tableDataConfig.Data {
  485. // if !utils.InArrayByStr(dateList, manualData.DataTime) {
  486. // tmpDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, manualData.DataTime, time.Local)
  487. // if tmpErr != nil {
  488. // err = tmpErr
  489. // return
  490. // }
  491. // if tmpDateTime.After(lastRealDateTime) {
  492. // dateList = append(dateList, manualData.DataTime)
  493. // }
  494. // }
  495. // }
  496. //
  497. // edbInfoIdDateDataMap[firstEdbInfo.EdbInfoId] = firstDateDataMap
  498. //
  499. // for k, edbInfoId := range tableDataConfig.EdbInfoIdList {
  500. // if k == 0 {
  501. // continue
  502. // }
  503. //
  504. // tmpEdbInfo, ok := edbInfoMap[edbInfoId]
  505. // if !ok {
  506. // err = errors.New("找不到A列指标")
  507. // return
  508. // }
  509. // otherDataList, tmpErr := GetOtherEdbDataList(tmpEdbInfo, dateList)
  510. // if tmpErr != nil {
  511. // err = tmpErr
  512. // return
  513. // }
  514. //
  515. // tmpDateDataMap := make(map[string]request.ManualDataReq)
  516. // for _, v := range otherDataList {
  517. // tmpDateDataMap[v.DataTime] = v
  518. // }
  519. // edbInfoIdDateDataMap[tmpEdbInfo.EdbInfoId] = tmpDateDataMap
  520. // }
  521. //
  522. // for _, v := range tableDataConfig.Data {
  523. // tmpDate := v.DataTime
  524. // if _, ok := dateMap[tmpDate]; !ok {
  525. // dateMap[v.DataTime] = tmpDate
  526. // }
  527. //
  528. // edbInfoIdDateData, ok := edbInfoIdDateDataMap[v.EdbInfoId]
  529. // if !ok {
  530. // edbInfoIdDateData = make(map[string]request.ManualDataReq)
  531. // }
  532. //
  533. // // 判断是否存在该日期的数据(不存在,那么插入数据吧,存在就不管了)
  534. // if _, ok = edbInfoIdDateData[tmpDate]; !ok {
  535. // edbInfoIdDateData[tmpDate] = request.ManualDataReq{
  536. // DataType: v.DataType,
  537. // DataTime: v.DataTime,
  538. // ShowValue: v.ShowValue,
  539. // Value: v.Value,
  540. // }
  541. // }
  542. // edbInfoIdDateDataMap[v.EdbInfoId] = edbInfoIdDateData
  543. // }
  544. //
  545. // // 获取数据的日期排序
  546. // sortDateTimeList := make([]time.Time, 0)
  547. // {
  548. // sortDateList := dateList
  549. // if tableDataConfig.Sort == 1 {
  550. // baseDateList := utils.StrArr{}
  551. // baseDateList = append(baseDateList, sortDateList...)
  552. // sort.Sort(baseDateList)
  553. // sortDateList = append([]string{}, baseDateList...)
  554. // } else {
  555. // sort.Strings(sortDateList)
  556. // }
  557. // for _, v := range sortDateList {
  558. // tmpDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, v, time.Local)
  559. // if tmpErr != nil {
  560. // err = tmpErr
  561. // return
  562. // }
  563. // sortDateTimeList = append(sortDateTimeList, tmpDateTime)
  564. // }
  565. // }
  566. //
  567. // // 数据处理,处理成表格的数据格式
  568. // tableDataMap, textRowListDataResp := handleTable(tagEdbInfoIdMap, lastRealDateTime, sortDateTimeList, edbInfoIdDateDataMap, tableDataConfig.Data, tableDataConfig.TextRowData)
  569. //
  570. // data := make([]request.EdbInfoData, 0)
  571. // for _, tableEdbInfo := range tableDataConfig.TableEdbInfoList {
  572. // tagEdbInfoIdMap[tableEdbInfo.Tag] = tableEdbInfo.EdbInfoId
  573. //
  574. // manualDataReqList := make([]request.ManualDataReq, 0)
  575. // tmpEdbInfoData := request.EdbInfoData{
  576. // EdbInfoId: tableEdbInfo.EdbInfoId,
  577. // Tag: tableEdbInfo.Tag,
  578. // EdbName: tableEdbInfo.EdbName,
  579. // EdbAliasName: tableEdbInfo.EdbAliasName,
  580. // Frequency: tableEdbInfo.Frequency,
  581. // Unit: tableEdbInfo.Unit,
  582. // Data: manualDataReqList,
  583. // }
  584. //
  585. // edbInfo, ok := edbInfoMap[tableEdbInfo.EdbInfoId]
  586. // if ok {
  587. // tmpEdbInfoData.EdbName = edbInfo.EdbName
  588. // tmpEdbInfoData.Frequency = edbInfo.Frequency
  589. // tmpEdbInfoData.Unit = edbInfo.Unit
  590. // }
  591. //
  592. // for index, dateTime := range sortDateTimeList {
  593. // dataTimeType := 1
  594. // if dateTime.After(lastRealDateTime) {
  595. // dataTimeType = 2
  596. // }
  597. // tmpDateTimeStr := dateTime.Format(utils.FormatDate)
  598. //
  599. // rowData, ok := tableDataMap[index+1]
  600. // if !ok {
  601. // manualDataReqList = append(manualDataReqList, request.ManualDataReq{
  602. // DataType: 3,
  603. // DataTime: tmpDateTimeStr,
  604. // DataTimeType: dataTimeType,
  605. // ShowValue: "",
  606. // Value: "",
  607. // RelationEdbInfoList: nil,
  608. // })
  609. // continue
  610. // }
  611. //
  612. // tmpData, ok := rowData[tableEdbInfo.Tag]
  613. // if !ok {
  614. // manualDataReqList = append(manualDataReqList, request.ManualDataReq{
  615. // DataType: 3,
  616. // DataTime: tmpDateTimeStr,
  617. // DataTimeType: dataTimeType,
  618. // ShowValue: "",
  619. // Value: "",
  620. // RelationEdbInfoList: nil,
  621. // })
  622. // continue
  623. // }
  624. //
  625. // tmpData.DataTimeType = dataTimeType
  626. // manualDataReqList = append(manualDataReqList, tmpData)
  627. // }
  628. //
  629. // tmpEdbInfoData.Data = manualDataReqList
  630. //
  631. // data = append(data, tmpEdbInfoData)
  632. // }
  633. //
  634. // resultResp = request.TableDataReq{
  635. // EdbInfoIdList: edbInfoIdList,
  636. // Sort: tableDataConfig.Sort,
  637. // TextRowData: textRowListDataResp,
  638. // Data: data,
  639. // }
  640. //
  641. // return
  642. //}
  643. //
  644. //func handleTable(tagEdbInfoIdMap map[string]int, lastRealDateTime time.Time, sortDateTimeList []time.Time, edbInfoIdDateDataMap map[int]map[string]request.ManualDataReq, manualDataList []ManualData, textRowData [][]request.ManualDataReq) (tableDataMap map[int]map[string]request.ManualDataReq, textRowListDataResp [][]request.ManualDataReq) {
  645. // tagList := make([]string, 0)
  646. // for tag := range tagEdbInfoIdMap {
  647. // tagList = append(tagList, tag)
  648. // }
  649. // sort.Strings(tagList)
  650. //
  651. // tableDataMap = make(map[int]map[string]request.ManualDataReq) //行、列数据
  652. //
  653. // // 日期与行的关系
  654. // dateIndexMap := make(map[string]int)
  655. //
  656. // for k, dateTime := range sortDateTimeList {
  657. // rowDataMap := make(map[string]request.ManualDataReq)
  658. //
  659. // dataTimeType := 1
  660. // if dateTime.After(lastRealDateTime) {
  661. // dataTimeType = 2
  662. // }
  663. //
  664. // tmpDateTimeStr := dateTime.Format(utils.FormatDate)
  665. // dateIndexMap[tmpDateTimeStr] = k + 1
  666. //
  667. // for _, tag := range tagList {
  668. // edbInfoId, ok := tagEdbInfoIdMap[tag]
  669. // if !ok { // 没有找到该指标的映射关系,那么就用空串填补
  670. // rowDataMap[tag] = request.ManualDataReq{
  671. // DataType: 3,
  672. // DataTime: tmpDateTimeStr,
  673. // DataTimeType: dataTimeType,
  674. // ShowValue: "",
  675. // Value: "",
  676. // RelationEdbInfoList: nil,
  677. // }
  678. // continue
  679. // }
  680. //
  681. // // 获取指标的数据map
  682. // dateDataMap, ok := edbInfoIdDateDataMap[edbInfoId]
  683. // if !ok { // 没有找到该指标的数据,那么就用空串填补
  684. // rowDataMap[tag] = request.ManualDataReq{
  685. // DataType: 3,
  686. // DataTime: tmpDateTimeStr,
  687. // DataTimeType: dataTimeType,
  688. // ShowValue: "",
  689. // Value: "",
  690. // RelationEdbInfoList: nil,
  691. // }
  692. // continue
  693. // }
  694. //
  695. // // 获取指标该日期的数据
  696. // tmpData, ok := dateDataMap[tmpDateTimeStr]
  697. // if !ok { // 该指标没有找到对应日期的数据,那么就用空串填补
  698. // rowDataMap[tag] = request.ManualDataReq{
  699. // DataType: 3,
  700. // DataTime: tmpDateTimeStr,
  701. // DataTimeType: dataTimeType,
  702. // ShowValue: "",
  703. // Value: "",
  704. // RelationEdbInfoList: nil,
  705. // }
  706. // continue
  707. // }
  708. // tmpData.DataTimeType = dataTimeType
  709. // rowDataMap[tag] = tmpData
  710. // }
  711. // tableDataMap[k+1] = rowDataMap
  712. // }
  713. //
  714. // // 替换手工设置的数据
  715. // for _, manualData := range manualDataList {
  716. // // 找不到该日期,说明这日期过期了,不处理
  717. // index, ok := dateIndexMap[manualData.DataTime]
  718. // if !ok {
  719. // continue
  720. // }
  721. //
  722. // // 获取对应行的数据
  723. // rowDataMap, ok := tableDataMap[index]
  724. // if !ok {
  725. // continue
  726. // }
  727. //
  728. // // 找到对应的单元格
  729. // tmpData, ok := rowDataMap[manualData.Tag]
  730. // if !ok {
  731. // continue
  732. // }
  733. //
  734. // // 如果该单元格实际有数据(包含预测值),或者插值法补充了数据的话,那么就不用手动填入的数据
  735. // if utils.InArrayByInt([]int{1, 2, 5}, tmpData.DataType) {
  736. // continue
  737. // }
  738. //
  739. // // 手工填写的数字
  740. // if tmpData.DataType == 3 {
  741. // tmpData.ShowValue = manualData.ShowValue
  742. // tmpData.Value = manualData.Value
  743. // tableDataMap[index][manualData.Tag] = tmpData
  744. //
  745. // //edbInfoIdDateDataMap[manualData.EdbInfoId][manualData.DataTime] = tmpData
  746. // continue
  747. // }
  748. //
  749. // // 公式
  750. // tmpData.DataType = manualData.DataType
  751. // tmpData.ShowValue = ``
  752. // tmpData.Value = manualData.Value
  753. // tmpData.RelationEdbInfoList = manualData.RelationEdbInfoList
  754. // tableDataMap[index][manualData.Tag] = tmpData
  755. //
  756. // }
  757. //
  758. // // 文本行的列表插入
  759. // lenTableData := len(tableDataMap)
  760. //
  761. // // 文本行第一列的数据列表(可能多行)
  762. // firstColTextRowList := make([]request.ManualDataReq, 0)
  763. // // 参与计算的文本行列表数据
  764. // tmpTextRowList := make([][]request.ManualDataReq, 0)
  765. // for k, textRowList := range textRowData {
  766. // // 判断列数是否匹配,不匹配的话那么过滤
  767. // if len(tagList)+1 != len(textRowList) {
  768. // continue
  769. // }
  770. // rowDataMap := make(map[string]request.ManualDataReq)
  771. // tmpTextRow := make([]request.ManualDataReq, 0)
  772. // for index, textRow := range textRowList {
  773. // // 移除第一列,因为第一列是日期列
  774. // if index == 0 {
  775. // firstColTextRowList = append(firstColTextRowList, textRow)
  776. // continue
  777. // }
  778. // rowDataMap[tagList[index-1]] = textRow
  779. // tmpTextRow = append(tmpTextRow, textRow)
  780. // }
  781. //
  782. // tableDataMap[lenTableData+k+1] = rowDataMap
  783. // tmpTextRowList = append(tmpTextRowList, tmpTextRow)
  784. // }
  785. //
  786. // // 参与计算的单元格
  787. // calculateCellMap := make(map[string]string)
  788. //
  789. // // 计算手工填写的单元格
  790. // for _, manualData := range manualDataList {
  791. // // 找不到该日期,说明这日期过期了,不处理
  792. // index, ok := dateIndexMap[manualData.DataTime]
  793. // if !ok {
  794. // continue
  795. // }
  796. //
  797. // // 获取对应行的数据
  798. // rowDataMap, ok := tableDataMap[index]
  799. // if !ok {
  800. // continue
  801. // }
  802. //
  803. // // 找到对应的单元格
  804. // colData, ok := rowDataMap[manualData.Tag]
  805. // if !ok {
  806. // continue
  807. // }
  808. //
  809. // // 如果该单元格不是计算公式的单元格,那么直接退出当前循环即可
  810. // if colData.DataType != 4 {
  811. // continue
  812. // }
  813. //
  814. // tagMap := make(map[string]float64)
  815. // lenRelation := len(colData.RelationEdbInfoList)
  816. // replaceNum := 0
  817. // for _, relation := range colData.RelationEdbInfoList {
  818. // relationCellTagName := strings.ToUpper(relation.Tag) + relation.Row
  819. // valStr, tmpErr := getCalculateValue(tableDataMap, relation.Tag, relation.Row, calculateCellMap)
  820. // if tmpErr != nil {
  821. // continue
  822. // }
  823. // tmpValDecimal, tmpErr := decimal.NewFromString(valStr)
  824. // if tmpErr != nil {
  825. // continue
  826. // }
  827. // tagMap[relationCellTagName], _ = tmpValDecimal.Float64()
  828. // replaceNum++
  829. // }
  830. //
  831. // // 如果替换的数据与关联的不一致,那么就退出当前循环
  832. // if lenRelation != replaceNum {
  833. // continue
  834. // }
  835. //
  836. // // 计算
  837. // val, _, err := calculate(strings.ToUpper(colData.Value), tagMap)
  838. // // 计算失败,退出循环
  839. // if err != nil {
  840. // continue
  841. // }
  842. // // 重新赋值
  843. // colData.ShowValue = val
  844. // tableDataMap[index][manualData.Tag] = colData
  845. //
  846. // }
  847. //
  848. // // 计算文本行的单元格
  849. // for k, textRow := range tmpTextRowList {
  850. // // 获取对应行的数据
  851. // index := lenTableData + k + 1
  852. // rowDataMap, ok := tableDataMap[index]
  853. // if !ok {
  854. // continue
  855. // }
  856. //
  857. // for colIndex := range textRow {
  858. // currTag := tagList[colIndex]
  859. // // 找到对应的单元格
  860. // colData, ok := rowDataMap[currTag]
  861. // if !ok {
  862. // continue
  863. // }
  864. //
  865. // // 如果该单元格不是计算公式的单元格,那么直接退出当前循环即可
  866. // if colData.DataType != 4 {
  867. // continue
  868. // }
  869. //
  870. // tagMap := make(map[string]float64)
  871. // lenRelation := len(colData.RelationEdbInfoList)
  872. // replaceNum := 0
  873. // for _, relation := range colData.RelationEdbInfoList {
  874. // relationCellTagName := strings.ToUpper(relation.Tag) + relation.Row
  875. // valStr, tmpErr := getCalculateValue(tableDataMap, relation.Tag, relation.Row, calculateCellMap)
  876. // if tmpErr != nil {
  877. // continue
  878. // }
  879. // tmpValDecimal, tmpErr := decimal.NewFromString(valStr)
  880. // if tmpErr != nil {
  881. // continue
  882. // }
  883. // tagMap[relationCellTagName], _ = tmpValDecimal.Float64()
  884. // replaceNum++
  885. // }
  886. //
  887. // // 如果替换的数据与关联的不一致,那么就退出当前循环
  888. // if lenRelation != replaceNum {
  889. // continue
  890. // }
  891. //
  892. // // 计算
  893. // val, _, err := calculate(strings.ToUpper(colData.Value), tagMap)
  894. // // 计算失败,退出循环
  895. // if err != nil {
  896. // continue
  897. // }
  898. // // 重新赋值
  899. // colData.ShowValue = val
  900. // tableDataMap[index][currTag] = colData
  901. // }
  902. //
  903. // }
  904. //
  905. // // 计算文本行第一列的数据值(多行)
  906. // for k, colData := range firstColTextRowList {
  907. // // 如果该单元格不是计算公式的单元格,那么直接退出当前循环即可
  908. // if colData.DataType != 4 {
  909. // continue
  910. // }
  911. //
  912. // tagMap := make(map[string]float64)
  913. // lenRelation := len(colData.RelationEdbInfoList)
  914. // replaceNum := 0
  915. // for _, relation := range colData.RelationEdbInfoList {
  916. // relationCellTagName := strings.ToUpper(relation.Tag) + relation.Row
  917. // valStr, tmpErr := getCalculateValue(tableDataMap, relation.Tag, relation.Row, calculateCellMap)
  918. // if tmpErr != nil {
  919. // continue
  920. // }
  921. // tmpValDecimal, tmpErr := decimal.NewFromString(valStr)
  922. // if tmpErr != nil {
  923. // continue
  924. // }
  925. // tagMap[relationCellTagName], _ = tmpValDecimal.Float64()
  926. // replaceNum++
  927. // }
  928. //
  929. // // 如果替换的数据与关联的不一致,那么就退出当前循环
  930. // if lenRelation != replaceNum {
  931. // continue
  932. // }
  933. //
  934. // // 计算
  935. // val, _, err := calculate(strings.ToUpper(colData.Value), tagMap)
  936. // // 计算失败,退出循环
  937. // if err != nil {
  938. // continue
  939. // }
  940. // // 重新赋值
  941. // colData.ShowValue = val
  942. // firstColTextRowList[k] = colData
  943. // }
  944. //
  945. // {
  946. // // 文本行的数据处理返回
  947. // textRowListDataResp = make([][]request.ManualDataReq, 0)
  948. // newLenTableDataMap := len(tableDataMap)
  949. // // 文本行的第一行所在的位置
  950. // firstTextRow := lenTableData + 1
  951. // for i := firstTextRow; i <= newLenTableDataMap; i++ {
  952. // textRowDataResp := make([]request.ManualDataReq, 0)
  953. //
  954. // textRowDataResp = append(textRowDataResp, firstColTextRowList[i-firstTextRow])
  955. // for _, tmpTag := range tagList {
  956. // textRowDataResp = append(textRowDataResp, tableDataMap[i][tmpTag])
  957. // }
  958. // textRowListDataResp = append(textRowListDataResp, textRowDataResp)
  959. // }
  960. //
  961. // }
  962. //
  963. // return
  964. //}
  965. //
  966. //// getCalculateValue 获取公式计算的结果
  967. //func getCalculateValue(tableDataMap map[int]map[string]request.ManualDataReq, tag, row string, calculateCellMap map[string]string) (val string, err error) {
  968. // rowInt, err := strconv.Atoi(row)
  969. // if err != nil {
  970. // return
  971. // }
  972. //
  973. // // 单元格的标签名
  974. // cellTagName := strings.ToUpper(tag) + row
  975. // val, ok := calculateCellMap[cellTagName]
  976. // if ok {
  977. // return
  978. // }
  979. //
  980. // // 查找行数据
  981. // rowData, ok := tableDataMap[rowInt]
  982. // if !ok {
  983. // err = errors.New("查找" + row + "行的数据失败")
  984. // return
  985. // }
  986. //
  987. // // 查找单元格数据
  988. // colData, ok := rowData[tag]
  989. // if !ok {
  990. // err = errors.New("查找单元格" + tag + row + "的数据失败")
  991. // return
  992. // }
  993. //
  994. // // 如果不是计算单元格
  995. // if colData.DataType != 4 {
  996. // val = colData.ShowValue
  997. // return
  998. // }
  999. //
  1000. // // 如果是计算单元格
  1001. // calculateCellMap[cellTagName] = ``
  1002. //
  1003. // tagMap := make(map[string]float64)
  1004. // for _, relation := range colData.RelationEdbInfoList {
  1005. // relationCellTagName := strings.ToUpper(relation.Tag) + relation.Row
  1006. // valStr, tmpErr := getCalculateValue(tableDataMap, relation.Tag, relation.Row, calculateCellMap)
  1007. // if tmpErr != nil {
  1008. // err = tmpErr
  1009. // return
  1010. // }
  1011. // tmpValDecimal, tmpErr := decimal.NewFromString(valStr)
  1012. // if tmpErr != nil {
  1013. // err = tmpErr
  1014. // return
  1015. // }
  1016. // tagMap[relationCellTagName], _ = tmpValDecimal.Float64()
  1017. // }
  1018. //
  1019. // // 计算
  1020. // val, _, err = calculate(strings.ToUpper(colData.Value), tagMap)
  1021. // if err != nil {
  1022. // return
  1023. // }
  1024. // // 重新赋值
  1025. // colData.ShowValue = val
  1026. // tableDataMap[rowInt][tag] = colData
  1027. // calculateCellMap[cellTagName] = val
  1028. //
  1029. // return
  1030. //}
  1031. //
  1032. //// calculate 公式计算
  1033. //func calculate(calculateFormula string, TagMap map[string]float64) (calVal, errMsg string, err error) {
  1034. // if calculateFormula == "" {
  1035. // errMsg = "公式异常"
  1036. // err = errors.New(errMsg)
  1037. // return
  1038. // }
  1039. // calculateFormula = strings.TrimPrefix(calculateFormula, "=")
  1040. // calculateFormula = strings.Replace(calculateFormula, "(", "(", -1)
  1041. // calculateFormula = strings.Replace(calculateFormula, ")", ")", -1)
  1042. // calculateFormula = strings.Replace(calculateFormula, ",", ",", -1)
  1043. // calculateFormula = strings.Replace(calculateFormula, "。", ".", -1)
  1044. // calculateFormula = strings.Replace(calculateFormula, "%", "*0.01", -1)
  1045. //
  1046. // formulaFormStr := utils.ReplaceFormula(TagMap, calculateFormula)
  1047. // //计算公式异常,那么就移除该指标
  1048. // if formulaFormStr == `` {
  1049. // errMsg = "公式异常"
  1050. // err = errors.New(errMsg)
  1051. // return
  1052. // }
  1053. //
  1054. // expression := formula.NewExpression(formulaFormStr)
  1055. // calResult, err := expression.Evaluate()
  1056. // if err != nil {
  1057. // errMsg = "计算失败"
  1058. // err = errors.New("计算失败:Err:" + err.Error() + ";formulaStr:" + formulaFormStr)
  1059. // // 分母为0的报错
  1060. // if strings.Contains(err.Error(), "divide by zero") {
  1061. // errMsg = "分母不能为0"
  1062. // err = errors.New("分母不能为空,计算公式:" + formulaFormStr)
  1063. // }
  1064. // return
  1065. // }
  1066. // // 如果计算结果是NAN,那么就提示报错
  1067. // if calResult.IsNan() {
  1068. // errMsg = "计算失败"
  1069. // err = errors.New("计算失败:计算结果是:NAN;formulaStr:" + formulaFormStr)
  1070. // return
  1071. // }
  1072. // calVal = calResult.String()
  1073. //
  1074. // // 转Decimal然后四舍五入
  1075. // valDecimal, err := decimal.NewFromString(calVal)
  1076. // if err != nil {
  1077. // errMsg = "计算失败"
  1078. // err = errors.New("计算失败,结果转 Decimal 失败:Err:" + err.Error() + ";formulaStr:" + formulaFormStr)
  1079. // return
  1080. // }
  1081. // calVal = valDecimal.Round(4).String()
  1082. //
  1083. // return
  1084. //}