balance_table.go 12 KB

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