excel_info.go 39 KB

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