excel_info.go 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701
  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. "sort"
  15. "time"
  16. )
  17. // GetExcelDetailInfoByExcelInfoId 根据表格id获取表格详情
  18. func GetExcelDetailInfoByExcelInfoId(excelInfoId, sysUserId int, lang string) (excelDetail response.ExcelInfoDetail, errMsg string, err error) {
  19. errMsg = `获取失败`
  20. //获取eta表格信息
  21. excelInfo, err := excel.GetExcelInfoById(excelInfoId)
  22. if err != nil {
  23. err = errors.New("获取ETA表格信息失败,Err:" + err.Error())
  24. if err.Error() == utils.ErrNoRow() {
  25. errMsg = "ETA表格被删除,请刷新页面"
  26. err = errors.New("ETA表格被删除,请刷新页面,Err:" + err.Error())
  27. }
  28. return
  29. }
  30. return formatExcelInfo2Detail(excelInfo, sysUserId, lang)
  31. }
  32. // GetExcelDetailInfoByUnicode 根据表格编码获取表格详情
  33. func GetExcelDetailInfoByUnicode(unicode string, sysUserId int, lang string) (excelDetail response.ExcelInfoDetail, errMsg string, err error) {
  34. errMsg = `获取失败`
  35. // 获取eta表格信息
  36. excelInfo, err := excel.GetExcelInfoByUnicode(unicode)
  37. if err != nil {
  38. err = errors.New("获取ETA表格信息失败,Err:" + err.Error())
  39. if err.Error() == utils.ErrNoRow() {
  40. errMsg = "ETA表格被删除,请刷新页面"
  41. err = errors.New("ETA表格被删除,请刷新页面,Err:" + err.Error())
  42. }
  43. return
  44. }
  45. return formatExcelInfo2Detail(excelInfo, sysUserId, lang)
  46. }
  47. func formatExcelInfo2Detail(excelInfo *excel.ExcelInfo, sysUserId int, lang string) (excelDetail response.ExcelInfoDetail, errMsg string, err error) {
  48. checkExcelInfo := excelInfo
  49. if excelInfo.Source == utils.BALANCE_TABLE {
  50. checkExcelInfoId := excelInfo.ExcelInfoId
  51. if excelInfo.BalanceType == 1 {
  52. checkExcelInfoId = excelInfo.RelExcelInfoId
  53. } else {
  54. if excelInfo.ParentId > 0 {
  55. checkExcelInfoId = excelInfo.ParentId
  56. }
  57. }
  58. if checkExcelInfoId != excelInfo.ExcelInfoId {
  59. checkExcelInfo, err = excel.GetExcelInfoById(checkExcelInfoId)
  60. if err != nil {
  61. errMsg = "获取平衡表格信息失败"
  62. err = errors.New("获取平衡表格信息失败,Err:" + err.Error())
  63. return
  64. }
  65. }
  66. }
  67. // 数据权限
  68. haveOperaAuth, err := data_manage_permission.CheckExcelPermissionByExcelInfoId(checkExcelInfo.ExcelInfoId, checkExcelInfo.ExcelClassifyId, checkExcelInfo.IsJoinPermission, sysUserId)
  69. if err != nil {
  70. err = errors.New("获取表格权限信息失败,Err" + err.Error())
  71. return
  72. }
  73. excelDetail = response.ExcelInfoDetail{
  74. ExcelInfoId: excelInfo.ExcelInfoId,
  75. Source: excelInfo.Source,
  76. ExcelType: excelInfo.ExcelType,
  77. ExcelName: excelInfo.ExcelName,
  78. UniqueCode: excelInfo.UniqueCode,
  79. ExcelClassifyId: excelInfo.ExcelClassifyId,
  80. SysUserId: excelInfo.SysUserId,
  81. SysUserRealName: excelInfo.SysUserRealName,
  82. Content: excelInfo.Content,
  83. ExcelImage: excelInfo.ExcelImage,
  84. FileUrl: excelInfo.FileUrl,
  85. Sort: excelInfo.Sort,
  86. IsDelete: excelInfo.IsDelete,
  87. ModifyTime: excelInfo.ModifyTime,
  88. CreateTime: excelInfo.CreateTime,
  89. TableData: nil,
  90. HaveOperaAuth: haveOperaAuth,
  91. ParentId: excelInfo.ParentId,
  92. BalanceType: excelInfo.BalanceType,
  93. UpdateUserId: excelInfo.UpdateUserId,
  94. UpdateUserRealName: excelInfo.UpdateUserRealName,
  95. RelExcelInfoId: excelInfo.RelExcelInfoId,
  96. }
  97. // 无权限,不需要返回数据
  98. if !haveOperaAuth {
  99. return
  100. }
  101. switch excelInfo.Source {
  102. case utils.TIME_TABLE: // 时间序列表格
  103. var tableDataConfig TableDataConfig
  104. err = json.Unmarshal([]byte(excelDetail.Content), &tableDataConfig)
  105. if err != nil {
  106. err = errors.New("表格json转结构体失败,Err:" + err.Error())
  107. return
  108. }
  109. result, tmpErr := GetDataByTableDataConfig(tableDataConfig)
  110. if tmpErr != nil {
  111. err = errors.New("获取最新的表格数据失败,Err:" + tmpErr.Error())
  112. return
  113. }
  114. if len(result.EdbInfoIdList) > 0 {
  115. classifyIdList := make([]int, 0)
  116. for _, v := range result.Data {
  117. classifyIdList = append(classifyIdList, v.ClassifyId)
  118. }
  119. classifyMap := make(map[int]*data_manage.EdbClassify)
  120. classifyList, tmpErr := data_manage.GetEdbClassifyByIdList(classifyIdList)
  121. if tmpErr != nil {
  122. err = errors.New("获取分类列表失败,Err:" + tmpErr.Error())
  123. return
  124. }
  125. for _, v := range classifyList {
  126. classifyMap[v.ClassifyId] = v
  127. }
  128. // 获取所有有权限的指标和分类
  129. permissionEdbIdList, permissionClassifyIdList, tmpErr := data_manage_permission.GetUserEdbAndClassifyPermissionList(sysUserId, 0, 0)
  130. if err != nil {
  131. err = errors.New("获取所有有权限的指标和分类失败,Err:" + tmpErr.Error())
  132. return
  133. }
  134. for i, v := range result.Data {
  135. if currClassify, ok := classifyMap[v.ClassifyId]; ok {
  136. result.Data[i].HaveOperaAuth = data_manage_permission.CheckEdbPermissionByPermissionIdList(v.IsJoinPermission, currClassify.IsJoinPermission, v.EdbInfoId, v.ClassifyId, permissionEdbIdList, permissionClassifyIdList)
  137. }
  138. }
  139. }
  140. excelDetail.TableData = result
  141. case utils.MIXED_TABLE, utils.BALANCE_TABLE: // 混合表格 平衡表
  142. var result request.MixedTableReq
  143. err = json.Unmarshal([]byte(excelDetail.Content), &result)
  144. if err != nil {
  145. err = errors.New("表格json转结构体失败,Err:" + err.Error())
  146. return
  147. }
  148. newData, tmpErr, tmpErrMsg := GetMixedTableCellData(result, lang)
  149. if tmpErr != nil {
  150. errMsg = "获取失败"
  151. if tmpErrMsg != `` {
  152. errMsg = tmpErrMsg
  153. }
  154. err = errors.New("获取最新的数据失败,Err:" + tmpErr.Error())
  155. return
  156. }
  157. result.Data = newData
  158. excelDetail.TableData = result
  159. }
  160. if excelDetail.Source == utils.BALANCE_TABLE {
  161. excelDetail.Button = GetBalanceExcelInfoOpButton(sysUserId, checkExcelInfo.SysUserId, excelDetail.HaveOperaAuth, checkExcelInfo.ExcelInfoId)
  162. }
  163. return
  164. }
  165. // GetExcelInfoOpButton 获取ETA表格的操作权限
  166. func GetExcelInfoOpButton(sysUser *system.Admin, belongUserId, source int, haveOperaAuth bool) (button excel.ExcelInfoDetailButton) {
  167. // 如果没有数据权限,那么直接返回
  168. if !haveOperaAuth {
  169. return
  170. }
  171. //非管理员角色查看其他用户创建的表格,可刷新、另存为、下载表格;
  172. button.RefreshButton = true
  173. button.CopyButton = true
  174. button.DownloadButton = true
  175. // 1、本用户创建的表格,可编辑、刷新、另存为、下载、删除,删除需二次确认;
  176. // 2、管理员角色对所有表格有如上权限;
  177. // 3、在线excel所有人都能编辑
  178. if sysUser.RoleTypeCode == utils.ROLE_TYPE_CODE_ADMIN || sysUser.RoleTypeCode == utils.ROLE_TYPE_CODE_FICC_ADMIN || sysUser.AdminId == belongUserId || source == utils.EXCEL_DEFAULT {
  179. button.OpButton = true
  180. button.DeleteButton = true
  181. }
  182. // 自定义分析
  183. if source == utils.CUSTOM_ANALYSIS_TABLE {
  184. if sysUser.RoleTypeCode == utils.ROLE_TYPE_CODE_ADMIN || sysUser.RoleTypeCode == utils.ROLE_TYPE_CODE_FICC_ADMIN || sysUser.AdminId == belongUserId {
  185. button.OpEdbButton = true // 生成、查看指标按钮
  186. button.RefreshEdbButton = true // 刷新指标按钮
  187. }
  188. }
  189. return
  190. }
  191. // GetFirstEdbDataList 获取第一列的数据
  192. func GetFirstEdbDataList(edbInfo *data_manage.EdbInfo, num int, manualDateList []string) (resultDataList []request.ManualDataReq, err error) {
  193. var dataList []*data_manage.EdbDataList
  194. switch edbInfo.EdbInfoType {
  195. case 0:
  196. dataList, err = data_manage.GetEdbDataList(edbInfo.Source, edbInfo.SubSource, edbInfo.EdbInfoId, ``, ``)
  197. case 1:
  198. _, dataList, _, _, err, _ = data.GetPredictDataListByPredictEdbInfoId(edbInfo.EdbInfoId, ``, ``, false)
  199. default:
  200. err = errors.New(fmt.Sprint("获取失败,指标类型异常", edbInfo.EdbInfoType))
  201. }
  202. if err != nil {
  203. return
  204. }
  205. // 获取需要的期数
  206. lenData := len(dataList)
  207. if lenData <= 0 {
  208. return
  209. }
  210. tmpManualDateNum := 0 // 手工数据的期数
  211. lenManualDate := len(manualDateList)
  212. if lenManualDate > 0 {
  213. sortDateList := manualDateList
  214. baseDateList := utils.StrArr{}
  215. baseDateList = append(baseDateList, sortDateList...)
  216. sort.Sort(baseDateList)
  217. sortDateList = append([]string{}, baseDateList...)
  218. lastData := dataList[lenData-1]
  219. lastDataDate, tmpErr := time.ParseInLocation(utils.FormatDate, lastData.DataTime, time.Local)
  220. if tmpErr != nil {
  221. err = tmpErr
  222. return
  223. }
  224. // 遍历倒序后的日期,然后匹配在实际数据之后日期的个数
  225. for _, tmpDateStr := range sortDateList {
  226. tmpDate, tmpErr := time.ParseInLocation(utils.FormatDate, tmpDateStr, time.Local)
  227. if tmpErr != nil {
  228. err = tmpErr
  229. return
  230. }
  231. if tmpDate.After(lastDataDate) {
  232. tmpManualDateNum++
  233. continue
  234. }
  235. break
  236. }
  237. }
  238. // 需要的期数减去手工数据的期数,这才是A列指标需要的数据
  239. num = num - tmpManualDateNum
  240. if num > lenData {
  241. num = lenData
  242. }
  243. latestDateTime, _ := time.ParseInLocation(utils.FormatDate, edbInfo.LatestDate, time.Local)
  244. for i := 1; i <= num; i++ {
  245. dataTime, _ := time.ParseInLocation(utils.FormatDate, dataList[lenData-i].DataTime, time.Local)
  246. dataType := 1
  247. // 如果是预测指标,且当前值的日期,晚于实际日期,那么是预测值
  248. if edbInfo.EdbInfoType == 1 && dataTime.After(latestDateTime) {
  249. dataType = 5
  250. }
  251. resultDataList = append(resultDataList, request.ManualDataReq{
  252. DataType: dataType,
  253. DataTime: dataList[lenData-i].DataTime,
  254. ShowValue: fmt.Sprint(dataList[lenData-i].Value),
  255. Value: fmt.Sprint(dataList[lenData-i].Value),
  256. DataTimeType: 1,
  257. })
  258. }
  259. return
  260. }
  261. // GetOtherEdbDataList 获取其他列的数据
  262. func GetOtherEdbDataList(edbInfo *data_manage.EdbInfo, dateList []string) (resultDataList []request.ManualDataReq, err error) {
  263. lenDate := len(dateList)
  264. if lenDate <= 0 {
  265. return
  266. }
  267. sortDateList := dateList
  268. baseDateList := utils.StrArr{}
  269. baseDateList = append(baseDateList, sortDateList...)
  270. sort.Sort(baseDateList)
  271. sortDateList = append([]string{}, baseDateList...)
  272. endDateTime, err := time.ParseInLocation(utils.FormatDate, sortDateList[0], time.Local)
  273. if err != nil {
  274. return
  275. }
  276. firstDateTime, err := time.ParseInLocation(utils.FormatDate, sortDateList[lenDate-1], time.Local)
  277. if err != nil {
  278. return
  279. }
  280. var dataList []*data_manage.EdbDataList
  281. switch edbInfo.EdbInfoType {
  282. case 0:
  283. dataList, err = data_manage.GetEdbDataList(edbInfo.Source, edbInfo.SubSource, edbInfo.EdbInfoId, ``, ``)
  284. case 1:
  285. _, dataList, _, _, err, _ = data.GetPredictDataListByPredictEdbInfoId(edbInfo.EdbInfoId, ``, ``, false)
  286. default:
  287. err = errors.New(fmt.Sprint("获取失败,指标类型异常", edbInfo.EdbInfoType))
  288. }
  289. if err != nil {
  290. return
  291. }
  292. // 获取日期内的数据(包含开始日期前一个日期,以及 结束日期后一个日期,目的为了做空日期时的 插值法兼容)
  293. baseDataList := make([]*data_manage.EdbDataList, 0)
  294. var lastData *data_manage.EdbDataList
  295. var isInsert bool
  296. for _, data := range dataList {
  297. tmpDate := data.DataTime
  298. tmpDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, tmpDate, time.Local)
  299. if tmpErr != nil {
  300. err = tmpErr
  301. return
  302. }
  303. if tmpDateTime.Before(firstDateTime) {
  304. lastData = data
  305. continue
  306. }
  307. // 如果是第一次写入数据
  308. if !isInsert && lastData != nil {
  309. baseDataList = append(baseDataList, lastData)
  310. }
  311. if tmpDateTime.After(endDateTime) {
  312. baseDataList = append(baseDataList, data)
  313. break
  314. }
  315. baseDataList = append(baseDataList, data)
  316. isInsert = true
  317. }
  318. // 实际数据的日期map
  319. realValMap := make(map[string]string)
  320. for _, v := range baseDataList {
  321. realValMap[v.DataTime] = v.DataTime
  322. }
  323. // 插值法处理
  324. handleDataMap := make(map[string]float64)
  325. err = data.HandleDataByLinearRegression(baseDataList, handleDataMap)
  326. if err != nil {
  327. return
  328. }
  329. latestDateTime, _ := time.ParseInLocation(utils.FormatDate, edbInfo.LatestDate, time.Local)
  330. // 对于不存在的数据做补充
  331. for _, date := range sortDateList {
  332. dataType := 1
  333. if _, ok := realValMap[date]; !ok {
  334. dataType = 2
  335. } else {
  336. dataTime, _ := time.ParseInLocation(utils.FormatDate, date, time.Local)
  337. // 如果是预测指标,且当前值的日期,晚于实际日期,那么是预测值
  338. if edbInfo.EdbInfoType == 1 && dataTime.After(latestDateTime) {
  339. dataType = 5
  340. }
  341. }
  342. var value, showValue string
  343. if tmpVal, ok := handleDataMap[date]; ok {
  344. value = fmt.Sprint(tmpVal)
  345. showValue = value
  346. } else {
  347. dataType = 3
  348. }
  349. resultDataList = append(resultDataList, request.ManualDataReq{
  350. DataType: dataType,
  351. DataTime: date,
  352. ShowValue: showValue,
  353. Value: value,
  354. })
  355. }
  356. return
  357. }
  358. // GetFirstHistoryEdbDataList 获取指标的历史的数据
  359. func GetFirstHistoryEdbDataList(edbInfo *data_manage.EdbInfo, num int, endDate string) (resultDataList []request.ManualDataReq, err error) {
  360. endDateTime, err := time.ParseInLocation(utils.FormatDate, endDate, time.Local)
  361. if err != nil {
  362. return
  363. }
  364. var dataList []*data_manage.EdbDataList
  365. switch edbInfo.EdbInfoType {
  366. case 0:
  367. dataList, err = data_manage.GetEdbDataList(edbInfo.Source, edbInfo.SubSource, edbInfo.EdbInfoId, ``, endDate)
  368. case 1:
  369. _, dataList, _, _, err, _ = data.GetPredictDataListByPredictEdbInfoId(edbInfo.EdbInfoId, ``, endDate, true)
  370. default:
  371. err = errors.New(fmt.Sprint("获取失败,指标类型异常", edbInfo.EdbInfoType))
  372. }
  373. if err != nil {
  374. return
  375. }
  376. // 获取需要的期数
  377. lenData := len(dataList)
  378. if lenData <= 0 {
  379. return
  380. }
  381. lastData := dataList[lenData-1]
  382. lastDataDateTime, err := time.ParseInLocation(utils.FormatDate, lastData.DataTime, time.Local)
  383. if err != nil {
  384. return
  385. }
  386. if endDateTime.Equal(lastDataDateTime) || lastDataDateTime.After(endDateTime) {
  387. dataList = dataList[:lenData-1]
  388. lenData = len(dataList)
  389. }
  390. if num > lenData {
  391. num = lenData
  392. }
  393. latestDateTime, _ := time.ParseInLocation(utils.FormatDate, edbInfo.LatestDate, time.Local)
  394. for i := 1; i <= num; i++ {
  395. dataTime, _ := time.ParseInLocation(utils.FormatDate, dataList[lenData-i].DataTime, time.Local)
  396. dataType := 1
  397. // 如果是预测指标,且当前值的日期,晚于实际日期,那么是预测值
  398. if edbInfo.EdbInfoType == 1 && dataTime.After(latestDateTime) {
  399. dataType = 5
  400. }
  401. resultDataList = append(resultDataList, request.ManualDataReq{
  402. DataType: dataType,
  403. DataTime: dataList[lenData-i].DataTime,
  404. ShowValue: fmt.Sprint(dataList[lenData-i].Value),
  405. Value: fmt.Sprint(dataList[lenData-i].Value),
  406. })
  407. }
  408. return
  409. }
  410. // GetEdbIdsFromExcelCodes 获取表格中的指标IDs
  411. func GetEdbIdsFromExcelCodes(excelCodes []string, sysUserId int, lang string) (edbIds []int, err error) {
  412. edbIds = make([]int, 0)
  413. edbIdExist := make(map[int]bool)
  414. for _, v := range excelCodes {
  415. // 表格详情
  416. detail, msg, e := GetExcelDetailInfoByUnicode(v, sysUserId, lang)
  417. if e != nil {
  418. err = fmt.Errorf("GetExcelDetailInfoByExcelInfoId err: %s, errMsg: %s", e.Error(), msg)
  419. return
  420. }
  421. // 自定义表格
  422. if detail.Source == utils.TIME_TABLE {
  423. jsonByte, e := json.Marshal(detail.TableData)
  424. if e != nil {
  425. err = fmt.Errorf("JSON格式化自定义表格数据失败, Err: %s", e.Error())
  426. return
  427. }
  428. var tableData request.TableDataReq
  429. if e = json.Unmarshal(jsonByte, &tableData); e != nil {
  430. err = fmt.Errorf("解析自定义表格数据失败, Err: %s", e.Error())
  431. return
  432. }
  433. for _, tv := range tableData.EdbInfoIdList {
  434. if edbIdExist[tv] {
  435. continue
  436. }
  437. edbIdExist[tv] = true
  438. edbIds = append(edbIds, tv)
  439. }
  440. }
  441. // 混合表格
  442. if detail.Source == utils.MIXED_TABLE {
  443. jsonByte, e := json.Marshal(detail.TableData)
  444. if e != nil {
  445. err = fmt.Errorf("JSON格式化混合表格数据失败, Err: %s", e.Error())
  446. return
  447. }
  448. var tableData request.MixedTableReq
  449. if e = json.Unmarshal(jsonByte, &tableData); e != nil {
  450. err = fmt.Errorf("解析混合表格数据失败, Err: %s", e.Error())
  451. return
  452. }
  453. if len(tableData.Data) > 0 {
  454. for _, td := range tableData.Data {
  455. for _, tv := range td {
  456. if tv.EdbInfoId > 0 && !edbIdExist[tv.EdbInfoId] {
  457. edbIdExist[tv.EdbInfoId] = true
  458. edbIds = append(edbIds, tv.EdbInfoId)
  459. }
  460. }
  461. }
  462. }
  463. }
  464. }
  465. return
  466. }
  467. // GetExcelEdbBatchRefreshKey 获取批量刷新表格指标缓存key
  468. func GetExcelEdbBatchRefreshKey(source string, primaryId, subId int) string {
  469. if source == `` {
  470. return ``
  471. }
  472. return fmt.Sprint("batch_refresh_excel_edb:", source, ":", primaryId, ":", subId)
  473. }
  474. // ReplaceEdbInExcel 替换表格中的指标
  475. func ReplaceEdbInExcel(oldEdbInfo, newEdbInfo *data_manage.EdbInfo) (err error) {
  476. //查询和指标相关的 表格
  477. mappingList, err := excel.GetExcelEdbMappingByEdbInfoId(oldEdbInfo.EdbInfoId)
  478. if err != nil {
  479. err = fmt.Errorf("查询和指标相关的表格失败,错误:%s", err.Error())
  480. return
  481. }
  482. updateList := make([]*excel.ExcelInfo, 0)
  483. // 循环列表,根据表格类型单独处理
  484. for _, excelMapping := range mappingList {
  485. //查询和指标相关的混合表格
  486. excelInfo, tmpErr := excel.GetExcelInfoById(excelMapping.ExcelInfoId)
  487. if tmpErr != nil {
  488. err = fmt.Errorf("查询和指标相关的混合表格失败,错误:%s", tmpErr.Error())
  489. return
  490. }
  491. // 清除缓存
  492. key := utils.HZ_CHART_LIB_EXCEL_TABLE_DETAIL + ":" + excelInfo.UniqueCode
  493. if utils.Re == nil {
  494. _ = utils.Rc.Delete(key)
  495. }
  496. // 根据表格类型,调用不同的处理函数
  497. switch excelMapping.Source {
  498. case utils.TIME_TABLE: // 时间序列表格
  499. // 替换余额表格中的指标
  500. newExcelInfo, e := replaceEdbInTimeExcel(oldEdbInfo, newEdbInfo, excelInfo)
  501. if e != nil {
  502. err = fmt.Errorf("替换余额表格中的指标失败,错误:%s", e.Error())
  503. return
  504. }
  505. updateList = append(updateList, newExcelInfo)
  506. case utils.MIXED_TABLE:
  507. // 替换余额表格中的指标
  508. newExcelInfo, e := replaceEdbInBalanceExcel(oldEdbInfo, newEdbInfo, excelInfo)
  509. if e != nil {
  510. err = fmt.Errorf("替换余额表格中的指标失败,错误:%s", e.Error())
  511. return
  512. }
  513. updateList = append(updateList, newExcelInfo)
  514. default:
  515. // 其他表格类型的处理逻辑
  516. }
  517. }
  518. /*err = excel.ReplaceEdbInExcel(oldEdbInfo, newEdbInfo, updateList)
  519. if err != nil {
  520. err = fmt.Errorf("替换表格中的指标失败,错误:%s", err.Error())
  521. }
  522. */
  523. //todo 是否需要刷新表格中的指标数据
  524. return
  525. }
  526. func replaceEdbInBalanceExcel(oldEdbInfo, newEdbInfo *data_manage.EdbInfo, excelInfo *excel.ExcelInfo) (newExcelInfo *excel.ExcelInfo, err error) {
  527. newExcelInfo = excelInfo
  528. var mixedTableReq request.MixedTableReq
  529. err = json.Unmarshal([]byte(excelInfo.Content), &mixedTableReq)
  530. if err != nil {
  531. err = fmt.Errorf("表格json转结构体失败,Err:" + err.Error())
  532. return
  533. }
  534. // 处理data
  535. configList := mixedTableReq.Data
  536. for ck, rowList := range configList {
  537. for rk, cell := range rowList {
  538. switch cell.DataType {
  539. case request.EdbDT: // 指标信息
  540. if cell.EdbInfoId == oldEdbInfo.EdbInfoId {
  541. //更换成新指标ID
  542. configList[ck][rk].EdbInfoId = newEdbInfo.EdbInfoId
  543. }
  544. case request.InsertDataDT, request.PopInsertDataDT: // 插值、弹框插值
  545. if cell.EdbInfoId == oldEdbInfo.EdbInfoId {
  546. //更换成新指标ID
  547. configList[ck][rk].EdbInfoId = newEdbInfo.EdbInfoId
  548. }
  549. case request.InsertEdbCalculateDataDT: // 插入指标计算公式生成的值
  550. var config request.CalculateConf
  551. err = json.Unmarshal([]byte(cell.Value), &config)
  552. if err != nil {
  553. return
  554. }
  555. if cell.EdbInfoId == oldEdbInfo.EdbInfoId {
  556. //更换成新指标ID
  557. configList[ck][rk].EdbInfoId = newEdbInfo.EdbInfoId
  558. }
  559. if config.EdbInfoId == oldEdbInfo.EdbInfoId {
  560. config.EdbInfoId = newEdbInfo.EdbInfoId
  561. }
  562. var configStr []byte
  563. configStr, err = json.Marshal(config)
  564. if err != nil {
  565. return
  566. }
  567. configList[ck][rk].Value = string(configStr)
  568. case request.DateDT: // 日期类型
  569. // 指标日期类型的单元格需要额外将指标id取出来
  570. if cell.DataTimeType == request.EdbDateDT {
  571. var config request.EdbDateConf
  572. err = json.Unmarshal([]byte(cell.Value), &config)
  573. if err != nil {
  574. return
  575. }
  576. if config.EdbInfoId == oldEdbInfo.EdbInfoId {
  577. config.EdbInfoId = newEdbInfo.EdbInfoId
  578. var configStr []byte
  579. configStr, err = json.Marshal(config)
  580. if err != nil {
  581. return
  582. }
  583. configList[ck][rk].Value = string(configStr)
  584. }
  585. }
  586. }
  587. }
  588. }
  589. mixedTableReq.Data = configList
  590. var newContentByte []byte
  591. newContentByte, err = json.Marshal(mixedTableReq)
  592. if err != nil {
  593. return
  594. }
  595. // 生成的新内容替换原先的旧内容
  596. excelInfo.Content = string(newContentByte)
  597. newExcelInfo = excelInfo
  598. return
  599. }
  600. func replaceEdbInTimeExcel(oldEdbInfo, newEdbInfo *data_manage.EdbInfo, excelInfo *excel.ExcelInfo) (newExcelInfo *excel.ExcelInfo, err error) {
  601. newExcelInfo = excelInfo
  602. var tableDataConfig TableDataConfig
  603. err = json.Unmarshal([]byte(excelInfo.Content), &tableDataConfig)
  604. if err != nil {
  605. err = errors.New("表格json转结构体失败,Err:" + err.Error())
  606. return
  607. }
  608. if len(tableDataConfig.EdbInfoIdList) <= 0 {
  609. return
  610. }
  611. // 实际期数没有的情况下,直接返回吧
  612. if tableDataConfig.Num <= 0 {
  613. return
  614. }
  615. // 先处理edbInfoList
  616. for k, id := range tableDataConfig.EdbInfoIdList {
  617. if id == oldEdbInfo.EdbInfoId {
  618. tableDataConfig.EdbInfoIdList[k] = newEdbInfo.EdbInfoId
  619. }
  620. }
  621. // 先处理tableEdbInfoList
  622. for k, tableEdbInfo := range tableDataConfig.TableEdbInfoList {
  623. if tableEdbInfo.EdbInfoId == oldEdbInfo.EdbInfoId {
  624. tableDataConfig.TableEdbInfoList[k].EdbInfoId = newEdbInfo.EdbInfoId
  625. }
  626. }
  627. var newContentByte []byte
  628. newContentByte, err = json.Marshal(tableDataConfig)
  629. if err != nil {
  630. return
  631. }
  632. // 生成的新内容替换原先的旧内容
  633. excelInfo.Content = string(newContentByte)
  634. newExcelInfo = excelInfo
  635. return
  636. }