excel_info.go 39 KB

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