rdluck 4 anni fa
parent
commit
dc3b41cd1e
3 ha cambiato i file con 131 aggiunte e 6 eliminazioni
  1. 64 0
      models/data_source_longzhong.go
  2. 1 0
      models/db.go
  3. 66 6
      services/data_source_longzhong.go

+ 64 - 0
models/data_source_longzhong.go

@@ -264,3 +264,67 @@ func GetLongzhongpriceinfoCount(unitid string) (count int, err error) {
 	err = o.Raw(sql, unitid).QueryRow(&count)
 	return
 }
+
+//判断指标数据是否已经录入
+func GetLongzhongpriceinfoByPriceId(unitid string) (item *Longzhongpriceinfo, err error) {
+	o := orm.NewOrm()
+	o.Using("edb")
+	sql := `SELECT * FROM longzhongpriceinfo WHERE price_id=? `
+	err = o.Raw(sql, unitid).QueryRow(&item)
+	return
+}
+
+type Longzhongpricedata 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            float64
+	UpdateDate           string
+}
+
+func AddLongzhongpricedata(item *Longzhongpricedata) (newId int64, err error) {
+	o := orm.NewOrm()
+	o.Using("edb")
+	newId, err = o.Insert(item)
+	return
+}
+
+//判断指标数据是否已经录入
+func GetLongzhongpricedataCount(unitid, priceDate string) (count int, err error) {
+	o := orm.NewOrm()
+	o.Using("edb")
+	sql := `SELECT COUNT(1) AS count FROM longzhongpricedata WHERE longzhongpriceinfo_id=? AND price_date=? `
+	err = o.Raw(sql, unitid, priceDate).QueryRow(&count)
+	return
+}
+
+type LzPriceData struct {
+	Id         string  `json:"id"`
+	PriceDate  string  `json:"priceDate"`
+	Memo       string  `json:"memo"`
+	Price      float64 `json:"price"`
+	CnyPrice   float64 `json:"cnyPrice"`
+	ZsyPrice   float64 `json:"zsyPrice"`
+	ZshPrice   float64 `json:"zshPrice"`
+	LowPrice   float64 `json:"lowPrice"`
+	HighPrice  float64 `json:"highPrice"`
+	RisePrice  float64 `json:"risePrice"`
+	TonPrice   float64 `json:"tonPrice"`
+	PriceType  float64 `json:"priceType"`
+	UpdateDate string  `json:"updateDate"`
+}
+
+type LzPriceDataResp struct {
+	Msg  string         `json:"msg"`
+	Code int            `json:"code"`
+	Data []*LzPriceData `json:"data"`
+}

+ 1 - 0
models/db.go

@@ -43,5 +43,6 @@ func init() {
 		new(Longzhongdata),
 		new(EdbdataClassify),
 		new(Longzhongpriceinfo),
+		new(Longzhongpricedata),
 	)
 }

+ 66 - 6
services/data_source_longzhong.go

@@ -707,6 +707,7 @@ func GetLzProductPriceData() (err error) {
 		if msg != "" {
 			fmt.Println("msg:", msg)
 			go utils.SendEmail("获取隆众价格指标失败"+time.Now().Format("2006-01-02 15:04:05"), ";msg:"+msg, utils.EmailSendToUsers)
+			utils.FileLog.Info("%s", msg)
 		}
 	}()
 
@@ -723,13 +724,72 @@ func GetLzProductPriceData() (err error) {
 	password := "HZMD2020"
 	for k, v := range typeMap {
 		fmt.Println(k, v)
-		getUrl := `https://openapi.oilchem.net/price/getPrice?password=` + password + `&type=` + k + `&username=` + username + `&inStartDate=2020-08-31&inEndDate=2020-09-02`
+		getUrl := `https://openapi.oilchem.net/price/getPrice?password=` + password + `&type=` + k + `&username=` + username + `&inStartDate=2017-01-01&inEndDate=2017-01-07`
 		body, err := http.Get(getUrl)
-		fmt.Println("body")
-		fmt.Println(err)
-		fmt.Println(string(body))
-		fmt.Println("sleep:")
-		time.Sleep(31 * time.Second)
+		if err != nil {
+			msg = "获取隆众价格数据失败:Err" + err.Error()
+			return err
+		}
+		fmt.Println("getUrl:", getUrl)
+		fmt.Println("body", string(body))
+		infoList := new(models.LzPriceDataResp)
+		err = json.Unmarshal(body, &infoList)
+		if err != nil {
+			msg = "获取隆众价格数据失败:Unmarshal Err " + err.Error() + " ;body:" + string(body)
+			return err
+		}
+		if infoList.Code != 1 {
+			msg = "获取隆众价格数据失败:body " + string(body)
+			return nil
+		}
+		for _, n := range infoList.Data {
+			infoItem, err := models.GetLongzhongpriceinfoByPriceId(n.Id)
+			if err != nil {
+				if err.Error() == utils.ErrNoRow() {
+					fmt.Println("指标不存在:id:" + n.Id)
+					msg = "指标不存在:id:" + n.Id
+					return
+				} else {
+					fmt.Println("获取指标失败:id:" + n.Id)
+					msg = "获取指标失败:id:" + n.Id
+					return
+				}
+			}
+			if infoItem == nil {
+				fmt.Println("指标不存在:id:" + n.Id)
+				msg = "指标不存在:id:" + n.Id
+				return
+			}
+			count, err := models.GetLongzhongpricedataCount(n.Id, n.PriceDate)
+			if err != nil {
+				msg = "获取隆众价格数据失败:err " + err.Error()
+				return err
+			}
+			if count > 0 {
+				fmt.Println("存在", n.PriceDate, n.Id)
+			} else {
+				item := new(models.Longzhongpricedata)
+				item.LongzhongpriceinfoId=infoItem.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()
+					return err
+				}
+			}
+		}
+
 	}
 	return
 }