123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408 |
- package excel
- import (
- "encoding/json"
- "errors"
- "eta/eta_mobile/models/data_manage"
- excelModel "eta/eta_mobile/models/data_manage/excel"
- "eta/eta_mobile/models/data_manage/excel/request"
- "eta/eta_mobile/utils"
- "fmt"
- "github.com/xuri/excelize/v2"
- "strconv"
- "time"
- )
- // 将表格信息转化成指标数据
- func GetBalanceExcelData(excelDetail *excelModel.ExcelInfo, lang string) (newDataMap map[int]map[int]request.MixedTableCellDataReq, allRows, allCols int, err error, errMsg string) {
- var result request.MixedTableReq
- err = json.Unmarshal([]byte(excelDetail.Content), &result)
- if err != nil {
- err = errors.New("表格json转结构体失败,Err:" + err.Error())
- return
- }
- newData, tmpErr, tmpErrMsg := GetMixedTableCellData(result, lang)
- if tmpErr != nil {
- errMsg = "获取失败"
- if tmpErrMsg != `` {
- errMsg = tmpErrMsg
- }
- err = errors.New("获取最新的数据失败,Err:" + tmpErr.Error())
- return
- }
- allRows = len(newData)
- allCols = 0
- newDataMap = make(map[int]map[int]request.MixedTableCellDataReq)
- for r, row := range newData {
- tmp := len(row)
- if tmp > allCols {
- allCols = tmp
- }
- colMap := make(map[int]request.MixedTableCellDataReq)
- for c, col := range row {
- colMap[c] = col
- }
- newDataMap[r] = colMap
- }
- return
- }
- // GetBalanceExcelEdbData 将表格信息转化成指标数据
- func GetBalanceExcelEdbData(excelEdbMappingItem *excelModel.ExcelChartEdb, newMixedTableCellDataListMap map[int]map[int]request.MixedTableCellDataReq, dataListMap map[int][]*data_manage.EdbDataList, allRows, allCols int) (err error, errMsg string) {
- var dateList, dataList []string
- // 日期序列
- {
- _, startColumnName, endColumnName, startNum, endNum, isAll, isRow, isColumn, tmpErr := GetSheetStr(excelEdbMappingItem.DateSequence)
- if tmpErr != nil {
- err = tmpErr
- return
- }
- startNum = startNum - 1
- endNum = endNum - 1
- // 选择行的数据
- if isRow {
- // 因为是选择一行的数据,所以开始行和结束行时一样的
- //endNum = startNum - 1
- // 开始列名、结束列
- var startColumn, endColumn int
- if isAll {
- // 结束列(其实也就是整列的个数)
- endColumn = allCols - 1
- } else {
- tmpStartColumn, tmpErr := excelize.ColumnNameToNumber(startColumnName)
- if tmpErr != nil {
- errMsg = "列名异常:" + startColumnName
- err = errors.New(errMsg)
- return
- }
- tmpEndColumn, tmpErr := excelize.ColumnNameToNumber(endColumnName)
- if tmpErr != nil {
- errMsg = "列名异常:" + endColumnName
- err = errors.New(errMsg)
- return
- }
- startColumn = tmpStartColumn - 1
- endColumn = tmpEndColumn - 1
- }
- // 最大列数,如果设置的超过了最大列数,那么结束列就是最大列数
- maxCol := allCols
- if endColumn > maxCol {
- endColumn = maxCol - 1
- }
- // 长度固定,避免一直申请内存空间
- //dateList = make([]string, endColumn-startColumn+1)
- dateList = make([]string, 0)
- i := 0
- for currColumn := startColumn; currColumn <= endColumn; currColumn++ {
- currCell, ok := newMixedTableCellDataListMap[startNum][currColumn]
- if !ok {
- errMsg = fmt.Sprintf("第%d列,第%d行数据异常", currColumn, startNum)
- err = errors.New(errMsg)
- return
- }
- dateList = append(dateList, currCell.ShowValue)
- //dateList[i] = currCell.ShowValue
- i++
- }
- } else if isColumn { // 选择列的数据
- if isAll {
- // 选择一整列的话,结束行得根据实际情况调整(其实也就是整个sheet有多少行)
- endNum = allRows - 1
- }
- startColumn, tmpErr := excelize.ColumnNameToNumber(startColumnName)
- if tmpErr != nil {
- errMsg = "列名异常:" + startColumnName
- err = errors.New(errMsg)
- return
- }
- startColumn = startColumn - 1
- // 最大行数,如果设置的超过了最大行数,那么结束行就是最大行数
- maxRow := allRows
- if endNum > maxRow {
- endNum = maxRow - 1
- }
- // 长度固定,避免一直申请内存空间
- //dateList = make([]string, endNum-startNum+1)
- fmt.Println("startNum:", startNum, "endNum:", endNum)
- dateList = make([]string, 0)
- i := 0
- for currRow := startNum; currRow <= endNum; currRow++ {
- currCell, ok := newMixedTableCellDataListMap[currRow][startColumn]
- if !ok {
- errMsg = fmt.Sprintf("第%d列,第%d行数据异常", currRow, startColumn)
- err = errors.New(errMsg)
- return
- }
- dateList = append(dateList, currCell.ShowValue)
- //dateList[i] = currCell.ShowValue
- i++
- }
- }
- }
- // 数据序列
- {
- _, startColumnName, endColumnName, startNum, endNum, isAll, isRow, isColumn, tmpErr := GetSheetStr(excelEdbMappingItem.DataSequence)
- if tmpErr != nil {
- err = tmpErr
- return
- }
- startNum = startNum - 1
- endNum = endNum - 1
- // 选择行的数据
- if isRow {
- // 开始列名、结束列
- var startColumn, endColumn int
- if isAll {
- // 结束列(其实也就是整列的个数)
- endColumn = allCols - 1
- } else {
- tmpStartColumn, tmpErr := excelize.ColumnNameToNumber(startColumnName)
- if tmpErr != nil {
- errMsg = "列名异常:" + startColumnName
- err = errors.New(errMsg)
- return
- }
- tmpEndColumn, tmpErr := excelize.ColumnNameToNumber(endColumnName)
- if tmpErr != nil {
- errMsg = "列名异常:" + endColumnName
- err = errors.New(errMsg)
- return
- }
- startColumn = tmpStartColumn - 1
- endColumn = tmpEndColumn - 1
- }
- // 最大列数,如果设置的超过了最大列数,那么结束列就是最大列数
- maxCol := allCols
- if endColumn > maxCol {
- endColumn = maxCol - 1
- }
- // 长度固定,避免一直申请内存空间
- //dataList = make([]string, endColumn-startColumn+1)
- dataList = make([]string, 0)
- i := 0
- for currColumn := startColumn; currColumn <= endColumn; currColumn++ {
- currCell, ok := newMixedTableCellDataListMap[startNum][currColumn]
- if !ok {
- errMsg = fmt.Sprintf("第%d列,第%d行数据异常", startColumn, currColumn)
- err = errors.New(errMsg)
- return
- }
- dataList = append(dataList, currCell.ShowValue)
- //dataList[i] = currCell.ShowValue
- i++
- }
- } else if isColumn { // 选择列的数据
- if isAll {
- // 选择一整列的话,结束行得根据实际情况调整(其实也就是整个sheet有多少行)
- endNum = allRows - 1
- }
- startColumn, tmpErr := excelize.ColumnNameToNumber(startColumnName)
- if tmpErr != nil {
- errMsg = "列名异常:" + startColumnName
- err = errors.New(errMsg)
- return
- }
- startColumn = startColumn - 1
- // 最大行数,如果设置的超过了最大行数,那么结束行就是最大行数
- maxRow := allRows
- if endNum > maxRow {
- endNum = maxRow - 1
- }
- // 长度固定,避免一直申请内存空间
- //dataList = make([]string, endNum-startNum+1)
- dataList = make([]string, 0)
- i := 0
- for currRow := startNum; currRow <= endNum; currRow++ {
- currCell, ok := newMixedTableCellDataListMap[currRow][startColumn]
- if !ok {
- errMsg = fmt.Sprintf("第%d列,第%d行数据异常", currRow, startColumn)
- err = errors.New(errMsg)
- return
- }
- dataList = append(dataList, currCell.ShowValue)
- //dataList[i] = currCell.ShowValue
- i++
- }
- }
- }
- //fmt.Println(dateList, dataList)
- //fmt.Println("日期序列结束")
- // 将excel中的日期、数据系列处理
- //newDateList, newDataList, err, errMsg := excel2.HandleEdbSequenceVal(dateList, dataList)
- newDateList, newDataList := dateList, dataList
- if err != nil {
- err = fmt.Errorf(" 处理日期和数据系列失败 %s", err.Error())
- return
- }
- newDataMap := make(map[int]float64, len(newDataList))
- for i, v := range newDataList {
- if v != "" {
- val, e := strconv.ParseFloat(v, 64)
- if e != nil {
- err = fmt.Errorf(" 处理日期和数据系列失败 %s", e.Error())
- return
- }
- newDataMap[i] = val
- }
- }
- //组装成excelEdbData
- list := make([]*data_manage.EdbDataList, 0)
- for i, v := range newDateList {
- val, ok := newDataMap[i]
- if !ok {
- continue
- }
- // todo 处理DataTimestamp
- dataTime, e := time.ParseInLocation(utils.FormatDate, v, time.Local)
- if e != nil {
- err = errors.New("time.Parse Err:" + e.Error())
- return
- }
- timestamp := dataTime.UnixNano() / 1e6
- tmp := &data_manage.EdbDataList{
- EdbDataId: i,
- EdbInfoId: excelEdbMappingItem.ExcelChartEdbId,
- DataTime: v,
- DataTimestamp: timestamp,
- Value: val,
- }
- list = append(list, tmp)
- }
- dataListMap[excelEdbMappingItem.ExcelChartEdbId] = list
- return
- }
- // 根据chartInfoId获取单个图表信息
- func GetBalanceExcelChartSingle(chartInfoId, ChartEdbId int, lang string) (mappingListTmp []*excelModel.ExcelChartEdb, dataListMap map[int][]*data_manage.EdbDataList, err error, errMsg string) {
- // 相关联指标
- if chartInfoId == 0 && ChartEdbId == 0 {
- err = fmt.Errorf(" 获取图表,指标信息失败 Err:chartInfoId和ChartEdbId不能同时为空")
- return
- }
- if chartInfoId == 0 {
- chartEdb, e := excelModel.GetExcelChartEdbById(ChartEdbId)
- if e != nil {
- err = fmt.Errorf(" 获取图表,指标信息失败 Err:%s", e.Error())
- return
- }
- chartInfoId = chartEdb.ChartInfoId
- }
- mappingListTmp, err = excelModel.GetExcelChartEdbMappingByChartInfoId(chartInfoId)
- if err != nil {
- errMsg = "获取失败"
- err = fmt.Errorf(" 获取图表,指标信息失败 Err:%s", err.Error())
- return
- }
- if len(mappingListTmp) <= 0 {
- errMsg = "获取失败"
- err = fmt.Errorf(" 获取图表,指标信息失败 Err:%s", err.Error())
- return
- }
- // 查询所有子表
- excelInfoId := mappingListTmp[0].ExcelInfoId
- excelInfo, err := excelModel.GetExcelInfoById(excelInfoId)
- if err != nil {
- if err.Error() == utils.ErrNoRow() {
- errMsg = "表格不存在"
- err = fmt.Errorf(errMsg)
- return
- }
- errMsg = "查询子表失败"
- err = fmt.Errorf(" 查询子表失败图表,指标信息失败 Err:%s", err.Error())
- return
- }
- dataListMap = make(map[int][]*data_manage.EdbDataList)
- if excelInfo.BalanceType == 1 {
- //查询库里是否有值
- chartDataList, e := excelModel.GetExcelChartDataByChartInfoId(chartInfoId)
- if e != nil {
- err = fmt.Errorf(" 获取图表,指标信息失败 Err:%s", e.Error())
- return
- }
- if len(chartDataList) == 0 {
- err = fmt.Errorf(" 获取图表,指标数据不存在")
- return
- }
- for _, v := range chartDataList {
- tmp := &data_manage.EdbDataList{
- EdbDataId: v.ExcelChartDataId,
- EdbInfoId: v.ExcelChartEdbId,
- DataTime: v.DataTime,
- DataTimestamp: v.DataTimestamp,
- Value: v.Value,
- }
- dataListMap[v.ExcelChartEdbId] = append(dataListMap[v.ExcelChartEdbId], tmp)
- }
- return
- }
- // 获取图表详情
- newExcelDataMap, excelAllRows, excelAllCols, err, errMsg := GetBalanceExcelData(excelInfo, lang)
- if err != nil {
- return
- }
- for _, mapping := range mappingListTmp {
- er, msg := GetBalanceExcelEdbData(mapping, newExcelDataMap, dataListMap, excelAllRows, excelAllCols)
- if er != nil {
- utils.FileLog.Info(fmt.Sprintf(" 获取图表,指标信息失败 Err:%s, %s", er.Error(), msg))
- continue
- }
- }
- return
- }
- // GetBalanceExcelInfoOpButton 获取ETA平衡表格的操作权限
- func GetBalanceExcelInfoOpButton(sysUserId, parentSysUserId int, haveOperaAuth bool, parentExcelInfoId int) (button excelModel.ExcelInfoDetailButton) {
- // 如果没有数据权限,那么直接返回
- if !haveOperaAuth {
- return
- }
- //非管理员角色查看其他用户创建的表格,可刷新、另存为、下载表格;
- button.RefreshButton = true
- button.CopyButton = true
- button.DownloadButton = true
- if sysUserId == parentSysUserId {
- button.OpButton = true
- button.RefreshEdbButton = true
- button.OpWorkerButton = true
- button.DeleteButton = true
- } else {
- obj := new(excelModel.ExcelWorker)
- workerList, err := obj.GetByExcelInfoId(parentExcelInfoId)
- if err == nil {
- for _, v := range workerList {
- if v.SysUserId == sysUserId {
- button.OpButton = true
- button.RefreshEdbButton = true
- button.DeleteButton = true
- break
- }
- }
- }
- }
- return
- }
|