excel_info.go 39 KB

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