excel_info.go 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295
  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_crm_api/models/data_manage"
  9. "hongze/hz_crm_api/models/data_manage/request"
  10. "hongze/hz_crm_api/models/data_manage/response"
  11. "hongze/hz_crm_api/models/system"
  12. "hongze/hz_crm_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. for i := 1; i <= num; i++ {
  150. resultDataList = append(resultDataList, request.ManualDataReq{
  151. DataType: 1,
  152. DataTime: dataList[lenData-i].DataTime,
  153. ShowValue: fmt.Sprint(dataList[lenData-i].Value),
  154. Value: fmt.Sprint(dataList[lenData-i].Value),
  155. DataTimeType: 1,
  156. })
  157. }
  158. return
  159. }
  160. // GetOtherEdbDataList 获取其他列的数据
  161. func GetOtherEdbDataList(edbInfo *data_manage.EdbInfo, dateList []string) (resultDataList []request.ManualDataReq, err error) {
  162. lenDate := len(dateList)
  163. if lenDate <= 0 {
  164. return
  165. }
  166. sortDateList := dateList
  167. baseDateList := utils.StrArr{}
  168. baseDateList = append(baseDateList, sortDateList...)
  169. sort.Sort(baseDateList)
  170. sortDateList = append([]string{}, baseDateList...)
  171. endDateTime, err := time.ParseInLocation(utils.FormatDate, sortDateList[0], time.Local)
  172. if err != nil {
  173. return
  174. }
  175. firstDateTime, err := time.ParseInLocation(utils.FormatDate, sortDateList[lenDate-1], time.Local)
  176. if err != nil {
  177. return
  178. }
  179. var dataList []*data_manage.EdbDataList
  180. switch edbInfo.EdbInfoType {
  181. case 0:
  182. dataList, err = data_manage.GetEdbDataList(edbInfo.Source, edbInfo.EdbInfoId, ``, ``)
  183. case 1:
  184. _, dataList, _, _, err, _ = GetPredictDataListByPredictEdbInfoId(edbInfo.EdbInfoId, ``, ``, false)
  185. default:
  186. err = errors.New(fmt.Sprint("获取失败,指标类型异常", edbInfo.EdbInfoType))
  187. }
  188. if err != nil {
  189. return
  190. }
  191. // 获取日期内的数据(包含开始日期前一个日期,以及 结束日期后一个日期,目的为了做空日期时的 插值法兼容)
  192. baseDataList := make([]*data_manage.EdbDataList, 0)
  193. var lastData *data_manage.EdbDataList
  194. var isInsert bool
  195. for _, data := range dataList {
  196. tmpDate := data.DataTime
  197. tmpDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, tmpDate, time.Local)
  198. if tmpErr != nil {
  199. err = tmpErr
  200. return
  201. }
  202. if tmpDateTime.Before(firstDateTime) {
  203. lastData = data
  204. continue
  205. }
  206. // 如果是第一次写入数据
  207. if !isInsert && lastData != nil {
  208. baseDataList = append(baseDataList, lastData)
  209. }
  210. if tmpDateTime.After(endDateTime) {
  211. baseDataList = append(baseDataList, data)
  212. break
  213. }
  214. baseDataList = append(baseDataList, data)
  215. isInsert = true
  216. }
  217. // 实际数据的日期map
  218. realValMap := make(map[string]string)
  219. for _, v := range baseDataList {
  220. realValMap[v.DataTime] = v.DataTime
  221. }
  222. // 插值法处理
  223. handleDataMap := make(map[string]float64)
  224. err = handleDataByLinearRegression(baseDataList, handleDataMap)
  225. if err != nil {
  226. return
  227. }
  228. // 对于不存在的数据做补充
  229. for _, date := range sortDateList {
  230. dataType := 1
  231. if _, ok := realValMap[date]; !ok {
  232. dataType = 2
  233. }
  234. var value, showValue string
  235. if tmpVal, ok := handleDataMap[date]; ok {
  236. value = fmt.Sprint(tmpVal)
  237. showValue = value
  238. } else {
  239. dataType = 3
  240. }
  241. resultDataList = append(resultDataList, request.ManualDataReq{
  242. DataType: dataType,
  243. DataTime: date,
  244. ShowValue: showValue,
  245. Value: value,
  246. })
  247. }
  248. return
  249. }
  250. // GetFirstHistoryEdbDataList 获取指标的历史的数据
  251. func GetFirstHistoryEdbDataList(edbInfo *data_manage.EdbInfo, num int, endDate string) (resultDataList []request.ManualDataReq, err error) {
  252. endDateTime, err := time.ParseInLocation(utils.FormatDate, endDate, time.Local)
  253. if err != nil {
  254. return
  255. }
  256. var dataList []*data_manage.EdbDataList
  257. switch edbInfo.EdbInfoType {
  258. case 0:
  259. dataList, err = data_manage.GetEdbDataList(edbInfo.Source, edbInfo.EdbInfoId, ``, endDate)
  260. case 1:
  261. _, dataList, _, _, err, _ = GetPredictDataListByPredictEdbInfoId(edbInfo.EdbInfoId, ``, endDate, true)
  262. default:
  263. err = errors.New(fmt.Sprint("获取失败,指标类型异常", edbInfo.EdbInfoType))
  264. }
  265. if err != nil {
  266. return
  267. }
  268. // 获取需要的期数
  269. lenData := len(dataList)
  270. if lenData <= 0 {
  271. return
  272. }
  273. lastData := dataList[lenData-1]
  274. lastDataDateTime, err := time.ParseInLocation(utils.FormatDate, lastData.DataTime, time.Local)
  275. if err != nil {
  276. return
  277. }
  278. if endDateTime.Equal(lastDataDateTime) || lastDataDateTime.After(endDateTime) {
  279. dataList = dataList[:lenData-1]
  280. lenData = len(dataList)
  281. }
  282. if num > lenData {
  283. num = lenData
  284. }
  285. for i := 1; i <= num; i++ {
  286. resultDataList = append(resultDataList, request.ManualDataReq{
  287. DataType: 1,
  288. DataTime: dataList[lenData-i].DataTime,
  289. ShowValue: fmt.Sprint(dataList[lenData-i].Value),
  290. Value: fmt.Sprint(dataList[lenData-i].Value),
  291. })
  292. }
  293. return
  294. }
  295. type TableDataConfig struct {
  296. EdbInfoIdList []int `description:"指标id列表,从左至右,从上到下的顺序"`
  297. Sort int `description:"日期排序,0:倒序,1:正序"`
  298. Data []ManualData `description:"数据列表"`
  299. Num int `description:"实际数据需要列出来的期数"`
  300. RemoveDate []string `description:"不展示的日期"`
  301. ManualDate []string `description:"手动配置的日期(未来的日期)"`
  302. TableEdbInfoList []TableEdbInfo `description:"表格内指标信息"`
  303. TextRowData [][]request.ManualDataReq `description:"文本列表"`
  304. }
  305. type TableEdbInfo struct {
  306. EdbInfoId int `description:"指标ID"`
  307. Tag string `description:"标签"`
  308. EdbName string `description:"指标名称"`
  309. EdbAliasName string `description:"指标别名"`
  310. Frequency string `description:"频度"`
  311. Unit string `description:"单位"`
  312. }
  313. type ManualData struct {
  314. DataType int `description:"数据类型,1:普通的,2:插值法,3:手动输入,4:公式计算"`
  315. DataTime string `description:"所属日期"`
  316. DataTimeType int `description:"日期类型,1:实际日期;2:未来日期"`
  317. ShowValue string `description:"展示值"`
  318. Value string `description:"实际值(计算公式)"`
  319. EdbInfoId int `description:"指标id"`
  320. Tag string `description:"下标"`
  321. RelationEdbInfoList []request.RelationEdbInfo `description:"关联指标(计算公式中关联的指标,用于计算的时候去匹配)"`
  322. }
  323. // GetTableDataConfig 根据TableDataReq配置获取相关数据配置
  324. func GetTableDataConfig(reqData request.TableDataReq) (tableDataConfig TableDataConfig, err error) {
  325. // 指标数据
  326. tableDataConfig.EdbInfoIdList = reqData.EdbInfoIdList
  327. tableDataConfig.Sort = reqData.Sort
  328. if len(reqData.Data) <= 0 {
  329. err = errors.New("数据不能为空")
  330. return
  331. }
  332. // 开始日期
  333. var startDate string
  334. // A列的指标id
  335. var firstEdbInfoId int
  336. // 手工操作的数据列
  337. manualDataList := make([]ManualData, 0)
  338. // 指标配置列表
  339. tableEdbInfoList := make([]TableEdbInfo, 0)
  340. // 第一列的日期map
  341. firstDateMap := make(map[string]string)
  342. manualDateMap := make(map[string]string)
  343. for _, v := range reqData.Data {
  344. // 指标信息
  345. tmpTableEdbInfo := TableEdbInfo{
  346. EdbInfoId: v.EdbInfoId,
  347. Tag: v.Tag,
  348. EdbName: v.EdbName,
  349. EdbAliasName: v.EdbAliasName,
  350. Frequency: v.Frequency,
  351. Unit: v.Unit,
  352. }
  353. tableEdbInfoList = append(tableEdbInfoList, tmpTableEdbInfo)
  354. // 确定数据A列
  355. if v.Tag == "A" {
  356. firstEdbInfoId = v.EdbInfoId
  357. lenData := len(v.Data)
  358. if lenData <= 0 {
  359. err = errors.New("A列不能为空")
  360. return
  361. }
  362. index := 0
  363. if reqData.Sort == 1 {
  364. // 倒序
  365. index = lenData - 1
  366. }
  367. startDate = v.Data[index].DataTime
  368. // 存在的日期列表
  369. for _, data := range v.Data {
  370. firstDateMap[data.DataTime] = data.DataTime
  371. if data.DataTimeType == 2 {
  372. manualDateMap[data.DataTime] = data.DataTime
  373. }
  374. }
  375. }
  376. for _, data := range v.Data {
  377. if data.DataType == 3 || data.DataType == 4 {
  378. tmpManualData := ManualData{
  379. DataType: data.DataType,
  380. DataTime: data.DataTime,
  381. DataTimeType: data.DataTimeType,
  382. ShowValue: data.ShowValue,
  383. Value: data.Value,
  384. EdbInfoId: v.EdbInfoId,
  385. Tag: v.Tag,
  386. RelationEdbInfoList: data.RelationEdbInfoList,
  387. }
  388. if data.DataType == 4 {
  389. tmpManualData.ShowValue = ``
  390. }
  391. manualDataList = append(manualDataList, tmpManualData)
  392. }
  393. }
  394. }
  395. // 总共需要的期数
  396. num := len(reqData.Data[0].Data)
  397. removeDate := make([]string, 0)
  398. // 获取期数
  399. {
  400. firstDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, startDate, time.Local)
  401. if tmpErr != nil {
  402. err = tmpErr
  403. return
  404. }
  405. edbInfo, tmpErr := data_manage.GetEdbInfoById(firstEdbInfoId)
  406. if tmpErr != nil {
  407. err = tmpErr
  408. return
  409. }
  410. var firstDataList []*data_manage.EdbDataList
  411. switch edbInfo.EdbInfoType {
  412. case 0:
  413. firstDataList, err = data_manage.GetEdbDataList(edbInfo.Source, edbInfo.EdbInfoId, ``, ``)
  414. case 1:
  415. _, firstDataList, _, _, err, _ = GetPredictDataListByPredictEdbInfoId(edbInfo.EdbInfoId, ``, ``, false)
  416. default:
  417. err = errors.New(fmt.Sprint("获取失败,指标类型异常", edbInfo.EdbInfoType))
  418. }
  419. if err != nil {
  420. return
  421. }
  422. // 获取日期内的数据(包含开始日期前一个日期,以及 结束日期后一个日期,目的为了做空日期时的 插值法兼容)
  423. baseDataList := make([]*data_manage.EdbDataList, 0)
  424. for _, data := range firstDataList {
  425. tmpDate := data.DataTime
  426. tmpDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, tmpDate, time.Local)
  427. if tmpErr != nil {
  428. err = tmpErr
  429. return
  430. }
  431. if tmpDateTime.Before(firstDateTime) {
  432. continue
  433. }
  434. baseDataList = append(baseDataList, data)
  435. }
  436. // 筛选出需要删除的日期
  437. for _, tmpData := range baseDataList {
  438. //firstDateMap{}
  439. if _, ok := firstDateMap[tmpData.DataTime]; !ok {
  440. removeDate = append(removeDate, tmpData.DataTime)
  441. }
  442. }
  443. }
  444. tableDataConfig.Num = num
  445. tableDataConfig.RemoveDate = removeDate
  446. tableDataConfig.Data = manualDataList
  447. tableDataConfig.TableEdbInfoList = tableEdbInfoList
  448. tableDataConfig.TextRowData = reqData.TextRowData
  449. return
  450. }
  451. // GetDataByTableDataConfig 根据数据配置获取表格数据
  452. func GetDataByTableDataConfig(tableDataConfig TableDataConfig) (resultResp request.TableDataReq, err error) {
  453. // 没有选择指标的情况下,直接返回吧
  454. if len(tableDataConfig.EdbInfoIdList) <= 0 {
  455. return
  456. }
  457. // 实际期数没有的情况下,直接返回吧
  458. if tableDataConfig.Num <= 0 {
  459. return
  460. }
  461. // 获取所有的指标信息
  462. edbInfoMap := make(map[int]*data_manage.EdbInfo)
  463. edbInfoIdList := make([]int, 0)
  464. // 标签与指标id的map
  465. tagEdbInfoIdMap := make(map[string]int)
  466. {
  467. for _, tableEdbInfo := range tableDataConfig.TableEdbInfoList {
  468. edbInfoIdList = append(edbInfoIdList, tableEdbInfo.EdbInfoId)
  469. tagEdbInfoIdMap[tableEdbInfo.Tag] = tableEdbInfo.EdbInfoId
  470. }
  471. edbInfoList, tmpErr := data_manage.GetEdbInfoByIdList(edbInfoIdList)
  472. if tmpErr != nil {
  473. err = tmpErr
  474. return
  475. }
  476. for _, v := range edbInfoList {
  477. edbInfoMap[v.EdbInfoId] = v
  478. }
  479. }
  480. manualDateMap := make(map[string]string, 0)
  481. manualDateList := make([]string, 0)
  482. for _, v := range tableDataConfig.Data {
  483. if _, ok := manualDateMap[v.DataTime]; !ok {
  484. manualDateMap[v.DataTime] = v.DataTime
  485. manualDateList = append(manualDateList, v.DataTime)
  486. }
  487. }
  488. // 寻找A列的数据列表
  489. firstEdbInfo, ok := edbInfoMap[tableDataConfig.TableEdbInfoList[0].EdbInfoId]
  490. if !ok {
  491. err = errors.New("找不到A列指标")
  492. return
  493. }
  494. baseFirstEdbInfoDataList, err := GetFirstEdbDataList(firstEdbInfo, tableDataConfig.Num, manualDateList)
  495. if err != nil {
  496. return
  497. }
  498. // A列找不到数据,那么就直接返回吧
  499. if len(baseFirstEdbInfoDataList) <= 0 {
  500. return
  501. }
  502. firstEdbInfoDataList := make([]request.ManualDataReq, 0)
  503. if tableDataConfig.RemoveDate != nil && len(tableDataConfig.RemoveDate) > 0 {
  504. for _, v := range baseFirstEdbInfoDataList {
  505. if utils.InArrayByStr(tableDataConfig.RemoveDate, v.DataTime) {
  506. continue
  507. }
  508. firstEdbInfoDataList = append(firstEdbInfoDataList, v)
  509. }
  510. } else {
  511. firstEdbInfoDataList = baseFirstEdbInfoDataList
  512. }
  513. if len(firstEdbInfoDataList) <= 0 {
  514. return
  515. }
  516. // 实际数据的最后一天
  517. lastRealDateTime, err := time.ParseInLocation(utils.FormatDate, firstEdbInfoDataList[0].DataTime, time.Local)
  518. if err != nil {
  519. return
  520. }
  521. dateMap := make(map[string]string)
  522. dateList := make([]string, 0)
  523. edbInfoIdDateDataMap := make(map[int]map[string]request.ManualDataReq)
  524. firstDateDataMap := make(map[string]request.ManualDataReq)
  525. for _, v := range firstEdbInfoDataList {
  526. dateList = append(dateList, v.DataTime)
  527. dateMap[v.DataTime] = v.DataTime
  528. firstDateDataMap[v.DataTime] = v
  529. }
  530. // 将手工数据的日期填补进去(未来的日期,过去的就不管了)
  531. for _, manualData := range tableDataConfig.Data {
  532. if !utils.InArrayByStr(dateList, manualData.DataTime) {
  533. tmpDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, manualData.DataTime, time.Local)
  534. if tmpErr != nil {
  535. err = tmpErr
  536. return
  537. }
  538. if tmpDateTime.After(lastRealDateTime) {
  539. dateList = append(dateList, manualData.DataTime)
  540. }
  541. }
  542. }
  543. edbInfoIdDateDataMap[firstEdbInfo.EdbInfoId] = firstDateDataMap
  544. for k, edbInfoId := range tableDataConfig.EdbInfoIdList {
  545. if k == 0 {
  546. continue
  547. }
  548. tmpEdbInfo, ok := edbInfoMap[edbInfoId]
  549. if !ok {
  550. err = errors.New("找不到A列指标")
  551. return
  552. }
  553. otherDataList, tmpErr := GetOtherEdbDataList(tmpEdbInfo, dateList)
  554. if tmpErr != nil {
  555. err = tmpErr
  556. return
  557. }
  558. tmpDateDataMap := make(map[string]request.ManualDataReq)
  559. for _, v := range otherDataList {
  560. tmpDateDataMap[v.DataTime] = v
  561. }
  562. edbInfoIdDateDataMap[tmpEdbInfo.EdbInfoId] = tmpDateDataMap
  563. }
  564. for _, v := range tableDataConfig.Data {
  565. tmpDate := v.DataTime
  566. if _, ok := dateMap[tmpDate]; !ok {
  567. dateMap[v.DataTime] = tmpDate
  568. }
  569. edbInfoIdDateData, ok := edbInfoIdDateDataMap[v.EdbInfoId]
  570. if !ok {
  571. edbInfoIdDateData = make(map[string]request.ManualDataReq)
  572. }
  573. // 判断是否存在该日期的数据(不存在,那么插入数据吧,存在就不管了)
  574. tmpManualData, ok := edbInfoIdDateData[tmpDate]
  575. if !ok {
  576. edbInfoIdDateData[tmpDate] = request.ManualDataReq{
  577. DataType: v.DataType,
  578. DataTime: v.DataTime,
  579. ShowValue: v.ShowValue,
  580. Value: v.Value,
  581. }
  582. } else {
  583. if (tmpManualData.DataType == 3 || tmpManualData.DataType == 4) && tmpManualData.ShowValue == `` {
  584. tmpManualData.DataType = v.DataType
  585. tmpManualData.ShowValue = v.ShowValue
  586. tmpManualData.Value = v.Value
  587. tmpManualData.RelationEdbInfoList = v.RelationEdbInfoList
  588. edbInfoIdDateData[tmpDate] = tmpManualData
  589. }
  590. }
  591. edbInfoIdDateDataMap[v.EdbInfoId] = edbInfoIdDateData
  592. }
  593. // 获取数据的日期排序
  594. sortDateTimeList := make([]time.Time, 0)
  595. {
  596. sortDateList := dateList
  597. if tableDataConfig.Sort == 1 {
  598. baseDateList := utils.StrArr{}
  599. baseDateList = append(baseDateList, sortDateList...)
  600. sort.Sort(baseDateList)
  601. sortDateList = append([]string{}, baseDateList...)
  602. } else {
  603. sort.Strings(sortDateList)
  604. }
  605. for _, v := range sortDateList {
  606. tmpDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, v, time.Local)
  607. if tmpErr != nil {
  608. err = tmpErr
  609. return
  610. }
  611. sortDateTimeList = append(sortDateTimeList, tmpDateTime)
  612. }
  613. }
  614. // 数据处理,处理成表格的数据格式
  615. tableDataMap, textRowListDataResp := handleTable(tagEdbInfoIdMap, lastRealDateTime, sortDateTimeList, edbInfoIdDateDataMap, tableDataConfig.Data, tableDataConfig.TextRowData)
  616. data := make([]request.EdbInfoData, 0)
  617. for _, tableEdbInfo := range tableDataConfig.TableEdbInfoList {
  618. tagEdbInfoIdMap[tableEdbInfo.Tag] = tableEdbInfo.EdbInfoId
  619. manualDataReqList := make([]request.ManualDataReq, 0)
  620. tmpEdbInfoData := request.EdbInfoData{
  621. EdbInfoId: tableEdbInfo.EdbInfoId,
  622. Tag: tableEdbInfo.Tag,
  623. EdbName: tableEdbInfo.EdbName,
  624. EdbAliasName: tableEdbInfo.EdbAliasName,
  625. Frequency: tableEdbInfo.Frequency,
  626. Unit: tableEdbInfo.Unit,
  627. Data: manualDataReqList,
  628. }
  629. edbInfo, ok := edbInfoMap[tableEdbInfo.EdbInfoId]
  630. if ok {
  631. tmpEdbInfoData.EdbName = edbInfo.EdbName
  632. tmpEdbInfoData.Frequency = edbInfo.Frequency
  633. tmpEdbInfoData.Unit = edbInfo.Unit
  634. }
  635. for index, dateTime := range sortDateTimeList {
  636. dataTimeType := 1
  637. if dateTime.After(lastRealDateTime) {
  638. dataTimeType = 2
  639. }
  640. tmpDateTimeStr := dateTime.Format(utils.FormatDate)
  641. rowData, ok := tableDataMap[index+1]
  642. if !ok {
  643. manualDataReqList = append(manualDataReqList, request.ManualDataReq{
  644. DataType: 3,
  645. DataTime: tmpDateTimeStr,
  646. DataTimeType: dataTimeType,
  647. ShowValue: "",
  648. Value: "",
  649. RelationEdbInfoList: nil,
  650. })
  651. continue
  652. }
  653. tmpData, ok := rowData[tableEdbInfo.Tag]
  654. if !ok {
  655. manualDataReqList = append(manualDataReqList, request.ManualDataReq{
  656. DataType: 3,
  657. DataTime: tmpDateTimeStr,
  658. DataTimeType: dataTimeType,
  659. ShowValue: "",
  660. Value: "",
  661. RelationEdbInfoList: nil,
  662. })
  663. continue
  664. }
  665. tmpData.DataTimeType = dataTimeType
  666. manualDataReqList = append(manualDataReqList, tmpData)
  667. }
  668. tmpEdbInfoData.Data = manualDataReqList
  669. data = append(data, tmpEdbInfoData)
  670. }
  671. // 处理一下数据格式
  672. for _, d := range data {
  673. for k2, d2 := range d.Data {
  674. // 可能有ShowValue非数值, 转换一下报错则continue
  675. vf, e := strconv.ParseFloat(d2.ShowValue, 64)
  676. if e != nil {
  677. continue
  678. }
  679. d.Data[k2].ShowValue = utils.FormatTableDataShowValue(vf)
  680. }
  681. }
  682. for _, d := range textRowListDataResp {
  683. for k2, d2 := range d {
  684. // 可能有ShowValue非数值, 转换一下报错则continue
  685. vf, e := strconv.ParseFloat(d2.ShowValue, 64)
  686. if e != nil {
  687. continue
  688. }
  689. d[k2].ShowValue = utils.FormatTableDataShowValue(vf)
  690. }
  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 tmpData.DataType == 1 || tmpData.DataType == 2 {
  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. for _, edbInfo := range edbInfoList {
  1108. edbInfoMap[edbInfo.EdbInfoId] = edbInfo
  1109. dataList := make([]*data_manage.EdbDataList, 0)
  1110. switch edbInfo.EdbInfoType {
  1111. case 0:
  1112. dataList, _ = data_manage.GetEdbDataList(edbInfo.Source, edbInfo.EdbInfoId, ``, ``)
  1113. case 1:
  1114. _, dataList, _, _, _, _ = GetPredictDataListByPredictEdbInfoId(edbInfo.EdbInfoId, ``, ``, false)
  1115. default:
  1116. err = errors.New(fmt.Sprint("获取失败,指标类型异常", edbInfo.EdbInfoType))
  1117. }
  1118. dateValMap := make(map[string]float64)
  1119. for _, data := range dataList {
  1120. dateValMap[data.DataTime] = data.Value
  1121. }
  1122. edbDataListMap[edbInfo.EdbInfoId] = dateValMap
  1123. }
  1124. for k, row := range newMixedTableCellDataList {
  1125. for i, cell := range row {
  1126. if cell.DataType == 2 {
  1127. if edbInfo, ok := edbInfoMap[cell.EdbInfoId]; ok {
  1128. cell.ShowValue = edbInfo.EdbName
  1129. }
  1130. } else if cell.DataType == 4 {
  1131. if dateValMap, ok := edbDataListMap[cell.EdbInfoId]; ok {
  1132. if val, ok2 := dateValMap[cell.DataTime]; ok2 {
  1133. cell.ShowValue = utils.FormatTableDataShowValue(val)
  1134. }
  1135. }
  1136. }
  1137. row[i] = cell
  1138. }
  1139. newMixedTableCellDataList[k] = row
  1140. }
  1141. return
  1142. }