excel_info.go 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  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. case utils.MIXED_TABLE:
  122. var result request.MixedTableReq
  123. err = json.Unmarshal([]byte(excelInfo.Content), &result)
  124. if err != nil {
  125. br.Msg = "获取失败"
  126. br.ErrMsg = "表格json转结构体失败,Err:" + err.Error()
  127. return
  128. }
  129. newResult, tmpErr, tmpErrMsg := excel2.GetMixedTableCellData(result, this.Lang)
  130. if tmpErr != nil {
  131. br.Msg = "获取失败"
  132. if tmpErrMsg != `` {
  133. br.Msg = tmpErrMsg
  134. }
  135. br.ErrMsg = "获取最新的数据失败,Err:" + tmpErr.Error()
  136. return
  137. }
  138. tableData, err = excel.GetTableDataByMixedTableData(newResult)
  139. if err != nil {
  140. br.Msg = "获取失败"
  141. br.ErrMsg = "转换成table失败,Err:" + err.Error()
  142. return
  143. }
  144. }
  145. tableData = excel.HandleTableCell(tableData)
  146. config := response.ExcelTableDetailConfigResp{
  147. FontSize: 9,
  148. }
  149. // 获取配置的字体大小
  150. confName := models.FromSceneMap[fromScene]
  151. if confName != `` {
  152. busConf, err := models.GetBusinessConfByKey(confName)
  153. if err == nil {
  154. sizeDeci, err := decimal.NewFromString(busConf.ConfVal)
  155. if err == nil {
  156. config.FontSize = int(sizeDeci.IntPart())
  157. }
  158. }
  159. }
  160. resp.TableInfo = tableData
  161. resp.Source = excelInfo.Source
  162. resp.ExcelType = excelInfo.ExcelType
  163. resp.ExcelName = excelInfo.ExcelName
  164. resp.ExcelImage = excelInfo.ExcelImage
  165. resp.UniqueCode = excelInfo.UniqueCode
  166. resp.Config = config
  167. if utils.Re == nil {
  168. data, _ := json.Marshal(resp)
  169. utils.Rc.Put(key, data, 5*time.Minute)
  170. }
  171. br.Ret = 200
  172. br.Success = true
  173. br.Msg = "获取成功"
  174. br.Data = resp
  175. }
  176. // Refresh
  177. // @Title 刷新
  178. // @Description 刷新接口
  179. // @Param UniqueCode query string true "表格id"
  180. // @router /refresh [get]
  181. func (this *ExcelInfoController) Refresh() {
  182. br := new(models.BaseResponse).Init()
  183. defer func() {
  184. if br.ErrMsg == "" {
  185. br.IsSendEmail = false
  186. }
  187. this.Data["json"] = br
  188. this.ServeJSON()
  189. }()
  190. uniqueCode := this.GetString("UniqueCode")
  191. if uniqueCode == "" {
  192. br.Msg = "参数错误"
  193. br.ErrMsg = "参数错误, UniqueCode is empty"
  194. return
  195. }
  196. key := utils.HZ_CHART_LIB_EXCEL_TABLE_DETAIL_REFRESH + ":" + uniqueCode
  197. ok := utils.Rc.SetNX(key, 1, time.Second*300)
  198. if !ok {
  199. br.Ret = 200
  200. br.Success = true
  201. br.Msg = "表格刷新中"
  202. return
  203. }
  204. defer func() {
  205. _ = utils.Rc.Delete(key)
  206. }()
  207. // 获取数据详情
  208. excelDetail, errMsg, err := excel2.GetExcelDetailInfoByExcelInfoId(uniqueCode, this.Lang)
  209. if err != nil {
  210. if err.Error() == utils.ErrNoRow() {
  211. br.Msg = "该表格已被删除,请刷新页面"
  212. br.ErrMsg = "该表格已被删除,请刷新页面,Err:" + err.Error()
  213. return
  214. }
  215. br.Msg = errMsg
  216. br.ErrMsg = "获取表格表信息失败,Err:" + err.Error()
  217. return
  218. }
  219. // 数据刷新(只有自定义表格有刷新)
  220. if excelDetail.Source == utils.TIME_TABLE {
  221. jsonByte, e := json.Marshal(excelDetail.TableData)
  222. if e != nil {
  223. br.Msg = "自定义表格数据获取失败"
  224. br.ErrMsg = "自定义表格数据获取失败,转json失败,Err:" + e.Error()
  225. return
  226. }
  227. var tableData request.TableDataReq
  228. if e = json.Unmarshal(jsonByte, &tableData); e != nil {
  229. br.Msg = "自定义表格数据获取失败"
  230. br.ErrMsg = "自定义表格数据获取失败,json转结构体失败,Err:" + e.Error()
  231. return
  232. }
  233. if len(tableData.EdbInfoIdList) > 0 {
  234. if e, _ = data.EdbInfoRefreshAllFromBase(tableData.EdbInfoIdList, false); e != nil {
  235. br.Msg = "刷新失败"
  236. br.ErrMsg = "刷新失败,Err:" + e.Error()
  237. return
  238. }
  239. }
  240. }
  241. // 数据刷新-混合表格
  242. if excelDetail.Source == utils.MIXED_TABLE {
  243. jsonByte, e := json.Marshal(excelDetail.TableData)
  244. if e != nil {
  245. br.Msg = "刷新失败"
  246. br.ErrMsg = "JSON格式化混合表格数据失败, Err: " + e.Error()
  247. return
  248. }
  249. var tableData request.MixedTableReq
  250. if e = json.Unmarshal(jsonByte, &tableData); e != nil {
  251. br.Msg = "刷新失败"
  252. br.ErrMsg = "解析混合表格数据失败, Err: " + e.Error()
  253. return
  254. }
  255. edbInfoIds := make([]int, 0)
  256. edbInfoIdExist := make(map[int]bool)
  257. if len(tableData.Data) > 0 {
  258. for _, t := range tableData.Data {
  259. for _, v := range t {
  260. if v.EdbInfoId > 0 && !edbInfoIdExist[v.EdbInfoId] {
  261. edbInfoIdExist[v.EdbInfoId] = true
  262. edbInfoIds = append(edbInfoIds, v.EdbInfoId)
  263. }
  264. }
  265. }
  266. }
  267. if len(edbInfoIds) > 0 {
  268. if e, _ = data.EdbInfoRefreshAllFromBase(edbInfoIds, false); e != nil {
  269. br.Msg = "刷新失败"
  270. br.ErrMsg = "刷新混合表格数据失败, Err: " + e.Error()
  271. return
  272. }
  273. }
  274. }
  275. // 清除缓存
  276. detailKey := utils.HZ_CHART_LIB_EXCEL_TABLE_DETAIL + ":" + excelDetail.UniqueCode
  277. if utils.Re == nil {
  278. _ = utils.Rc.Delete(detailKey)
  279. }
  280. br.Ret = 200
  281. br.Success = true
  282. br.Msg = "刷新成功"
  283. }
  284. func refreshBalanceTable(excelDetail *excel3.ExcelInfo, lang string) (err error) {
  285. edbInfoIds := make([]int, 0)
  286. edbInfoIdExist := make(map[int]bool)
  287. var result request.MixedTableReq
  288. err = json.Unmarshal([]byte(excelDetail.Content), &result)
  289. if err != nil {
  290. err = fmt.Errorf("表格json转结构体失败,Err:" + err.Error())
  291. return
  292. }
  293. newData, tmpErr, _ := excel2.GetMixedTableCellData(result, lang)
  294. if tmpErr != nil {
  295. err = tmpErr
  296. return
  297. }
  298. if len(newData) > 0 {
  299. for _, t := range newData {
  300. for _, v := range t {
  301. if v.EdbInfoId > 0 && !edbInfoIdExist[v.EdbInfoId] {
  302. edbInfoIdExist[v.EdbInfoId] = true
  303. edbInfoIds = append(edbInfoIds, v.EdbInfoId)
  304. }
  305. }
  306. }
  307. }
  308. // 清除缓存
  309. key := utils.HZ_CHART_LIB_EXCEL_TABLE_DETAIL + ":" + excelDetail.UniqueCode
  310. if utils.Re == nil {
  311. _ = utils.Rc.Delete(key)
  312. }
  313. if len(edbInfoIds) > 0 {
  314. err, _ = data.EdbInfoRefreshAllFromBase(edbInfoIds, false)
  315. if err != nil {
  316. err = fmt.Errorf("刷新混合表格数据失败, Err: " + err.Error())
  317. return
  318. }
  319. }
  320. return
  321. }