|
@@ -0,0 +1,142 @@
|
|
|
+package services
|
|
|
+
|
|
|
+import (
|
|
|
+ "eta/eta_data_init/utils"
|
|
|
+ "fmt"
|
|
|
+ "github.com/araddon/dateparse"
|
|
|
+ "github.com/xuri/excelize/v2"
|
|
|
+ "math"
|
|
|
+ "os"
|
|
|
+ "path/filepath"
|
|
|
+ "strconv"
|
|
|
+ "strings"
|
|
|
+ "time"
|
|
|
+)
|
|
|
+
|
|
|
+// InitPCSGBloombergData 初始化中石油新加坡Bloomberg指标
|
|
|
+func InitPCSGBloombergData(dataPath string) {
|
|
|
+ var err error
|
|
|
+ defer func() {
|
|
|
+ if err != nil {
|
|
|
+ fmt.Println(err.Error())
|
|
|
+ utils.FileLog.Info("InitPCSGBloombergData Err: " + err.Error())
|
|
|
+ }
|
|
|
+ }()
|
|
|
+
|
|
|
+ // 读取excel
|
|
|
+ path, e := filepath.Abs(os.Args[0])
|
|
|
+ if e != nil {
|
|
|
+ err = fmt.Errorf("file abs err: %s", e.Error())
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ dir := filepath.Dir(path)
|
|
|
+ dataPath = dir + dataPath
|
|
|
+ //dataPath = "docs/HistorySample_0508.xlsx"
|
|
|
+ f, e := excelize.OpenFile(dataPath)
|
|
|
+ if e != nil {
|
|
|
+ err = fmt.Errorf("open file err: %s", e.Error())
|
|
|
+ return
|
|
|
+ }
|
|
|
+ defer func() {
|
|
|
+ if e = f.Close(); e != nil {
|
|
|
+ err = fmt.Errorf("f close err: %s", e.Error())
|
|
|
+ }
|
|
|
+ }()
|
|
|
+
|
|
|
+ type ExcelRows struct {
|
|
|
+ IndexCode string `description:"指标编码"`
|
|
|
+ DataMap map[time.Time]float64 `description:"数据日期/值"`
|
|
|
+ }
|
|
|
+ indexMap := make(map[string]*ExcelRows, 0)
|
|
|
+
|
|
|
+ sheets := f.GetSheetList()
|
|
|
+ for _, sheet := range sheets {
|
|
|
+ fmt.Printf("sheet: %s\n", sheet)
|
|
|
+
|
|
|
+ rows, e := f.GetRows(sheet)
|
|
|
+ if e != nil {
|
|
|
+ err = fmt.Errorf("f GetRows err: %s", e.Error())
|
|
|
+ return
|
|
|
+ }
|
|
|
+ if len(rows) == 0 {
|
|
|
+ continue
|
|
|
+ }
|
|
|
+
|
|
|
+ for rk, row := range rows {
|
|
|
+ // 标题行
|
|
|
+ if rk <= 0 {
|
|
|
+ continue
|
|
|
+ }
|
|
|
+
|
|
|
+ // 数据行
|
|
|
+ var rCode, rDate, rVal string
|
|
|
+ for ck, cell := range row {
|
|
|
+ cell = strings.TrimSpace(cell)
|
|
|
+ switch ck {
|
|
|
+ case 4:
|
|
|
+ rCode = cell
|
|
|
+ case 6:
|
|
|
+ rDate = cell
|
|
|
+ case 7:
|
|
|
+ rVal = cell
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if rCode == "" {
|
|
|
+ fmt.Printf("%d行指标编码为空\n", rk+1)
|
|
|
+ continue
|
|
|
+ }
|
|
|
+ if indexMap[rCode] == nil {
|
|
|
+ indexMap[rCode] = new(ExcelRows)
|
|
|
+ indexMap[rCode].DataMap = make(map[time.Time]float64, 0)
|
|
|
+ }
|
|
|
+ indexMap[rCode].IndexCode = rCode
|
|
|
+
|
|
|
+ // 日期/值
|
|
|
+ if rDate == "" {
|
|
|
+ fmt.Printf("%d行数据日期为空\n", rk+1)
|
|
|
+ continue
|
|
|
+ }
|
|
|
+ // 格式dd-mm-yy
|
|
|
+ rDate = strings.ReplaceAll(rDate, "-", ".")
|
|
|
+ d, e := dateparse.ParseAny(rDate)
|
|
|
+ if e != nil {
|
|
|
+ fmt.Printf("%d行数据日期格式有误: %s, err: %v\n", rk+1, rDate, e)
|
|
|
+ continue
|
|
|
+ }
|
|
|
+ if rVal == "" || rVal == "NULL" {
|
|
|
+ fmt.Printf("%d行数据值为空\n", rk+1)
|
|
|
+ continue
|
|
|
+ }
|
|
|
+ val, e := strconv.ParseFloat(rVal, 64)
|
|
|
+ if e != nil {
|
|
|
+ fmt.Printf("%d行数据值有误: %s\n", rk+1, rVal)
|
|
|
+ continue
|
|
|
+ }
|
|
|
+ // 保留四位小数
|
|
|
+ rounded := math.Round(val*10000) / 10000
|
|
|
+ if rounded == math.Floor(rounded) {
|
|
|
+ val = float64(int64(rounded))
|
|
|
+ } else {
|
|
|
+ val = rounded
|
|
|
+ }
|
|
|
+ indexMap[rCode].DataMap[d] = val
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 请求接口导入数据
|
|
|
+ method := "bloomberg/pcsg/import_history_data"
|
|
|
+ for _, v := range indexMap {
|
|
|
+ if v.IndexCode == "" {
|
|
|
+ continue
|
|
|
+ }
|
|
|
+ requestMap := make(map[string]interface{})
|
|
|
+ requestMap["IndexCode"] = v.IndexCode
|
|
|
+ requestMap["DataMap"] = v.DataMap
|
|
|
+ indexRes, e := PostEdbLib(requestMap, method)
|
|
|
+ if e != nil {
|
|
|
+ fmt.Printf("post edb lib err: %s; result: %s", e.Error(), string(indexRes))
|
|
|
+ continue
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|