package services

import (
	"encoding/json"
	"errors"
	"fmt"
	"github.com/tealeg/xlsx"
	"hongze/hongze_task/models"
	"hongze/hongze_task/utils"
	"net/url"
	"os"
	"path/filepath"
	"rdluck_tools/http"
	"strconv"
	"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() (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() (err error) {
	utils.FileLog.Info("start GetLzProductDetail")
	defer func() {
		if err != nil {
			fmt.Println("Err:" + err.Error())
		}
	}()

	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++ {
			fmt.Println("page:", i, totalPage)
			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.FileLog.Info("%s", param.Encode())
			body, err := http.Post(postUrl, param.Encode())
			if err != nil {
				fmt.Println("err:", err.Error())
				return err
			}
			fmt.Println(string(body))
			utils.FileLog.Info("%s", string(body))
			resp := new(models.LzProductInfoDetailResp)
			err = json.Unmarshal(body, &resp)
			if err != nil {
				fmt.Println("Unmarshal Err:" + 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 {
						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
					}
				}
			}
			totalPage = resp.Page
			//time.Sleep(1 * time.Second)
		}
	}
	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() (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() (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
	}
}
