excel_info.go 39 KB

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