浏览代码

初始化kpler产品

xyxie 13 小时之前
父节点
当前提交
5740ed98f8
共有 3 个文件被更改,包括 121 次插入6 次删除
  1. 4 2
      controllers/base_from_kpler.go
  2. 27 0
      models/base_from_kpler.go
  3. 90 4
      services/base_from_kpler.go

+ 4 - 2
controllers/base_from_kpler.go

@@ -327,9 +327,11 @@ func (this *KplerController) RefreshByApi() {
 		br.ErrMsg = "参数解析失败,Err:" + err.Error()
 		return
 	}
-	
+	if req.StartDate == "" {
+		req.StartDate = time.Now().Format("2006-01-02")
+	}
 	// 查询所有通过API获取的指标
-	refreshIndexes, err := models.GetApiKplerIndexesByFrequencyEndDateTime(req.Frequency, time.Now().Format("2006-01-02"))
+	refreshIndexes, err := models.GetApiKplerIndexesByFrequencyEndDateTime(req.Frequency, req.StartDate)
 	if err != nil {
 		br.Msg = "获取指标失败"
 		br.ErrMsg = "获取指标失败,Err:" + err.Error()

+ 27 - 0
models/base_from_kpler.go

@@ -521,6 +521,12 @@ func GetAllBaseFromKplerClassify() (items []*BaseFromKplerClassify, err error) {
 	return
 }
 
+func GetAllBaseFromKplerClassifyByClassifyType(classifyType string) (items []*BaseFromKplerClassify, err error) {
+	sql := ` SELECT * FROM base_from_kpler_classify WHERE classify_type=? and modify_time < "2025-06-18 15:37:00" limit 100`
+	err = global.DEFAULT_DB.Raw(sql, classifyType).Find(&items).Error
+	return
+}
+
 // 获取分类
 func (y *BaseFromKplerClassify) GetByProductIds(productIds []int) (items []*BaseFromKplerClassify, err error) {
 	sql := ` SELECT * FROM base_from_kpler_classify WHERE product_id in (?) `
@@ -528,6 +534,13 @@ func (y *BaseFromKplerClassify) GetByProductIds(productIds []int) (items []*Base
 	return
 }
 
+// 获取单个分类
+func (y *BaseFromKplerClassify) GetByProductId(productId int) (item *BaseFromKplerClassify, err error) {
+	sql := ` SELECT * FROM base_from_kpler_classify WHERE product_id=? `
+	err = global.DEFAULT_DB.Raw(sql, productId).First(&item).Error
+	return
+}
+
 // 获取分类
 func (y *BaseFromKplerClassify) GetByProductName(productName string) (item *BaseFromKplerClassify, err error) {
 	sql := ` SELECT * FROM base_from_kpler_classify WHERE product_name=? `
@@ -619,10 +632,24 @@ type KplerZoneDataLibReq struct {
 
 type KplerRefreshByApiReq struct {
 	Frequency string `description:"频度"`
+	StartDate string `description:"开始日期"`
 }
 
 func GetApiKplerIndexesByFrequencyEndDateTime(frequency string, endDateTime string) (items []*BaseFromKplerIndex, err error) {
 	sql := ` SELECT * FROM base_from_kpler_index WHERE frequency=? AND end_date < ? and api_query_url != '' order by base_from_kpler_index_id asc, api_query_url asc `
 	err = global.DEFAULT_DB.Raw(sql, frequency, endDateTime).Find(&items).Error
 	return
+}
+
+type KplerProductLibReq struct {
+	AncestorFamilyIds string `description:"祖先家族ID"`
+	AncestorFamilyNames string `description:"祖先家族名称"`
+	AncestorGroupIds string `description:"祖先组ID"`
+	AncestorGroupNames string `description:"祖先组名称"`
+	AncestorProductIds string `description:"祖先产品ID"`
+	AncestorProductNames string `description:"祖先产品名称"`
+	AncestorGradeIds string `description:"祖先等级ID"`
+	AncestorGradeNames string `description:"祖先等级名称"`
+	Products string `description:"产品"`
+	ProductIds string `description:"产品ID"`
 }

+ 90 - 4
services/base_from_kpler.go

@@ -66,7 +66,7 @@ func getKplerDataByApi(params models.KplerSearchEdbReq, serverUrl string, isRefr
 	libreq.WithIntraRegion = "true"
 	libreq.WithForecast = "true"
 	libreq.OnlyRealized = "false"
-	libreq.StartDate = "2025-01-01"
+	libreq.StartDate = "2013-01-01"
 	libreq.EndDate = time.Now().Format(utils.FormatDate)
 	// 请求接口
 	apiResp, err := getKplerFlowDataLib(serverUrl, libreq)
@@ -416,9 +416,13 @@ func getKplerFlowDataLib(libUrl string, dataMap *models.KplerFlowDataLibReq) (re
 	return resp, nil
 }
 
-func getKplerProductLib(libUrl string) (resp *models.KplerProductLibResp, err error) {
+func getKplerProductLib(libUrl string, req *models.KplerProductLibReq) (resp *models.KplerProductLibResp, err error) {
 	postUrl := fmt.Sprintf("%s%s", libUrl, "/v1/kpler/getProductData")
-	result, err := HttpPost(postUrl, "", "application/json")
+	postData, err := json.Marshal(req)
+	if err != nil {
+		return
+	}
+	result, err := HttpPost(postUrl, string(postData), "application/json")
 	if err != nil {
 		return
 	}
@@ -798,7 +802,18 @@ func GetKplerGranularity(frequency string) (granularity string) {
 
 func InitKplerProduct ()(err error) {
 	libUrl := "http://127.0.0.1:8915"
-	libResp, err := getKplerProductLib(libUrl)
+	libResp, err := getKplerProductLib(libUrl, &models.KplerProductLibReq{
+		AncestorFamilyIds: "",
+		AncestorFamilyNames: "",
+		AncestorGroupIds: "",
+		AncestorGroupNames: "",
+		AncestorProductIds: "",
+		AncestorProductNames: "",
+		AncestorGradeIds: "",
+		AncestorGradeNames: "",
+		Products: "",
+		ProductIds: "",
+	})
 	if err != nil {
 		return
 	}
@@ -872,6 +887,77 @@ func InitKplerProduct ()(err error) {
 	return
 }
 
+func InitKplerProductGrade ()(err error) {
+	libUrl := "http://127.0.0.1:8915"
+	
+	classifyList, err := models.GetAllBaseFromKplerClassifyByClassifyType("subgrade4")
+	if err != nil {
+		return
+	}
+	for _, c := range classifyList {
+		c.ModifyTime = time.Now()
+	err = c.Update([]string{"ModifyTime"})
+	if err != nil {
+		fmt.Println("更新开普勒产品库失败", err)
+		utils.FileLog.Info("更新开普勒产品库失败", err)
+	}
+		// 每个分类都发起分组请求
+	libResp, er := getKplerProductLib(libUrl, &models.KplerProductLibReq{
+		// AncestorFamilyIds: familyIds,
+		// AncestorFamilyNames: familyNames,
+		// AncestorGroupIds: groupIds,
+		// AncestorGroupNames: groupNames,
+		// AncestorProductIds: productIds,
+		// AncestorProductNames: productNames,
+		AncestorGradeIds: strconv.Itoa(c.ProductId),
+		AncestorGradeNames: c.ProductName,
+	})
+	if er != nil {
+		fmt.Println("获取开普勒产品库失败", er)
+		utils.FileLog.Info("获取开普勒产品库失败", er)
+		continue
+	}
+	if libResp.Ret != 200 {
+		fmt.Println("获取开普勒产品库失败", libResp.ErrMsg)
+		utils.FileLog.Info("获取开普勒产品库失败", libResp.ErrMsg)
+		continue
+	}
+	classifyObj := new(models.BaseFromKplerClassify)
+	for _, v := range libResp.Data {
+		id, _ := strconv.Atoi(v.Id)
+		if id == 0 {
+			continue
+		}
+		if id == c.ProductId {
+			continue
+		}
+		// 更新对应的分类等级和父级
+		// 查找所有子分类
+
+		classifyItem, er := classifyObj.GetByProductId(id)
+		if er != nil {
+			err = fmt.Errorf("子分类不存在 Err:%s", er)
+			continue
+		}
+		
+		classifyItem.Level = c.Level + 1
+		classifyItem.ParentId = c.ClassifyId
+		classifyItem.ClassifyType = "subgrade5"
+		classifyItem.ModifyTime = time.Now()
+		err = classifyItem.Update([]string{"Level", "ParentId", "ClassifyType", "ModifyTime"})
+		if err != nil {
+			fmt.Println("更新开普勒产品库失败", err)
+			utils.FileLog.Info("更新开普勒产品库失败", err)
+			}
+		}
+		
+	}
+	
+	fmt.Println("classifyList:", classifyList)
+	
+	return
+}
+
 
 func InitKplerZone() (err error) {
 	libUrl := "http://127.0.0.1:8915"