Commit ae69a017 by gongyang

修改获取

parent 9e3743ba
Showing with 85 additions and 54 deletions
package main package main
import ( import (
"bytes"
"flag" "flag"
"fmt" "fmt"
"github.com/hprose/hprose-golang/rpc"
"github.com/ichunt2019/cfg/lib"
"github.com/tidwall/gjson"
"golang-asynctask/app/dao/scm_data_dao" "golang-asynctask/app/dao/scm_data_dao"
"io"
"io/ioutil" "io/ioutil"
"mime/multipart"
"net/http" "net/http"
"runtime" "runtime"
"strconv" "strconv"
"strings" "strings"
"sync" "sync"
"time" "time"
"github.com/hprose/hprose-golang/rpc"
"github.com/ichunt2019/cfg/lib"
"github.com/tidwall/gjson"
) )
var ( var (
...@@ -23,7 +27,7 @@ var ( ...@@ -23,7 +27,7 @@ var (
currentUnix int64 currentUnix int64
) )
func init(){ func init() {
var ( var (
configPath string configPath string
logPath string logPath string
...@@ -32,14 +36,12 @@ func init(){ ...@@ -32,14 +36,12 @@ func init(){
flag.StringVar(&logPath, "logdir", "./logs/", "日志文件存储目录") flag.StringVar(&logPath, "logdir", "./logs/", "日志文件存储目录")
flag.Parse() flag.Parse()
err := lib.Init(configPath) err := lib.Init(configPath)
if err != nil{ if err != nil {
panic(err) panic(err)
} }
return return
} }
func main() { func main() {
runtime.GOMAXPROCS(2) runtime.GOMAXPROCS(2)
...@@ -48,9 +50,8 @@ func main() { ...@@ -48,9 +50,8 @@ func main() {
//请求数据 //请求数据
currentUnix = time.Now().Unix() currentUnix = time.Now().Unix()
timeLocal,_ := time.LoadLocation("Local") timeLocal, _ := time.LoadLocation("Local")
timeStart,_ = time.ParseInLocation("2006-01-02",currentDate,timeLocal) timeStart, _ = time.ParseInLocation("2006-01-02", currentDate, timeLocal)
//获取当前时间 //获取当前时间
fmt.Println(currentDate) fmt.Println(currentDate)
...@@ -72,7 +73,6 @@ func main_back() { ...@@ -72,7 +73,6 @@ func main_back() {
sqlDb.Exec("delete from lie_btv_daily_top_search where `datetime` = '2021-05-31'") sqlDb.Exec("delete from lie_btv_daily_top_search where `datetime` = '2021-05-31'")
} }
func GetTotalStatistic() { func GetTotalStatistic() {
defer wg.Done() defer wg.Done()
...@@ -80,8 +80,8 @@ func GetTotalStatistic() { ...@@ -80,8 +80,8 @@ func GetTotalStatistic() {
jsonStr := RequestTableInfo("totalStatistic") jsonStr := RequestTableInfo("totalStatistic")
//{"data": [{"materialQty": 0, "materialCount": 0, "materialTotalQty": 966724502}], "returnCode": "0000", "returnMsg": "\u6210\u529f"} //{"data": [{"materialQty": 0, "materialCount": 0, "materialTotalQty": 966724502}], "returnCode": "0000", "returnMsg": "\u6210\u529f"}
fmt.Println("totalStatistic"+jsonStr) fmt.Println("totalStatistic" + jsonStr)
if gjson.Get(jsonStr,"returnCode").String() != "0000" || gjson.Get(jsonStr, "data").String() == "[]" { if gjson.Get(jsonStr, "returnCode").String() != "0000" || gjson.Get(jsonStr, "data").String() == "[]" {
return return
} }
...@@ -98,20 +98,18 @@ func GetTotalStatistic() { ...@@ -98,20 +98,18 @@ func GetTotalStatistic() {
return true return true
}) })
//获取当前日元器件型号搜索量 //获取当前日元器件型号搜索量
//添加数据 //添加数据
_, err := sqlDb.Exec("insert into lie_btv_daily_order_transaction (datetime,daily_ic_num,daily_ic_sn_num,history_ic_search_xinum,current_sku_sum,daily_ic_search_num,daily_ic_search_xinum)" + _, err := sqlDb.Exec("insert into lie_btv_daily_order_transaction (datetime,daily_ic_num,daily_ic_sn_num,history_ic_search_xinum,current_sku_sum,daily_ic_search_num,daily_ic_search_xinum)"+
"value(?,?,?,?,?,?,?)",currentDate,erpKey["materialQty"],erpKey["materialCount"],erpKey["materialTotalQty"],GetCurrentSku(),currentSearch,currentSearch) "value(?,?,?,?,?,?,?)", currentDate, erpKey["materialQty"], erpKey["materialCount"], erpKey["materialTotalQty"], GetCurrentSku(), currentSearch, currentSearch)
if err != nil { if err != nil {
fmt.Println("getTotalStatistic_sql_"+err.Error()) fmt.Println("getTotalStatistic_sql_" + err.Error())
} }
} }
} }
func GetMaterialstatistic() { func GetMaterialstatistic() {
defer wg.Done() defer wg.Done()
...@@ -119,16 +117,15 @@ func GetMaterialstatistic() { ...@@ -119,16 +117,15 @@ func GetMaterialstatistic() {
jsonStr := RequestTableInfo("topStatistic") jsonStr := RequestTableInfo("topStatistic")
//{"data": [], "returnCode": "0000", "returnMsg": "\u6210\u529f"} //{"data": [], "returnCode": "0000", "returnMsg": "\u6210\u529f"}
fmt.Println("topStatistic json:"+jsonStr) fmt.Println("topStatistic json:" + jsonStr)
if gjson.Get(jsonStr,"returnCode").String() != "0000" || gjson.Get(jsonStr, "data").String() == "[]" { if gjson.Get(jsonStr, "returnCode").String() != "0000" || gjson.Get(jsonStr, "data").String() == "[]" {
return return
} }
sqlDb := scm_data_dao.GetDashboardDb() sqlDb := scm_data_dao.GetDashboardDb()
i:=1 i := 1
for _, res := range gjson.Get(jsonStr, "data").Array() { for _, res := range gjson.Get(jsonStr, "data").Array() {
erpKey := make(map[string]interface{}) erpKey := make(map[string]interface{})
...@@ -138,8 +135,8 @@ func GetMaterialstatistic() { ...@@ -138,8 +135,8 @@ func GetMaterialstatistic() {
}) })
//添加数据 //添加数据
_, err := sqlDb.Exec("insert into lie_btv_daily_top_order (sort,datetime,sn,order_num)" + _, err := sqlDb.Exec("insert into lie_btv_daily_top_order (sort,datetime,sn,order_num)"+
"value(?,?,?,?)",i,currentDate,erpKey["materialName"],erpKey["materialQty"]) "value(?,?,?,?)", i, currentDate, erpKey["materialName"], erpKey["materialQty"])
i++ i++
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)
...@@ -147,21 +144,19 @@ func GetMaterialstatistic() { ...@@ -147,21 +144,19 @@ func GetMaterialstatistic() {
} }
} }
// 获取搜索数据 request example http://so12.ichunt.com/search/SearchLog/aggs?start_time=1621785600&end_time=1621871999&limit=2
//
//获取搜索数据 request example http://so12.ichunt.com/search/SearchLog/aggs?start_time=1621785600&end_time=1621871999&limit=2
// start_time 开始时间戳 // start_time 开始时间戳
// end_time 结束时间戳 // end_time 结束时间戳
// limit 显示前几条 // limit 显示前几条
func TopSearch()int64 { func TopSearch() int64 {
//获取前两百条数据 //获取前两百条数据
requestUrl := "http://so12.ichunt.com/search/SearchLog/aggs?start_time="+strconv.FormatInt(timeStart.Unix(),10)+"&end_time="+strconv.FormatInt(currentUnix,10)+"&limit=200"
//requestUrl = "http://so12.ichunt.com/search/SearchLog/aggs?start_time=1621785600&end_time=1621871999&limit=200" jsonStr := PostTopSearchUrl()
jsonStr := RequestUrl(requestUrl)
//{"error_code":0,"error_msg":"","data":[{"goods_name":"1N5408","count":20},{"goods_name":"OPA356AQDBVRQ1","count":19}]} //{"error_code":0,"error_msg":"","data":[{"goods_name":"1N5408","count":20},{"goods_name":"OPA356AQDBVRQ1","count":19}]}
if gjson.Get(jsonStr,"error_code").String() != "0"{ if gjson.Get(jsonStr, "error_code").String() != "0" {
fmt.Println("获取搜索数据失败",jsonStr) fmt.Println("获取搜索数据失败", jsonStr)
return 0 return 0
} }
...@@ -169,8 +164,7 @@ func TopSearch()int64 { ...@@ -169,8 +164,7 @@ func TopSearch()int64 {
var allNum int64 var allNum int64
sqlDb := scm_data_dao.GetDashboardDb() sqlDb := scm_data_dao.GetDashboardDb()
i := 1
i:=1
for _, res := range gjson.Get(jsonStr, "data").Array() { for _, res := range gjson.Get(jsonStr, "data").Array() {
erpKey := make(map[string]interface{}) erpKey := make(map[string]interface{})
...@@ -178,15 +172,15 @@ func TopSearch()int64 { ...@@ -178,15 +172,15 @@ func TopSearch()int64 {
res.ForEach(func(key, value gjson.Result) bool { res.ForEach(func(key, value gjson.Result) bool {
erpKey[key.String()] = value.String() erpKey[key.String()] = value.String()
if key.String() == "count" { if key.String() == "count" {
allNum +=value.Int() allNum += value.Int()
} }
return true return true
}) })
if insertDataNum > 0{ if insertDataNum > 0 {
//添加数据 //添加数据
_, err := sqlDb.Exec("insert into lie_btv_daily_top_search (sort,datetime,sn,search_num)" + _, err := sqlDb.Exec("insert into lie_btv_daily_top_search (sort,datetime,sn,search_num)"+
"value(?,?,?,?)",i,currentDate,erpKey["goods_name"],erpKey["count"]) "value(?,?,?,?)", i, currentDate, erpKey["goods_name"], erpKey["count"])
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)
} }
...@@ -198,11 +192,11 @@ func TopSearch()int64 { ...@@ -198,11 +192,11 @@ func TopSearch()int64 {
return allNum return allNum
} }
func GetSearchData() string
//获取表信息,返回byte // 获取表信息,返回byte
//request example //http://119.23.228.186:50005/ getCustomsDockingData {"tableName":"SYS_OPERATE_LOG","bizDate":"2021-05-07"} // request example //http://119.23.228.186:50005/ getCustomsDockingData {"tableName":"SYS_OPERATE_LOG","bizDate":"2021-05-07"}
func RequestTableInfo(requestType string)string { func RequestTableInfo(requestType string) string {
type Sub struct { type Sub struct {
Get_materialstatistic func(string) (string, error) Get_materialstatistic func(string) (string, error)
...@@ -212,9 +206,9 @@ func RequestTableInfo(requestType string)string { ...@@ -212,9 +206,9 @@ func RequestTableInfo(requestType string)string {
client := rpc.NewHTTPClient("http://119.23.228.186:50005/") client := rpc.NewHTTPClient("http://119.23.228.186:50005/")
client.UseService(&sub) client.UseService(&sub)
result,err := sub.Get_materialstatistic(`{"date":"`+strings.Replace(currentDate, "-", "", -1)+`","requestType":"`+requestType+`"}`) result, err := sub.Get_materialstatistic(`{"date":"` + strings.Replace(currentDate, "-", "", -1) + `","requestType":"` + requestType + `"}`)
if err != nil { if err != nil {
fmt.Println(requestType+"请求服务器出错"+err.Error()) fmt.Println(requestType + "请求服务器出错" + err.Error())
return "" return ""
} }
...@@ -222,25 +216,22 @@ func RequestTableInfo(requestType string)string { ...@@ -222,25 +216,22 @@ func RequestTableInfo(requestType string)string {
return result return result
} }
func GetCurrentSku() string {
func GetCurrentSku()string { requestStr := "http://so12.ichunt.com/search/Es/searchSku?goods_status/condition=1"
requestStr :="http://so12.ichunt.com/search/Es/searchSku?goods_status/condition=1"
fmt.Println(requestStr) fmt.Println(requestStr)
requestResult := RequestUrl(requestStr) requestResult := RequestUrl(requestStr)
fmt.Println("sku总量json"+requestResult) fmt.Println("sku总量json" + requestResult)
return gjson.Get(requestResult,"data.total").String() return gjson.Get(requestResult, "data.total").String()
} }
func RequestUrl(url string) string {
func RequestUrl(url string)string {
//获取sku数量 http://soso12.ichunt.com/search/Es/searchSku?create_time/range=1620379867,1621489319&goods_status/condition=1 //获取sku数量 http://soso12.ichunt.com/search/Es/searchSku?create_time/range=1620379867,1621489319&goods_status/condition=1
///{"error_code":0,"error_msg":"","data":{"total":"1765","scroll_id":null,"goods_id":[]}} ///{"error_code":0,"error_msg":"","data":{"total":"1765","scroll_id":null,"goods_id":[]}}
//获取当日搜索前二十 //获取当日搜索前二十
res, err :=http.Get(url) res, err := http.Get(url)
if err != nil { if err != nil {
return "" return ""
} }
...@@ -251,3 +242,43 @@ func RequestUrl(url string)string { ...@@ -251,3 +242,43 @@ func RequestUrl(url string)string {
} }
return string(robots) return string(robots)
} }
func PostTopSearchUrl() string {
url := "https://icso.ichunt.com/search/SearchLog/aggs"
method := "POST"
payload := &bytes.Buffer{}
writer := multipart.NewWriter(payload)
_ = writer.WriteField("start_time", strconv.FormatInt(timeStart.Unix(), 10))
_ = writer.WriteField("end_time", strconv.FormatInt(currentUnix, 10))
_ = writer.WriteField("no_rule", "1122")
_ = writer.WriteField("limit", "200")
err := writer.Close()
if err != nil {
fmt.Println(err)
return ""
}
client := &http.Client{}
req, err := http.NewRequest(method, url, payload)
if err != nil {
fmt.Println(err)
return ""
}
req.Header.Set("Content-Type", writer.FormDataContentType())
res, err := client.Do(req)
if err != nil {
fmt.Println(err)
return ""
}
defer res.Body.Close()
body, err := io.ReadAll(res.Body)
if err != nil {
fmt.Println(err)
return ""
}
return string(body)
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment