rdluck 4 年之前
父节点
当前提交
8cb79405c8
共有 6 个文件被更改,包括 238 次插入20 次删除
  1. 38 0
      models/data_source_longzhong.go
  2. 12 0
      models/edbdata_classify.go
  3. 9 0
      models/longzhong.go
  4. 171 18
      services/data_source_longzhong.go
  5. 8 2
      services/task.go
  6. 二进制
      static/longzhong_0824.xlsx

+ 38 - 0
models/data_source_longzhong.go

@@ -328,3 +328,41 @@ type LzPriceDataResp struct {
 	Code int            `json:"code"`
 	Data []*LzPriceData `json:"data"`
 }
+
+func GetLongzhongPriceDataMaxCount(productName string) (count int, err error) {
+	o := orm.NewOrm()
+	o.Using("edb")
+	sql := `SELECT MAX(t.num) AS count FROM (
+				SELECT COUNT(1) AS num  FROM longzhongpriceinfo AS a
+				INNER JOIN longzhongpricedata AS b ON a.longzhongpriceinfo_id=b.longzhongpriceinfo_id
+				WHERE a.product_name=?
+				GROUP BY a.product_name
+			)AS t `
+	err = o.Raw(sql, productName).QueryRow(&count)
+	return
+}
+
+type LongzhongpricedataItems struct {
+	LongzhongpricedataId int `orm:"column(longzhongpricedata_id);pk"`
+	LongzhongpriceinfoId int
+	PriceDate            string
+	Memo                 string
+	Price                float64
+	CnyPrice             float64
+	ZsyPrice             float64
+	ZshPrice             float64
+	LowPrice             float64
+	HighPrice            float64
+	RisePrice            float64
+	TonPrice             float64
+	PriceType            string
+	UpdateDate           string
+}
+
+func GetLongzhongPriceDataById(lzPriceInfoId int) (items []*LongzhongpricedataItems, err error) {
+	o := orm.NewOrm()
+	o.Using("edb")
+	sql := `SELECT * FROM longzhongpricedata WHERE longzhongpriceinfo_id=? ORDER BY price_date DESC `
+	_, err = o.Raw(sql, lzPriceInfoId).QueryRows(&items)
+	return
+}

+ 12 - 0
models/edbdata_classify.go

@@ -34,4 +34,16 @@ func GetEdbdataClassifyByParentId(parentId int) (items []*EdbdataClassify, err e
 	o.Using("edb")
 	o.Raw(sql, parentId).QueryRows(&items)
 	return
+}
+
+type LzPriceClassify struct {
+	ProductName string
+}
+
+func GetLzPriceClassify() (items []*LzPriceClassify, err error) {
+	sql := ` SELECT product_name  FROM longzhongpriceinfo GROUP BY product_name ORDER BY product_name DESC `
+	o := orm.NewOrm()
+	o.Using("edb")
+	o.Raw(sql).QueryRows(&items)
+	return
 }

+ 9 - 0
models/longzhong.go

@@ -9,3 +9,12 @@ func GetLongzhonginfoByClassifyId(classifyId int) (items []*Longzhonginfo, err e
 	_, err = o.Raw(sql, classifyId).QueryRows(&items)
 	return
 }
+
+
+func GetLongzhongpriceinfoByClassifyName(productName string) (items []*Longzhongpriceinfo, err error) {
+	sql := `SELECT * FROM longzhongpriceinfo WHERE product_name=? ORDER BY longzhongpriceinfo_id ASC `
+	o := orm.NewOrm()
+	o.Using("edb")
+	_, err = o.Raw(sql, productName).QueryRows(&items)
+	return
+}

+ 171 - 18
services/data_source_longzhong.go

@@ -655,6 +655,7 @@ func GetLzProductPriceProduct() (err error) {
 		}
 		fmt.Println("getUrl:", getUrl)
 		fmt.Println("body", string(body))
+		utils.FileLog.Info("%s", string(body))
 		infoList := new(models.LzPriceInfoResp)
 		err = json.Unmarshal(body, &infoList)
 		if err != nil {
@@ -711,16 +712,18 @@ func GetLzProductPriceData() (err error) {
 		}
 	}()
 
-	priceInfoList,err:=models.GetLongzhongpriceinfo()
-	if err!=nil {
-		msg="获取价格指标失败,Err:"+err.Error()
+	priceInfoList, err := models.GetLongzhongpriceinfo()
+	if err != nil {
+		msg = "获取价格指标失败,Err:" + err.Error()
 		return
 	}
 	username := "hzmd"
 	password := "HZMD2020"
+	startDate := time.Now().AddDate(0, 0, 0).Format(utils.FormatDate)
+	endDate := time.Now().AddDate(0, 0, 0).Format(utils.FormatDate)
 	for k, v := range priceInfoList {
 		fmt.Println(k, v)
-		getUrl := `https://openapi.oilchem.net/price/getPrice?password=` + password + `&type=` + v.InfoType + `&username=` + username + `&inStartDate=2020-01-01&inEndDate=2020-12-31&id=`+v.PriceId
+		getUrl := `https://openapi.oilchem.net/price/getPrice?password=` + password + `&type=` + v.InfoType + `&username=` + username + `&inStartDate=` + startDate + `&inEndDate=` + endDate + `&id=` + v.PriceId
 		body, err := http.Get(getUrl)
 		if err != nil {
 			msg = "获取隆众价格数据失败:Err" + err.Error()
@@ -748,19 +751,19 @@ func GetLzProductPriceData() (err error) {
 				fmt.Println("存在", n.PriceDate, n.Id)
 			} else {
 				item := new(models.Longzhongpricedata)
-				item.LongzhongpriceinfoId=v.LongzhongpriceinfoId
-				item.PriceDate=n.PriceDate
-				item.Memo=n.Memo
-				item.Price=n.Price
-				item.CnyPrice=n.CnyPrice
-				item.ZsyPrice=n.ZsyPrice
-				item.ZshPrice=n.ZshPrice
-				item.LowPrice=n.LowPrice
-				item.HighPrice=n.HighPrice
-				item.RisePrice=n.RisePrice
-				item.TonPrice=n.TonPrice
-				item.PriceType=n.PriceType
-				item.UpdateDate=n.UpdateDate
+				item.LongzhongpriceinfoId = v.LongzhongpriceinfoId
+				item.PriceDate = n.PriceDate
+				item.Memo = n.Memo
+				item.Price = n.Price
+				item.CnyPrice = n.CnyPrice
+				item.ZsyPrice = n.ZsyPrice
+				item.ZshPrice = n.ZshPrice
+				item.LowPrice = n.LowPrice
+				item.HighPrice = n.HighPrice
+				item.RisePrice = n.RisePrice
+				item.TonPrice = n.TonPrice
+				item.PriceType = n.PriceType
+				item.UpdateDate = n.UpdateDate
 				_, err = models.AddLongzhongpricedata(item)
 				if err != nil {
 					msg = "新增隆众价格数据失败:err " + err.Error()
@@ -770,4 +773,154 @@ func GetLzProductPriceData() (err error) {
 		}
 	}
 	return
-}
+}
+
+func LzPriceExportExcel() {
+	var err error
+	defer func() {
+		if err != nil {
+			fmt.Println("err:" + err.Error())
+		}
+	}()
+	classifyList, err := models.GetLzPriceClassify()
+	if err != nil {
+		return
+	}
+	xlsxFile := xlsx.NewFile()
+	if err != nil {
+		fmt.Println("生成文件失败" + err.Error())
+		return
+	}
+	dir, _ := os.Executable()
+	exPath := filepath.Dir(dir)
+	downLoadnFilePath := exPath + "/static/" + time.Now().Format(utils.FormatDateTimeUnSpace) + ".xlsx"
+	fmt.Println("downLoadnFilePath")
+	fmt.Println(downLoadnFilePath)
+	for _, v := range classifyList {
+		fmt.Println(v.ProductName)
+		//获取指标
+		secNameList, err := models.GetLongzhongpriceinfoByClassifyName(v.ProductName)
+		if err != nil {
+			fmt.Println("获取数据失败,Err:" + err.Error())
+			return
+		}
+		if len(secNameList) <= 0 {
+			continue
+		}
+		sheetNew, err := xlsxFile.AddSheet(v.ProductName)
+		if err != nil {
+			fmt.Println("新增Sheet失败", err.Error())
+			return
+		}
+		//获取指标数据
+		windRow := sheetNew.AddRow()
+		secNameRow := sheetNew.AddRow()
+		frequencyRow := sheetNew.AddRow()
+		unitRow := sheetNew.AddRow()
+		lastModifyDateRow := sheetNew.AddRow()
+		titleRow := sheetNew.AddRow()
+		//获取分类下指标最大数据量
+		dataMax, err := models.GetLongzhongPriceDataMaxCount(v.ProductName)
+		if err != nil {
+			fmt.Println("获取指标最大数据量失败", err.Error())
+			return
+		}
+		for _, sv := range secNameList {
+			fmt.Println("line 399:", v.ProductName, sv.InfoTypeRemark, sv.AreaName)
+
+			windRow.AddCell().SetString("Wind")
+			secNameRow.AddCell().SetValue("分类")
+			frequencyRow.AddCell().SetValue("价格类型")
+			unitRow.AddCell().SetValue("单位")
+			lastModifyDateRow.AddCell().SetValue("地区")
+
+			secNameRow.AddCell().SetValue(sv.InfoTypeRemark)
+			frequencyRow.AddCell().SetValue(sv.PriceType)
+			unitRow.AddCell().SetValue(sv.Unit)
+			lastModifyDateRow.AddCell().SetValue(sv.AreaName)
+
+			windRow.AddCell()
+			windRow.AddCell()
+			windRow.AddCell()
+			windRow.AddCell()
+
+			secNameRow.AddCell()
+			frequencyRow.AddCell()
+			unitRow.AddCell()
+			lastModifyDateRow.AddCell()
+			secNameRow.AddCell()
+			frequencyRow.AddCell()
+			unitRow.AddCell()
+			lastModifyDateRow.AddCell()
+			secNameRow.AddCell()
+			frequencyRow.AddCell()
+			unitRow.AddCell()
+			lastModifyDateRow.AddCell()
+			titleRow.AddCell().SetValue("价格日期")
+			titleRow.AddCell().SetValue("价格")
+			titleRow.AddCell().SetValue("低端价格")
+			titleRow.AddCell().SetValue("高端价格")
+			titleRow.AddCell()
+			//获取数据
+			dataList, err := models.GetLongzhongPriceDataById(sv.LongzhongpriceinfoId)
+			if err != nil {
+				fmt.Println("GetLongzhongDataById Err:" + err.Error())
+				return
+			}
+
+			if len(dataList) <= 0 {
+				for n := 0; n < dataMax; n++ {
+					rowIndex := 6 + n
+					row, err := sheetNew.Row(rowIndex)
+					if err != nil {
+						fmt.Println("add row err:" + err.Error())
+						return
+					}
+					row.AddCell()
+					row.AddCell()
+					row.AddCell()
+					row.AddCell()
+					row.AddCell()
+				}
+			} else {
+				endRowIndex := 0
+				for rk, dv := range dataList {
+					rowIndex := 6 + rk
+					row, err := sheetNew.Row(rowIndex)
+					if err != nil {
+						fmt.Println("add row err:" + err.Error())
+						return
+					}
+					row.AddCell().SetValue(dv.PriceDate)
+					row.AddCell().SetFloat(dv.Price)
+					row.AddCell().SetFloat(dv.LowPrice)
+					row.AddCell().SetFloat(dv.HighPrice)
+
+					row.AddCell()
+					endRowIndex = rowIndex
+				}
+				if len(dataList) < dataMax {
+					dataLen := dataMax - len(dataList)
+					for n := 0; n < dataLen; n++ {
+						rowIndex := (endRowIndex + 1) + n
+						row, err := sheetNew.Row(rowIndex)
+						if err != nil {
+							fmt.Println("add row err:" + err.Error())
+							return
+						}
+						row.AddCell()
+						row.AddCell()
+						row.AddCell()
+						row.AddCell()
+						row.AddCell()
+					}
+				}
+			}
+		}
+	}
+	err = xlsxFile.Save(downLoadnFilePath)
+	fmt.Print("downLoadnFilePath:", downLoadnFilePath)
+	if err != nil {
+		return
+	}
+}

+ 8 - 2
services/task.go

@@ -21,6 +21,13 @@ func Task123() {
 	getLzProductDetail := toolbox.NewTask("getLzProductDetail", "0 5 11-19/1 * * * ", GetLzProductDetail)
 	toolbox.AddTask("getLzProductDetail", getLzProductDetail)
 
+	//隆众价格指标获取
+	getLzProductPriceProduct := toolbox.NewTask("getLzProductPriceProduct", "0 5 11-19/1 * * * ", GetLzProductPriceProduct)
+	toolbox.AddTask("getLzProductPriceProduct", getLzProductPriceProduct)
+	//隆众价格指标数据获取
+	getLzProductPriceData := toolbox.NewTask("getLzProductPriceData", "0 10 11-19/1 * * * ", GetLzProductPriceData)
+	toolbox.AddTask("getLzProductPriceData", getLzProductPriceData)
+
 	toolbox.StartTask()
 	//GetHistoryLzProductDetail()
 	//GetLzPrice()
@@ -32,8 +39,7 @@ func Task123() {
 
 func Task() {
 	fmt.Println("start")
-	//GetLzProductPriceProduct()
-	GetLzProductPriceData()
+	GetLzProductPriceProduct()
 	fmt.Println("end")
 }
 

二进制
static/longzhong_0824.xlsx