excel_info.go 39 KB

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