hongze %!s(int64=3) %!d(string=hai) anos
pai
achega
abd74434da
Modificáronse 3 ficheiros con 129 adicións e 69 borrados
  1. 42 1
      models/data_manage/edb_data_lz.go
  2. 85 67
      services/data_source_longzhong.go
  3. 2 1
      services/task.go

+ 42 - 1
models/data_manage/edb_data_lz.go

@@ -9,12 +9,45 @@ import (
 	"time"
 )
 
-
 type lzSurveyData struct {
 	DataTime   string `orm:"column(data_time)" description:"日期"`
 	InputValue string `orm:"column(input_value)" description:"值"`
 }
 
+type LongzhongSurveyData struct {
+	SurveyDataId         int `orm:"column(survey_data_id);pk"`
+	SurveyProductId      int
+	ProjectQuotaId       int64
+	BreedId              string
+	BreedName            string
+	QuotaId              string
+	QuotaName            string
+	UnitId               string
+	UnitName             string
+	SampleType           int64
+	SampleId             string
+	SampleName           string
+	DeviceId             string
+	Device               string
+	ProductCraftId       string
+	ProductCraft         string
+	ProductLine          string
+	InputMode            int64
+	Frequency            int64
+	InputValue           string
+	TaskShouldFinishTime int64
+	CustomId             string
+	CustomType           int64
+	Custom               string
+	QuotaSampleId        int64
+	TaskActualFinishTime int64
+	AreaName             string
+	ProvinceName         string
+	ResearchStartData    int64
+	ResearchStopData     int64
+	DataTime             string
+}
+
 func GetLzSurveyDataByTradeCode(condition string, pars []interface{}) (item []*lzSurveyData, err error) {
 	sql := ` SELECT  a.* FROM longzhong_survey_data AS a
 				INNER JOIN longzhong_survey_product AS b ON a.survey_product_id=b.survey_product_id
@@ -29,6 +62,14 @@ func GetLzSurveyDataByTradeCode(condition string, pars []interface{}) (item []*l
 	return
 }
 
+func GetLzSurveyDataExistByTradeCode(surveyProductId int) (items []*LongzhongSurveyData, err error) {
+	sql := ` SELECT  * FROM longzhong_survey_data WHERE 1=1 AND survey_product_id=? `
+	o := orm.NewOrm()
+	o.Using("edb")
+	_, err = o.Raw(sql, surveyProductId).QueryRows(&items)
+	return
+}
+
 func GetEdbDataLzByCodeAndDate(edbCode string, startDate string) (count int, err error) {
 	o := orm.NewOrm()
 	o.Using("data")

+ 85 - 67
services/data_source_longzhong.go

@@ -7,12 +7,14 @@ import (
 	"fmt"
 	"github.com/tealeg/xlsx"
 	"hongze/hongze_task/models"
+	"hongze/hongze_task/models/data_manage"
 	"hongze/hongze_task/utils"
 	"net/url"
 	"os"
 	"path/filepath"
 	"rdluck_tools/http"
 	"strconv"
+	"strings"
 	"time"
 )
 
@@ -1077,18 +1079,29 @@ func GetLzSurveyProductData(cont context.Context) (err error) {
 	paramMap["passWord"] = password
 	paramMap["pageSize"] = 100
 
-	startDate := time.Now().AddDate(-4, 0, 0).UnixNano() / 1e6
+	//startDate := time.Now().AddDate(-4, 0, 0).UnixNano() / 1e6
+	startDateTime,_ := time.Parse(utils.FormatDate,"2018-01-01")
+	startDate := startDateTime.UnixNano()/1e6
 	endDate := time.Now().UnixNano() / 1e6
 
 	for k, v := range priceInfoList {
-		pageIndex := 1
-		if v.EndDate != "" {
-			endDateTime, _ := time.Parse(utils.FormatDate, v.EndDate)
-			startDate = endDateTime.UnixNano() / 1e6
+		existList, err := data_manage.GetLzSurveyDataExistByTradeCode(v.SurveyProductId)
+		existMap := make(map[string]string)
+		for _, v := range existList {
+			existMap[v.DataTime] = v.InputValue
 		}
+		fmt.Println(existMap)
+		pageIndex := 1
+		//if v.EndDate != "" {
+		//	endDateTime, _ := time.Parse(utils.FormatDate, v.EndDate)
+		//	startDate = endDateTime.UnixNano() / 1e6
+		//}
 		if startDate < 0 {
 			startDate = time.Now().AddDate(-4, 0, 0).UnixNano() / 1e6
 		}
+		fmt.Println("startDate:" + time.Now().AddDate(-4, 0, 0).Format(utils.FormatDate))
+		fmt.Println("endDate:" + time.Now().Format(utils.FormatDate))
+
 		for {
 			paramMap["pageNum"] = pageIndex
 			paramMap["projectQuotaId"] = v.ProjectQuotaId
@@ -1102,79 +1115,84 @@ func GetLzSurveyProductData(cont context.Context) (err error) {
 			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))
-			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 !strings.Contains(string(body), "<html>") {
+				utils.FileLogLz.Info("GetLzSurveyProductData:" + string(body))
+				dataList := new(models.LzSurveyData)
+				err = json.Unmarshal(body, &dataList)
 				if err != nil {
-					msg = "获取隆众调研指标数据失败:err " + err.Error()
+					msg = "获取隆众调研指标数据失败:Unmarshal Err " + err.Error() + " ;body:" + string(body)
 					return err
 				}
-				if count <= 0 {
-					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
+				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
+					//}
+					if _, ok := existMap[shouldDateTimeStr]; !ok {
+						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:", shouldDateTimeStr)
 					}
 				}
-			}
-			pageIndex = pageIndex + 1
-			totalPage := utils.PageCount(int(dataList.Response.Total), 100)
-			if pageIndex > totalPage {
-				break
+				pageIndex = pageIndex + 1
+				totalPage := utils.PageCount(int(dataList.Response.Total), 100)
+				if pageIndex > totalPage {
+					break
+				}
 			}
 		}
 

+ 2 - 1
services/task.go

@@ -55,6 +55,7 @@ func Task() {
 	//GetLzProductDetail()
 	//LzExportExcel()
 	//GetLzProductList()GetLzProductDetail
+
 	fmt.Println("task end")
 }
 
@@ -157,7 +158,7 @@ func RefreshData(cont context.Context) (err error) {
 	//有色
 	go data.RefreshDataFromYs(&wg)
 	wg.Wait()
-	
+
 	data.RefreshDataFromCalculateAll()
 	time.Sleep(2 * time.Second)
 	data.RefreshNotice()