excel_info.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492
  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. key := utils.HZ_CHART_LIB_EXCEL_TABLE_DETAIL + ":" + uniqueCode
  52. resp := new(response.ExcelTableDetailResp)
  53. excelInfo, err := models.GetExcelInfoByUniqueCode(uniqueCode)
  54. if err != nil {
  55. if err.Error() == utils.ErrNoRow() {
  56. br.Msg = "该表格已被删除,请刷新页面"
  57. br.ErrMsg = "该表格已被删除,请刷新页面,Err:" + err.Error()
  58. return
  59. }
  60. br.Msg = "获取失败"
  61. br.ErrMsg = "获取表格表信息失败,Err:" + err.Error()
  62. return
  63. }
  64. if businessConf.ConfVal == `true` {
  65. resp.ExcelType = excelInfo.ExcelType
  66. resp.ExcelName = excelInfo.ExcelName
  67. resp.ExcelImage = excelInfo.ExcelImage
  68. resp.UniqueCode = excelInfo.UniqueCode
  69. resp.SourcesFrom = excelInfo.SourcesFrom
  70. if authToken == `` {
  71. br.Ret = 200
  72. br.Success = true
  73. br.Msg = "无鉴权"
  74. br.Data = resp
  75. return
  76. }
  77. tmpKey := fmt.Sprint(utils.CACHE_CHART_AUTH, authToken)
  78. redisChartCode, err := utils.Rc.RedisString(tmpKey)
  79. if err != nil {
  80. br.Ret = 200
  81. br.Success = true
  82. br.Msg = "获取成功"
  83. br.Data = resp
  84. return
  85. }
  86. if redisChartCode != uniqueCode {
  87. br.Ret = 200
  88. br.Success = true
  89. br.Msg = "获取成功"
  90. br.Data = resp
  91. return
  92. }
  93. }
  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 && err.Error() != utils.ErrNoRow() {
  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. // Refresh
  316. // @Title 刷新
  317. // @Description 刷新接口
  318. // @Param UniqueCode query string true "表格id"
  319. // @router /refresh [get]
  320. func (this *ExcelInfoController) Refresh() {
  321. br := new(models.BaseResponse).Init()
  322. defer func() {
  323. if br.ErrMsg == "" {
  324. br.IsSendEmail = false
  325. }
  326. this.Data["json"] = br
  327. this.ServeJSON()
  328. }()
  329. uniqueCode := this.GetString("UniqueCode")
  330. if uniqueCode == "" {
  331. br.Msg = "参数错误"
  332. br.ErrMsg = "参数错误, UniqueCode is empty"
  333. return
  334. }
  335. key := utils.HZ_CHART_LIB_EXCEL_TABLE_DETAIL_REFRESH + ":" + uniqueCode
  336. ok := utils.Rc.SetNX(key, 1, time.Second*300)
  337. if !ok {
  338. br.Ret = 200
  339. br.Success = true
  340. br.Msg = "表格刷新中"
  341. return
  342. }
  343. defer func() {
  344. _ = utils.Rc.Delete(key)
  345. }()
  346. // 获取数据详情
  347. excelDetail, errMsg, err := excel2.GetExcelDetailInfoByExcelInfoId(uniqueCode, this.Lang)
  348. if err != nil {
  349. if err.Error() == utils.ErrNoRow() {
  350. br.Msg = "该表格已被删除,请刷新页面"
  351. br.ErrMsg = "该表格已被删除,请刷新页面,Err:" + err.Error()
  352. return
  353. }
  354. br.Msg = errMsg
  355. br.ErrMsg = "获取表格表信息失败,Err:" + err.Error()
  356. return
  357. }
  358. // 数据刷新(只有自定义表格有刷新)
  359. if excelDetail.Source == utils.TIME_TABLE {
  360. jsonByte, e := json.Marshal(excelDetail.TableData)
  361. if e != nil {
  362. br.Msg = "自定义表格数据获取失败"
  363. br.ErrMsg = "自定义表格数据获取失败,转json失败,Err:" + e.Error()
  364. return
  365. }
  366. var tableData request.TableDataReq
  367. if e = json.Unmarshal(jsonByte, &tableData); e != nil {
  368. br.Msg = "自定义表格数据获取失败"
  369. br.ErrMsg = "自定义表格数据获取失败,json转结构体失败,Err:" + e.Error()
  370. return
  371. }
  372. if len(tableData.EdbInfoIdList) > 0 {
  373. if e, _ = data.EdbInfoRefreshAllFromBase(tableData.EdbInfoIdList, false); e != nil {
  374. br.Msg = "刷新失败"
  375. br.ErrMsg = "刷新失败,Err:" + e.Error()
  376. return
  377. }
  378. }
  379. }
  380. // 数据刷新-混合表格
  381. if excelDetail.Source == utils.MIXED_TABLE {
  382. jsonByte, e := json.Marshal(excelDetail.TableData)
  383. if e != nil {
  384. br.Msg = "刷新失败"
  385. br.ErrMsg = "JSON格式化混合表格数据失败, Err: " + e.Error()
  386. return
  387. }
  388. var tableData request.MixedTableReq
  389. if e = json.Unmarshal(jsonByte, &tableData); e != nil {
  390. br.Msg = "刷新失败"
  391. br.ErrMsg = "解析混合表格数据失败, Err: " + e.Error()
  392. return
  393. }
  394. edbInfoIds := make([]int, 0)
  395. edbInfoIdExist := make(map[int]bool)
  396. if len(tableData.Data) > 0 {
  397. for _, t := range tableData.Data {
  398. for _, v := range t {
  399. if v.EdbInfoId > 0 && !edbInfoIdExist[v.EdbInfoId] {
  400. edbInfoIdExist[v.EdbInfoId] = true
  401. edbInfoIds = append(edbInfoIds, v.EdbInfoId)
  402. }
  403. }
  404. }
  405. }
  406. if len(edbInfoIds) > 0 {
  407. if e, _ = data.EdbInfoRefreshAllFromBase(edbInfoIds, false); e != nil {
  408. br.Msg = "刷新失败"
  409. br.ErrMsg = "刷新混合表格数据失败, Err: " + e.Error()
  410. return
  411. }
  412. }
  413. }
  414. // 清除缓存
  415. detailKey := utils.HZ_CHART_LIB_EXCEL_TABLE_DETAIL + ":" + excelDetail.UniqueCode
  416. if utils.Re == nil {
  417. _ = utils.Rc.Delete(detailKey)
  418. }
  419. br.Ret = 200
  420. br.Success = true
  421. br.Msg = "刷新成功"
  422. }
  423. func refreshBalanceTable(excelDetail *excel3.ExcelInfo, lang string) (err error) {
  424. edbInfoIds := make([]int, 0)
  425. edbInfoIdExist := make(map[int]bool)
  426. var result request.MixedTableReq
  427. err = json.Unmarshal([]byte(excelDetail.Content), &result)
  428. if err != nil {
  429. err = fmt.Errorf("表格json转结构体失败,Err:" + err.Error())
  430. return
  431. }
  432. newData, tmpErr, _ := excel2.GetMixedTableCellData(result, lang)
  433. if tmpErr != nil {
  434. err = tmpErr
  435. return
  436. }
  437. if len(newData) > 0 {
  438. for _, t := range newData {
  439. for _, v := range t {
  440. if v.EdbInfoId > 0 && !edbInfoIdExist[v.EdbInfoId] {
  441. edbInfoIdExist[v.EdbInfoId] = true
  442. edbInfoIds = append(edbInfoIds, v.EdbInfoId)
  443. }
  444. }
  445. }
  446. }
  447. // 清除缓存
  448. key := utils.HZ_CHART_LIB_EXCEL_TABLE_DETAIL + ":" + excelDetail.UniqueCode
  449. if utils.Re == nil {
  450. _ = utils.Rc.Delete(key)
  451. }
  452. if len(edbInfoIds) > 0 {
  453. err, _ = data.EdbInfoRefreshAllFromBase(edbInfoIds, false)
  454. if err != nil {
  455. err = fmt.Errorf("刷新混合表格数据失败, Err: " + err.Error())
  456. return
  457. }
  458. }
  459. return
  460. }