excel_info.go 38 KB

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