excel_info.go 13 KB

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