package controllers import ( "encoding/json" "eta/eta_chart_lib/models" excel3 "eta/eta_chart_lib/models/data_manage/excel" "eta/eta_chart_lib/models/request" "eta/eta_chart_lib/models/response" "eta/eta_chart_lib/services/data" excel2 "eta/eta_chart_lib/services/data/excel" "eta/eta_chart_lib/services/excel" "eta/eta_chart_lib/utils" "fmt" "strings" "time" "github.com/shopspring/decimal" ) // ExcelInfoController excel表格 type ExcelInfoController struct { BaseAuthController } // GetTableDetail // @Title 获取表格表详情 // @Description 获取表格表详情接口 // @Param UniqueCode query string true "表格表唯一编码,如果是管理后台访问,传固定字符串:7c69b590249049942070ae9dcd5bf6dc" // @Param FromScene query int true "场景来源,1:智能研报,2:研报列表;3:英文研报;4:中文PPT;5:英文PPT" // @Success 200 {object} response.ExcelTableDetailResp // @router /detail [get] func (this *ExcelInfoController) GetTableDetail() { br := new(models.BaseResponse).Init() defer func() { this.Data["json"] = br this.ServeJSON() }() uniqueCode := this.GetString("UniqueCode") if uniqueCode == "" { br.Msg = "参数错误" br.ErrMsg = "参数错误,uniqueCode is empty" return } fromScene, _ := this.GetInt("FromScene", 0) key := utils.HZ_CHART_LIB_EXCEL_TABLE_DETAIL + ":" + uniqueCode resp := new(response.ExcelTableDetailResp) switch fromScene { case 1, 2: fromScene = 2 } //判断是否有缓存 //if utils.Re == nil { // if utils.Re == nil && utils.Rc.IsExist(key) { // if data, err1 := utils.Rc.RedisBytes(key); err1 == nil { // err := json.Unmarshal(data, &resp) // if err == nil && resp != nil { // // 配置项 // config := response.ExcelTableDetailConfigResp{ // FontSize: 9, // } // // 获取配置的字体大小 // confName := models.FromSceneMap[fromScene] // if confName != `` { // busConf, err := models.GetBusinessConfByKey(confName) // if err == nil { // sizeDeci, err := decimal.NewFromString(busConf.ConfVal) // if err == nil { // config.FontSize = int(sizeDeci.IntPart()) // } // } // } // resp.Config = config // // br.Ret = 200 // br.Success = true // br.Msg = "获取成功" // br.Data = resp // fmt.Println("source redis") // return // } // } // } //} excelInfo, err := models.GetExcelInfoByUniqueCode(uniqueCode) if err != nil { if err.Error() == utils.ErrNoRow() { br.Msg = "该表格已被删除,请刷新页面" br.ErrMsg = "该表格已被删除,请刷新页面,Err:" + err.Error() return } br.Msg = "获取失败" br.ErrMsg = "获取表格表信息失败,Err:" + err.Error() return } var tableData excel.TableData switch excelInfo.Source { case utils.EXCEL_DEFAULT: luckySheetData, err := excel.GetLuckySheetData(excelInfo.Content) if err != nil { br.Msg = "获取失败" br.ErrMsg = "获取excel数据失败,Err:" + err.Error() return } tableData, err = luckySheetData.GetTableDataByLuckySheetDataStr() if err != nil { br.Msg = "获取失败" br.ErrMsg = "转换成table失败,Err:" + err.Error() return } case utils.TIME_TABLE: var tableDataConfig excel2.TableDataConfig err = json.Unmarshal([]byte(excelInfo.Content), &tableDataConfig) if err != nil { br.Msg = "获取失败" br.ErrMsg = "表格json转结构体失败,Err:" + err.Error() return } result, err := excel2.GetDataByTableDataConfig(tableDataConfig) if err != nil { br.Msg = "获取失败" br.ErrMsg = "获取最新的表格数据失败,Err:" + err.Error() return } tableData, err = excel.GetTableDataByCustomData(excelInfo.ExcelType, result, this.Lang) if err != nil { br.Msg = "获取失败" br.ErrMsg = "转换成table失败,Err:" + err.Error() return } sourceNameList, sourceNameEnList, err := data.GetEdbSourceByEdbInfoIdListForExcel(result.EdbInfoIdList) if err != nil { br.Msg = "自定义表格数据获取失败" br.ErrMsg = "自定义表格数据获取失败,Err:" + err.Error() return } resp.ExcelSource = strings.Join(sourceNameList, ",") resp.ExcelSourceEn = strings.Join(sourceNameEnList, ",") case utils.MIXED_TABLE: var result request.MixedTableReq err = json.Unmarshal([]byte(excelInfo.Content), &result) if err != nil { br.Msg = "获取失败" br.ErrMsg = "表格json转结构体失败,Err:" + err.Error() return } newResult, tmpErr, tmpErrMsg := excel2.GetMixedTableCellData(result, this.Lang) if tmpErr != nil { br.Msg = "获取失败" if tmpErrMsg != `` { br.Msg = tmpErrMsg } br.ErrMsg = "获取最新的数据失败,Err:" + tmpErr.Error() return } tableData, err = excel.GetTableDataByMixedTableData(newResult, true, excelInfo.ExcelInfoId) if err != nil { br.Msg = "获取失败" br.ErrMsg = "转换成table失败,Err:" + err.Error() return } edbInfoIds := make([]int, 0) edbInfoIdExist := make(map[int]bool) if len(newResult) > 0 { for _, t := range newResult { for _, v := range t { if v.EdbInfoId > 0 && !edbInfoIdExist[v.EdbInfoId] { edbInfoIdExist[v.EdbInfoId] = true edbInfoIds = append(edbInfoIds, v.EdbInfoId) } } } } sourceNameList, sourceNameEnList, err := data.GetEdbSourceByEdbInfoIdListForExcel(edbInfoIds) if err != nil { br.Msg = "自定义表格数据获取失败" br.ErrMsg = "自定义表格数据获取失败,Err:" + err.Error() return } resp.ExcelSource = strings.Join(sourceNameList, ",") resp.ExcelSourceEn = strings.Join(sourceNameEnList, ",") case utils.BALANCE_TABLE: var result request.MixedTableReq err = json.Unmarshal([]byte(excelInfo.Content), &result) if err != nil { br.Msg = "获取失败" br.ErrMsg = "表格json转结构体失败,Err:" + err.Error() return } newResult, tmpErr, tmpErrMsg := excel2.GetMixedTableCellData(result, this.Lang) if tmpErr != nil { br.Msg = "获取失败" if tmpErrMsg != `` { br.Msg = tmpErrMsg } br.ErrMsg = "获取最新的数据失败,Err:" + tmpErr.Error() return } tableData, err = excel.GetTableDataByMixedTableData(newResult, true, excelInfo.ExcelInfoId) if err != nil { br.Msg = "获取失败" br.ErrMsg = "转换成table失败,Err:" + err.Error() return } edbInfoIds := make([]int, 0) edbInfoIdExist := make(map[int]bool) if len(newResult) > 0 { for _, t := range newResult { for _, v := range t { if v.EdbInfoId > 0 && !edbInfoIdExist[v.EdbInfoId] { edbInfoIdExist[v.EdbInfoId] = true edbInfoIds = append(edbInfoIds, v.EdbInfoId) } } } } sourceNameList, sourceNameEnList, err := data.GetEdbSourceByEdbInfoIdListForExcel(edbInfoIds) if err != nil { br.Msg = "自定义表格数据获取失败" br.ErrMsg = "自定义表格数据获取失败,Err:" + err.Error() return } resp.ExcelSource = strings.Join(sourceNameList, ",") resp.ExcelSourceEn = strings.Join(sourceNameEnList, ",") } tableData = excel.HandleTableCell(tableData) // tableData, err = excel.HandleRuleToTableCell(excelInfo.ExcelInfoId, tableData) // if err != nil { // utils.FileLog.Info("表格管理规则处理失败,HandleRuleToTableCell err:", err.Error()) // } config := response.ExcelTableDetailConfigResp{ FontSize: 9, } waterMarkConf, err := models.GetBusinessConfByKeys([]string{"WatermarkTable", "CompanyWatermark"}) if err != nil { br.Msg = "获取水印失败" br.ErrMsg = "获取水印失败,Err:" + err.Error() return } if waterMarkConf["WatermarkTable"] == "true" && waterMarkConf["CompanyWatermark"] != "" { config.Watermark = waterMarkConf["CompanyWatermark"] } // 获取配置的字体大小 confName := models.FromSceneMap[fromScene] if confName != `` { busConf, err := models.GetBusinessConfByKey(confName) if err == nil { sizeDeci, err := decimal.NewFromString(busConf.ConfVal) if err == nil { config.FontSize = int(sizeDeci.IntPart()) } } } resp.TableInfo = tableData resp.Source = excelInfo.Source resp.ExcelType = excelInfo.ExcelType resp.ExcelName = excelInfo.ExcelName resp.ExcelImage = excelInfo.ExcelImage resp.UniqueCode = excelInfo.UniqueCode resp.Config = config resp.SourcesFrom = excelInfo.SourcesFrom if utils.Re == nil { data, _ := json.Marshal(resp) utils.Rc.Put(key, data, 5*time.Minute) } br.Ret = 200 br.Success = true br.Msg = "获取成功" br.Data = resp } // Refresh // @Title 刷新 // @Description 刷新接口 // @Param UniqueCode query string true "表格id" // @router /refresh [get] func (this *ExcelInfoController) Refresh() { br := new(models.BaseResponse).Init() defer func() { if br.ErrMsg == "" { br.IsSendEmail = false } this.Data["json"] = br this.ServeJSON() }() uniqueCode := this.GetString("UniqueCode") if uniqueCode == "" { br.Msg = "参数错误" br.ErrMsg = "参数错误, UniqueCode is empty" return } key := utils.HZ_CHART_LIB_EXCEL_TABLE_DETAIL_REFRESH + ":" + uniqueCode ok := utils.Rc.SetNX(key, 1, time.Second*300) if !ok { br.Ret = 200 br.Success = true br.Msg = "表格刷新中" return } defer func() { _ = utils.Rc.Delete(key) }() // 获取数据详情 excelDetail, errMsg, err := excel2.GetExcelDetailInfoByExcelInfoId(uniqueCode, this.Lang) if err != nil { if err.Error() == utils.ErrNoRow() { br.Msg = "该表格已被删除,请刷新页面" br.ErrMsg = "该表格已被删除,请刷新页面,Err:" + err.Error() return } br.Msg = errMsg br.ErrMsg = "获取表格表信息失败,Err:" + err.Error() return } // 数据刷新(只有自定义表格有刷新) if excelDetail.Source == utils.TIME_TABLE { jsonByte, e := json.Marshal(excelDetail.TableData) if e != nil { br.Msg = "自定义表格数据获取失败" br.ErrMsg = "自定义表格数据获取失败,转json失败,Err:" + e.Error() return } var tableData request.TableDataReq if e = json.Unmarshal(jsonByte, &tableData); e != nil { br.Msg = "自定义表格数据获取失败" br.ErrMsg = "自定义表格数据获取失败,json转结构体失败,Err:" + e.Error() return } if len(tableData.EdbInfoIdList) > 0 { if e, _ = data.EdbInfoRefreshAllFromBase(tableData.EdbInfoIdList, false); e != nil { br.Msg = "刷新失败" br.ErrMsg = "刷新失败,Err:" + e.Error() return } } } // 数据刷新-混合表格 if excelDetail.Source == utils.MIXED_TABLE { jsonByte, e := json.Marshal(excelDetail.TableData) if e != nil { br.Msg = "刷新失败" br.ErrMsg = "JSON格式化混合表格数据失败, Err: " + e.Error() return } var tableData request.MixedTableReq if e = json.Unmarshal(jsonByte, &tableData); e != nil { br.Msg = "刷新失败" br.ErrMsg = "解析混合表格数据失败, Err: " + e.Error() return } edbInfoIds := make([]int, 0) edbInfoIdExist := make(map[int]bool) if len(tableData.Data) > 0 { for _, t := range tableData.Data { for _, v := range t { if v.EdbInfoId > 0 && !edbInfoIdExist[v.EdbInfoId] { edbInfoIdExist[v.EdbInfoId] = true edbInfoIds = append(edbInfoIds, v.EdbInfoId) } } } } if len(edbInfoIds) > 0 { if e, _ = data.EdbInfoRefreshAllFromBase(edbInfoIds, false); e != nil { br.Msg = "刷新失败" br.ErrMsg = "刷新混合表格数据失败, Err: " + e.Error() return } } } // 清除缓存 detailKey := utils.HZ_CHART_LIB_EXCEL_TABLE_DETAIL + ":" + excelDetail.UniqueCode if utils.Re == nil { _ = utils.Rc.Delete(detailKey) } br.Ret = 200 br.Success = true br.Msg = "刷新成功" } func refreshBalanceTable(excelDetail *excel3.ExcelInfo, lang string) (err error) { edbInfoIds := make([]int, 0) edbInfoIdExist := make(map[int]bool) var result request.MixedTableReq err = json.Unmarshal([]byte(excelDetail.Content), &result) if err != nil { err = fmt.Errorf("表格json转结构体失败,Err:" + err.Error()) return } newData, tmpErr, _ := excel2.GetMixedTableCellData(result, lang) if tmpErr != nil { err = tmpErr return } if len(newData) > 0 { for _, t := range newData { for _, v := range t { if v.EdbInfoId > 0 && !edbInfoIdExist[v.EdbInfoId] { edbInfoIdExist[v.EdbInfoId] = true edbInfoIds = append(edbInfoIds, v.EdbInfoId) } } } } // 清除缓存 key := utils.HZ_CHART_LIB_EXCEL_TABLE_DETAIL + ":" + excelDetail.UniqueCode if utils.Re == nil { _ = utils.Rc.Delete(key) } if len(edbInfoIds) > 0 { err, _ = data.EdbInfoRefreshAllFromBase(edbInfoIds, false) if err != nil { err = fmt.Errorf("刷新混合表格数据失败, Err: " + err.Error()) return } } return }