excel_info.go 39 KB

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