浏览代码

初始化钢联化工指标

tuoling805 1 年之前
父节点
当前提交
49eee5ebcd
共有 3 个文件被更改,包括 249 次插入1 次删除
  1. 35 0
      models/init_base_index.go
  2. 212 0
      services/init_base_index.go
  3. 2 1
      services/task.go

+ 35 - 0
models/init_base_index.go

@@ -0,0 +1,35 @@
+package models
+
+import "time"
+
+type MysteelIndexResp struct {
+	Ret     int    `json:"Ret"`
+	Msg     string `json:"Msg"`
+	ErrMsg  string `json:"ErrMsg"`
+	ErrCode string `json:"ErrCode"`
+	Data    struct {
+		BaseFromMysteelChemicalIndexId    int64     `orm:"column(base_from_mysteel_chemical_index_id);pk"`
+		BaseFromMysteelChemicalClassifyId int       `description:"分类id"`
+		IndexCode                         string    `description:"指标编码"`
+		IndexName                         string    `description:"指标名称"`
+		Unit                              string    `description:"单位"`
+		Source                            string    `description:"数据来源"`
+		Frequency                         string    `description:"频度"`
+		StartDate                         time.Time `description:"开始日期"`
+		EndDate                           time.Time `description:"结束日期"`
+		Describe                          string    `description:"指标描述"`
+		UpdateWeek                        string    `description:"更新周期"`
+		UpdateTime                        string    `description:"更新时间,多个时间点用英文,隔开"`
+		UpdateTime2                       string    `description:"更新时间2"`
+		SysUserId                         int       `description:"创建人id"`
+		SysUserRealName                   string    `description:"创建人姓名"`
+		FilePath                          string    `description:"文件存储路径"`
+		MergeFilePath                     string    `description:"更新文件"`
+		FileIndex                         int       `description:"文件索引"`
+		MergeUpdateWeek                   string    `description:"合并文件的更新周"`
+		UpdateDate                        string    `description:"更新日期"`
+		CreateTime                        time.Time `description:"创建时间"`
+		ModifyTime                        time.Time `description:"修改时间"`
+	} `json:"Data"`
+	Success bool `json:"Success"`
+}

+ 212 - 0
services/init_base_index.go

@@ -196,6 +196,218 @@ func InitBaseIndexData() {
 	}
 }
 
+// 初始化基础指标数据-钢联
+func InitBaseIndexDataFromMysteel() {
+	var err error
+
+	defer func() {
+		if err != nil {
+			fmt.Println("InitBaseIndexDataFromMysteel Err:" + err.Error())
+		}
+	}()
+
+	//读取excel
+	path, err := filepath.Abs(os.Args[0])
+	if err != nil {
+		fmt.Println(err)
+	}
+
+	dir := filepath.Dir(path)
+	fmt.Println("dir:" + dir)
+	dataPath := dir + "/docs/逸诺钢联数据录入.xlsx"
+	fmt.Println("dataPath:" + dataPath)
+	f, err := excelize.OpenFile(dataPath)
+	if err != nil {
+		fmt.Println(err)
+		return
+	}
+	defer func() {
+		// Close the spreadsheet.
+		if err := f.Close(); err != nil {
+			fmt.Println(err)
+		}
+	}()
+	rows, err := f.GetRows("Sheet1")
+	if err != nil {
+		fmt.Println(err)
+		return
+	}
+	fmt.Println("rows len:", len(rows))
+	for rk, row := range rows {
+		if rk > 0 {
+			var classifyFirst, classifySecond, classifyThree, indexCode, indexName, frequency, unit, source string
+			for ck, colCell := range row {
+				switch ck {
+				case 0:
+					classifyFirst = colCell
+				case 1:
+					classifySecond = colCell
+				case 2:
+					classifyThree = colCell
+				case 3:
+					indexCode = colCell
+				case 4:
+					indexName = colCell
+				case 5:
+					frequency = colCell
+				case 6:
+					unit = colCell
+				case 7:
+					source = colCell
+				}
+			}
+
+			if classifyFirst != "" &&
+				classifySecond != "" &&
+				classifyThree != "" &&
+				indexCode != "" &&
+				indexName != "" &&
+				unit != "" &&
+				frequency != "" &&
+				source != "" {
+
+				//判断指标是否存在
+				method := "mysteel_chemical/index_detail"
+				indexSearchMap := make(map[string]interface{})
+				indexSearchMap["EdbCode"] = indexCode
+				indexResult, err := PostEdbLib(indexSearchMap, method)
+				if err != nil {
+					fmt.Println("判断指标是否存在失败,Err:" + err.Error())
+					return
+				}
+
+				mysteelIndexResp := new(models.MysteelIndexResp)
+				err = json.Unmarshal(indexResult, &mysteelIndexResp)
+				if err != nil {
+					utils.FileLog.Info("判断指标是否存在失败:" + err.Error())
+					fmt.Println("判断指标是否存在失败,Err:" + err.Error())
+					return
+				}
+
+				if mysteelIndexResp.Ret != 200 {
+					fmt.Println("判断指标是否存在失败,Err:" + err.Error())
+					utils.FileLog.Info("判断指标是否存在失败:" + err.Error())
+					return
+				}
+
+				if mysteelIndexResp.Data.BaseFromMysteelChemicalIndexId <= 0 {
+					fmt.Println("指标:" + indexCode + ";不存在")
+					continue
+				}
+
+				method = "classify/get_or_add"
+				classifyFirstMap := make(map[string]interface{})
+				classifyFirstMap["ClassifyName"] = classifyFirst
+				classifyFirstMap["ParentId"] = 0
+				classifyFirstMap["Level"] = 0
+				classifyFirstMap["ClassifyType"] = 0
+				result, err := PostEdbLib(classifyFirstMap, method)
+				if err != nil {
+					utils.FileLog.Info("初始化分类1失败:" + err.Error())
+					return
+				}
+
+				resp := new(models.ClassifyResp)
+				err = json.Unmarshal(result, &resp)
+				if err != nil {
+					utils.FileLog.Info("初始化分类1失败:" + err.Error())
+					return
+				}
+
+				if resp.Ret != 200 {
+					utils.FileLog.Info("初始化分类1失败:" + resp.Msg + ";" + resp.ErrMsg)
+					return
+				}
+
+				classifySecondMap := make(map[string]interface{})
+				classifySecondMap["ClassifyName"] = classifySecond
+				classifySecondMap["ParentId"] = resp.Data.ClassifyId
+				classifySecondMap["Level"] = 1
+				classifySecondMap["ClassifyType"] = 0
+				result, err = PostEdbLib(classifySecondMap, method)
+				if err != nil {
+					utils.FileLog.Info("初始化分类2失败:" + err.Error())
+					return
+				}
+				resp = new(models.ClassifyResp)
+				err = json.Unmarshal(result, &resp)
+				if err != nil {
+					utils.FileLog.Info("初始化分类2失败:" + err.Error())
+					return
+				}
+				if resp.Ret != 200 {
+					utils.FileLog.Info("初始化分类2失败:" + resp.Msg + ";" + resp.ErrMsg)
+					return
+				}
+
+				classifyThreeMap := make(map[string]interface{})
+				classifyThreeMap["ClassifyName"] = classifyThree
+				classifyThreeMap["ParentId"] = resp.Data.ClassifyId
+				classifyThreeMap["Level"] = 2
+				classifyThreeMap["ClassifyType"] = 0
+				result, err = PostEdbLib(classifyThreeMap, method)
+				if err != nil {
+					utils.FileLog.Info("初始化分类3失败:" + err.Error())
+					return
+				}
+				resp = new(models.ClassifyResp)
+				err = json.Unmarshal(result, &resp)
+				if err != nil {
+					utils.FileLog.Info("初始化分类3失败:" + err.Error())
+					return
+				}
+
+				if resp.Ret != 200 {
+					utils.FileLog.Info("初始化分类3失败:" + resp.Msg + ";" + resp.ErrMsg)
+					return
+				}
+				method = "edb_info/add"
+				indexMap := make(map[string]interface{})
+				indexMap["Source"] = IndexSourceMap[source]
+				indexMap["EdbCode"] = indexCode
+				indexMap["EdbName"] = indexName
+				indexMap["Frequency"] = frequency
+				indexMap["Unit"] = unit
+				indexMap["ClassifyId"] = resp.Data.ClassifyId
+				result, err = PostEdbLib(indexMap, method)
+				if err != nil {
+					utils.FileLog.Info("初始化指标失败:" + err.Error() + " result:" + string(result))
+					return
+				}
+
+				indexResp := new(models.EdbInfoResp)
+				err = json.Unmarshal(result, &indexResp)
+				if err != nil {
+					utils.FileLog.Info("初始化分类2失败:" + err.Error())
+					return
+				}
+
+				if indexResp.Ret != 200 {
+					if strings.Contains(indexResp.Msg, "新增指标失败") {
+						continue
+					} else {
+						fmt.Println("初始化指标失败:" + indexResp.Msg + ";" + indexResp.ErrMsg)
+						utils.FileLog.Info("初始化指标失败:" + indexResp.Msg + ";" + indexResp.ErrMsg)
+						return
+					}
+				}
+				fmt.Println("add index success:" + indexCode)
+
+				//刷新指标
+				method = "mysteel_chemical/refresh"
+				refreshMap := make(map[string]interface{})
+				refreshMap["EdbInfoId"] = indexResp.Data.EdbInfoId
+				refreshMap["EdbCode"] = indexCode
+				refreshMap["StartDate"] = "1990-01-01"
+				PostEdbLib(refreshMap, method)
+			} else {
+				fmt.Println("data is empty")
+				fmt.Println(classifyFirst, classifySecond, classifyThree, indexCode, indexName, frequency, unit, source)
+			}
+		}
+	}
+}
+
 /*
 // PostEdbLib 调用指标接口
 func PostEdbLib(param map[string]interface{}, method string) (resp *models.BaseResponse, err error) {

+ 2 - 1
services/task.go

@@ -21,11 +21,12 @@ func Task() {
 	IndexSourceMap = make(map[string]int)
 	IndexSourceMap["ths"] = utils.DATA_SOURCE_THS
 	IndexSourceMap["wind"] = utils.DATA_SOURCE_WIND
+	IndexSourceMap["钢联"] = utils.DATA_SOURCE_MYSTEEL_CHEMICAL
 
 	time.Sleep(3 * time.Second)
 
 	fmt.Println("start InitCalculateIndex")
-	InitCalculateIndex()
+	InitBaseIndexDataFromMysteel()
 	fmt.Println("end InitCalculateIndex")
 
 	fmt.Println("task end")