package services import ( "context" "encoding/json" "errors" "fmt" "github.com/tealeg/xlsx" "hongze/hongze_task/models" "hongze/hongze_task/utils" "net/url" "os" "path/filepath" "github.com/rdlucklib/rdluck_tools/http" "strconv" "strings" "sync" "time" ) //获取隆众信息表中指标 func GetLongZhongTargetInfoByExcel() { var err error defer func() { if err != nil { fmt.Println("err:" + err.Error()) go utils.SendEmail("获取隆众指标信息失败"+time.Now().Format("2006-01-02 15:04:05"), ";Err:"+err.Error(), utils.EmailSendToUsers) return } }() excelPath := `./static/longzhong_0824.xlsx` file, err := xlsx.OpenFile(excelPath) if err != nil { fmt.Println("err:", err.Error()) return } sheets := file.Sheets for k, v := range sheets { fmt.Println(k, v.Name) classify, err := models.GetEdbdataClassifyByClassifyName(v.Name) if err != nil { if err.Error() == utils.ErrNoRow() { err = errors.New("分类不存在:Err:" + err.Error()) } else { err = err } return } if classify == nil { err = errors.New("分类不存在,请重新核对") return } classifyId := classify.ClassifyId classifyName := classify.ClassifyName fmt.Println(classifyId, classifyName) maxRow := v.MaxRow //maxCol := v.MaxCol fmt.Println(classifyName, maxRow) for i := 0; i < maxRow; i++ { row := v.Row(i) longzhongClassify := "" if i > 0 { cells := row.Cells longzhongClassifyNew := cells[0].String() if longzhongClassifyNew != "" { longzhongClassify = longzhongClassifyNew } secName := cells[1].Value frequency := cells[2].Value LongzhongId := cells[3].Value remark := cells[4].String() item := new(models.DataSourceLongzhong) item.ClassifyId = classifyId item.ClassifyName = classifyName item.SecName = secName item.Frequency = frequency item.LongzhongId = LongzhongId item.Remark = remark item.LongzhongClassify = longzhongClassify _, err = models.AddDataSourceLongzhong(item) if err != nil { fmt.Println("Err:", err.Error()) return } } } } } func GetLzProductList(cont context.Context) (err error) { utils.FileLog.Info("start GetLzProductList") var bodyStr string defer func() { if err != nil { fmt.Println("Err:" + err.Error()) go utils.SendEmail("获取隆众指标数据失败"+time.Now().Format("2006-01-02 15:04:05"), ";Err:"+err.Error()+";body:"+bodyStr, utils.EmailSendToUsers) } }() postUrl := `http://info.oilchem.net/cust/interfacenew/getProductUnitListNew` username := "hzmd" password := "HZMD2020" timestamp := fmt.Sprintf("%d", time.Now().Unix()) fmt.Println(timestamp) token := utils.MD5(username + timestamp + utils.MD5(password)) fmt.Println("token:", token) param := url.Values{} param.Add("username", username) param.Add("password", password) param.Add("timestamp", timestamp) param.Add("token", token) utils.FileLog.Info("%s", param.Encode()) body, err := http.Post(postUrl, param.Encode()) if err != nil { fmt.Println("err:", err.Error()) return } result := string(body) if result == "" { err = errors.New("返回内容为空") return } resp := new(models.LzProductInfoResp) err = json.Unmarshal(body, &resp) if err != nil { bodyStr = string(body) fmt.Println("json.Unmarshal Err:" + err.Error()) return } if resp.Code != "1" { fmt.Println("获取信息失败") err = errors.New("Code:" + resp.Code + ";Msg:" + resp.Msg) return } fmt.Println("len") fmt.Println(len(resp.Data)) utils.FileLog.Info("%s", string(body)) for _, v := range resp.Data { fmt.Println(v.Unitid, v.ProName, v.ProUnitName) var classifyId int var classifyName string classify, err := models.GetEdbdataClassifyByClassifyName(v.ProName) if err != nil && err.Error() != utils.ErrNoRow() { fmt.Println("获取分类失败,Err:" + err.Error()) return err } if classify == nil || (err != nil && err.Error() == utils.ErrNoRow()) { classifyItem := new(models.EdbdataClassify) classifyItem.ClassifyName = v.ProName classifyItem.ParentId = 4 classifyItem.CreateTime = time.Now() newId, err := models.AddEdbdataClassify(classifyItem) if err != nil { fmt.Println("新增分类失败,Err:" + err.Error()) return err } classifyId = int(newId) classifyName = v.ProName } else { classifyId = classify.ClassifyId classifyName = classify.ClassifyName } count, err := models.GetLongzhonginfoCount(classifyId, v.Unitid) if err != nil { fmt.Println("判断指标是否存在失败,Err:" + err.Error()) return err } secNameCount, _ := models.GetLongzhonginfoBySecNameCount(v.ProUnitName) if count > 0 || secNameCount > 0 { //修改 lzProduct := new(models.Longzhonginfo) lzProduct.SecName = v.ProUnitName lzProduct.Frequency = v.LzType lzProduct.ClassifyId = classifyId lzProduct.ClassifyName = classifyName lzProduct.Unit = v.Unit lzProduct.CreateTime = time.Now() lzProduct.IsNormal = v.IsNormal lzProduct.LastModifyDate = v.Maxdate lzProduct.Unitid = v.Unitid lzProduct.TempId = v.Tempid lzProduct.TempName = v.TempName lzProduct.ModifyTime = time.Now() err = models.ModifyLongzhonginfo(lzProduct) if err != nil { fmt.Println("修改失败,Err:" + err.Error()) return err } } else { //新增 maxId, err := models.GetLongzhonginfoMaxId() if err != nil { fmt.Println("获取编码失败,err:" + err.Error()) return err } tradeCode := fmt.Sprintf("L%06d", maxId+1) lzProduct := new(models.Longzhonginfo) lzProduct.TradeCode = tradeCode lzProduct.SecName = v.ProUnitName lzProduct.Frequency = v.LzType lzProduct.ClassifyId = classifyId lzProduct.ClassifyName = classifyName lzProduct.Unit = v.Unit lzProduct.CreateTime = time.Now() if v.IsNormal == "" { v.IsNormal = "1" } lzProduct.IsNormal = v.IsNormal lzProduct.LastModifyDate = v.Maxdate lzProduct.Unitid = v.Unitid lzProduct.TempId = v.Tempid lzProduct.TempName = v.TempName lzProduct.ModifyTime = time.Now() _, err = models.AddLongzhonginfo(lzProduct) if err != nil { fmt.Println("新增指标信息失败") return err } } } return } func GetLzProductDetail(cont context.Context) (err error) { utils.FileLogLz.Info("start GetLzProductDetail") defer func() { if err != nil { utils.FileLogLz.Info("Err:" + err.Error()) go utils.SendEmail(utils.APPNAME+"【"+utils.RunMode+"】"+"GetLzProductDetail:失败提醒", "ErrMsg:"+err.Error(), utils.EmailSendToUsers) } }() startDate := time.Now().AddDate(0, -1, 0).Format(utils.FormatDate) endDate := time.Now().AddDate(0, 0, 0).Format(utils.FormatDate) time.Sleep(5 * time.Second) pageSize := 30 postUrl := `http://info.oilchem.net/cust/interfacenew/getProductUnitDetailNew` username := "hzmd" password := "HZMD2020" infoList, err := models.GetLongzhonginfoList() if err != nil { return } for _, v := range infoList { totalPage := 10 for i := 1; i <= totalPage; i++ { displayTime, _ := models.GetDisplayTime(v.LongzhonginfoId) if displayTime != "" { startDate = displayTime } fmt.Println("page:", i, totalPage) utils.FileLogLz.Info("Page:%s", strconv.Itoa(i)) timestamp := fmt.Sprintf("%d", time.Now().Unix()) token := utils.MD5(username + timestamp + utils.MD5(password)) param := url.Values{} param.Add("username", username) param.Add("password", password) param.Add("timestamp", timestamp) param.Add("token", token) param.Add("id", strconv.Itoa(v.Unitid)) param.Add("startDate", startDate) param.Add("endDate", endDate) param.Add("pagesize", strconv.Itoa(pageSize)) param.Add("page", strconv.Itoa(i)) utils.FileLogLz.Info("GetLzProductDetail Param: %s", param.Encode()) body, err := http.Post(postUrl, param.Encode()) if err != nil { utils.FileLogLz.Info("Post Err:%s", err.Error()) return err } utils.FileLogLz.Info("GetLzProductDetail Result: %s", string(body)) resp := new(models.LzProductInfoDetailResp) err = json.Unmarshal(body, &resp) if err != nil { utils.FileLogLz.Info("Unmarshal Err:%s", err.Error()) return err } if resp.Code != "1" { fmt.Println("获取信息失败") err = errors.New("Code:" + resp.Code + ";Msg:" + resp.Msg) return err } for k, v := range resp.Data { fmt.Println(k, v.UnitId, v.ProCnName) target, err := models.GetLongzhonginfoByUnitId(v.ProCnName, v.UnitId) if err != nil { if err.Error() != utils.ErrNoRow() { fmt.Println("获取target失败,err:" + err.Error()) continue } fmt.Println("err:" + err.Error()) return err } if target == nil { fmt.Println("target is empty:"+v.ProCnName, v.UnitId) continue } count, err := models.GetLongzhongdataCount(target.LongzhonginfoId, v.DataTime) if err != nil { fmt.Println("GetLongzhongdataCount Err:" + err.Error()) return err } if count > 0 { lzdetail := new(models.Longzhongdata) lzdetail.CreateTime = time.Now() lzdetail.ModifyTime = time.Now() lzdetail.TradeCode = target.TradeCode lzdetail.LongzhonginfoId = target.LongzhonginfoId lzdetail.Dt = v.DataTime lzdetail.Close = v.UnitValue lzdetail.UnitDesc = v.UnitDesc lzdetail.UpdTime = v.UpdTime lzdetail.AddTime = v.AddTime lzdetail.DisplayTime = v.DisplayTime err = models.ModifyLongzhongdata(lzdetail) if err != nil { utils.FileLogLz.Info("ModifyLongzhongdata Err:" + err.Error()) continue } } else { lzdetail := new(models.Longzhongdata) lzdetail.CreateTime = time.Now() lzdetail.ModifyTime = time.Now() lzdetail.TradeCode = target.TradeCode lzdetail.LongzhonginfoId = target.LongzhonginfoId lzdetail.Dt = v.DataTime lzdetail.Close = v.UnitValue lzdetail.UnitDesc = v.UnitDesc lzdetail.UpdTime = v.UpdTime lzdetail.AddTime = v.AddTime lzdetail.DisplayTime = v.DisplayTime err = models.AddLongzhongdata(lzdetail) if err != nil { utils.FileLogLz.Info("AddLongzhongdata Err:" + err.Error()) continue } } } totalPage = resp.Page //time.Sleep(1 * time.Second) } } utils.FileLogLz.Info("end GetLzProductDetail") return } //获取历史隆众石化数据 func GetHistoryLzProductDetail() (err error) { utils.FileLog.Info("start GetHistoryLzProductDetail") var msg string defer func() { if err != nil { fmt.Println("Err:" + err.Error()) } if msg != "" { fmt.Println("msg:" + msg) } }() infoList, err := models.GetLongzhonginfoList() if err != nil { msg = "获取指标列表失败,Err:" + err.Error() return } for ik, iv := range infoList { fmt.Println(ik, iv.SecName, iv.LongzhonginfoId) startDate := "2020-01-01" //time.Now().AddDate(0, 0, 0).Format(utils.FormatDate) endDate := "2020-12-31" //time.Now().AddDate(0, 0, 0).Format(utils.FormatDate) //time.Sleep(1 * time.Second) pageSize := 30 postUrl := `http://info.oilchem.net/cust/interfacenew/getProductUnitDetailNew` username := "hzmd" password := "HZMD2020" totalPage := 10 for i := 1; i <= totalPage; i++ { fmt.Println("page:", i) timestamp := fmt.Sprintf("%d", time.Now().Unix()) token := utils.MD5(username + timestamp + utils.MD5(password)) param := url.Values{} param.Add("username", username) param.Add("password", password) param.Add("timestamp", timestamp) param.Add("token", token) param.Add("id", strconv.Itoa(iv.Unitid)) param.Add("startDate", startDate) param.Add("endDate", endDate) if i > 1 { param.Add("page", strconv.Itoa(i)) } param.Add("pagesize", strconv.Itoa(pageSize)) utils.FileLog.Info("%s", param.Encode()) fmt.Println("post url:", postUrl) fmt.Println("post params:", param.Encode()) body, err := http.Post(postUrl, param.Encode()) if err != nil { fmt.Println("err:", err.Error()) return err } utils.FileLog.Info("%s", string(body)) fmt.Println("body:", string(body)) resp := new(models.LzProductInfoDetailResp) err = json.Unmarshal(body, &resp) if err != nil { fmt.Println("Unmarshal Err:" + err.Error()) fmt.Println("body:", string(body)) return err } if resp.Code != "1" { fmt.Println("获取信息失败") err = errors.New("Code:" + resp.Code + ";Msg:" + resp.Msg) return err } if i == totalPage && resp.Pagesize >= pageSize { totalPage = totalPage + 5 } for k, v := range resp.Data { fmt.Println(k, v.UnitId, v.ProCnName) target, err := models.GetLongzhonginfoByUnitId(v.ProCnName, v.UnitId) if err != nil { if err.Error() != utils.ErrNoRow() { fmt.Println("获取target失败,err:" + err.Error()) continue } fmt.Println("err:" + err.Error()) return err } if target == nil { fmt.Println("target is empty:"+v.ProCnName, v.UnitId) continue } count, err := models.GetLongzhongdataCount(target.LongzhonginfoId, v.DataTime) if err != nil { fmt.Println("GetLongzhongdataCount Err:" + err.Error()) return err } if count > 0 { fmt.Println("数据已存在:", iv.Unitid, iv.SecName, v.UnitValue, v.DataTime) lzdetail := new(models.Longzhongdata) lzdetail.CreateTime = time.Now() lzdetail.ModifyTime = time.Now() lzdetail.TradeCode = target.TradeCode lzdetail.LongzhonginfoId = target.LongzhonginfoId lzdetail.Dt = v.DataTime lzdetail.Close = v.UnitValue lzdetail.UnitDesc = v.UnitDesc lzdetail.UpdTime = v.UpdTime lzdetail.AddTime = v.AddTime lzdetail.DisplayTime = v.DisplayTime err = models.ModifyLongzhongdata(lzdetail) if err != nil { fmt.Println("ModifyLongzhongdata Err:" + err.Error()) return err } } else { lzdetail := new(models.Longzhongdata) lzdetail.CreateTime = time.Now() lzdetail.ModifyTime = time.Now() lzdetail.TradeCode = target.TradeCode lzdetail.LongzhonginfoId = target.LongzhonginfoId lzdetail.Dt = v.DataTime lzdetail.Close = v.UnitValue lzdetail.UnitDesc = v.UnitDesc lzdetail.UpdTime = v.UpdTime lzdetail.AddTime = v.AddTime lzdetail.DisplayTime = v.DisplayTime err = models.AddLongzhongdata(lzdetail) if err != nil { fmt.Println("AddLongzhongdata Err:" + err.Error()) return err } } } if i == 1 && len(resp.Data) == 0 { utils.FileLog.Info("%d", iv.Unitid) } if resp.Pagesize < pageSize || len(resp.Data) == 0 { break } //time.Sleep(1 * time.Second) } err = models.ModifyLongzhonginfoIsNormal(iv.LongzhonginfoId) if err != nil { fmt.Println("ModifyLongzhonginfoIsNormal:Err" + err.Error()) } fmt.Println("sleep", iv.SecName) time.Sleep(1 * time.Second) } return } func GetLzPrice() { fmt.Println("start") /*getUrl:=`https://openapi.oilchem.net/price/getProduct?password=HZMD2020&type=guoji&username=hzmd` result,err:=http.Get(getUrl) fmt.Println(string(result),err) utils.FileLog.Info("%s",string(result))*/ getUrl := `https://openapi.oilchem.net/price/getPrice?password=HZMD2020&type=guoji&username=hzmd&inStartDate=2020-08-20&inEndDate=2020-08-26` result, err := http.Get(getUrl) fmt.Println(string(result), err) utils.FileLog.Info("%s", string(result)) fmt.Println("end") } func LzExportExcel() { var err error defer func() { if err != nil { fmt.Println("err:" + err.Error()) } }() parentId := 4 classifyList, err := models.GetEdbdataClassifyByParentId(parentId) 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.ClassifyId, v.ClassifyName) //获取指标 secNameList, err := models.GetLongzhonginfoByClassifyId(v.ClassifyId) if err != nil { fmt.Println("获取数据失败,Err:" + err.Error()) return } if len(secNameList) <= 0 { continue } sheetNew, err := xlsxFile.AddSheet(v.ClassifyName) if err != nil { fmt.Println("新增Sheet失败", err.Error()) return } //获取指标数据 windRow := sheetNew.AddRow() secNameRow := sheetNew.AddRow() frequencyRow := sheetNew.AddRow() unitRow := sheetNew.AddRow() lastModifyDateRow := sheetNew.AddRow() //获取分类下指标最大数据量 dataMax, err := models.GetLongzhongDataMaxCount(v.ClassifyId) if err != nil { fmt.Println("获取指标最大数据量失败", err.Error()) return } for _, sv := range secNameList { fmt.Println("line 399:", v.ClassifyId, v.ClassifyName, sv.SecName, sv.Frequency) windRow.AddCell().SetValue("Wind") secNameRow.AddCell().SetValue("指标名称") frequencyRow.AddCell().SetValue("频率") unitRow.AddCell().SetValue("单位") lastModifyDateRow.AddCell().SetValue("更新时间") secNameRow.AddCell().SetValue(sv.SecName) frequencyRow.AddCell().SetValue(sv.Frequency) unitRow.AddCell().SetValue(sv.Unit) lastModifyDateRow.AddCell().SetValue(sv.LastModifyDate) windRow.AddCell() windRow.AddCell() secNameRow.AddCell() frequencyRow.AddCell() unitRow.AddCell() lastModifyDateRow.AddCell() //获取数据 dataList, err := models.GetLongzhongDataById(sv.LongzhonginfoId) if err != nil { fmt.Println("GetLongzhongDataById Err:" + err.Error()) return } if len(dataList) <= 0 { for n := 0; n < dataMax; n++ { rowIndex := 5 + n row := sheetNew.Row(rowIndex) row.AddCell() row.AddCell() row.AddCell() } } else { endRowIndex := 0 for rk, dv := range dataList { rowIndex := 5 + rk row := sheetNew.Row(rowIndex) row.AddCell().SetValue(dv.Dt) row.AddCell().SetFloat(dv.Close) row.AddCell() endRowIndex = rowIndex } if len(dataList) < dataMax { dataLen := dataMax - len(dataList) for n := 0; n < dataLen; n++ { rowIndex := (endRowIndex + 1) + n row := sheetNew.Row(rowIndex) row.AddCell() row.AddCell() row.AddCell() } } } } } err = xlsxFile.Save(downLoadnFilePath) fmt.Print("downLoadnFilePath:", downLoadnFilePath) if err != nil { return } } func GetLzProductPriceProduct(cont context.Context) (err error) { var msg string defer func() { if err != nil { fmt.Println("Err:" + err.Error()) go utils.SendEmail("获取隆众价格指标失败"+time.Now().Format("2006-01-02 15:04:05"), ";Err:"+err.Error(), utils.EmailSendToUsers) } if msg != "" { fmt.Println("msg:", msg) go utils.SendEmail("获取隆众价格指标失败"+time.Now().Format("2006-01-02 15:04:05"), ";msg:"+msg, utils.EmailSendToUsers) } }() //chuchang, guonei, guoji, oil ,lingshou,guojioil,base,国内出厂,国内市场,国际市场,国内成品油批发价,国内成品油零售价,国际原油,基准价 typeMap := make(map[string]string) typeMap["chuchang"] = "国内出厂" typeMap["guonei"] = "国内市场" typeMap["guoji"] = "国际市场" typeMap["oil"] = "国内成品油批发价" typeMap["lingshou"] = "国内成品油零售价" typeMap["guojioil"] = "国际原油" typeMap["base"] = "基准价" username := "hzmd" password := "HZMD2020" for k, v := range typeMap { fmt.Println(k, v) getUrl := `https://openapi.oilchem.net/price/getProduct?password=` + password + `&type=` + k + `&username=` + username + `` body, err := http.Get(getUrl) if err != nil { msg = "获取隆众价格指标失败:Err" + err.Error() return err } 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 { 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 { count, err := models.GetLongzhongpriceinfoCount(n.Id) if err != nil { msg = "判断隆众价格指标是否存在失败:err " + err.Error() return err } item := new(models.Longzhongpriceinfo) item.Standard = n.Standard item.ModelName = n.ModelName item.Unit = n.Unit item.AreaName = n.AreaName item.PriceType = n.PriceType item.Memo = n.Memo item.PriceId = n.Id item.ProductName = n.ProductName item.InfoType = k item.InfoTypeRemark = v item.MarketName = n.MarketName item.ManufactureName = n.ManufactureName if count > 0 { fmt.Println("存在", n.ProductName, n.Id) //修改 err = models.ModifyLongzhongpriceinfo(item) if err != nil { msg = "修改隆众价格指标失败:err " + err.Error() return err } } else { _, err = models.AddLongzhongpriceinfo(item) if err != nil { msg = "新增隆众价格指标失败:err " + err.Error() return err } } } time.Sleep(31 * time.Second) } return } func GetLzProductPriceData(cont context.Context) (err error) { var msg string defer func() { if err != nil { fmt.Println("Err:" + err.Error()) go utils.SendEmail("获取隆众价格指标失败"+time.Now().Format("2006-01-02 15:04:05"), ";Err:"+err.Error(), utils.EmailSendToUsers) } 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) } }() priceInfoList, err := models.GetLongzhongpriceinfo() if err != nil { msg = "获取价格指标失败,Err:" + err.Error() return } username := "hzmd" password := "HZMD2020" startDate := time.Now().AddDate(0, 0, -7).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=` + startDate + `&inEndDate=` + endDate + `&id=` + v.PriceId body, err := http.Get(getUrl) if err != nil { msg = "获取隆众价格数据失败:Err" + err.Error() return err } fmt.Println("getUrl:", getUrl) fmt.Println("body", string(body)) utils.FileLog.Info("%s", 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 { count, err := models.GetLongzhongpricedataCount(v.LongzhongpriceinfoId, 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 = 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() return err } } } } 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() marketRow := sheetNew.AddRow() manufactureRow := 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("地区") marketRow.AddCell().SetValue("市场名称") manufactureRow.AddCell().SetValue("厂家名称") secNameRow.AddCell().SetValue(sv.InfoTypeRemark) frequencyRow.AddCell().SetValue(sv.PriceType) unitRow.AddCell().SetValue(sv.Unit) lastModifyDateRow.AddCell().SetValue(sv.AreaName) marketRow.AddCell().SetValue(sv.MarketName) manufactureRow.AddCell().SetValue(sv.ManufactureName) windRow.AddCell() windRow.AddCell() windRow.AddCell() windRow.AddCell() secNameRow.AddCell() frequencyRow.AddCell() unitRow.AddCell() lastModifyDateRow.AddCell() marketRow.AddCell() manufactureRow.AddCell() secNameRow.AddCell() frequencyRow.AddCell() unitRow.AddCell() lastModifyDateRow.AddCell() marketRow.AddCell() manufactureRow.AddCell() secNameRow.AddCell() frequencyRow.AddCell() unitRow.AddCell() lastModifyDateRow.AddCell() marketRow.AddCell() manufactureRow.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 := 8 + n row := sheetNew.Row(rowIndex) row.AddCell() row.AddCell() row.AddCell() row.AddCell() row.AddCell() } } else { endRowIndex := 0 for rk, dv := range dataList { rowIndex := 8 + rk row := sheetNew.Row(rowIndex) 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 := sheetNew.Row(rowIndex) row.AddCell() row.AddCell() row.AddCell() row.AddCell() row.AddCell() } } } } } err = xlsxFile.Save(downLoadnFilePath) fmt.Print("downLoadnFilePath:", downLoadnFilePath) if err != nil { return } } //隆众-调研指标 func GetLzSurveyProduct(cont context.Context) (err error) { utils.FileLogLz.Info("GetLzSurveyProduct start:" + time.Now().Format(utils.FormatDateTime)) var msg string defer func() { if err != nil { fmt.Println("Err:" + err.Error()) go utils.SendEmail("获取隆众调研指标失败"+time.Now().Format("2006-01-02 15:04:05"), ";Err:"+err.Error(), utils.EmailSendToUsers) } if msg != "" { fmt.Println("msg:", msg) go utils.SendEmail("获取隆众调研指标失败"+time.Now().Format("2006-01-02 15:04:05"), ";msg:"+msg, utils.EmailSendToUsers) } }() username := "hzmd" password := "HZMD2020" pageIndex := 0 for { pageIndex++ getUrl := `https://openapi.oilchem.net/research/getProduct?passWord=` + password + `&userName=` + username + `&pageSize=100&pageNum=` + strconv.Itoa(pageIndex) utils.FileLogLz.Info("GetLzSurveyProduct getUrl:" + getUrl) body, err := http.Get(getUrl) if err != nil { msg = "获取隆众调研指标失败:Err" + err.Error() return err } utils.FileLogLz.Info("GetLzSurveyProduct:" + string(body)) infoItem := new(models.SurveyProduct) err = json.Unmarshal(body, &infoItem) if err != nil { msg = "获取隆众调研指标失败:Unmarshal Err " + err.Error() + " ;body:" + string(body) return err } if infoItem.Status != "200" { msg = "获取隆众调研指标失败:body " + string(body) return nil } for _, n := range infoItem.Response.List { count, err := models.GetLongzhongSurveyProductCount(n.QuotaSampleID) if err != nil { msg = "判断隆众调研指标是否存在失败:err " + err.Error() return err } item := new(models.LongzhongSurveyProduct) item.ProjectQuotaId = n.ProjectQuotaID item.BreedId = n.BreedID item.BreedName = n.BreedName item.QuotaId = n.QuotaID item.QuotaName = n.QuotaName item.UnitId = n.UnitID item.UnitName = n.UnitName item.SampleType = n.SampleType item.SampleId = n.SampleID item.SampleName = n.SampleName item.DeviceId = n.DeviceID item.Device = n.Device item.ProductCraftId = n.ProductCraftID item.ProductCraft = n.ProductCraft item.ProductLine = n.ProductLine item.InputMode = n.InputMode item.Frequency = n.Frequency item.CustomId = n.CustomID item.CustomType = n.CustomType item.Custom = n.Custom item.QuotaSampleId = n.QuotaSampleID item.ModifyTime = time.Now() if count > 0 { fmt.Println("存在", n.QuotaName, n.QuotaSampleID) //修改 err = models.ModifLongzhongSurveyProduct(item) if err != nil { msg = "修改隆众价格指标失败:err " + err.Error() return err } existItem, err := models.GetLongzhongSurveyProductItem(n.QuotaSampleID) if err != nil { msg = "获取已存在隆众指标信息失败:err " + err.Error() return err } if existItem != nil { surveyProductId := existItem.SurveyProductId lzCode := fmt.Sprintf("lz%04d", surveyProductId) models.ModifLongzhongSurveyProductCode(lzCode, surveyProductId) } } else { item.StartDate = "2018-01-01" item.EndDate = "2018-01-01" lastId, err := models.AddLongzhongSurveyProduct(item) if err != nil { msg = "新增隆众价格指标失败:err " + err.Error() return err } lzCode := fmt.Sprintf("lz%04d", lastId) models.ModifLongzhongSurveyProductCode(lzCode, int(lastId)) } } totalPage := utils.PageCount(int(infoItem.Response.Total), 100) if pageIndex > totalPage { break } } return } var lzLock sync.Mutex //隆众-调研指标数据 func GetLzSurveyProductData(cont context.Context) (err error) { lzLock.Lock() utils.FileLogLz.Info("GetLzSurveyProductData start:" + time.Now().Format(utils.FormatDateTime)) go utils.SendEmail("隆众数据开始获取"+time.Now().Format("2006-01-02 15:04:05"), time.Now().Format("2006-01-02 15:04:05"), utils.EmailSendToUsers) var msg string defer func() { go utils.SendEmail("隆众数据获取完成"+time.Now().Format("2006-01-02 15:04:05"), ";msg:"+msg, utils.EmailSendToUsers) if err != nil { fmt.Println("Err:" + err.Error()) go utils.SendEmail("获取隆众调研指标数据失败"+time.Now().Format("2006-01-02 15:04:05"), ";Err:"+err.Error(), utils.EmailSendToUsers) } 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) } }() priceInfoList, err := models.GetLongzhongSurveyList() if err != nil { msg = "获取隆众调研指标数据失败,Err:" + err.Error() return } username := "hzmd" password := "HZMD2020" paramMap := make(map[string]interface{}) paramMap["userName"] = username paramMap["passWord"] = password paramMap["pageSize"] = 100 startDate := time.Now().AddDate(-4, 0, 0).UnixNano() / 1e6 //startDateTime, _ := time.Parse(utils.FormatDate, "2021-12-01") //startDate := startDateTime.UnixNano() / 1e6 endDate := time.Now().AddDate(utils.DATA_END_DATE_LIMIT, 0, 0).UnixNano() / 1e6 for k, v := range priceInfoList { //existList, err := data_manage.GetLzSurveyDataExistByTradeCode(v.SurveyProductId) //existMap := make(map[string]*data_manage.LongzhongSurveyData) //for _, v := range existList { // key := strconv.Itoa(int(v.QuotaSampleId)) + v.DataTime // existMap[key] = v //} //fmt.Println(existMap) pageIndex := 1 //if v.EndDate != "" { // endDateTime, _ := time.Parse(utils.FormatDate, v.EndDate) // startDate = endDateTime.UnixNano() / 1e6 //} if v.EndDate != "" { endDateTime, _ := time.Parse(utils.FormatDate, v.EndDate) startDate = endDateTime.AddDate(0, -8, 0).UnixNano() / 1e6 } if startDate < 0 { startDate = time.Now().AddDate(0, -8, 0).UnixNano() / 1e6 } fmt.Println("endDate:" + time.Now().Format(utils.FormatDate)) for { paramMap["pageNum"] = pageIndex paramMap["projectQuotaId"] = v.ProjectQuotaId paramMap["quotaSampleId"] = v.QuotaSampleId paramMap["researchStartDateTime"] = startDate paramMap["researchEndDateTime"] = endDate fmt.Println(k, v) getUrl := `https://openapi.oilchem.net/research/productPage` postData, err := json.Marshal(paramMap) if err != nil { fmt.Println("postData Err:", err.Error()) } fmt.Println("getUrl:" + getUrl) utils.FileLogLz.Info("GetLzSurveyProductData postData:" + string(postData)) body, err := http.Post(getUrl, string(postData), "application/json") if err != nil { msg = "获取隆众调研指标数据失败:Err" + err.Error() return err } utils.FileLogLz.Info("GetLzSurveyProductData:" + string(body)) fmt.Println("result:" + string(body)) if !strings.Contains(string(body), "") { dataList := new(models.LzSurveyData) err = json.Unmarshal(body, &dataList) if err != nil { msg = "获取隆众调研指标数据失败:Unmarshal Err " + err.Error() + " ;body:" + string(body) return err } if dataList.Status != "200" { msg = "获取隆众调研指标数据失败:body " + string(body) return nil } for _, n := range dataList.Response.List { //dateTime := time.Unix(n.TaskActualFinishTime/1000, 0) //dateTimeStr := dateTime.Format(utils.FormatDate) shouldDateTime := time.Unix(n.TaskShouldFinishTime/1000, 0) shouldDateTimeStr := shouldDateTime.Format(utils.FormatDate) //fmt.Println("QuotaSampleID:", n.QuotaSampleID, "taskActualFinishTime:", dateTimeStr, "taskShouldFinishTime:", shouldDateTimeStr) count, err := models.GetLzSurveyDataCount(v.SurveyProductId, int(v.QuotaSampleId), shouldDateTimeStr) if err != nil { msg = "获取隆众调研指标数据失败:err " + err.Error() return err } key := strconv.Itoa(int(v.QuotaSampleId)) + shouldDateTimeStr if count <= 0 { fmt.Println("not exist:" + key) item := new(models.LongzhongSurveyData) item.SurveyProductId = v.SurveyProductId item.ProjectQuotaId = n.ProjectQuotaID item.BreedId = n.BreedID item.BreedName = n.BreedName item.QuotaId = n.QuotaID item.QuotaName = n.QuotaName item.UnitId = n.UnitID item.UnitName = n.UnitName item.SampleType = n.SampleType item.SampleId = n.SampleID item.SampleName = n.SampleName item.DeviceId = n.DeviceID item.Device = n.Device item.ProductCraftId = n.ProductCraftID item.ProductCraft = n.ProductCraft item.ProductLine = n.ProductLine item.InputMode = n.InputMode item.Frequency = n.Frequency item.InputValue = n.InputValue item.TaskShouldFinishTime = n.TaskShouldFinishTime item.CustomId = n.CustomID item.CustomType = n.CustomType item.Custom = n.Custom item.QuotaSampleId = n.QuotaSampleID item.TaskActualFinishTime = n.TaskActualFinishTime //item.AreaName = n.AreaName.(string) //item.ProvinceName = n.ProvinceName.(string) item.ResearchStartData = n.ResearchStartDate item.ResearchStopData = n.ResearchStopDate item.DataTime = shouldDateTimeStr err = models.AddLongzhongSurveyData(item) if err != nil { msg = "新增调研指标数据失败:err " + err.Error() return err } } else { fmt.Println("exist:" + key) err = models.ModifyLzSurveyDataV1(n.InputValue, int(n.QuotaSampleID), shouldDateTimeStr) if err != nil { fmt.Println("ModifyLzSurveyDataV1 Err:" + err.Error()) } } //key:=strconv.Itoa(int(v.QuotaSampleId))+shouldDateTimeStr //if findItem, ok := existMap[key]; !ok && n.InputValue != "0" { // fmt.Println("not exist:"+key) // item := new(models.LongzhongSurveyData) // item.SurveyProductId = v.SurveyProductId // item.ProjectQuotaId = n.ProjectQuotaID // item.BreedId = n.BreedID // item.BreedName = n.BreedName // item.QuotaId = n.QuotaID // item.QuotaName = n.QuotaName // item.UnitId = n.UnitID // item.UnitName = n.UnitName // item.SampleType = n.SampleType // item.SampleId = n.SampleID // item.SampleName = n.SampleName // item.DeviceId = n.DeviceID // item.Device = n.Device // item.ProductCraftId = n.ProductCraftID // item.ProductCraft = n.ProductCraft // item.ProductLine = n.ProductLine // item.InputMode = n.InputMode // item.Frequency = n.Frequency // item.InputValue = n.InputValue // item.TaskShouldFinishTime = n.TaskShouldFinishTime // item.CustomId = n.CustomID // item.CustomType = n.CustomType // item.Custom = n.Custom // item.QuotaSampleId = n.QuotaSampleID // item.TaskActualFinishTime = n.TaskActualFinishTime // //item.AreaName = n.AreaName.(string) // //item.ProvinceName = n.ProvinceName.(string) // item.ResearchStartData = n.ResearchStartDate // item.ResearchStopData = n.ResearchStopDate // item.DataTime = shouldDateTimeStr // err = models.AddLongzhongSurveyData(item) // if err != nil { // msg = "新增调研指标数据失败:err " + err.Error() // return err // } //} else { // fmt.Println("exist:"+key) // if findItem != nil && findItem.InputValue != n.InputValue && n.InputValue != "0" { // models.ModifyLzSurveyData(n.InputValue, findItem.SurveyDataId) // } // fmt.Println("exist:", shouldDateTimeStr) //} } pageIndex = pageIndex + 1 totalPage := utils.PageCount(int(dataList.Response.Total), 100) if pageIndex > totalPage { break } } } item, err := models.GetLzSurveyMaxAndMinInfo(v.SurveyProductId, int(v.QuotaSampleId)) if err != nil { return err } if item != nil && item.MaxDate != "" && item.MinDate != "" { err = models.ModifyLzSurveyMaxAndMinInfo(item, v.SurveyProductId) if err != nil { return err } } time.Sleep(2 * time.Second) } lzLock.Unlock() fmt.Println("GetLzSurveyProductData start:" + time.Now().Format(utils.FormatDateTime)) utils.FileLogLz.Info("GetLzSurveyProductData start:" + time.Now().Format(utils.FormatDateTime)) return }