excel_info.go 14 KB

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