excel_info.go 39 KB

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