excel_info.go 34 KB

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