excel_info.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  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. "github.com/shopspring/decimal"
  14. "time"
  15. )
  16. // ExcelInfoController excel表格
  17. type ExcelInfoController struct {
  18. BaseAuthController
  19. }
  20. // GetTableDetail
  21. // @Title 获取表格表详情
  22. // @Description 获取表格表详情接口
  23. // @Param UniqueCode query string true "表格表唯一编码,如果是管理后台访问,传固定字符串:7c69b590249049942070ae9dcd5bf6dc"
  24. // @Param FromScene query int true "场景来源,1:智能研报,2:研报列表;3:英文研报;4:中文PPT;5:英文PPT"
  25. // @Success 200 {object} response.ExcelTableDetailResp
  26. // @router /detail [get]
  27. func (this *ExcelInfoController) GetTableDetail() {
  28. br := new(models.BaseResponse).Init()
  29. defer func() {
  30. this.Data["json"] = br
  31. this.ServeJSON()
  32. }()
  33. uniqueCode := this.GetString("UniqueCode")
  34. if uniqueCode == "" {
  35. br.Msg = "参数错误"
  36. br.ErrMsg = "参数错误,uniqueCode is empty"
  37. return
  38. }
  39. fromScene, _ := this.GetInt("FromScene", 0)
  40. key := utils.HZ_CHART_LIB_EXCEL_TABLE_DETAIL + ":" + uniqueCode
  41. resp := new(response.ExcelTableDetailResp)
  42. //判断是否有缓存
  43. //if utils.Re == nil {
  44. // if utils.Re == nil && utils.Rc.IsExist(key) {
  45. // if data, err1 := utils.Rc.RedisBytes(key); err1 == nil {
  46. // err := json.Unmarshal(data, &resp)
  47. // if err == nil && resp != nil {
  48. // // 配置项
  49. // config := response.ExcelTableDetailConfigResp{
  50. // FontSize: 9,
  51. // }
  52. // // 获取配置的字体大小
  53. // confName := models.FromSceneMap[fromScene]
  54. // if confName != `` {
  55. // busConf, err := models.GetBusinessConfByKey(confName)
  56. // if err == nil {
  57. // sizeDeci, err := decimal.NewFromString(busConf.ConfVal)
  58. // if err == nil {
  59. // config.FontSize = int(sizeDeci.IntPart())
  60. // }
  61. // }
  62. // }
  63. // resp.Config = config
  64. //
  65. // br.Ret = 200
  66. // br.Success = true
  67. // br.Msg = "获取成功"
  68. // br.Data = resp
  69. // fmt.Println("source redis")
  70. // return
  71. // }
  72. // }
  73. // }
  74. //}
  75. excelInfo, err := models.GetExcelInfoByUniqueCode(uniqueCode)
  76. if err != nil {
  77. if err.Error() == utils.ErrNoRow() {
  78. br.Msg = "该表格已被删除,请刷新页面"
  79. br.ErrMsg = "该表格已被删除,请刷新页面,Err:" + err.Error()
  80. return
  81. }
  82. br.Msg = "获取失败"
  83. br.ErrMsg = "获取表格表信息失败,Err:" + err.Error()
  84. return
  85. }
  86. var tableData excel.TableData
  87. switch excelInfo.Source {
  88. case utils.EXCEL_DEFAULT:
  89. luckySheetData, err := excel.GetLuckySheetData(excelInfo.Content)
  90. if err != nil {
  91. br.Msg = "获取失败"
  92. br.ErrMsg = "获取excel数据失败,Err:" + err.Error()
  93. return
  94. }
  95. tableData, err = luckySheetData.GetTableDataByLuckySheetDataStr()
  96. if err != nil {
  97. br.Msg = "获取失败"
  98. br.ErrMsg = "转换成table失败,Err:" + err.Error()
  99. return
  100. }
  101. case utils.TIME_TABLE:
  102. var tableDataConfig excel2.TableDataConfig
  103. err = json.Unmarshal([]byte(excelInfo.Content), &tableDataConfig)
  104. if err != nil {
  105. br.Msg = "获取失败"
  106. br.ErrMsg = "表格json转结构体失败,Err:" + err.Error()
  107. return
  108. }
  109. result, err := excel2.GetDataByTableDataConfig(tableDataConfig)
  110. if err != nil {
  111. br.Msg = "获取失败"
  112. br.ErrMsg = "获取最新的表格数据失败,Err:" + err.Error()
  113. return
  114. }
  115. tableData, err = excel.GetTableDataByCustomData(excelInfo.ExcelType, result, this.Lang)
  116. if err != nil {
  117. br.Msg = "获取失败"
  118. br.ErrMsg = "转换成table失败,Err:" + err.Error()
  119. return
  120. }
  121. //sourceNameList, sourceNameEnList, err := data.GetEdbSourceByEdbInfoIdListForExcel(result.EdbInfoIdList)
  122. //if err != nil {
  123. // br.Msg = "自定义表格数据获取失败"
  124. // br.ErrMsg = "自定义表格数据获取失败,Err:" + err.Error()
  125. // return
  126. //}
  127. //resp.ExcelSource = strings.Join(sourceNameList, ",")
  128. //resp.ExcelSourceEn = strings.Join(sourceNameEnList, ",")
  129. case utils.MIXED_TABLE:
  130. var result request.MixedTableReq
  131. err = json.Unmarshal([]byte(excelInfo.Content), &result)
  132. if err != nil {
  133. br.Msg = "获取失败"
  134. br.ErrMsg = "表格json转结构体失败,Err:" + err.Error()
  135. return
  136. }
  137. newResult, tmpErr, tmpErrMsg := excel2.GetMixedTableCellData(result, this.Lang)
  138. if tmpErr != nil {
  139. br.Msg = "获取失败"
  140. if tmpErrMsg != `` {
  141. br.Msg = tmpErrMsg
  142. }
  143. br.ErrMsg = "获取最新的数据失败,Err:" + tmpErr.Error()
  144. return
  145. }
  146. tableData, err = excel.GetTableDataByMixedTableData(newResult)
  147. if err != nil {
  148. br.Msg = "获取失败"
  149. br.ErrMsg = "转换成table失败,Err:" + err.Error()
  150. return
  151. }
  152. //edbInfoIds := make([]int, 0)
  153. //edbInfoIdExist := make(map[int]bool)
  154. //if len(newResult) > 0 {
  155. // for _, t := range newResult {
  156. // for _, v := range t {
  157. // if v.EdbInfoId > 0 && !edbInfoIdExist[v.EdbInfoId] {
  158. // edbInfoIdExist[v.EdbInfoId] = true
  159. // edbInfoIds = append(edbInfoIds, v.EdbInfoId)
  160. // }
  161. // }
  162. // }
  163. //}
  164. //sourceNameList, sourceNameEnList, err := data.GetEdbSourceByEdbInfoIdListForExcel(edbInfoIds)
  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. }
  173. tableData = excel.HandleTableCell(tableData)
  174. config := response.ExcelTableDetailConfigResp{
  175. FontSize: 9,
  176. }
  177. // 获取配置的字体大小
  178. confName := models.FromSceneMap[fromScene]
  179. if confName != `` {
  180. busConf, err := models.GetBusinessConfByKey(confName)
  181. if err == nil {
  182. sizeDeci, err := decimal.NewFromString(busConf.ConfVal)
  183. if err == nil {
  184. config.FontSize = int(sizeDeci.IntPart())
  185. }
  186. }
  187. }
  188. resp.TableInfo = tableData
  189. resp.Source = excelInfo.Source
  190. resp.ExcelType = excelInfo.ExcelType
  191. resp.ExcelName = excelInfo.ExcelName
  192. resp.ExcelImage = excelInfo.ExcelImage
  193. resp.UniqueCode = excelInfo.UniqueCode
  194. resp.Config = config
  195. //resp.SourcesFrom = excelInfo.SourcesFrom
  196. if utils.Re == nil {
  197. data, _ := json.Marshal(resp)
  198. utils.Rc.Put(key, data, 5*time.Minute)
  199. }
  200. br.Ret = 200
  201. br.Success = true
  202. br.Msg = "获取成功"
  203. br.Data = resp
  204. }
  205. func refreshBalanceTable(excelDetail *excel3.ExcelInfo, lang string) (err error) {
  206. edbInfoIds := make([]int, 0)
  207. edbInfoIdExist := make(map[int]bool)
  208. var result request.MixedTableReq
  209. err = json.Unmarshal([]byte(excelDetail.Content), &result)
  210. if err != nil {
  211. err = fmt.Errorf("表格json转结构体失败,Err:" + err.Error())
  212. return
  213. }
  214. newData, tmpErr, _ := excel2.GetMixedTableCellData(result, lang)
  215. if tmpErr != nil {
  216. err = tmpErr
  217. return
  218. }
  219. if len(newData) > 0 {
  220. for _, t := range newData {
  221. for _, v := range t {
  222. if v.EdbInfoId > 0 && !edbInfoIdExist[v.EdbInfoId] {
  223. edbInfoIdExist[v.EdbInfoId] = true
  224. edbInfoIds = append(edbInfoIds, v.EdbInfoId)
  225. }
  226. }
  227. }
  228. }
  229. // 清除缓存
  230. key := utils.HZ_CHART_LIB_EXCEL_TABLE_DETAIL + ":" + excelDetail.UniqueCode
  231. if utils.Re == nil {
  232. _ = utils.Rc.Delete(key)
  233. }
  234. if len(edbInfoIds) > 0 {
  235. err, _ = data.EdbInfoRefreshAllFromBase(edbInfoIds, false)
  236. if err != nil {
  237. err = fmt.Errorf("刷新混合表格数据失败, Err: " + err.Error())
  238. return
  239. }
  240. }
  241. return
  242. }
  243. // Refresh
  244. // @Title 刷新
  245. // @Description 刷新接口
  246. // @Param UniqueCode query string true "表格id"
  247. // @router /refresh [get]
  248. func (this *ExcelInfoController) Refresh() {
  249. br := new(models.BaseResponse).Init()
  250. defer func() {
  251. if br.ErrMsg == "" {
  252. br.IsSendEmail = false
  253. }
  254. this.Data["json"] = br
  255. this.ServeJSON()
  256. }()
  257. uniqueCode := this.GetString("UniqueCode")
  258. if uniqueCode == "" {
  259. br.Msg = "参数错误"
  260. br.ErrMsg = "参数错误, UniqueCode is empty"
  261. return
  262. }
  263. key := utils.HZ_CHART_LIB_EXCEL_TABLE_DETAIL_REFRESH + ":" + uniqueCode
  264. ok := utils.Rc.SetNX(key, 1, time.Second*300)
  265. if !ok {
  266. br.Ret = 200
  267. br.Success = true
  268. br.Msg = "表格刷新中"
  269. return
  270. }
  271. defer func() {
  272. _ = utils.Rc.Delete(key)
  273. }()
  274. // 获取数据详情
  275. excelDetail, errMsg, err := excel2.GetExcelDetailInfoByExcelInfoId(uniqueCode, this.Lang)
  276. if err != nil {
  277. if err.Error() == utils.ErrNoRow() {
  278. br.Msg = "该表格已被删除,请刷新页面"
  279. br.ErrMsg = "该表格已被删除,请刷新页面,Err:" + err.Error()
  280. return
  281. }
  282. br.Msg = errMsg
  283. br.ErrMsg = "获取表格表信息失败,Err:" + err.Error()
  284. return
  285. }
  286. // 数据刷新(只有自定义表格有刷新)
  287. if excelDetail.Source == utils.TIME_TABLE {
  288. jsonByte, e := json.Marshal(excelDetail.TableData)
  289. if e != nil {
  290. br.Msg = "自定义表格数据获取失败"
  291. br.ErrMsg = "自定义表格数据获取失败,转json失败,Err:" + e.Error()
  292. return
  293. }
  294. var tableData request.TableDataReq
  295. if e = json.Unmarshal(jsonByte, &tableData); e != nil {
  296. br.Msg = "自定义表格数据获取失败"
  297. br.ErrMsg = "自定义表格数据获取失败,json转结构体失败,Err:" + e.Error()
  298. return
  299. }
  300. if len(tableData.EdbInfoIdList) > 0 {
  301. if e, _ = data.EdbInfoRefreshAllFromBase(tableData.EdbInfoIdList, false); e != nil {
  302. br.Msg = "刷新失败"
  303. br.ErrMsg = "刷新失败,Err:" + e.Error()
  304. return
  305. }
  306. }
  307. }
  308. // 数据刷新-混合表格
  309. if excelDetail.Source == utils.MIXED_TABLE {
  310. jsonByte, e := json.Marshal(excelDetail.TableData)
  311. if e != nil {
  312. br.Msg = "刷新失败"
  313. br.ErrMsg = "JSON格式化混合表格数据失败, Err: " + e.Error()
  314. return
  315. }
  316. var tableData request.MixedTableReq
  317. if e = json.Unmarshal(jsonByte, &tableData); e != nil {
  318. br.Msg = "刷新失败"
  319. br.ErrMsg = "解析混合表格数据失败, Err: " + e.Error()
  320. return
  321. }
  322. edbInfoIds := make([]int, 0)
  323. edbInfoIdExist := make(map[int]bool)
  324. if len(tableData.Data) > 0 {
  325. for _, t := range tableData.Data {
  326. for _, v := range t {
  327. if v.EdbInfoId > 0 && !edbInfoIdExist[v.EdbInfoId] {
  328. edbInfoIdExist[v.EdbInfoId] = true
  329. edbInfoIds = append(edbInfoIds, v.EdbInfoId)
  330. }
  331. }
  332. }
  333. }
  334. if len(edbInfoIds) > 0 {
  335. if e, _ = data.EdbInfoRefreshAllFromBase(edbInfoIds, false); e != nil {
  336. br.Msg = "刷新失败"
  337. br.ErrMsg = "刷新混合表格数据失败, Err: " + e.Error()
  338. return
  339. }
  340. }
  341. }
  342. // 清除缓存
  343. detailKey := utils.HZ_CHART_LIB_EXCEL_TABLE_DETAIL + ":" + excelDetail.UniqueCode
  344. if utils.Re == nil {
  345. _ = utils.Rc.Delete(detailKey)
  346. }
  347. br.Ret = 200
  348. br.Success = true
  349. br.Msg = "刷新成功"
  350. }