balance_table.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  1. package excel
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "eta/eta_chart_lib/models"
  6. "eta/eta_chart_lib/models/data_manage/excel"
  7. "eta/eta_chart_lib/models/request"
  8. "eta/eta_chart_lib/utils"
  9. "fmt"
  10. "github.com/shopspring/decimal"
  11. "github.com/xuri/excelize/v2"
  12. "strconv"
  13. "strings"
  14. "time"
  15. )
  16. // 将表格信息转化成指标数据
  17. func GetBalanceExcelData(excelDetail *excel.ExcelInfo, lang string) (newDataMap map[int]map[int]request.MixedTableCellDataReq, allRows, allCols int, err error, errMsg string) {
  18. var result request.MixedTableReq
  19. err = json.Unmarshal([]byte(excelDetail.Content), &result)
  20. if err != nil {
  21. err = errors.New("表格json转结构体失败,Err:" + err.Error())
  22. return
  23. }
  24. newData, tmpErr, tmpErrMsg := GetMixedTableCellData(result, lang)
  25. if tmpErr != nil {
  26. errMsg = "获取失败"
  27. if tmpErrMsg != `` {
  28. errMsg = tmpErrMsg
  29. }
  30. err = errors.New("获取最新的数据失败,Err:" + tmpErr.Error())
  31. return
  32. }
  33. allRows = len(newData)
  34. allCols = 0
  35. newDataMap = make(map[int]map[int]request.MixedTableCellDataReq)
  36. for r, row := range newData {
  37. tmp := len(row)
  38. if tmp > allCols {
  39. allCols = tmp
  40. }
  41. colMap := make(map[int]request.MixedTableCellDataReq)
  42. for c, col := range row {
  43. colMap[c] = col
  44. }
  45. newDataMap[r] = colMap
  46. }
  47. return
  48. }
  49. // 根据chartInfoId获取单个图表信息
  50. func GetBalanceExcelChartSingle(chartInfoId, ChartEdbId int, lang string) (mappingListTmp []*excel.ExcelChartEdb, dataListMap map[int][]*models.EdbDataList, err error, errMsg string) {
  51. // 相关联指标
  52. if chartInfoId == 0 && ChartEdbId == 0 {
  53. err = fmt.Errorf(" 获取图表,指标信息失败 Err:chartInfoId和ChartEdbId不能同时为空")
  54. return
  55. }
  56. if chartInfoId == 0 {
  57. chartEdb, e := excel.GetExcelChartEdbById(ChartEdbId)
  58. if e != nil {
  59. err = fmt.Errorf(" 获取图表,指标信息失败 Err:%s", e.Error())
  60. return
  61. }
  62. chartInfoId = chartEdb.ChartInfoId
  63. }
  64. mappingListTmp, err = excel.GetExcelChartEdbMappingByChartInfoId(chartInfoId)
  65. if err != nil {
  66. errMsg = "获取失败"
  67. err = fmt.Errorf(" 获取图表,指标信息失败 Err:%s", err.Error())
  68. return
  69. }
  70. if len(mappingListTmp) <= 0 {
  71. errMsg = "获取失败"
  72. err = fmt.Errorf(" 获取图表,指标信息失败 Err:%s", err.Error())
  73. return
  74. }
  75. // 查询所有子表
  76. excelInfoId := mappingListTmp[0].ExcelInfoId
  77. excelInfo, err := excel.GetExcelInfoById(excelInfoId)
  78. if err != nil {
  79. if err.Error() == utils.ErrNoRow() {
  80. errMsg = "表格不存在"
  81. err = fmt.Errorf(errMsg)
  82. return
  83. }
  84. errMsg = "查询子表失败"
  85. err = fmt.Errorf(" 查询子表失败图表,指标信息失败 Err:%s", err.Error())
  86. return
  87. }
  88. dataListMap = make(map[int][]*models.EdbDataList)
  89. if excelInfo.BalanceType == 1 {
  90. //查询库里是否有值
  91. chartDataList, e := excel.GetExcelChartDataByChartInfoId(chartInfoId)
  92. if e != nil {
  93. err = fmt.Errorf(" 获取图表,指标信息失败 Err:%s", e.Error())
  94. return
  95. }
  96. if len(chartDataList) == 0 {
  97. err = fmt.Errorf(" 获取图表,指标数据不存在")
  98. return
  99. }
  100. for _, v := range chartDataList {
  101. tmp := &models.EdbDataList{
  102. EdbDataId: v.ExcelChartDataId,
  103. EdbInfoId: v.ExcelChartEdbId,
  104. DataTime: v.DataTime,
  105. DataTimestamp: v.DataTimestamp,
  106. Value: v.Value,
  107. }
  108. dataListMap[v.ExcelChartEdbId] = append(dataListMap[v.ExcelChartEdbId], tmp)
  109. }
  110. return
  111. }
  112. // 获取图表详情
  113. newExcelDataMap, excelAllRows, excelAllCols, err, errMsg := GetBalanceExcelData(excelInfo, lang)
  114. if err != nil {
  115. return
  116. }
  117. for _, mapping := range mappingListTmp {
  118. er, msg := GetBalanceExcelEdbData(mapping, newExcelDataMap, dataListMap, excelAllRows, excelAllCols)
  119. if er != nil {
  120. utils.FileLog.Info(fmt.Sprintf(" 获取图表,指标信息失败 Err:%s, %s", er.Error(), msg))
  121. continue
  122. }
  123. }
  124. return
  125. }
  126. // 将表格信息转化成指标数据
  127. func GetBalanceExcelEdbData(excelEdbMappingItem *excel.ExcelChartEdb, newMixedTableCellDataListMap map[int]map[int]request.MixedTableCellDataReq, dataListMap map[int][]*models.EdbDataList, allRows, allCols int) (err error, errMsg string) {
  128. var dateList, dataList []string
  129. // 日期序列
  130. {
  131. _, startColumnName, endColumnName, startNum, endNum, isAll, isRow, isColumn, tmpErr := GetSheetStr(excelEdbMappingItem.DateSequence)
  132. if tmpErr != nil {
  133. err = tmpErr
  134. return
  135. }
  136. startNum = startNum - 1
  137. endNum = endNum - 1
  138. // 选择行的数据
  139. if isRow {
  140. // 因为是选择一行的数据,所以开始行和结束行时一样的
  141. //endNum = startNum - 1
  142. // 开始列名、结束列
  143. var startColumn, endColumn int
  144. if isAll {
  145. // 结束列(其实也就是整列的个数)
  146. endColumn = allCols - 1
  147. } else {
  148. tmpStartColumn, tmpErr := excelize.ColumnNameToNumber(startColumnName)
  149. if tmpErr != nil {
  150. errMsg = "列名异常:" + startColumnName
  151. err = errors.New(errMsg)
  152. return
  153. }
  154. tmpEndColumn, tmpErr := excelize.ColumnNameToNumber(endColumnName)
  155. if tmpErr != nil {
  156. errMsg = "列名异常:" + endColumnName
  157. err = errors.New(errMsg)
  158. return
  159. }
  160. startColumn = tmpStartColumn - 1
  161. endColumn = tmpEndColumn - 1
  162. }
  163. // 最大列数,如果设置的超过了最大列数,那么结束列就是最大列数
  164. maxCol := allCols
  165. if endColumn > maxCol {
  166. endColumn = maxCol - 1
  167. }
  168. // 长度固定,避免一直申请内存空间
  169. //dateList = make([]string, endColumn-startColumn+1)
  170. dateList = make([]string, 0)
  171. i := 0
  172. for currColumn := startColumn; currColumn <= endColumn; currColumn++ {
  173. currCell, ok := newMixedTableCellDataListMap[startNum][currColumn]
  174. if !ok {
  175. errMsg = fmt.Sprintf("第%d列,第%d行数据异常", currColumn, startNum)
  176. err = errors.New(errMsg)
  177. return
  178. }
  179. dateList = append(dateList, currCell.ShowValue)
  180. //dateList[i] = currCell.ShowValue
  181. i++
  182. }
  183. } else if isColumn { // 选择列的数据
  184. if isAll {
  185. // 选择一整列的话,结束行得根据实际情况调整(其实也就是整个sheet有多少行)
  186. endNum = allRows - 1
  187. }
  188. startColumn, tmpErr := excelize.ColumnNameToNumber(startColumnName)
  189. if tmpErr != nil {
  190. errMsg = "列名异常:" + startColumnName
  191. err = errors.New(errMsg)
  192. return
  193. }
  194. startColumn = startColumn - 1
  195. // 最大行数,如果设置的超过了最大行数,那么结束行就是最大行数
  196. maxRow := allRows
  197. if endNum > maxRow {
  198. endNum = maxRow - 1
  199. }
  200. // 长度固定,避免一直申请内存空间
  201. //dateList = make([]string, endNum-startNum+1)
  202. dateList = make([]string, 0)
  203. i := 0
  204. for currRow := startNum; currRow <= endNum; currRow++ {
  205. currCell, ok := newMixedTableCellDataListMap[currRow][startColumn]
  206. if !ok {
  207. errMsg = fmt.Sprintf("第%d列,第%d行数据异常", currRow, startColumn)
  208. err = errors.New(errMsg)
  209. return
  210. }
  211. dateList = append(dateList, currCell.ShowValue)
  212. //dateList[i] = currCell.ShowValue
  213. i++
  214. }
  215. }
  216. }
  217. // 数据序列
  218. {
  219. _, startColumnName, endColumnName, startNum, endNum, isAll, isRow, isColumn, tmpErr := GetSheetStr(excelEdbMappingItem.DataSequence)
  220. if tmpErr != nil {
  221. err = tmpErr
  222. return
  223. }
  224. startNum = startNum - 1
  225. endNum = endNum - 1
  226. // 选择行的数据
  227. if isRow {
  228. // 开始列名、结束列
  229. var startColumn, endColumn int
  230. if isAll {
  231. // 结束列(其实也就是整列的个数)
  232. endColumn = allCols - 1
  233. } else {
  234. tmpStartColumn, tmpErr := excelize.ColumnNameToNumber(startColumnName)
  235. if tmpErr != nil {
  236. errMsg = "列名异常:" + startColumnName
  237. err = errors.New(errMsg)
  238. return
  239. }
  240. tmpEndColumn, tmpErr := excelize.ColumnNameToNumber(endColumnName)
  241. if tmpErr != nil {
  242. errMsg = "列名异常:" + endColumnName
  243. err = errors.New(errMsg)
  244. return
  245. }
  246. startColumn = tmpStartColumn - 1
  247. endColumn = tmpEndColumn - 1
  248. }
  249. // 最大列数,如果设置的超过了最大列数,那么结束列就是最大列数
  250. maxCol := allCols
  251. if endColumn > maxCol {
  252. endColumn = maxCol - 1
  253. }
  254. // 长度固定,避免一直申请内存空间
  255. //dataList = make([]string, endColumn-startColumn+1)
  256. dataList = make([]string, 0)
  257. i := 0
  258. for currColumn := startColumn; currColumn <= endColumn; currColumn++ {
  259. currCell, ok := newMixedTableCellDataListMap[startNum][currColumn]
  260. if !ok {
  261. errMsg = fmt.Sprintf("第%d列,第%d行数据异常", startColumn, currColumn)
  262. err = errors.New(errMsg)
  263. return
  264. }
  265. dataList = append(dataList, currCell.ShowValue)
  266. //dataList[i] = currCell.ShowValue
  267. i++
  268. }
  269. } else if isColumn { // 选择列的数据
  270. if isAll {
  271. // 选择一整列的话,结束行得根据实际情况调整(其实也就是整个sheet有多少行)
  272. endNum = allRows - 1
  273. }
  274. startColumn, tmpErr := excelize.ColumnNameToNumber(startColumnName)
  275. if tmpErr != nil {
  276. errMsg = "列名异常:" + startColumnName
  277. err = errors.New(errMsg)
  278. return
  279. }
  280. startColumn = startColumn - 1
  281. // 最大行数,如果设置的超过了最大行数,那么结束行就是最大行数
  282. maxRow := allRows
  283. if endNum > maxRow {
  284. endNum = maxRow - 1
  285. }
  286. // 长度固定,避免一直申请内存空间
  287. //dataList = make([]string, endNum-startNum+1)
  288. dataList = make([]string, 0)
  289. i := 0
  290. for currRow := startNum; currRow <= endNum; currRow++ {
  291. currCell, ok := newMixedTableCellDataListMap[currRow][startColumn]
  292. if !ok {
  293. errMsg = fmt.Sprintf("第%d列,第%d行数据异常", currRow, startColumn)
  294. err = errors.New(errMsg)
  295. return
  296. }
  297. dataList = append(dataList, currCell.ShowValue)
  298. //dataList[i] = currCell.ShowValue
  299. i++
  300. }
  301. }
  302. }
  303. //fmt.Println(dateList, dataList)
  304. //fmt.Println("日期序列结束")
  305. // 将excel中的日期、数据系列处理
  306. //newDateList, newDataList, err, errMsg := excel2.HandleEdbSequenceVal(dateList, dataList)
  307. newDateList, newDataList := dateList, dataList
  308. newDataMap := make(map[int]float64, len(newDataList))
  309. for i, v := range newDataList {
  310. if v != "" {
  311. val, e := strconv.ParseFloat(v, 64)
  312. if e != nil {
  313. err = fmt.Errorf(" 处理日期和数据系列失败 %s", e.Error())
  314. return
  315. }
  316. newDataMap[i] = val
  317. }
  318. }
  319. //组装成excelEdbData
  320. list := make([]*models.EdbDataList, 0)
  321. for i, v := range newDateList {
  322. val, ok := newDataMap[i]
  323. if !ok {
  324. continue
  325. }
  326. // todo 处理DataTimestamp
  327. dataTime, e := time.ParseInLocation(utils.FormatDate, v, time.Local)
  328. if e != nil {
  329. err = errors.New("time.Parse Err:" + err.Error())
  330. return
  331. }
  332. timestamp := dataTime.UnixNano() / 1e6
  333. tmp := &models.EdbDataList{
  334. EdbDataId: i,
  335. EdbInfoId: excelEdbMappingItem.ExcelChartEdbId,
  336. DataTime: v,
  337. DataTimestamp: timestamp,
  338. Value: val,
  339. }
  340. list = append(list, tmp)
  341. }
  342. dataListMap[excelEdbMappingItem.ExcelChartEdbId] = list
  343. return
  344. }
  345. // GetSheetStr
  346. // @return sheetName string 用户选择的sheet名称
  347. // @return startColumnName string 用户选择的开始列名称
  348. // @return endColumnName string 用户选择的结束列名称
  349. // @return startNum int 用户选择的开始列单元格位置
  350. // @return endNum int 用户选择的结束列单元格位置
  351. // @return isAll bool 是否选择整行/列数据
  352. // @return isRow bool 是否选择行数据
  353. // @return isColumn bool 是否选择列数据
  354. func GetSheetStr(sequenceStr string) (sheetName, startColumnName, endColumnName string, startNum, endNum int, isAll, isRow, isColumn bool, err error) {
  355. // 找出sheetName
  356. tmpList := strings.Split(sequenceStr, "!")
  357. if len(tmpList) != 2 {
  358. err = errors.New("错误的公式,查找sheet异常:" + sequenceStr)
  359. return
  360. }
  361. sheetName = tmpList[0]
  362. // 分离开始/结束单元格
  363. tmpList = strings.Split(tmpList[1], ":")
  364. if len(tmpList) != 2 {
  365. err = errors.New("错误的公式,查找开始/结束单元格异常:" + sequenceStr)
  366. return
  367. }
  368. startList := strings.Split(tmpList[0], "$")
  369. endList := strings.Split(tmpList[1], "$")
  370. lenList := len(startList)
  371. if lenList != len(endList) {
  372. err = errors.New("错误的公式,开始与结束单元格异常:" + sequenceStr)
  373. return
  374. }
  375. if lenList != 3 && lenList != 2 {
  376. err = errors.New("错误的公式:" + sequenceStr)
  377. return
  378. }
  379. startColumnName = startList[1]
  380. endColumnName = endList[1]
  381. // 长度为2的话,那说明是整行或整列
  382. if lenList == 2 {
  383. isAll = true
  384. startDeci, tmpErr1 := decimal.NewFromString(startList[1])
  385. endDeci, tmpErr2 := decimal.NewFromString(endList[1])
  386. if tmpErr1 == nil && tmpErr2 == nil {
  387. isRow = true // 正常转换的话,那么就是整行
  388. startNum = int(startDeci.IntPart())
  389. endNum = int(endDeci.IntPart())
  390. startColumnName = ``
  391. endColumnName = ``
  392. return
  393. }
  394. if tmpErr1 == nil || tmpErr2 == nil {
  395. err = errors.New("错误的公式2:" + sequenceStr)
  396. return
  397. }
  398. // 如果不能转成数字,那么就是整列
  399. isColumn = true
  400. return
  401. }
  402. // 确定行
  403. startDeci, tmpErr1 := decimal.NewFromString(startList[2])
  404. endDeci, tmpErr2 := decimal.NewFromString(endList[2])
  405. if tmpErr1 != nil && tmpErr1 != tmpErr2 {
  406. err = errors.New("错误的公式3:" + sequenceStr)
  407. return
  408. }
  409. startNum = int(startDeci.IntPart())
  410. endNum = int(endDeci.IntPart())
  411. if startColumnName != endColumnName && startNum != endNum {
  412. err = errors.New("选区不允许跨行或者跨列")
  413. }
  414. if startColumnName == endColumnName {
  415. isColumn = true // 列数据
  416. } else {
  417. isRow = true // 行数据
  418. }
  419. return
  420. }