excel_info.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  1. package controllers
  2. import (
  3. "encoding/json"
  4. "eta/eta_chart_lib/models"
  5. excel3 "eta/eta_chart_lib/models/data_manage/excel"
  6. "eta/eta_chart_lib/models/request"
  7. "eta/eta_chart_lib/models/response"
  8. "eta/eta_chart_lib/services/data"
  9. excel2 "eta/eta_chart_lib/services/data/excel"
  10. "eta/eta_chart_lib/services/excel"
  11. "eta/eta_chart_lib/utils"
  12. "fmt"
  13. "strings"
  14. "time"
  15. "github.com/shopspring/decimal"
  16. )
  17. // ExcelInfoController excel表格
  18. type ExcelInfoController struct {
  19. BaseAuthController
  20. }
  21. // GetTableDetail
  22. // @Title 获取表格表详情
  23. // @Description 获取表格表详情接口
  24. // @Param UniqueCode query string true "表格表唯一编码,如果是管理后台访问,传固定字符串:7c69b590249049942070ae9dcd5bf6dc"
  25. // @Param FromScene query int true "场景来源,1:智能研报,2:研报列表;3:英文研报;4:中文PPT;5:英文PPT"
  26. // @Param AuthToken query string true "图表权限鉴权token"
  27. // @Success 200 {object} response.ExcelTableDetailResp
  28. // @router /detail [get]
  29. func (this *ExcelInfoController) GetTableDetail() {
  30. br := new(models.BaseResponse).Init()
  31. defer func() {
  32. this.Data["json"] = br
  33. this.ServeJSON()
  34. }()
  35. uniqueCode := this.GetString("UniqueCode")
  36. if uniqueCode == "" {
  37. br.Msg = "参数错误"
  38. br.ErrMsg = "参数错误,uniqueCode is empty"
  39. return
  40. }
  41. fromScene, _ := this.GetInt("FromScene", 0)
  42. referencedId, _ := this.GetInt("ReferencedId", 0)
  43. uuid := this.GetString("Uuid")
  44. authToken := this.GetString("AuthToken")
  45. businessConf, err := models.GetBusinessConfByKey(models.BusinessConfIsOpenChartExpired)
  46. if err != nil {
  47. br.Msg = "获取失败"
  48. br.ErrMsg = "获取配置失败,Err:" + err.Error()
  49. return
  50. }
  51. resp := new(response.ExcelTableDetailResp)
  52. excelInfo, err := models.GetExcelInfoByUniqueCode(uniqueCode)
  53. if err != nil {
  54. if utils.IsErrNoRow(err) {
  55. br.Msg = "该表格已被删除,请刷新页面"
  56. br.ErrMsg = "该表格已被删除,请刷新页面,Err:" + err.Error()
  57. return
  58. }
  59. br.Msg = "获取失败"
  60. br.ErrMsg = "获取表格表信息失败,Err:" + err.Error()
  61. return
  62. }
  63. if businessConf.ConfVal == `true` {
  64. resp.ExcelType = excelInfo.ExcelType
  65. resp.ExcelName = excelInfo.ExcelName
  66. resp.ExcelImage = excelInfo.ExcelImage
  67. resp.UniqueCode = excelInfo.UniqueCode
  68. resp.SourcesFrom = excelInfo.SourcesFrom
  69. if authToken == `` {
  70. br.Ret = 200
  71. br.Success = true
  72. br.Msg = "无鉴权"
  73. br.Data = resp
  74. return
  75. }
  76. tmpKey := fmt.Sprint(utils.CACHE_CHART_AUTH, authToken)
  77. redisChartCode, err := utils.Rc.RedisString(tmpKey)
  78. if err != nil {
  79. br.Ret = 200
  80. br.Success = true
  81. br.Msg = "获取成功"
  82. br.Data = resp
  83. return
  84. }
  85. if redisChartCode != uniqueCode {
  86. br.Ret = 200
  87. br.Success = true
  88. br.Msg = "获取成功"
  89. br.Data = resp
  90. return
  91. }
  92. }
  93. key := utils.HZ_CHART_LIB_EXCEL_TABLE_DETAIL + ":" + uniqueCode
  94. switch fromScene {
  95. case 1, 2:
  96. fromScene = 2
  97. }
  98. //判断是否有缓存
  99. //if utils.Re == nil {
  100. // if utils.Re == nil && utils.Rc.IsExist(key) {
  101. // if data, err1 := utils.Rc.RedisBytes(key); err1 == nil {
  102. // err := json.Unmarshal(data, &resp)
  103. // if err == nil && resp != nil {
  104. // // 配置项
  105. // config := response.ExcelTableDetailConfigResp{
  106. // FontSize: 9,
  107. // }
  108. // // 获取配置的字体大小
  109. // confName := models.FromSceneMap[fromScene]
  110. // if confName != `` {
  111. // busConf, err := models.GetBusinessConfByKey(confName)
  112. // if err == nil {
  113. // sizeDeci, err := decimal.NewFromString(busConf.ConfVal)
  114. // if err == nil {
  115. // config.FontSize = int(sizeDeci.IntPart())
  116. // }
  117. // }
  118. // }
  119. // resp.Config = config
  120. //
  121. // br.Ret = 200
  122. // br.Success = true
  123. // br.Msg = "获取成功"
  124. // br.Data = resp
  125. // fmt.Println("source redis")
  126. // return
  127. // }
  128. // }
  129. // }
  130. //}
  131. var tableData excel.TableData
  132. switch excelInfo.Source {
  133. case utils.EXCEL_DEFAULT:
  134. luckySheetData, err := excel.GetLuckySheetData(excelInfo.Content)
  135. if err != nil {
  136. br.Msg = "获取失败"
  137. br.ErrMsg = "获取excel数据失败,Err:" + err.Error()
  138. return
  139. }
  140. tableData, err = luckySheetData.GetTableDataByLuckySheetDataStr()
  141. if err != nil {
  142. br.Msg = "获取失败"
  143. br.ErrMsg = "转换成table失败,Err:" + err.Error()
  144. return
  145. }
  146. case utils.TIME_TABLE:
  147. var tableDataConfig excel2.TableDataConfig
  148. err = json.Unmarshal([]byte(excelInfo.Content), &tableDataConfig)
  149. if err != nil {
  150. br.Msg = "获取失败"
  151. br.ErrMsg = "表格json转结构体失败,Err:" + err.Error()
  152. return
  153. }
  154. result, err := excel2.GetDataByTableDataConfig(tableDataConfig)
  155. if err != nil {
  156. br.Msg = "获取失败"
  157. br.ErrMsg = "获取最新的表格数据失败,Err:" + err.Error()
  158. return
  159. }
  160. tableData, err = excel.GetTableDataByCustomData(excelInfo.ExcelType, result, this.Lang)
  161. if err != nil {
  162. br.Msg = "获取失败"
  163. br.ErrMsg = "转换成table失败,Err:" + err.Error()
  164. return
  165. }
  166. sourceNameList, sourceNameEnList, err := data.GetEdbSourceByEdbInfoIdListForExcel(result.EdbInfoIdList)
  167. if err != nil {
  168. br.Msg = "自定义表格数据获取失败"
  169. br.ErrMsg = "自定义表格数据获取失败,Err:" + err.Error()
  170. return
  171. }
  172. resp.ExcelSource = strings.Join(sourceNameList, ",")
  173. resp.ExcelSourceEn = strings.Join(sourceNameEnList, ",")
  174. case utils.MIXED_TABLE:
  175. var result request.MixedTableReq
  176. err = json.Unmarshal([]byte(excelInfo.Content), &result)
  177. if err != nil {
  178. br.Msg = "获取失败"
  179. br.ErrMsg = "表格json转结构体失败,Err:" + err.Error()
  180. return
  181. }
  182. newResult, tmpErr, tmpErrMsg := excel2.GetMixedTableCellData(result, this.Lang)
  183. if tmpErr != nil {
  184. br.Msg = "获取失败"
  185. if tmpErrMsg != `` {
  186. br.Msg = tmpErrMsg
  187. }
  188. br.ErrMsg = "获取最新的数据失败,Err:" + tmpErr.Error()
  189. return
  190. }
  191. tableData, err = excel.GetTableDataByMixedTableData(newResult, true, excelInfo.ExcelInfoId)
  192. if err != nil {
  193. br.Msg = "获取失败"
  194. br.ErrMsg = "转换成table失败,Err:" + err.Error()
  195. return
  196. }
  197. edbInfoIds := make([]int, 0)
  198. edbInfoIdExist := make(map[int]bool)
  199. if len(newResult) > 0 {
  200. for _, t := range newResult {
  201. for _, v := range t {
  202. if v.EdbInfoId > 0 && !edbInfoIdExist[v.EdbInfoId] {
  203. edbInfoIdExist[v.EdbInfoId] = true
  204. edbInfoIds = append(edbInfoIds, v.EdbInfoId)
  205. }
  206. }
  207. }
  208. }
  209. sourceNameList, sourceNameEnList, err := data.GetEdbSourceByEdbInfoIdListForExcel(edbInfoIds)
  210. if err != nil {
  211. br.Msg = "自定义表格数据获取失败"
  212. br.ErrMsg = "自定义表格数据获取失败,Err:" + err.Error()
  213. return
  214. }
  215. resp.ExcelSource = strings.Join(sourceNameList, ",")
  216. resp.ExcelSourceEn = strings.Join(sourceNameEnList, ",")
  217. case utils.BALANCE_TABLE:
  218. var result request.MixedTableReq
  219. err = json.Unmarshal([]byte(excelInfo.Content), &result)
  220. if err != nil {
  221. br.Msg = "获取失败"
  222. br.ErrMsg = "表格json转结构体失败,Err:" + err.Error()
  223. return
  224. }
  225. newResult, tmpErr, tmpErrMsg := excel2.GetMixedTableCellData(result, this.Lang)
  226. if tmpErr != nil {
  227. br.Msg = "获取失败"
  228. if tmpErrMsg != `` {
  229. br.Msg = tmpErrMsg
  230. }
  231. br.ErrMsg = "获取最新的数据失败,Err:" + tmpErr.Error()
  232. return
  233. }
  234. tableData, err = excel.GetTableDataByMixedTableData(newResult, true, excelInfo.ExcelInfoId)
  235. if err != nil {
  236. br.Msg = "获取失败"
  237. br.ErrMsg = "转换成table失败,Err:" + err.Error()
  238. return
  239. }
  240. edbInfoIds := make([]int, 0)
  241. edbInfoIdExist := make(map[int]bool)
  242. if len(newResult) > 0 {
  243. for _, t := range newResult {
  244. for _, v := range t {
  245. if v.EdbInfoId > 0 && !edbInfoIdExist[v.EdbInfoId] {
  246. edbInfoIdExist[v.EdbInfoId] = true
  247. edbInfoIds = append(edbInfoIds, v.EdbInfoId)
  248. }
  249. }
  250. }
  251. }
  252. sourceNameList, sourceNameEnList, err := data.GetEdbSourceByEdbInfoIdListForExcel(edbInfoIds)
  253. if err != nil {
  254. br.Msg = "自定义表格数据获取失败"
  255. br.ErrMsg = "自定义表格数据获取失败,Err:" + err.Error()
  256. return
  257. }
  258. resp.ExcelSource = strings.Join(sourceNameList, ",")
  259. resp.ExcelSourceEn = strings.Join(sourceNameEnList, ",")
  260. }
  261. tableData = excel.HandleTableCell(tableData)
  262. // tableData, err = excel.HandleRuleToTableCell(excelInfo.ExcelInfoId, tableData)
  263. // if err != nil {
  264. // utils.FileLog.Info("表格管理规则处理失败,HandleRuleToTableCell err:", err.Error())
  265. // }
  266. config := response.ExcelTableDetailConfigResp{
  267. FontSize: 9,
  268. }
  269. waterMarkConf, err := models.GetBusinessConfByKeys([]string{"WatermarkTable", "CompanyWatermark"})
  270. if err != nil {
  271. br.Msg = "获取水印失败"
  272. br.ErrMsg = "获取水印失败,Err:" + err.Error()
  273. return
  274. }
  275. if waterMarkConf["WatermarkTable"] == "true" && waterMarkConf["CompanyWatermark"] != "" {
  276. config.Watermark = waterMarkConf["CompanyWatermark"]
  277. }
  278. // 获取配置的字体大小
  279. confName := models.FromSceneMap[fromScene]
  280. if confName != `` {
  281. busConf, err := models.GetBusinessConfByKey(confName)
  282. if err == nil {
  283. sizeDeci, err := decimal.NewFromString(busConf.ConfVal)
  284. if err == nil {
  285. config.FontSize = int(sizeDeci.IntPart())
  286. }
  287. }
  288. }
  289. // 获取表格引用
  290. reference, err := models.GetReferencedExcelConfig(referencedId, fromScene, uniqueCode, uuid)
  291. if err != nil && !utils.IsErrNoRow(err) {
  292. br.Msg = "获取失败"
  293. br.ErrMsg = "获取表格引用失败,Err:" + err.Error()
  294. return
  295. }
  296. resp.IsAuth = true
  297. resp.TableInfo = tableData
  298. resp.Source = excelInfo.Source
  299. resp.ExcelType = excelInfo.ExcelType
  300. resp.ExcelName = excelInfo.ExcelName
  301. resp.ExcelImage = excelInfo.ExcelImage
  302. resp.UniqueCode = excelInfo.UniqueCode
  303. resp.Config = config
  304. resp.SourcesFrom = excelInfo.SourcesFrom
  305. resp.ReferencedExcelConfig = reference
  306. if utils.Re == nil {
  307. data, _ := json.Marshal(resp)
  308. utils.Rc.Put(key, data, 5*time.Minute)
  309. }
  310. br.Ret = 200
  311. br.Success = true
  312. br.Msg = "获取成功"
  313. br.Data = resp
  314. }
  315. func refreshBalanceTable(excelDetail *excel3.ExcelInfo, lang string) (err error) {
  316. edbInfoIds := make([]int, 0)
  317. edbInfoIdExist := make(map[int]bool)
  318. var result request.MixedTableReq
  319. err = json.Unmarshal([]byte(excelDetail.Content), &result)
  320. if err != nil {
  321. err = fmt.Errorf("表格json转结构体失败,Err:" + err.Error())
  322. return
  323. }
  324. newData, tmpErr, _ := excel2.GetMixedTableCellData(result, lang)
  325. if tmpErr != nil {
  326. err = tmpErr
  327. return
  328. }
  329. if len(newData) > 0 {
  330. for _, t := range newData {
  331. for _, v := range t {
  332. if v.EdbInfoId > 0 && !edbInfoIdExist[v.EdbInfoId] {
  333. edbInfoIdExist[v.EdbInfoId] = true
  334. edbInfoIds = append(edbInfoIds, v.EdbInfoId)
  335. }
  336. }
  337. }
  338. }
  339. // 清除缓存
  340. key := utils.HZ_CHART_LIB_EXCEL_TABLE_DETAIL + ":" + excelDetail.UniqueCode
  341. if utils.Re == nil {
  342. _ = utils.Rc.Delete(key)
  343. }
  344. if len(edbInfoIds) > 0 {
  345. err, _ = data.EdbInfoRefreshAllFromBase(edbInfoIds, false)
  346. if err != nil {
  347. err = fmt.Errorf("刷新混合表格数据失败, Err: " + err.Error())
  348. return
  349. }
  350. }
  351. return
  352. }
  353. // Refresh
  354. // @Title 刷新
  355. // @Description 刷新接口
  356. // @Param UniqueCode query string true "表格id"
  357. // @router /refresh [get]
  358. func (this *ExcelInfoController) Refresh() {
  359. br := new(models.BaseResponse).Init()
  360. defer func() {
  361. if br.ErrMsg == "" {
  362. br.IsSendEmail = false
  363. }
  364. this.Data["json"] = br
  365. this.ServeJSON()
  366. }()
  367. uniqueCode := this.GetString("UniqueCode")
  368. if uniqueCode == "" {
  369. br.Msg = "参数错误"
  370. br.ErrMsg = "参数错误, UniqueCode is empty"
  371. return
  372. }
  373. key := utils.HZ_CHART_LIB_EXCEL_TABLE_DETAIL_REFRESH + ":" + uniqueCode
  374. ok := utils.Rc.SetNX(key, 1, time.Second*300)
  375. if !ok {
  376. br.Ret = 200
  377. br.Success = true
  378. br.Msg = "表格刷新中"
  379. return
  380. }
  381. defer func() {
  382. _ = utils.Rc.Delete(key)
  383. }()
  384. // 获取数据详情
  385. excelDetail, errMsg, err := excel2.GetExcelDetailInfoByExcelInfoId(uniqueCode, this.Lang)
  386. if err != nil {
  387. if utils.IsErrNoRow(err) {
  388. br.Msg = "该表格已被删除,请刷新页面"
  389. br.ErrMsg = "该表格已被删除,请刷新页面,Err:" + err.Error()
  390. return
  391. }
  392. br.Msg = errMsg
  393. br.ErrMsg = "获取表格表信息失败,Err:" + err.Error()
  394. return
  395. }
  396. // 数据刷新(只有自定义表格有刷新)
  397. if excelDetail.Source == utils.TIME_TABLE {
  398. jsonByte, e := json.Marshal(excelDetail.TableData)
  399. if e != nil {
  400. br.Msg = "自定义表格数据获取失败"
  401. br.ErrMsg = "自定义表格数据获取失败,转json失败,Err:" + e.Error()
  402. return
  403. }
  404. var tableData request.TableDataReq
  405. if e = json.Unmarshal(jsonByte, &tableData); e != nil {
  406. br.Msg = "自定义表格数据获取失败"
  407. br.ErrMsg = "自定义表格数据获取失败,json转结构体失败,Err:" + e.Error()
  408. return
  409. }
  410. if len(tableData.EdbInfoIdList) > 0 {
  411. if e, _ = data.EdbInfoRefreshAllFromBase(tableData.EdbInfoIdList, false); e != nil {
  412. br.Msg = "刷新失败"
  413. br.ErrMsg = "刷新失败,Err:" + e.Error()
  414. return
  415. }
  416. }
  417. }
  418. // 数据刷新-混合表格
  419. if excelDetail.Source == utils.MIXED_TABLE {
  420. jsonByte, e := json.Marshal(excelDetail.TableData)
  421. if e != nil {
  422. br.Msg = "刷新失败"
  423. br.ErrMsg = "JSON格式化混合表格数据失败, Err: " + e.Error()
  424. return
  425. }
  426. var tableData request.MixedTableReq
  427. if e = json.Unmarshal(jsonByte, &tableData); e != nil {
  428. br.Msg = "刷新失败"
  429. br.ErrMsg = "解析混合表格数据失败, Err: " + e.Error()
  430. return
  431. }
  432. edbInfoIds := make([]int, 0)
  433. edbInfoIdExist := make(map[int]bool)
  434. if len(tableData.Data) > 0 {
  435. for _, t := range tableData.Data {
  436. for _, v := range t {
  437. if v.EdbInfoId > 0 && !edbInfoIdExist[v.EdbInfoId] {
  438. edbInfoIdExist[v.EdbInfoId] = true
  439. edbInfoIds = append(edbInfoIds, v.EdbInfoId)
  440. }
  441. }
  442. }
  443. }
  444. if len(edbInfoIds) > 0 {
  445. if e, _ = data.EdbInfoRefreshAllFromBase(edbInfoIds, false); e != nil {
  446. br.Msg = "刷新失败"
  447. br.ErrMsg = "刷新混合表格数据失败, Err: " + e.Error()
  448. return
  449. }
  450. }
  451. }
  452. // 清除缓存
  453. detailKey := utils.HZ_CHART_LIB_EXCEL_TABLE_DETAIL + ":" + excelDetail.UniqueCode
  454. if utils.Re == nil {
  455. _ = utils.Rc.Delete(detailKey)
  456. }
  457. br.Ret = 200
  458. br.Success = true
  459. br.Msg = "刷新成功"
  460. }