excel_info.go 43 KB

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