excel_info.go 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. package controllers
  2. import (
  3. "encoding/json"
  4. "eta_gn/eta_chart_lib/models"
  5. excel3 "eta_gn/eta_chart_lib/models/data_manage/excel"
  6. "eta_gn/eta_chart_lib/models/request"
  7. "eta_gn/eta_chart_lib/models/response"
  8. "eta_gn/eta_chart_lib/services/data"
  9. excel2 "eta_gn/eta_chart_lib/services/data/excel"
  10. "eta_gn/eta_chart_lib/services/excel"
  11. "eta_gn/eta_chart_lib/utils"
  12. "fmt"
  13. "strings"
  14. "time"
  15. "github.com/shopspring/decimal"
  16. )
  17. type ExcelInfoController struct {
  18. BaseAuthController
  19. }
  20. // @router /detail [get]
  21. func (this *ExcelInfoController) GetTableDetail() {
  22. br := new(models.BaseResponse).Init()
  23. defer func() {
  24. this.Data["json"] = br
  25. this.ServeJSON()
  26. }()
  27. uniqueCode := this.GetString("UniqueCode")
  28. if uniqueCode == "" {
  29. br.Msg = "参数错误"
  30. br.ErrMsg = "参数错误,uniqueCode is empty"
  31. return
  32. }
  33. fromScene, _ := this.GetInt("FromScene", 0)
  34. key := utils.HZ_CHART_LIB_EXCEL_TABLE_DETAIL + ":" + uniqueCode
  35. resp := new(response.ExcelTableDetailResp)
  36. excelInfo, err := models.GetExcelInfoByUniqueCode(uniqueCode)
  37. if err != nil {
  38. if utils.IsErrNoRow(err) {
  39. br.Msg = "该表格已被删除,请刷新页面"
  40. br.ErrMsg = "该表格已被删除,请刷新页面,Err:" + err.Error()
  41. return
  42. }
  43. br.Msg = "获取失败"
  44. br.ErrMsg = "获取表格表信息失败,Err:" + err.Error()
  45. return
  46. }
  47. var tableData excel.TableData
  48. switch excelInfo.Source {
  49. case utils.EXCEL_DEFAULT:
  50. luckySheetData, err := excel.GetLuckySheetData(excelInfo.Content)
  51. if err != nil {
  52. br.Msg = "获取失败"
  53. br.ErrMsg = "获取excel数据失败,Err:" + err.Error()
  54. return
  55. }
  56. tableData, err = luckySheetData.GetTableDataByLuckySheetDataStr()
  57. if err != nil {
  58. br.Msg = "获取失败"
  59. br.ErrMsg = "转换成table失败,Err:" + err.Error()
  60. return
  61. }
  62. case utils.TIME_TABLE:
  63. var tableDataConfig excel2.TableDataConfig
  64. err = json.Unmarshal([]byte(excelInfo.Content), &tableDataConfig)
  65. if err != nil {
  66. br.Msg = "获取失败"
  67. br.ErrMsg = "表格json转结构体失败,Err:" + err.Error()
  68. return
  69. }
  70. result, err := excel2.GetDataByTableDataConfig(tableDataConfig)
  71. if err != nil {
  72. br.Msg = "获取失败"
  73. br.ErrMsg = "获取最新的表格数据失败,Err:" + err.Error()
  74. return
  75. }
  76. tableData, err = excel.GetTableDataByCustomData(excelInfo.ExcelType, result, this.Lang)
  77. if err != nil {
  78. br.Msg = "获取失败"
  79. br.ErrMsg = "转换成table失败,Err:" + err.Error()
  80. return
  81. }
  82. sourceNameList, sourceNameEnList, err := data.GetEdbSourceByEdbInfoIdListForExcel(result.EdbInfoIdList)
  83. if err != nil {
  84. br.Msg = "自定义表格数据获取失败"
  85. br.ErrMsg = "自定义表格数据获取失败,Err:" + err.Error()
  86. return
  87. }
  88. resp.ExcelSource = strings.Join(sourceNameList, ",")
  89. resp.ExcelSourceEn = strings.Join(sourceNameEnList, ",")
  90. case utils.MIXED_TABLE:
  91. var result request.MixedTableReq
  92. err = json.Unmarshal([]byte(excelInfo.Content), &result)
  93. if err != nil {
  94. br.Msg = "获取失败"
  95. br.ErrMsg = "表格json转结构体失败,Err:" + err.Error()
  96. return
  97. }
  98. newResult, tmpErr, tmpErrMsg := excel2.GetMixedTableCellData(result, this.Lang)
  99. if tmpErr != nil {
  100. br.Msg = "获取失败"
  101. if tmpErrMsg != `` {
  102. br.Msg = tmpErrMsg
  103. }
  104. br.ErrMsg = "获取最新的数据失败,Err:" + tmpErr.Error()
  105. return
  106. }
  107. tableData, err = excel.GetTableDataByMixedTableData(newResult, true)
  108. if err != nil {
  109. br.Msg = "获取失败"
  110. br.ErrMsg = "转换成table失败,Err:" + err.Error()
  111. return
  112. }
  113. edbInfoIds := make([]int, 0)
  114. edbInfoIdExist := make(map[int]bool)
  115. if len(newResult) > 0 {
  116. for _, t := range newResult {
  117. for _, v := range t {
  118. if v.EdbInfoId > 0 && !edbInfoIdExist[v.EdbInfoId] {
  119. edbInfoIdExist[v.EdbInfoId] = true
  120. edbInfoIds = append(edbInfoIds, v.EdbInfoId)
  121. }
  122. }
  123. }
  124. }
  125. sourceNameList, sourceNameEnList, err := data.GetEdbSourceByEdbInfoIdListForExcel(edbInfoIds)
  126. if err != nil {
  127. br.Msg = "自定义表格数据获取失败"
  128. br.ErrMsg = "自定义表格数据获取失败,Err:" + err.Error()
  129. return
  130. }
  131. resp.ExcelSource = strings.Join(sourceNameList, ",")
  132. resp.ExcelSourceEn = strings.Join(sourceNameEnList, ",")
  133. }
  134. tableData = excel.HandleTableCell(tableData)
  135. tableData, err = excel.HandleRuleToTableCell(excelInfo.ExcelInfoId, tableData)
  136. if err != nil {
  137. utils.FileLog.Info("表格管理规则处理失败,HandleRuleToTableCell err:", err.Error())
  138. }
  139. config := response.ExcelTableDetailConfigResp{
  140. FontSize: 9,
  141. }
  142. confName := models.FromSceneMap[fromScene]
  143. if confName != `` {
  144. busConf, err := models.GetBusinessConfByKey(confName)
  145. if err == nil {
  146. sizeDeci, err := decimal.NewFromString(busConf.ConfVal)
  147. if err == nil {
  148. config.FontSize = int(sizeDeci.IntPart())
  149. }
  150. }
  151. }
  152. resp.TableInfo = tableData
  153. resp.Source = excelInfo.Source
  154. resp.ExcelType = excelInfo.ExcelType
  155. resp.ExcelName = excelInfo.ExcelName
  156. resp.ExcelImage = excelInfo.ExcelImage
  157. resp.UniqueCode = excelInfo.UniqueCode
  158. resp.Config = config
  159. resp.SourcesFrom = excelInfo.SourcesFrom
  160. if utils.Re == nil {
  161. data, _ := json.Marshal(resp)
  162. utils.Rc.Put(key, data, 5*time.Minute)
  163. }
  164. br.Ret = 200
  165. br.Success = true
  166. br.Msg = "获取成功"
  167. br.Data = resp
  168. }
  169. func refreshBalanceTable(excelDetail *excel3.ExcelInfo, lang string) (err error) {
  170. edbInfoIds := make([]int, 0)
  171. edbInfoIdExist := make(map[int]bool)
  172. var result request.MixedTableReq
  173. err = json.Unmarshal([]byte(excelDetail.Content), &result)
  174. if err != nil {
  175. err = fmt.Errorf("表格json转结构体失败,Err:" + err.Error())
  176. return
  177. }
  178. newData, tmpErr, _ := excel2.GetMixedTableCellData(result, lang)
  179. if tmpErr != nil {
  180. err = tmpErr
  181. return
  182. }
  183. if len(newData) > 0 {
  184. for _, t := range newData {
  185. for _, v := range t {
  186. if v.EdbInfoId > 0 && !edbInfoIdExist[v.EdbInfoId] {
  187. edbInfoIdExist[v.EdbInfoId] = true
  188. edbInfoIds = append(edbInfoIds, v.EdbInfoId)
  189. }
  190. }
  191. }
  192. }
  193. key := utils.HZ_CHART_LIB_EXCEL_TABLE_DETAIL + ":" + excelDetail.UniqueCode
  194. if utils.Re == nil {
  195. _ = utils.Rc.Delete(key)
  196. }
  197. if len(edbInfoIds) > 0 {
  198. err, _ = data.EdbInfoRefreshAllFromBase(edbInfoIds, false)
  199. if err != nil {
  200. err = fmt.Errorf("刷新混合表格数据失败, Err: " + err.Error())
  201. return
  202. }
  203. }
  204. return
  205. }
  206. // @router /refresh [get]
  207. func (this *ExcelInfoController) Refresh() {
  208. br := new(models.BaseResponse).Init()
  209. defer func() {
  210. if br.ErrMsg == "" {
  211. br.IsSendEmail = false
  212. }
  213. this.Data["json"] = br
  214. this.ServeJSON()
  215. }()
  216. uniqueCode := this.GetString("UniqueCode")
  217. if uniqueCode == "" {
  218. br.Msg = "参数错误"
  219. br.ErrMsg = "参数错误, UniqueCode is empty"
  220. return
  221. }
  222. key := utils.HZ_CHART_LIB_EXCEL_TABLE_DETAIL_REFRESH + ":" + uniqueCode
  223. ok := utils.Rc.SetNX(key, 1, time.Second*300)
  224. if !ok {
  225. br.Ret = 200
  226. br.Success = true
  227. br.Msg = "表格刷新中"
  228. return
  229. }
  230. defer func() {
  231. _ = utils.Rc.Delete(key)
  232. }()
  233. excelDetail, errMsg, err := excel2.GetExcelDetailInfoByExcelInfoId(uniqueCode, this.Lang)
  234. if err != nil {
  235. if utils.IsErrNoRow(err) {
  236. br.Msg = "该表格已被删除,请刷新页面"
  237. br.ErrMsg = "该表格已被删除,请刷新页面,Err:" + err.Error()
  238. return
  239. }
  240. br.Msg = errMsg
  241. br.ErrMsg = "获取表格表信息失败,Err:" + err.Error()
  242. return
  243. }
  244. if excelDetail.Source == utils.TIME_TABLE {
  245. jsonByte, e := json.Marshal(excelDetail.TableData)
  246. if e != nil {
  247. br.Msg = "自定义表格数据获取失败"
  248. br.ErrMsg = "自定义表格数据获取失败,转json失败,Err:" + e.Error()
  249. return
  250. }
  251. var tableData request.TableDataReq
  252. if e = json.Unmarshal(jsonByte, &tableData); e != nil {
  253. br.Msg = "自定义表格数据获取失败"
  254. br.ErrMsg = "自定义表格数据获取失败,json转结构体失败,Err:" + e.Error()
  255. return
  256. }
  257. if len(tableData.EdbInfoIdList) > 0 {
  258. if e, _ = data.EdbInfoRefreshAllFromBase(tableData.EdbInfoIdList, false); e != nil {
  259. br.Msg = "刷新失败"
  260. br.ErrMsg = "刷新失败,Err:" + e.Error()
  261. return
  262. }
  263. }
  264. }
  265. if excelDetail.Source == utils.MIXED_TABLE {
  266. jsonByte, e := json.Marshal(excelDetail.TableData)
  267. if e != nil {
  268. br.Msg = "刷新失败"
  269. br.ErrMsg = "JSON格式化混合表格数据失败, Err: " + e.Error()
  270. return
  271. }
  272. var tableData request.MixedTableReq
  273. if e = json.Unmarshal(jsonByte, &tableData); e != nil {
  274. br.Msg = "刷新失败"
  275. br.ErrMsg = "解析混合表格数据失败, Err: " + e.Error()
  276. return
  277. }
  278. edbInfoIds := make([]int, 0)
  279. edbInfoIdExist := make(map[int]bool)
  280. if len(tableData.Data) > 0 {
  281. for _, t := range tableData.Data {
  282. for _, v := range t {
  283. if v.EdbInfoId > 0 && !edbInfoIdExist[v.EdbInfoId] {
  284. edbInfoIdExist[v.EdbInfoId] = true
  285. edbInfoIds = append(edbInfoIds, v.EdbInfoId)
  286. }
  287. }
  288. }
  289. }
  290. if len(edbInfoIds) > 0 {
  291. if e, _ = data.EdbInfoRefreshAllFromBase(edbInfoIds, false); e != nil {
  292. br.Msg = "刷新失败"
  293. br.ErrMsg = "刷新混合表格数据失败, Err: " + e.Error()
  294. return
  295. }
  296. }
  297. }
  298. detailKey := utils.HZ_CHART_LIB_EXCEL_TABLE_DETAIL + ":" + excelDetail.UniqueCode
  299. if utils.Re == nil {
  300. _ = utils.Rc.Delete(detailKey)
  301. }
  302. br.Ret = 200
  303. br.Success = true
  304. br.Msg = "刷新成功"
  305. }