excel_info.go 13 KB

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