custom_analysis_edb.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623
  1. package excel
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "eta/eta_api/models/data_manage/excel"
  6. "eta/eta_api/services/data"
  7. excelServices "eta/eta_api/services/excel"
  8. "eta/eta_api/utils"
  9. "fmt"
  10. "github.com/araddon/dateparse"
  11. "github.com/shopspring/decimal"
  12. "github.com/tealeg/xlsx"
  13. "github.com/xuri/excelize/v2"
  14. "strings"
  15. )
  16. // GetCustomAnalysisExcelData 获取自定义分析的表格data数据
  17. func GetCustomAnalysisExcelData(excelInfo *excel.ExcelInfo) (luckySheet excelServices.LuckySheet, err error, errMsg string) {
  18. // 查找当前excel的sheet列表
  19. sheetList, err := excel.GetAllSheetList(excelInfo.ExcelInfoId)
  20. if err != nil {
  21. errMsg = "保存失败"
  22. err = errors.New("查找当前excel的sheet列表失败,Err:" + err.Error())
  23. return
  24. }
  25. currSheetMap := make(map[string]string)
  26. for _, sheet := range sheetList {
  27. currSheetMap[sheet.SheetName] = sheet.SheetName
  28. }
  29. sheetCellDataMapList := make(map[int][]excelServices.LuckySheetCellData)
  30. // 通过excel的id获取各个sheet的单元格数据map
  31. {
  32. dataList, tmpErr := excel.GetAllSheetDataListByExcelInfoId(excelInfo.ExcelInfoId)
  33. if tmpErr != nil {
  34. err = tmpErr
  35. return
  36. }
  37. for _, cellData := range dataList {
  38. sheetDataList, ok := sheetCellDataMapList[cellData.ExcelSheetId]
  39. if !ok {
  40. sheetDataList = make([]excelServices.LuckySheetCellData, 0)
  41. }
  42. tmpSheetDataList := make([]excelServices.LuckySheetCellData, 0)
  43. err = json.Unmarshal([]byte(cellData.Data), &tmpSheetDataList)
  44. if err != nil {
  45. return
  46. }
  47. sheetCellDataMapList[cellData.ExcelSheetId] = append(sheetDataList, tmpSheetDataList...)
  48. }
  49. }
  50. // 转成luckySheet的数据格式
  51. luckySheet = excelServices.LuckySheet{
  52. SheetList: make([]excelServices.LuckySheetData, 0),
  53. }
  54. for _, sheet := range sheetList {
  55. var luckySheetDataConfig excelServices.LuckySheetDataConfig
  56. err = json.Unmarshal([]byte(sheet.Config), &luckySheetDataConfig)
  57. if err != nil {
  58. return
  59. }
  60. tmpLuckySheetDataInfo := excelServices.LuckySheetData{
  61. Name: sheet.SheetName,
  62. Index: sheet.Sort,
  63. CellData: sheetCellDataMapList[sheet.ExcelSheetId],
  64. Config: luckySheetDataConfig,
  65. }
  66. luckySheet.SheetList = append(luckySheet.SheetList, tmpLuckySheetDataInfo)
  67. }
  68. return
  69. }
  70. // GenerateExcelCustomAnalysisExcel 根据自定义分析的表格data数据生成excel
  71. func GenerateExcelCustomAnalysisExcel(excelInfo *excel.ExcelInfo) (downloadFilePath string, err error, errMsg string) {
  72. luckySheet, err, errMsg := GetCustomAnalysisExcelData(excelInfo)
  73. if err != nil {
  74. return
  75. }
  76. downloadFilePath, err = luckySheet.ToExcel(false)
  77. return
  78. }
  79. // 数据集
  80. type dataStruct struct {
  81. Value float64
  82. Ok bool
  83. }
  84. // HandleEdbSequenceVal 处理日期集和数据集(获取可用的日期、数据集)
  85. func HandleEdbSequenceVal(dateSequenceVal, dataSequenceVal []string) (newDateList []string, newDataList []float64, err error, errMsg string) {
  86. newDateList = make([]string, 0)
  87. newDataList = make([]float64, 0)
  88. dataList := make([]dataStruct, 0)
  89. {
  90. for _, v := range dataSequenceVal {
  91. // 如果没有数据集,那么就过滤
  92. if v == `` {
  93. // 如果开始插入数据了,那么就需要插入不存在值
  94. dataList = append(dataList, dataStruct{
  95. Value: 0,
  96. Ok: false,
  97. })
  98. continue
  99. }
  100. v = strings.Replace(v, ",", "", -1)
  101. v = strings.Replace(v, ",", "", -1)
  102. // 过滤空格
  103. v = strings.Replace(v, " ", "", -1)
  104. var tmpVal float64
  105. if strings.Contains(v, "%") {
  106. // 百分比的数
  107. isPercentage, percentageValue := utils.IsPercentage(v)
  108. if !isPercentage {
  109. dataList = append(dataList, dataStruct{
  110. Value: 0,
  111. Ok: false,
  112. })
  113. continue
  114. }
  115. tmpValDec, tmpErr := decimal.NewFromString(percentageValue)
  116. if tmpErr != nil {
  117. dataList = append(dataList, dataStruct{
  118. Value: 0,
  119. Ok: false,
  120. })
  121. continue
  122. }
  123. tmpVal, _ = tmpValDec.Div(decimal.NewFromFloat(100)).Float64()
  124. } else {
  125. tmpValDec, tmpErr := decimal.NewFromString(v)
  126. if tmpErr != nil {
  127. dataList = append(dataList, dataStruct{
  128. Value: 0,
  129. Ok: false,
  130. })
  131. continue
  132. }
  133. tmpVal, _ = tmpValDec.Float64()
  134. }
  135. dataList = append(dataList, dataStruct{
  136. Value: tmpVal,
  137. Ok: true,
  138. })
  139. }
  140. }
  141. // 日期集
  142. dateList := make([]string, 0)
  143. {
  144. for _, v := range dateSequenceVal {
  145. // 如果没有数据集,那么就过滤
  146. if v == `` {
  147. // 如果开始插入数据了,那么就需要插入不存在值
  148. dateList = append(dateList, "")
  149. continue
  150. }
  151. // 过滤空格
  152. v = strings.Replace(v, " ", "", -1)
  153. t1, tmpErr := dateparse.ParseAny(v)
  154. if tmpErr != nil {
  155. dateList = append(dateList, "")
  156. continue
  157. }
  158. dateList = append(dateList, t1.Format(utils.FormatDate))
  159. }
  160. }
  161. lenData := len(dataList)
  162. lenDate := len(dateList)
  163. // 最小个数
  164. num := lenDate
  165. if num > lenData {
  166. num = lenData
  167. }
  168. for i := 0; i < num; i++ {
  169. date := dateList[i]
  170. tmpData := dataList[i]
  171. // 日期为空、数据为空
  172. if !tmpData.Ok || date == `` {
  173. continue
  174. }
  175. newDateList = append(newDateList, date)
  176. newDataList = append(newDataList, tmpData.Value)
  177. }
  178. return
  179. }
  180. // ResetCustomAnalysisData 数据重置的结构体
  181. type ResetCustomAnalysisData struct {
  182. EdbInfoId int
  183. DateList []string
  184. DataList []float64
  185. }
  186. // Refresh 刷新表格关联的指标信息
  187. func Refresh(excelInfo *excel.ExcelInfo) (err error, errMsg string, isSendEmail bool) {
  188. isSendEmail = true
  189. list, err := excel.GetAllExcelEdbMappingItemByExcelInfoId(excelInfo.ExcelInfoId)
  190. if err != nil {
  191. errMsg = "获取失败"
  192. return
  193. }
  194. // 没有关联指标,那么就退出吧
  195. if len(list) <= 0 {
  196. return
  197. }
  198. for k, v := range list {
  199. var tmpCalculateFormula excel.CalculateFormula
  200. err = json.Unmarshal([]byte(v.CalculateFormula), &tmpCalculateFormula)
  201. if err != nil {
  202. errMsg = "获取失败"
  203. err = errors.New("公式转换失败,Err:" + err.Error())
  204. return
  205. }
  206. v.DateSequenceStr = tmpCalculateFormula.DateSequenceStr
  207. v.DataSequenceStr = tmpCalculateFormula.DataSequenceStr
  208. list[k] = v
  209. }
  210. luckySheet, err, errMsg := GetCustomAnalysisExcelData(excelInfo)
  211. if err != nil {
  212. return
  213. }
  214. // 获取excel表格数据
  215. xlsxFile, err := luckySheet.GetExcelData(false)
  216. if err != nil {
  217. return
  218. }
  219. //fmt.Println(xlsxFile)
  220. edbInfoIdList := make([]int, 0)
  221. for _, v := range list {
  222. edbInfoIdList = append(edbInfoIdList, v.EdbInfoId)
  223. // 获取对应的日期和数据列表
  224. relDateList, relDataList, tmpErr, tmpErrMsg := getDateAndDataList(v, xlsxFile)
  225. if tmpErr != nil {
  226. err = tmpErr
  227. errMsg = tmpErrMsg
  228. return
  229. }
  230. //fmt.Println(v)
  231. req2 := &ResetCustomAnalysisData{
  232. EdbInfoId: v.EdbInfoId,
  233. DateList: relDateList,
  234. DataList: relDataList,
  235. }
  236. // 调用指标库去更新
  237. reqJson, tmpErr := json.Marshal(req2)
  238. if tmpErr != nil {
  239. err = tmpErr
  240. return
  241. }
  242. respItem, tmpErr := data.ResetCustomAnalysisData(string(reqJson))
  243. if tmpErr != nil {
  244. err = tmpErr
  245. return
  246. }
  247. if respItem.Ret != 200 {
  248. errMsg = respItem.Msg
  249. err = errors.New(respItem.ErrMsg)
  250. return
  251. }
  252. }
  253. if len(edbInfoIdList) > 0 {
  254. err, _ = data.EdbInfoRefreshAllFromBaseV3(edbInfoIdList, false, true, true)
  255. }
  256. //xlsxFile.Sheet[]
  257. return
  258. }
  259. // getDateAndDataList
  260. // @Description: 获取待刷新的日期和数据
  261. // @author: Roc
  262. // @datetime 2023-12-21 15:21:14
  263. // @param excelEdbMappingItem *excel.ExcelEdbMappingItem
  264. // @param xlsxFile *xlsx.File
  265. // @return newDateList []string
  266. // @return newDataList []float64
  267. // @return err error
  268. // @return errMsg string
  269. func getDateAndDataList(excelEdbMappingItem *excel.ExcelEdbMappingItem, xlsxFile *xlsx.File) (newDateList []string, newDataList []float64, err error, errMsg string) {
  270. var dateList, dataList []string
  271. // 日期序列
  272. {
  273. sheetName, startColumnName, endColumnName, startNum, endNum, isAll, isRow, isColumn, tmpErr := GetSheetStr(excelEdbMappingItem.DateSequenceStr)
  274. if tmpErr != nil {
  275. err = tmpErr
  276. return
  277. }
  278. // 查找sheet页
  279. sheetInfo, ok := xlsxFile.Sheet[sheetName]
  280. if !ok {
  281. errMsg = "找不到" + sheetName
  282. err = errors.New(errMsg)
  283. return
  284. }
  285. startNum = startNum - 1
  286. endNum = endNum - 1
  287. // 选择行的数据
  288. if isRow {
  289. // 因为是选择一行的数据,所以开始行和结束行时一样的
  290. //endNum = startNum - 1
  291. // 开始列名、结束列
  292. var startColumn, endColumn int
  293. if isAll {
  294. // 结束列(其实也就是整列的个数)
  295. endColumn = len(sheetInfo.Cols) - 1
  296. } else {
  297. tmpStartColumn, tmpErr := excelize.ColumnNameToNumber(startColumnName)
  298. if tmpErr != nil {
  299. errMsg = "列名异常:" + startColumnName
  300. err = errors.New(errMsg)
  301. return
  302. }
  303. tmpEndColumn, tmpErr := excelize.ColumnNameToNumber(endColumnName)
  304. if tmpErr != nil {
  305. errMsg = "列名异常:" + endColumnName
  306. err = errors.New(errMsg)
  307. return
  308. }
  309. startColumn = tmpStartColumn - 1
  310. endColumn = tmpEndColumn - 1
  311. }
  312. // 最大列数,如果设置的超过了最大列数,那么结束列就是最大列数
  313. maxCol := len(sheetInfo.Cols)
  314. if endColumn > maxCol {
  315. endColumn = maxCol - 1
  316. }
  317. // 长度固定,避免一直申请内存空间
  318. dateList = make([]string, endColumn-startColumn+1)
  319. i := 0
  320. for currColumn := startColumn; currColumn <= endColumn; currColumn++ {
  321. currCell := sheetInfo.Cell(startNum, currColumn)
  322. if currCell == nil {
  323. errMsg = fmt.Sprintf("第%d列,第%d行数据异常", startColumn, startNum)
  324. err = errors.New(errMsg)
  325. return
  326. }
  327. //dateList = append(dateList, currCell.Value)
  328. dateList[i] = currCell.Value
  329. i++
  330. }
  331. } else if isColumn { // 选择列的数据
  332. if isAll {
  333. // 选择一整列的话,结束行得根据实际情况调整(其实也就是整个sheet有多少行)
  334. endNum = len(sheetInfo.Rows) - 1
  335. }
  336. startColumn, tmpErr := excelize.ColumnNameToNumber(startColumnName)
  337. if tmpErr != nil {
  338. errMsg = "列名异常:" + startColumnName
  339. err = errors.New(errMsg)
  340. return
  341. }
  342. startColumn = startColumn - 1
  343. // 最大行数,如果设置的超过了最大行数,那么结束行就是最大行数
  344. maxRow := len(sheetInfo.Rows)
  345. if endNum > maxRow {
  346. endNum = maxRow - 1
  347. }
  348. // 长度固定,避免一直申请内存空间
  349. dateList = make([]string, endNum-startNum+1)
  350. i := 0
  351. for currRow := startNum; currRow <= endNum; currRow++ {
  352. currCell := sheetInfo.Cell(currRow, startColumn)
  353. if currCell == nil {
  354. errMsg = fmt.Sprintf("第%d列,第%d行数据异常", startColumn, startNum)
  355. err = errors.New(errMsg)
  356. return
  357. }
  358. //dateList = append(dateList, currCell.Value)
  359. dateList[i] = currCell.Value
  360. i++
  361. }
  362. }
  363. }
  364. // 数据序列
  365. {
  366. sheetName, startColumnName, endColumnName, startNum, endNum, isAll, isRow, isColumn, tmpErr := GetSheetStr(excelEdbMappingItem.DataSequenceStr)
  367. if tmpErr != nil {
  368. err = tmpErr
  369. return
  370. }
  371. // 查找sheet页
  372. sheetInfo, ok := xlsxFile.Sheet[sheetName]
  373. if !ok {
  374. errMsg = "找不到" + sheetName
  375. err = errors.New(errMsg)
  376. return
  377. }
  378. startNum = startNum - 1
  379. endNum = endNum - 1
  380. // 选择行的数据
  381. if isRow {
  382. // 开始列名、结束列
  383. var startColumn, endColumn int
  384. if isAll {
  385. // 结束列(其实也就是整列的个数)
  386. endColumn = len(sheetInfo.Cols) - 1
  387. } else {
  388. tmpStartColumn, tmpErr := excelize.ColumnNameToNumber(startColumnName)
  389. if tmpErr != nil {
  390. errMsg = "列名异常:" + startColumnName
  391. err = errors.New(errMsg)
  392. return
  393. }
  394. tmpEndColumn, tmpErr := excelize.ColumnNameToNumber(endColumnName)
  395. if tmpErr != nil {
  396. errMsg = "列名异常:" + endColumnName
  397. err = errors.New(errMsg)
  398. return
  399. }
  400. startColumn = tmpStartColumn - 1
  401. endColumn = tmpEndColumn - 1
  402. }
  403. // 最大列数,如果设置的超过了最大列数,那么结束列就是最大列数
  404. maxCol := len(sheetInfo.Cols)
  405. if endColumn > maxCol {
  406. endColumn = maxCol - 1
  407. }
  408. // 长度固定,避免一直申请内存空间
  409. dataList = make([]string, endColumn-startColumn+1)
  410. i := 0
  411. for currColumn := startColumn; currColumn <= endColumn; currColumn++ {
  412. currCell := sheetInfo.Cell(startNum, currColumn)
  413. if currCell == nil {
  414. errMsg = fmt.Sprintf("第%d列,第%d行数据异常", startColumn, startNum)
  415. err = errors.New(errMsg)
  416. return
  417. }
  418. //dataList = append(dataList, currCell.Value)
  419. dataList[i] = currCell.Value
  420. i++
  421. }
  422. } else if isColumn { // 选择列的数据
  423. if isAll {
  424. // 选择一整列的话,结束行得根据实际情况调整(其实也就是整个sheet有多少行)
  425. endNum = len(sheetInfo.Rows) - 1
  426. }
  427. startColumn, tmpErr := excelize.ColumnNameToNumber(startColumnName)
  428. if tmpErr != nil {
  429. errMsg = "列名异常:" + startColumnName
  430. err = errors.New(errMsg)
  431. return
  432. }
  433. startColumn = startColumn - 1
  434. // 最大行数,如果设置的超过了最大行数,那么结束行就是最大行数
  435. maxRow := len(sheetInfo.Rows)
  436. if endNum > maxRow {
  437. endNum = maxRow - 1
  438. }
  439. // 长度固定,避免一直申请内存空间
  440. dataList = make([]string, endNum-startNum+1)
  441. i := 0
  442. for currRow := startNum; currRow <= endNum; currRow++ {
  443. currCell := sheetInfo.Cell(currRow, startColumn)
  444. if currCell == nil {
  445. errMsg = fmt.Sprintf("第%d列,第%d行数据异常", startColumn, startNum)
  446. err = errors.New(errMsg)
  447. return
  448. }
  449. //dataList = append(dataList, currCell.Value)
  450. dataList[i] = currCell.Value
  451. i++
  452. }
  453. }
  454. }
  455. //fmt.Println(dateList, dataList)
  456. //fmt.Println("日期序列结束")
  457. // 将excel中的日期、数据系列处理
  458. newDateList, newDataList, err, errMsg = HandleEdbSequenceVal(dateList, dataList)
  459. return
  460. }
  461. // GetSheetStr
  462. // @return sheetName string 用户选择的sheet名称
  463. // @return startColumnName string 用户选择的开始列名称
  464. // @return endColumnName string 用户选择的结束列名称
  465. // @return startNum int 用户选择的开始列单元格位置
  466. // @return endNum int 用户选择的结束列单元格位置
  467. // @return isAll bool 是否选择整行/列数据
  468. // @return isRow bool 是否选择行数据
  469. // @return isColumn bool 是否选择列数据
  470. func GetSheetStr(sequenceStr string) (sheetName, startColumnName, endColumnName string, startNum, endNum int, isAll, isRow, isColumn bool, err error) {
  471. // 找出sheetName
  472. tmpList := strings.Split(sequenceStr, "!")
  473. if len(tmpList) != 2 {
  474. err = errors.New("错误的公式,查找sheet异常:" + sequenceStr)
  475. return
  476. }
  477. sheetName = tmpList[0]
  478. // 分离开始/结束单元格
  479. tmpList = strings.Split(tmpList[1], ":")
  480. if len(tmpList) != 2 {
  481. err = errors.New("错误的公式,查找开始/结束单元格异常:" + sequenceStr)
  482. return
  483. }
  484. startList := strings.Split(tmpList[0], "$")
  485. endList := strings.Split(tmpList[1], "$")
  486. lenList := len(startList)
  487. if lenList != len(endList) {
  488. err = errors.New("错误的公式,开始与结束单元格异常:" + sequenceStr)
  489. return
  490. }
  491. if lenList != 3 && lenList != 2 {
  492. err = errors.New("错误的公式:" + sequenceStr)
  493. return
  494. }
  495. startColumnName = startList[1]
  496. endColumnName = endList[1]
  497. // 长度为2的话,那说明是整行或整列
  498. if lenList == 2 {
  499. isAll = true
  500. startDeci, tmpErr1 := decimal.NewFromString(startList[1])
  501. endDeci, tmpErr2 := decimal.NewFromString(endList[1])
  502. if tmpErr1 == nil && tmpErr2 == nil {
  503. isRow = true // 正常转换的话,那么就是整行
  504. startNum = int(startDeci.IntPart())
  505. endNum = int(endDeci.IntPart())
  506. startColumnName = ``
  507. endColumnName = ``
  508. return
  509. }
  510. if tmpErr1 == nil || tmpErr2 == nil {
  511. err = errors.New("错误的公式2:" + sequenceStr)
  512. return
  513. }
  514. // 如果不能转成数字,那么就是整列
  515. isColumn = true
  516. return
  517. }
  518. // 确定行
  519. startDeci, tmpErr1 := decimal.NewFromString(startList[2])
  520. endDeci, tmpErr2 := decimal.NewFromString(endList[2])
  521. if tmpErr1 != nil && tmpErr1 != tmpErr2 {
  522. err = errors.New("错误的公式3:" + sequenceStr)
  523. return
  524. }
  525. startNum = int(startDeci.IntPart())
  526. endNum = int(endDeci.IntPart())
  527. if startColumnName != endColumnName && startNum != endNum {
  528. err = errors.New("选区不允许跨行或者跨列")
  529. }
  530. if startColumnName == endColumnName {
  531. isColumn = true // 列数据
  532. } else {
  533. isRow = true // 行数据
  534. }
  535. return
  536. }