123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378 |
- package controllers
- import (
- "encoding/json"
- "eta_gn/eta_chart_lib/models"
- excel3 "eta_gn/eta_chart_lib/models/data_manage/excel"
- "eta_gn/eta_chart_lib/models/request"
- "eta_gn/eta_chart_lib/models/response"
- "eta_gn/eta_chart_lib/services/data"
- excel2 "eta_gn/eta_chart_lib/services/data/excel"
- "eta_gn/eta_chart_lib/services/excel"
- "eta_gn/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)
- //判断是否有缓存
- //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 utils.IsErrNoRow(err) {
- 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)
- 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,
- }
- // 获取配置的字体大小
- 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
- }
- 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
- }
- // 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 utils.IsErrNoRow(err) {
- 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 = "刷新成功"
- }
|