excel_info.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530
  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. SourcesFrom: excelInfo.SourcesFrom,
  97. }
  98. // 无权限,不需要返回数据
  99. if !haveOperaAuth {
  100. return
  101. }
  102. switch excelInfo.Source {
  103. case utils.TIME_TABLE: // 时间序列表格
  104. var tableDataConfig TableDataConfig
  105. err = json.Unmarshal([]byte(excelDetail.Content), &tableDataConfig)
  106. if err != nil {
  107. err = errors.New("表格json转结构体失败,Err:" + err.Error())
  108. return
  109. }
  110. result, tmpErr := GetDataByTableDataConfig(tableDataConfig)
  111. if tmpErr != nil {
  112. err = errors.New("获取最新的表格数据失败,Err:" + tmpErr.Error())
  113. return
  114. }
  115. if len(result.EdbInfoIdList) > 0 {
  116. classifyIdList := make([]int, 0)
  117. for _, v := range result.Data {
  118. classifyIdList = append(classifyIdList, v.ClassifyId)
  119. }
  120. classifyMap := make(map[int]*data_manage.EdbClassify)
  121. classifyList, tmpErr := data_manage.GetEdbClassifyByIdList(classifyIdList)
  122. if tmpErr != nil {
  123. err = errors.New("获取分类列表失败,Err:" + tmpErr.Error())
  124. return
  125. }
  126. for _, v := range classifyList {
  127. classifyMap[v.ClassifyId] = v
  128. }
  129. // 获取所有有权限的指标和分类
  130. permissionEdbIdList, permissionClassifyIdList, tmpErr := data_manage_permission.GetUserEdbAndClassifyPermissionList(sysUserId, 0, 0)
  131. if err != nil {
  132. err = errors.New("获取所有有权限的指标和分类失败,Err:" + tmpErr.Error())
  133. return
  134. }
  135. for i, v := range result.Data {
  136. if currClassify, ok := classifyMap[v.ClassifyId]; ok {
  137. result.Data[i].HaveOperaAuth = data_manage_permission.CheckEdbPermissionByPermissionIdList(v.IsJoinPermission, currClassify.IsJoinPermission, v.EdbInfoId, v.ClassifyId, permissionEdbIdList, permissionClassifyIdList)
  138. }
  139. }
  140. }
  141. excelDetail.TableData = result
  142. case utils.MIXED_TABLE, utils.BALANCE_TABLE: // 混合表格 平衡表
  143. var result request.MixedTableReq
  144. err = json.Unmarshal([]byte(excelDetail.Content), &result)
  145. if err != nil {
  146. err = errors.New("表格json转结构体失败,Err:" + err.Error())
  147. return
  148. }
  149. newData, tmpErr, tmpErrMsg := GetMixedTableCellData(result, lang)
  150. if tmpErr != nil {
  151. errMsg = "获取失败"
  152. if tmpErrMsg != `` {
  153. errMsg = tmpErrMsg
  154. }
  155. err = errors.New("获取最新的数据失败,Err:" + tmpErr.Error())
  156. return
  157. }
  158. result.Data = newData
  159. excelDetail.TableData = result
  160. }
  161. if excelDetail.Source == utils.BALANCE_TABLE {
  162. excelDetail.Button = GetBalanceExcelInfoOpButton(sysUserId, checkExcelInfo.SysUserId, excelDetail.HaveOperaAuth, checkExcelInfo.ExcelInfoId)
  163. }
  164. return
  165. }
  166. // GetExcelInfoOpButton 获取ETA表格的操作权限
  167. func GetExcelInfoOpButton(sysUser *system.Admin, belongUserId, source int, haveOperaAuth bool) (button excel.ExcelInfoDetailButton) {
  168. // 如果没有数据权限,那么直接返回
  169. if !haveOperaAuth {
  170. return
  171. }
  172. //非管理员角色查看其他用户创建的表格,可刷新、另存为、下载表格;
  173. button.RefreshButton = true
  174. button.CopyButton = true
  175. button.DownloadButton = true
  176. // 1、本用户创建的表格,可编辑、刷新、另存为、下载、删除,删除需二次确认;
  177. // 2、管理员角色对所有表格有如上权限;
  178. // 3、在线excel所有人都能编辑
  179. if sysUser.RoleTypeCode == utils.ROLE_TYPE_CODE_ADMIN || sysUser.RoleTypeCode == utils.ROLE_TYPE_CODE_FICC_ADMIN || sysUser.AdminId == belongUserId || source == utils.EXCEL_DEFAULT {
  180. button.OpButton = true
  181. button.DeleteButton = true
  182. }
  183. // 自定义分析
  184. if source == utils.CUSTOM_ANALYSIS_TABLE {
  185. if sysUser.RoleTypeCode == utils.ROLE_TYPE_CODE_ADMIN || sysUser.RoleTypeCode == utils.ROLE_TYPE_CODE_FICC_ADMIN || sysUser.AdminId == belongUserId {
  186. button.OpEdbButton = true // 生成、查看指标按钮
  187. button.RefreshEdbButton = true // 刷新指标按钮
  188. }
  189. }
  190. return
  191. }
  192. // GetFirstEdbDataList 获取第一列的数据
  193. func GetFirstEdbDataList(edbInfo *data_manage.EdbInfo, num int, manualDateList []string) (resultDataList []request.ManualDataReq, err error) {
  194. var dataList []*data_manage.EdbDataList
  195. switch edbInfo.EdbInfoType {
  196. case 0:
  197. dataList, err = data_manage.GetEdbDataList(edbInfo.Source, edbInfo.SubSource, edbInfo.EdbInfoId, ``, ``)
  198. case 1:
  199. _, dataList, _, _, err, _ = data.GetPredictDataListByPredictEdbInfoId(edbInfo.EdbInfoId, ``, ``, false)
  200. default:
  201. err = errors.New(fmt.Sprint("获取失败,指标类型异常", edbInfo.EdbInfoType))
  202. }
  203. if err != nil {
  204. return
  205. }
  206. // 获取需要的期数
  207. lenData := len(dataList)
  208. if lenData <= 0 {
  209. return
  210. }
  211. tmpManualDateNum := 0 // 手工数据的期数
  212. lenManualDate := len(manualDateList)
  213. if lenManualDate > 0 {
  214. sortDateList := manualDateList
  215. baseDateList := utils.StrArr{}
  216. baseDateList = append(baseDateList, sortDateList...)
  217. sort.Sort(baseDateList)
  218. sortDateList = append([]string{}, baseDateList...)
  219. lastData := dataList[lenData-1]
  220. lastDataDate, tmpErr := time.ParseInLocation(utils.FormatDate, lastData.DataTime, time.Local)
  221. if tmpErr != nil {
  222. err = tmpErr
  223. return
  224. }
  225. // 遍历倒序后的日期,然后匹配在实际数据之后日期的个数
  226. for _, tmpDateStr := range sortDateList {
  227. tmpDate, tmpErr := time.ParseInLocation(utils.FormatDate, tmpDateStr, time.Local)
  228. if tmpErr != nil {
  229. err = tmpErr
  230. return
  231. }
  232. if tmpDate.After(lastDataDate) {
  233. tmpManualDateNum++
  234. continue
  235. }
  236. break
  237. }
  238. }
  239. // 需要的期数减去手工数据的期数,这才是A列指标需要的数据
  240. num = num - tmpManualDateNum
  241. if num > lenData {
  242. num = lenData
  243. }
  244. latestDateTime, _ := time.ParseInLocation(utils.FormatDate, edbInfo.LatestDate, time.Local)
  245. for i := 1; i <= num; i++ {
  246. dataTime, _ := time.ParseInLocation(utils.FormatDate, dataList[lenData-i].DataTime, time.Local)
  247. dataType := 1
  248. // 如果是预测指标,且当前值的日期,晚于实际日期,那么是预测值
  249. if edbInfo.EdbInfoType == 1 && dataTime.After(latestDateTime) {
  250. dataType = 5
  251. }
  252. resultDataList = append(resultDataList, request.ManualDataReq{
  253. DataType: dataType,
  254. DataTime: dataList[lenData-i].DataTime,
  255. ShowValue: fmt.Sprint(dataList[lenData-i].Value),
  256. Value: fmt.Sprint(dataList[lenData-i].Value),
  257. DataTimeType: 1,
  258. })
  259. }
  260. return
  261. }
  262. // GetOtherEdbDataList 获取其他列的数据
  263. func GetOtherEdbDataList(edbInfo *data_manage.EdbInfo, dateList []string) (resultDataList []request.ManualDataReq, err error) {
  264. lenDate := len(dateList)
  265. if lenDate <= 0 {
  266. return
  267. }
  268. sortDateList := dateList
  269. baseDateList := utils.StrArr{}
  270. baseDateList = append(baseDateList, sortDateList...)
  271. sort.Sort(baseDateList)
  272. sortDateList = append([]string{}, baseDateList...)
  273. endDateTime, err := time.ParseInLocation(utils.FormatDate, sortDateList[0], time.Local)
  274. if err != nil {
  275. return
  276. }
  277. firstDateTime, err := time.ParseInLocation(utils.FormatDate, sortDateList[lenDate-1], time.Local)
  278. if err != nil {
  279. return
  280. }
  281. var dataList []*data_manage.EdbDataList
  282. switch edbInfo.EdbInfoType {
  283. case 0:
  284. dataList, err = data_manage.GetEdbDataList(edbInfo.Source, edbInfo.SubSource, edbInfo.EdbInfoId, ``, ``)
  285. case 1:
  286. _, dataList, _, _, err, _ = data.GetPredictDataListByPredictEdbInfoId(edbInfo.EdbInfoId, ``, ``, false)
  287. default:
  288. err = errors.New(fmt.Sprint("获取失败,指标类型异常", edbInfo.EdbInfoType))
  289. }
  290. if err != nil {
  291. return
  292. }
  293. // 获取日期内的数据(包含开始日期前一个日期,以及 结束日期后一个日期,目的为了做空日期时的 插值法兼容)
  294. baseDataList := make([]*data_manage.EdbDataList, 0)
  295. var lastData *data_manage.EdbDataList
  296. var isInsert bool
  297. for _, data := range dataList {
  298. tmpDate := data.DataTime
  299. tmpDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, tmpDate, time.Local)
  300. if tmpErr != nil {
  301. err = tmpErr
  302. return
  303. }
  304. if tmpDateTime.Before(firstDateTime) {
  305. lastData = data
  306. continue
  307. }
  308. // 如果是第一次写入数据
  309. if !isInsert && lastData != nil {
  310. baseDataList = append(baseDataList, lastData)
  311. }
  312. if tmpDateTime.After(endDateTime) {
  313. baseDataList = append(baseDataList, data)
  314. break
  315. }
  316. baseDataList = append(baseDataList, data)
  317. isInsert = true
  318. }
  319. // 实际数据的日期map
  320. realValMap := make(map[string]string)
  321. for _, v := range baseDataList {
  322. realValMap[v.DataTime] = v.DataTime
  323. }
  324. // 插值法处理
  325. handleDataMap := make(map[string]float64)
  326. err = data.HandleDataByLinearRegression(baseDataList, handleDataMap)
  327. if err != nil {
  328. return
  329. }
  330. latestDateTime, _ := time.ParseInLocation(utils.FormatDate, edbInfo.LatestDate, time.Local)
  331. // 对于不存在的数据做补充
  332. for _, date := range sortDateList {
  333. dataType := 1
  334. if _, ok := realValMap[date]; !ok {
  335. dataType = 2
  336. } else {
  337. dataTime, _ := time.ParseInLocation(utils.FormatDate, date, time.Local)
  338. // 如果是预测指标,且当前值的日期,晚于实际日期,那么是预测值
  339. if edbInfo.EdbInfoType == 1 && dataTime.After(latestDateTime) {
  340. dataType = 5
  341. }
  342. }
  343. var value, showValue string
  344. if tmpVal, ok := handleDataMap[date]; ok {
  345. value = fmt.Sprint(tmpVal)
  346. showValue = value
  347. } else {
  348. dataType = 3
  349. }
  350. resultDataList = append(resultDataList, request.ManualDataReq{
  351. DataType: dataType,
  352. DataTime: date,
  353. ShowValue: showValue,
  354. Value: value,
  355. })
  356. }
  357. return
  358. }
  359. // GetFirstHistoryEdbDataList 获取指标的历史的数据
  360. func GetFirstHistoryEdbDataList(edbInfo *data_manage.EdbInfo, num int, endDate string) (resultDataList []request.ManualDataReq, err error) {
  361. endDateTime, err := time.ParseInLocation(utils.FormatDate, endDate, time.Local)
  362. if err != nil {
  363. return
  364. }
  365. var dataList []*data_manage.EdbDataList
  366. switch edbInfo.EdbInfoType {
  367. case 0:
  368. dataList, err = data_manage.GetEdbDataList(edbInfo.Source, edbInfo.SubSource, edbInfo.EdbInfoId, ``, endDate)
  369. case 1:
  370. _, dataList, _, _, err, _ = data.GetPredictDataListByPredictEdbInfoId(edbInfo.EdbInfoId, ``, endDate, true)
  371. default:
  372. err = errors.New(fmt.Sprint("获取失败,指标类型异常", edbInfo.EdbInfoType))
  373. }
  374. if err != nil {
  375. return
  376. }
  377. // 获取需要的期数
  378. lenData := len(dataList)
  379. if lenData <= 0 {
  380. return
  381. }
  382. lastData := dataList[lenData-1]
  383. lastDataDateTime, err := time.ParseInLocation(utils.FormatDate, lastData.DataTime, time.Local)
  384. if err != nil {
  385. return
  386. }
  387. if endDateTime.Equal(lastDataDateTime) || lastDataDateTime.After(endDateTime) {
  388. dataList = dataList[:lenData-1]
  389. lenData = len(dataList)
  390. }
  391. if num > lenData {
  392. num = lenData
  393. }
  394. latestDateTime, _ := time.ParseInLocation(utils.FormatDate, edbInfo.LatestDate, time.Local)
  395. for i := 1; i <= num; i++ {
  396. dataTime, _ := time.ParseInLocation(utils.FormatDate, dataList[lenData-i].DataTime, time.Local)
  397. dataType := 1
  398. // 如果是预测指标,且当前值的日期,晚于实际日期,那么是预测值
  399. if edbInfo.EdbInfoType == 1 && dataTime.After(latestDateTime) {
  400. dataType = 5
  401. }
  402. resultDataList = append(resultDataList, request.ManualDataReq{
  403. DataType: dataType,
  404. DataTime: dataList[lenData-i].DataTime,
  405. ShowValue: fmt.Sprint(dataList[lenData-i].Value),
  406. Value: fmt.Sprint(dataList[lenData-i].Value),
  407. })
  408. }
  409. return
  410. }
  411. // GetEdbIdsFromExcelCodes 获取表格中的指标IDs
  412. func GetEdbIdsFromExcelCodes(excelCodes []string, sysUserId int, lang string) (edbIds []int, err error) {
  413. edbIds = make([]int, 0)
  414. edbIdExist := make(map[int]bool)
  415. for _, v := range excelCodes {
  416. // 表格详情
  417. detail, msg, e := GetExcelDetailInfoByUnicode(v, sysUserId, lang)
  418. if e != nil {
  419. err = fmt.Errorf("GetExcelDetailInfoByExcelInfoId err: %s, errMsg: %s", e.Error(), msg)
  420. return
  421. }
  422. // 自定义表格
  423. if detail.Source == utils.TIME_TABLE {
  424. jsonByte, e := json.Marshal(detail.TableData)
  425. if e != nil {
  426. err = fmt.Errorf("JSON格式化自定义表格数据失败, Err: %s", e.Error())
  427. return
  428. }
  429. var tableData request.TableDataReq
  430. if e = json.Unmarshal(jsonByte, &tableData); e != nil {
  431. err = fmt.Errorf("解析自定义表格数据失败, Err: %s", e.Error())
  432. return
  433. }
  434. for _, tv := range tableData.EdbInfoIdList {
  435. if edbIdExist[tv] {
  436. continue
  437. }
  438. edbIdExist[tv] = true
  439. edbIds = append(edbIds, tv)
  440. }
  441. }
  442. // 混合表格
  443. if detail.Source == utils.MIXED_TABLE {
  444. jsonByte, e := json.Marshal(detail.TableData)
  445. if e != nil {
  446. err = fmt.Errorf("JSON格式化混合表格数据失败, Err: %s", e.Error())
  447. return
  448. }
  449. var tableData request.MixedTableReq
  450. if e = json.Unmarshal(jsonByte, &tableData); e != nil {
  451. err = fmt.Errorf("解析混合表格数据失败, Err: %s", e.Error())
  452. return
  453. }
  454. if len(tableData.Data) > 0 {
  455. for _, td := range tableData.Data {
  456. for _, tv := range td {
  457. if tv.EdbInfoId > 0 && !edbIdExist[tv.EdbInfoId] {
  458. edbIdExist[tv.EdbInfoId] = true
  459. edbIds = append(edbIds, tv.EdbInfoId)
  460. }
  461. }
  462. }
  463. }
  464. }
  465. }
  466. return
  467. }
  468. // GetExcelEdbBatchRefreshKey 获取批量刷新表格指标缓存key
  469. func GetExcelEdbBatchRefreshKey(source string, reportId, chapterId int) string {
  470. if source == `` {
  471. return ``
  472. }
  473. return fmt.Sprint("batch_refresh_excel_edb:", source, ":", reportId, ":", chapterId)
  474. }