excel_info.go 12 KB

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