excel_info.go 31 KB

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