excel_info.go 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163
  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. }
  961. // GetMixedTableCellData 获取混合表格数据
  962. func GetMixedTableCellData(config [][]request.MixedTableCellDataReq) (newMixedTableCellDataList [][]request.MixedTableCellDataReq, err error) {
  963. newMixedTableCellDataList = config
  964. edbInfoIdList := make([]int, 0)
  965. dataEdbInfoIdList := make([]int, 0)
  966. for _, row := range config {
  967. for _, cell := range row {
  968. if cell.DataType == 2 {
  969. edbInfoIdList = append(edbInfoIdList, cell.EdbInfoId)
  970. } else if cell.DataType == 4 {
  971. dataEdbInfoIdList = append(dataEdbInfoIdList, cell.EdbInfoId)
  972. }
  973. }
  974. }
  975. edbInfoList, err := data_manage.GetEdbInfoByIdList(edbInfoIdList)
  976. if err != nil {
  977. return
  978. }
  979. // 指标信息map
  980. edbInfoMap := make(map[int]*data_manage.EdbInfo)
  981. // 指标数据map
  982. edbDataListMap := make(map[int]map[string]float64)
  983. for _, edbInfo := range edbInfoList {
  984. edbInfoMap[edbInfo.EdbInfoId] = edbInfo
  985. dataList := make([]*models.EdbDataList, 0)
  986. switch edbInfo.EdbInfoType {
  987. case 0:
  988. dataList, _ = models.GetEdbDataList(edbInfo.Source, edbInfo.EdbInfoId, ``, ``)
  989. case 1:
  990. _, dataList, _, _, _, _ = GetPredictDataListByPredictEdbInfoId(edbInfo.EdbInfoId, ``, ``, false)
  991. default:
  992. err = errors.New(fmt.Sprint("获取失败,指标类型异常", edbInfo.EdbInfoType))
  993. }
  994. dateValMap := make(map[string]float64)
  995. for _, data := range dataList {
  996. dateValMap[data.DataTime] = data.Value
  997. }
  998. edbDataListMap[edbInfo.EdbInfoId] = dateValMap
  999. }
  1000. for k, row := range newMixedTableCellDataList {
  1001. for i, cell := range row {
  1002. if cell.DataType == 2 {
  1003. if edbInfo, ok := edbInfoMap[cell.EdbInfoId]; ok {
  1004. cell.ShowValue = edbInfo.EdbName
  1005. }
  1006. } else if cell.DataType == 4 {
  1007. if dateValMap, ok := edbDataListMap[cell.EdbInfoId]; ok {
  1008. if val, ok2 := dateValMap[cell.DataTime]; ok2 {
  1009. cell.ShowValue = fmt.Sprint(val)
  1010. }
  1011. }
  1012. }
  1013. row[i] = cell
  1014. }
  1015. newMixedTableCellDataList[k] = row
  1016. }
  1017. return
  1018. }