Commit ae69a017 by gongyang

修改获取

parent 9e3743ba
Showing with 96 additions and 66 deletions
package main
import (
"bytes"
"flag"
"fmt"
"github.com/hprose/hprose-golang/rpc"
"github.com/ichunt2019/cfg/lib"
"github.com/tidwall/gjson"
"golang-asynctask/app/dao/scm_data_dao"
"io"
"io/ioutil"
"mime/multipart"
"net/http"
"runtime"
"strconv"
"strings"
"sync"
"time"
"github.com/hprose/hprose-golang/rpc"
"github.com/ichunt2019/cfg/lib"
"github.com/tidwall/gjson"
)
var (
wg sync.WaitGroup
wg sync.WaitGroup
currentDate string
timeStart time.Time
timeStart time.Time
currentUnix int64
)
func init(){
func init() {
var (
configPath string
logPath string
logPath string
)
flag.StringVar(&configPath, "config", "./config/dev/", "配置文件")
flag.StringVar(&logPath, "logdir", "./logs/", "日志文件存储目录")
flag.Parse()
err := lib.Init(configPath)
if err != nil{
if err != nil {
panic(err)
}
return
}
func main() {
func main() {
runtime.GOMAXPROCS(2)
......@@ -48,9 +50,8 @@ func main() {
//请求数据
currentUnix = time.Now().Unix()
timeLocal,_ := time.LoadLocation("Local")
timeStart,_ = time.ParseInLocation("2006-01-02",currentDate,timeLocal)
timeLocal, _ := time.LoadLocation("Local")
timeStart, _ = time.ParseInLocation("2006-01-02", currentDate, timeLocal)
//获取当前时间
fmt.Println(currentDate)
......@@ -65,23 +66,22 @@ func main() {
wg.Wait()
}
func main_back() {
func main_back() {
sqlDb := scm_data_dao.GetDashboardDb()
sqlDb.Exec("delete from lie_btv_daily_order_transaction where `datetime` = '2021-05-31'")
sqlDb.Exec("delete from lie_btv_daily_top_order 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()
jsonStr := RequestTableInfo("totalStatistic")
//{"data": [{"materialQty": 0, "materialCount": 0, "materialTotalQty": 966724502}], "returnCode": "0000", "returnMsg": "\u6210\u529f"}
fmt.Println("totalStatistic"+jsonStr)
if gjson.Get(jsonStr,"returnCode").String() != "0000" || gjson.Get(jsonStr, "data").String() == "[]" {
fmt.Println("totalStatistic" + jsonStr)
if gjson.Get(jsonStr, "returnCode").String() != "0000" || gjson.Get(jsonStr, "data").String() == "[]" {
return
}
......@@ -98,37 +98,34 @@ func GetTotalStatistic() {
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)" +
"value(?,?,?,?,?,?,?)",currentDate,erpKey["materialQty"],erpKey["materialCount"],erpKey["materialTotalQty"],GetCurrentSku(),currentSearch,currentSearch)
_, 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)
if err != nil {
fmt.Println("getTotalStatistic_sql_"+err.Error())
fmt.Println("getTotalStatistic_sql_" + err.Error())
}
}
}
func GetMaterialstatistic() {
func GetMaterialstatistic() {
defer wg.Done()
jsonStr := RequestTableInfo("topStatistic")
//{"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
}
sqlDb := scm_data_dao.GetDashboardDb()
i:=1
i := 1
for _, res := range gjson.Get(jsonStr, "data").Array() {
erpKey := make(map[string]interface{})
......@@ -138,8 +135,8 @@ func GetMaterialstatistic() {
})
//添加数据
_, err := sqlDb.Exec("insert into lie_btv_daily_top_order (sort,datetime,sn,order_num)" +
"value(?,?,?,?)",i,currentDate,erpKey["materialName"],erpKey["materialQty"])
_, err := sqlDb.Exec("insert into lie_btv_daily_top_order (sort,datetime,sn,order_num)"+
"value(?,?,?,?)", i, currentDate, erpKey["materialName"], erpKey["materialQty"])
i++
if err != nil {
fmt.Println(err)
......@@ -147,21 +144,19 @@ func GetMaterialstatistic() {
}
}
//获取搜索数据 request example http://so12.ichunt.com/search/SearchLog/aggs?start_time=1621785600&end_time=1621871999&limit=2
// start_time 开始时间戳
// end_time 结束时间戳
// limit 显示前几条
func TopSearch()int64 {
// 获取搜索数据 request example http://so12.ichunt.com/search/SearchLog/aggs?start_time=1621785600&end_time=1621871999&limit=2
//
// start_time 开始时间戳
// end_time 结束时间戳
// limit 显示前几条
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 := RequestUrl(requestUrl)
jsonStr := PostTopSearchUrl()
//{"error_code":0,"error_msg":"","data":[{"goods_name":"1N5408","count":20},{"goods_name":"OPA356AQDBVRQ1","count":19}]}
if gjson.Get(jsonStr,"error_code").String() != "0"{
fmt.Println("获取搜索数据失败",jsonStr)
if gjson.Get(jsonStr, "error_code").String() != "0" {
fmt.Println("获取搜索数据失败", jsonStr)
return 0
}
......@@ -169,8 +164,7 @@ func TopSearch()int64 {
var allNum int64
sqlDb := scm_data_dao.GetDashboardDb()
i:=1
i := 1
for _, res := range gjson.Get(jsonStr, "data").Array() {
erpKey := make(map[string]interface{})
......@@ -178,15 +172,15 @@ func TopSearch()int64 {
res.ForEach(func(key, value gjson.Result) bool {
erpKey[key.String()] = value.String()
if key.String() == "count" {
allNum +=value.Int()
allNum += value.Int()
}
return true
})
if insertDataNum > 0{
if insertDataNum > 0 {
//添加数据
_, err := sqlDb.Exec("insert into lie_btv_daily_top_search (sort,datetime,sn,search_num)" +
"value(?,?,?,?)",i,currentDate,erpKey["goods_name"],erpKey["count"])
_, err := sqlDb.Exec("insert into lie_btv_daily_top_search (sort,datetime,sn,search_num)"+
"value(?,?,?,?)", i, currentDate, erpKey["goods_name"], erpKey["count"])
if err != nil {
fmt.Println(err)
}
......@@ -198,11 +192,11 @@ func TopSearch()int64 {
return allNum
}
func GetSearchData() string
//获取表信息,返回byte
//request example //http://119.23.228.186:50005/ getCustomsDockingData {"tableName":"SYS_OPERATE_LOG","bizDate":"2021-05-07"}
func RequestTableInfo(requestType string)string {
// 获取表信息,返回byte
// request example //http://119.23.228.186:50005/ getCustomsDockingData {"tableName":"SYS_OPERATE_LOG","bizDate":"2021-05-07"}
func RequestTableInfo(requestType string) string {
type Sub struct {
Get_materialstatistic func(string) (string, error)
......@@ -212,9 +206,9 @@ func RequestTableInfo(requestType string)string {
client := rpc.NewHTTPClient("http://119.23.228.186:50005/")
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 {
fmt.Println(requestType+"请求服务器出错"+err.Error())
fmt.Println(requestType + "请求服务器出错" + err.Error())
return ""
}
......@@ -222,25 +216,22 @@ func RequestTableInfo(requestType string)string {
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)
requestResult := RequestUrl(requestStr)
fmt.Println("sku总量json"+requestResult)
return gjson.Get(requestResult,"data.total").String()
fmt.Println("sku总量json" + requestResult)
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
///{"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 {
return ""
}
......@@ -250,4 +241,44 @@ func RequestUrl(url string)string {
return ""
}
return string(robots)
}
\ No newline at end of file
}
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