oil_supply_analysis_processor.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. package ruizide
  2. import (
  3. "encoding/json"
  4. "eta/eta_data_analysis/models"
  5. "eta/eta_data_analysis/utils"
  6. "fmt"
  7. "github.com/shopspring/decimal"
  8. "github.com/xuri/excelize/v2"
  9. "os"
  10. "path/filepath"
  11. "strings"
  12. )
  13. const (
  14. OilSupplyAnalysis = "Oil Supply Analysis"
  15. OsaBeginRow = 1
  16. OsaRows = 3
  17. )
  18. var (
  19. OilSupplyAnalysisConfigMap = map[string]OSAPConfig{
  20. "Chart 1": {
  21. Frequency: "月度",
  22. Unit: "千桶每天",
  23. IndexNameColSuffix: "Oil Classification Group",
  24. IndexRow: 1,
  25. DataRow: 2,
  26. DateRow: 0,
  27. },
  28. "Chart 3": {
  29. Frequency: "月度",
  30. Unit: "千桶每天",
  31. IndexNameColSuffix: "CapacityDetail",
  32. IndexRow: 1,
  33. DataRow: 2,
  34. DateRow: 0,
  35. },
  36. "Chart 4": {
  37. Frequency: "月度",
  38. Unit: "千桶每天",
  39. IndexNameColSuffix: "Region",
  40. IndexRow: 2,
  41. DataRow: 1,
  42. DateRow: 0,
  43. },
  44. "Chart 5": {
  45. Frequency: "月度",
  46. Unit: "千桶每天",
  47. IndexNameColSuffix: "OilAndGasCategory",
  48. IndexRow: 1,
  49. DataRow: 2,
  50. DateRow: 0,
  51. },
  52. }
  53. )
  54. type OSAPConfig struct {
  55. Frequency string
  56. Unit string
  57. IndexNameColSuffix string
  58. IndexRow int
  59. DataRow int
  60. DateRow int
  61. }
  62. type OilSupplyAnalysisProcessor struct {
  63. }
  64. func (p *OilSupplyAnalysisProcessor) Process(tableName string) (err error) {
  65. var fileName string
  66. // 解析表格
  67. fileName = tableName + "_" + utils.GetCurrentYearMonth() + ".xlsx"
  68. filePath := filepath.Join(excelDir, fileName)
  69. if _, err = os.Stat(filePath); os.IsNotExist(err) {
  70. utils.FileLog.Error("文件不存在: %v", err)
  71. return
  72. }
  73. // 打开 Excel 文件
  74. file, err := excelize.OpenFile(filePath)
  75. if err != nil {
  76. utils.FileLog.Error("无法打开 Excel 文件: %v", err)
  77. return
  78. }
  79. sheetNames := file.GetSheetList()
  80. classifyIds, err := dealClassify(CubeDashboards, []string{OilSupplyAnalysis})
  81. // 获取所有工作表
  82. if err != nil {
  83. utils.FileLog.Error("获取分类Id失败: %v", err)
  84. return
  85. }
  86. //按照sheet页来处理数据,一个sheet发送一次服务器
  87. for _, sheetName := range sheetNames {
  88. var indexMap = make(map[string]*models.IndexInfo)
  89. // 获取工作表的最大行数
  90. dataRows, excelErr := file.GetRows(sheetName) // 直接获取所有行数据
  91. if excelErr != nil {
  92. utils.FileLog.Error("获取工作表数据时出错: %v", excelErr)
  93. continue
  94. }
  95. if sheetConfig, ok := OilSupplyAnalysisConfigMap[sheetName]; ok {
  96. // 匹配非数字字符
  97. classifyId := classifyIds[OilSupplyAnalysis]
  98. indexMap, err = DataHandler(dataRows, sheetConfig, classifyId)
  99. // 新增数据源指标数据
  100. if len(indexMap) > 0 {
  101. for _, index := range indexMap {
  102. pars := chunkIndexData(index, rzdBatchSize)
  103. for _, par := range pars {
  104. // 转换成json
  105. marshal, parseErr := json.Marshal(par)
  106. if parseErr != nil {
  107. utils.FileLog.Error("json.Marshal err: %v", parseErr)
  108. return parseErr
  109. }
  110. // 发送 HTTP POST 请求
  111. _, err = utils.HttpPostRequest(utils.EDB_LIB_URL+utils.ADD_BATCH_RZD_DATA, string(marshal), "application/json")
  112. if err != nil {
  113. utils.FileLog.Error("postEdbLib err: %v", err)
  114. return err
  115. }
  116. }
  117. }
  118. }
  119. }
  120. }
  121. return
  122. }
  123. func DataHandler(dataRows [][]string, sheetConfig OSAPConfig, classifyId int) (indexMap map[string]*models.IndexInfo, err error) {
  124. indexMap = make(map[string]*models.IndexInfo)
  125. for _, rowData := range dataRows[BeginRow : len(dataRows)-1] {
  126. less := Rows - len(rowData)
  127. if less > 0 {
  128. for i := 0; i < less; i++ {
  129. rowData = append(rowData, "")
  130. }
  131. }
  132. // step_2: 指标
  133. // 指标名称
  134. indexName := OilSupplyAnalysis + "/" + sheetConfig.IndexNameColSuffix + "/" + rowData[sheetConfig.IndexRow]
  135. // 生成指标编码
  136. indexCode := getIndexId(fmt.Sprintf("%s %s", OilSupplyAnalysis, sheetConfig.IndexNameColSuffix), strings.ReplaceAll(strings.ToLower(rowData[sheetConfig.IndexRow]), " ", ""), "")
  137. dataTime := rowData[sheetConfig.DateRow]
  138. if dataTime == "0" {
  139. continue
  140. }
  141. date, _, convertErr := utils.ConvertDateFormatForOSA(dataTime)
  142. if convertErr != nil {
  143. utils.FileLog.Error(fmt.Sprintf("转换时间数据失败,index_code:%s,time_value:%s err:%v", indexCode, dataTime, convertErr))
  144. continue
  145. }
  146. if rowData[sheetConfig.DataRow] != "" {
  147. value, parseErr := decimal.NewFromString(rowData[sheetConfig.DataRow])
  148. if parseErr != nil {
  149. utils.FileLog.Error(fmt.Sprintf("转换data数据失败,index_code:%s,data_value:%s err:%v", indexCode, rowData[sheetConfig.DataRow], err))
  150. continue
  151. } else {
  152. if index, ok := indexMap[indexCode]; ok {
  153. if index.StartDate.After(date) {
  154. index.StartDate = date
  155. }
  156. if index.EndDate.Before(date) {
  157. index.EndDate = date
  158. index.LatestValue = value
  159. }
  160. index.DataList = append(index.DataList, models.IndexData{
  161. DataTime: date,
  162. Value: value,
  163. })
  164. } else {
  165. indexMap[indexCode] = &models.IndexInfo{
  166. IndexName: indexName,
  167. IndexCode: indexCode,
  168. Frequency: sheetConfig.Frequency,
  169. Unit: sheetConfig.Unit,
  170. StartDate: date,
  171. EndDate: date,
  172. LatestValue: value,
  173. ClassifyId: classifyId,
  174. DataList: []models.IndexData{
  175. {
  176. DataTime: date,
  177. Value: value,
  178. },
  179. },
  180. }
  181. }
  182. }
  183. }
  184. }
  185. return
  186. }