excel_info.go 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559
  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. // GetBalanceExcelInfoOpButton 获取ETA平衡表格的操作权限
  192. func GetBalanceExcelInfoOpButton(sysUserId, parentSysUserId int, haveOperaAuth bool, parentExcelInfoId int) (button excel.ExcelInfoDetailButton) {
  193. // 如果没有数据权限,那么直接返回
  194. if !haveOperaAuth {
  195. return
  196. }
  197. //非管理员角色查看其他用户创建的表格,可刷新、另存为、下载表格;
  198. button.RefreshButton = true
  199. button.CopyButton = true
  200. button.DownloadButton = true
  201. if sysUserId == parentSysUserId {
  202. button.OpButton = true
  203. button.RefreshEdbButton = true
  204. button.OpWorkerButton = true
  205. } else {
  206. obj := new(excel.ExcelWorker)
  207. workerList, err := obj.GetByExcelInfoId(parentExcelInfoId)
  208. if err == nil {
  209. for _, v := range workerList {
  210. if v.SysUserId == sysUserId {
  211. button.OpButton = true
  212. button.RefreshEdbButton = true
  213. break
  214. }
  215. }
  216. }
  217. }
  218. return
  219. }
  220. // GetFirstEdbDataList 获取第一列的数据
  221. func GetFirstEdbDataList(edbInfo *data_manage.EdbInfo, num int, manualDateList []string) (resultDataList []request.ManualDataReq, err error) {
  222. var dataList []*data_manage.EdbDataList
  223. switch edbInfo.EdbInfoType {
  224. case 0:
  225. dataList, err = data_manage.GetEdbDataList(edbInfo.Source, edbInfo.SubSource, edbInfo.EdbInfoId, ``, ``)
  226. case 1:
  227. _, dataList, _, _, err, _ = data.GetPredictDataListByPredictEdbInfoId(edbInfo.EdbInfoId, ``, ``, false)
  228. default:
  229. err = errors.New(fmt.Sprint("获取失败,指标类型异常", edbInfo.EdbInfoType))
  230. }
  231. if err != nil {
  232. return
  233. }
  234. // 获取需要的期数
  235. lenData := len(dataList)
  236. if lenData <= 0 {
  237. return
  238. }
  239. tmpManualDateNum := 0 // 手工数据的期数
  240. lenManualDate := len(manualDateList)
  241. if lenManualDate > 0 {
  242. sortDateList := manualDateList
  243. baseDateList := utils.StrArr{}
  244. baseDateList = append(baseDateList, sortDateList...)
  245. sort.Sort(baseDateList)
  246. sortDateList = append([]string{}, baseDateList...)
  247. lastData := dataList[lenData-1]
  248. lastDataDate, tmpErr := time.ParseInLocation(utils.FormatDate, lastData.DataTime, time.Local)
  249. if tmpErr != nil {
  250. err = tmpErr
  251. return
  252. }
  253. // 遍历倒序后的日期,然后匹配在实际数据之后日期的个数
  254. for _, tmpDateStr := range sortDateList {
  255. tmpDate, tmpErr := time.ParseInLocation(utils.FormatDate, tmpDateStr, time.Local)
  256. if tmpErr != nil {
  257. err = tmpErr
  258. return
  259. }
  260. if tmpDate.After(lastDataDate) {
  261. tmpManualDateNum++
  262. continue
  263. }
  264. break
  265. }
  266. }
  267. // 需要的期数减去手工数据的期数,这才是A列指标需要的数据
  268. num = num - tmpManualDateNum
  269. if num > lenData {
  270. num = lenData
  271. }
  272. latestDateTime, _ := time.ParseInLocation(utils.FormatDate, edbInfo.LatestDate, time.Local)
  273. for i := 1; i <= num; i++ {
  274. dataTime, _ := time.ParseInLocation(utils.FormatDate, dataList[lenData-i].DataTime, time.Local)
  275. dataType := 1
  276. // 如果是预测指标,且当前值的日期,晚于实际日期,那么是预测值
  277. if edbInfo.EdbInfoType == 1 && dataTime.After(latestDateTime) {
  278. dataType = 5
  279. }
  280. resultDataList = append(resultDataList, request.ManualDataReq{
  281. DataType: dataType,
  282. DataTime: dataList[lenData-i].DataTime,
  283. ShowValue: fmt.Sprint(dataList[lenData-i].Value),
  284. Value: fmt.Sprint(dataList[lenData-i].Value),
  285. DataTimeType: 1,
  286. })
  287. }
  288. return
  289. }
  290. // GetOtherEdbDataList 获取其他列的数据
  291. func GetOtherEdbDataList(edbInfo *data_manage.EdbInfo, dateList []string) (resultDataList []request.ManualDataReq, err error) {
  292. lenDate := len(dateList)
  293. if lenDate <= 0 {
  294. return
  295. }
  296. sortDateList := dateList
  297. baseDateList := utils.StrArr{}
  298. baseDateList = append(baseDateList, sortDateList...)
  299. sort.Sort(baseDateList)
  300. sortDateList = append([]string{}, baseDateList...)
  301. endDateTime, err := time.ParseInLocation(utils.FormatDate, sortDateList[0], time.Local)
  302. if err != nil {
  303. return
  304. }
  305. firstDateTime, err := time.ParseInLocation(utils.FormatDate, sortDateList[lenDate-1], time.Local)
  306. if err != nil {
  307. return
  308. }
  309. var dataList []*data_manage.EdbDataList
  310. switch edbInfo.EdbInfoType {
  311. case 0:
  312. dataList, err = data_manage.GetEdbDataList(edbInfo.Source, edbInfo.SubSource, edbInfo.EdbInfoId, ``, ``)
  313. case 1:
  314. _, dataList, _, _, err, _ = data.GetPredictDataListByPredictEdbInfoId(edbInfo.EdbInfoId, ``, ``, false)
  315. default:
  316. err = errors.New(fmt.Sprint("获取失败,指标类型异常", edbInfo.EdbInfoType))
  317. }
  318. if err != nil {
  319. return
  320. }
  321. // 获取日期内的数据(包含开始日期前一个日期,以及 结束日期后一个日期,目的为了做空日期时的 插值法兼容)
  322. baseDataList := make([]*data_manage.EdbDataList, 0)
  323. var lastData *data_manage.EdbDataList
  324. var isInsert bool
  325. for _, data := range dataList {
  326. tmpDate := data.DataTime
  327. tmpDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, tmpDate, time.Local)
  328. if tmpErr != nil {
  329. err = tmpErr
  330. return
  331. }
  332. if tmpDateTime.Before(firstDateTime) {
  333. lastData = data
  334. continue
  335. }
  336. // 如果是第一次写入数据
  337. if !isInsert && lastData != nil {
  338. baseDataList = append(baseDataList, lastData)
  339. }
  340. if tmpDateTime.After(endDateTime) {
  341. baseDataList = append(baseDataList, data)
  342. break
  343. }
  344. baseDataList = append(baseDataList, data)
  345. isInsert = true
  346. }
  347. // 实际数据的日期map
  348. realValMap := make(map[string]string)
  349. for _, v := range baseDataList {
  350. realValMap[v.DataTime] = v.DataTime
  351. }
  352. // 插值法处理
  353. handleDataMap := make(map[string]float64)
  354. err = data.HandleDataByLinearRegression(baseDataList, handleDataMap)
  355. if err != nil {
  356. return
  357. }
  358. latestDateTime, _ := time.ParseInLocation(utils.FormatDate, edbInfo.LatestDate, time.Local)
  359. // 对于不存在的数据做补充
  360. for _, date := range sortDateList {
  361. dataType := 1
  362. if _, ok := realValMap[date]; !ok {
  363. dataType = 2
  364. } else {
  365. dataTime, _ := time.ParseInLocation(utils.FormatDate, date, time.Local)
  366. // 如果是预测指标,且当前值的日期,晚于实际日期,那么是预测值
  367. if edbInfo.EdbInfoType == 1 && dataTime.After(latestDateTime) {
  368. dataType = 5
  369. }
  370. }
  371. var value, showValue string
  372. if tmpVal, ok := handleDataMap[date]; ok {
  373. value = fmt.Sprint(tmpVal)
  374. showValue = value
  375. } else {
  376. dataType = 3
  377. }
  378. resultDataList = append(resultDataList, request.ManualDataReq{
  379. DataType: dataType,
  380. DataTime: date,
  381. ShowValue: showValue,
  382. Value: value,
  383. })
  384. }
  385. return
  386. }
  387. // GetFirstHistoryEdbDataList 获取指标的历史的数据
  388. func GetFirstHistoryEdbDataList(edbInfo *data_manage.EdbInfo, num int, endDate string) (resultDataList []request.ManualDataReq, err error) {
  389. endDateTime, err := time.ParseInLocation(utils.FormatDate, endDate, time.Local)
  390. if err != nil {
  391. return
  392. }
  393. var dataList []*data_manage.EdbDataList
  394. switch edbInfo.EdbInfoType {
  395. case 0:
  396. dataList, err = data_manage.GetEdbDataList(edbInfo.Source, edbInfo.SubSource, edbInfo.EdbInfoId, ``, endDate)
  397. case 1:
  398. _, dataList, _, _, err, _ = data.GetPredictDataListByPredictEdbInfoId(edbInfo.EdbInfoId, ``, endDate, true)
  399. default:
  400. err = errors.New(fmt.Sprint("获取失败,指标类型异常", edbInfo.EdbInfoType))
  401. }
  402. if err != nil {
  403. return
  404. }
  405. // 获取需要的期数
  406. lenData := len(dataList)
  407. if lenData <= 0 {
  408. return
  409. }
  410. lastData := dataList[lenData-1]
  411. lastDataDateTime, err := time.ParseInLocation(utils.FormatDate, lastData.DataTime, time.Local)
  412. if err != nil {
  413. return
  414. }
  415. if endDateTime.Equal(lastDataDateTime) || lastDataDateTime.After(endDateTime) {
  416. dataList = dataList[:lenData-1]
  417. lenData = len(dataList)
  418. }
  419. if num > lenData {
  420. num = lenData
  421. }
  422. latestDateTime, _ := time.ParseInLocation(utils.FormatDate, edbInfo.LatestDate, time.Local)
  423. for i := 1; i <= num; i++ {
  424. dataTime, _ := time.ParseInLocation(utils.FormatDate, dataList[lenData-i].DataTime, time.Local)
  425. dataType := 1
  426. // 如果是预测指标,且当前值的日期,晚于实际日期,那么是预测值
  427. if edbInfo.EdbInfoType == 1 && dataTime.After(latestDateTime) {
  428. dataType = 5
  429. }
  430. resultDataList = append(resultDataList, request.ManualDataReq{
  431. DataType: dataType,
  432. DataTime: dataList[lenData-i].DataTime,
  433. ShowValue: fmt.Sprint(dataList[lenData-i].Value),
  434. Value: fmt.Sprint(dataList[lenData-i].Value),
  435. })
  436. }
  437. return
  438. }
  439. // GetEdbIdsFromExcelCodes 获取表格中的指标IDs
  440. func GetEdbIdsFromExcelCodes(excelCodes []string, sysUserId int, lang string) (edbIds []int, err error) {
  441. edbIds = make([]int, 0)
  442. edbIdExist := make(map[int]bool)
  443. for _, v := range excelCodes {
  444. // 表格详情
  445. detail, msg, e := GetExcelDetailInfoByUnicode(v, sysUserId, lang)
  446. if e != nil {
  447. err = fmt.Errorf("GetExcelDetailInfoByExcelInfoId err: %s, errMsg: %s", e.Error(), msg)
  448. return
  449. }
  450. // 自定义表格
  451. if detail.Source == utils.TIME_TABLE {
  452. jsonByte, e := json.Marshal(detail.TableData)
  453. if e != nil {
  454. err = fmt.Errorf("JSON格式化自定义表格数据失败, Err: %s", e.Error())
  455. return
  456. }
  457. var tableData request.TableDataReq
  458. if e = json.Unmarshal(jsonByte, &tableData); e != nil {
  459. err = fmt.Errorf("解析自定义表格数据失败, Err: %s", e.Error())
  460. return
  461. }
  462. for _, tv := range tableData.EdbInfoIdList {
  463. if edbIdExist[tv] {
  464. continue
  465. }
  466. edbIdExist[tv] = true
  467. edbIds = append(edbIds, tv)
  468. }
  469. }
  470. // 混合表格
  471. if detail.Source == utils.MIXED_TABLE {
  472. jsonByte, e := json.Marshal(detail.TableData)
  473. if e != nil {
  474. err = fmt.Errorf("JSON格式化混合表格数据失败, Err: %s", e.Error())
  475. return
  476. }
  477. var tableData request.MixedTableReq
  478. if e = json.Unmarshal(jsonByte, &tableData); e != nil {
  479. err = fmt.Errorf("解析混合表格数据失败, Err: %s", e.Error())
  480. return
  481. }
  482. if len(tableData.Data) > 0 {
  483. for _, td := range tableData.Data {
  484. for _, tv := range td {
  485. if tv.EdbInfoId > 0 && !edbIdExist[tv.EdbInfoId] {
  486. edbIdExist[tv.EdbInfoId] = true
  487. edbIds = append(edbIds, tv.EdbInfoId)
  488. }
  489. }
  490. }
  491. }
  492. }
  493. }
  494. return
  495. }
  496. // GetExcelEdbBatchRefreshKey 获取批量刷新表格指标缓存key
  497. func GetExcelEdbBatchRefreshKey(source string, primaryId, subId int) string {
  498. if source == `` {
  499. return ``
  500. }
  501. return fmt.Sprint("batch_refresh_excel_edb:", source, ":", primaryId, ":", subId)
  502. }