From acd68471a51ca80244565f5ac04588aa1d694087 Mon Sep 17 00:00:00 2001 From: guzeng Date: Thu, 14 Oct 2021 18:29:16 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=95=86=E5=93=81=E6=B4=BB?= =?UTF-8?q?=E5=8A=A8=E6=9F=A5=E8=AF=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- activity.go | 18 +-- activity_test.go | 104 ++++++++--------- go.mod | 1 + go.sum | 2 + product_activity.go | 246 +++++++++++++++++++++++++++++++++++++++ product_activity_test.go | 17 +++ 6 files changed, 325 insertions(+), 63 deletions(-) create mode 100644 product_activity.go create mode 100644 product_activity_test.go diff --git a/activity.go b/activity.go index 4350931..bbbc7d6 100644 --- a/activity.go +++ b/activity.go @@ -58,12 +58,12 @@ func GetActivity(site_id, dbname, id string, sku_id string, url ...string) ([]ma var start int for _, item := range res_arr { - start, err = strconv.Atoi(item["Starttime"]) + start, err = strconv.Atoi(item["starttime"]) if err != nil { log.Println(err) } activitys = append(activitys, Activity{ - Id: item["Id"], + Id: item["id"], Start: start, }) } @@ -76,7 +76,7 @@ func GetActivity(site_id, dbname, id string, sku_id string, url ...string) ([]ma if len(activitys) > 0 { for _, v := range activitys { for _, old := range res_arr { - if v.Id == old["Id"] { + if v.Id == old["id"] { list = append(list, old) break } @@ -136,12 +136,12 @@ func GetWarmingActivity(site_id, dbname, id string, sku_id string, url ...string var start int for _, item := range res_arr { - start, err = strconv.Atoi(item["Starttime"]) + start, err = strconv.Atoi(item["starttime"]) if err != nil { log.Println(err) } activitys = append(activitys, Activity{ - Id: item["Id"], + Id: item["id"], Start: start, }) } @@ -154,7 +154,7 @@ func GetWarmingActivity(site_id, dbname, id string, sku_id string, url ...string if len(activitys) > 0 { for _, v := range activitys { for _, old := range res_arr { - if v.Id == old["Id"] { + if v.Id == old["id"] { list = append(list, old) break } @@ -214,12 +214,12 @@ func GetAllActivity(site_id, dbname, id string, sku_id string, url ...string) ([ var start int for _, item := range res_arr { - start, err = strconv.Atoi(item["Starttime"]) + start, err = strconv.Atoi(item["starttime"]) if err != nil { log.Println(err) } activitys = append(activitys, Activity{ - Id: item["Id"], + Id: item["id"], Start: start, }) } @@ -232,7 +232,7 @@ func GetAllActivity(site_id, dbname, id string, sku_id string, url ...string) ([ if len(activitys) > 0 { for _, v := range activitys { for _, old := range res_arr { - if v.Id == old["Id"] { + if v.Id == old["id"] { list = append(list, old) break } diff --git a/activity_test.go b/activity_test.go index 458c038..e2bccbe 100644 --- a/activity_test.go +++ b/activity_test.go @@ -1,73 +1,69 @@ package productrpc import ( - "log" - "sort" - "strconv" "testing" ) func Test_GetAllActivity(t *testing.T) { - // dbname := "shop_v2" - // id := "10248" - // site_id := "10248" - // sku_id := "10248" + dbname := "shop_v2" + id := "122" + site_id := "1058278" - // ret, err := GetAllActivity(site_id, dbname, id, sku_id) + ret, err := GetAllActivity(site_id, dbname, id, "0") - // t.Log(ret) - // t.Log(err) + t.Log(ret) + t.Log(err) - res_arr := []map[string]string{ - map[string]string{ - "Id": "55", - "Starttime": "19852365", - }, - map[string]string{ - "Id": "22", - "Starttime": "17852365", - }, - map[string]string{ - "Id": "11", - "Starttime": "18852365", - }, - } - t.Log(res_arr) + // res_arr := []map[string]string{ + // map[string]string{ + // "Id": "55", + // "Starttime": "19852365", + // }, + // map[string]string{ + // "Id": "22", + // "Starttime": "17852365", + // }, + // map[string]string{ + // "Id": "11", + // "Starttime": "18852365", + // }, + // } + // t.Log(res_arr) - var activitys Activitys - var err error + // var activitys Activitys + // var err error - if len(res_arr) > 0 { - var start int + // if len(res_arr) > 0 { + // var start int - for _, item := range res_arr { - start, err = strconv.Atoi(item["Starttime"]) - if err != nil { - log.Println(err) - } - activitys = append(activitys, Activity{ - Id: item["Id"], - Start: start, - }) - } - } + // for _, item := range res_arr { + // start, err = strconv.Atoi(item["Starttime"]) + // if err != nil { + // log.Println(err) + // } + // activitys = append(activitys, Activity{ + // Id: item["Id"], + // Start: start, + // }) + // } + // } - sort.Sort(activitys) + // sort.Sort(activitys) - t.Log(activitys) + // t.Log(activitys) - var list []map[string]string = make([]map[string]string, 0) + // var list []map[string]string = make([]map[string]string, 0) - if len(activitys) > 0 { - for _, v := range activitys { - for _, old := range res_arr { - if v.Id == old["Id"] { - list = append(list, old) - break - } - } - } - } + // if len(activitys) > 0 { + // for _, v := range activitys { + // for _, old := range res_arr { + // if v.Id == old["Id"] { + // list = append(list, old) + // break + // } + // } + // } + // } - t.Log(list) + // t.Log(list) } diff --git a/go.mod b/go.mod index fe2c9b0..80d201a 100644 --- a/go.mod +++ b/go.mod @@ -5,6 +5,7 @@ go 1.14 require ( git.tetele.net/tgo/conf v0.33.1 // indirect git.tetele.net/tgo/crypter v0.2.2 // indirect + git.tetele.net/tgo/helper v0.1.9 // indirect github.com/chai2010/protorpc v1.1.3 // indirect golang.org/x/crypto v0.0.0-20210921155107-089bfa567519 // indirect golang.org/x/sync v0.0.0-20210220032951-036812b2e83c // indirect diff --git a/go.sum b/go.sum index 779d8fe..bc4e14e 100644 --- a/go.sum +++ b/go.sum @@ -2,6 +2,8 @@ git.tetele.net/tgo/conf v0.33.1 h1:ZEIv3Vq35RCv5f3T3Uz97s2mkZLl7W5OlmXvzI3/sS8= git.tetele.net/tgo/conf v0.33.1/go.mod h1:AWVIBEDE5dtotthUgR0SWaR2Qa6/f+O5WQ3s7Tj8q7A= git.tetele.net/tgo/crypter v0.2.2 h1:YMQJh2Gj5Po4ZfelJUmXBKi01UbmtiSy3bmqRfnYQMo= git.tetele.net/tgo/crypter v0.2.2/go.mod h1:vfvRLZA8+lHNgNXneOcgvVhDyuv25ZRb+C6xHOmXNx0= +git.tetele.net/tgo/helper v0.1.9 h1:i1KMqWAlilnrFfkJMw1+aJVU2Mlfz1ISBLLCFGvwCHA= +git.tetele.net/tgo/helper v0.1.9/go.mod h1:89mQwyfqZ+t8YXiVwzSxA70gLlUNqoZGDEUxvV46jXk= github.com/chai2010/protorpc v1.1.3 h1:VJK5hIoZn0XCGol0GmbxZkUG6FbTI5LP2Lam6RVd15w= github.com/chai2010/protorpc v1.1.3/go.mod h1:/wO0kiyVdu7ug8dCMrA2yDr2vLfyhsLEuzLa9J2HJ+I= github.com/golang/protobuf v1.0.0 h1:lsek0oXi8iFE9L+EXARyHIjU5rlWIhhTkjDz3vHhWWQ= diff --git a/product_activity.go b/product_activity.go new file mode 100644 index 0000000..10a1cd6 --- /dev/null +++ b/product_activity.go @@ -0,0 +1,246 @@ +package productrpc + +import ( + "encoding/json" + "log" + "sort" + "strconv" + + // "strconv" + // "time" + "git.tetele.net/tgo/helper" +) + +/** + * 获取商品进行中活动 + * 2021/10/06 + * GZ + */ +func GetProductActivity(site_id, dbname, id string, url ...string) ([]map[string]string, error) { + + conn, err := rpc_server_conn(url...) + if err != nil { + return nil, err + } + defer conn.Close() + + arg := GetActivityParam{site_id, dbname, id, "0"} + + req, err := SetReqData(arg) + if err != nil { + return nil, err + } + + res := &Response{} + + err = conn.GetProductActivity(req, res) + + if err != nil { + return nil, err + } + + res_data_de, err := GetResData(res) + if err != nil { + return nil, err + } + if res_data_de == "" { + return nil, nil + } + var res_arr []map[string]string + + err = json.Unmarshal([]byte(res_data_de), &res_arr) + + if err != nil { + return nil, err + } + + var activitys Activitys + + if len(res_arr) > 0 { + var start int + + for _, item := range res_arr { + start, err = strconv.Atoi(item["Starttime"]) + if err != nil { + log.Println(err) + } + activitys = append(activitys, Activity{ + Id: item["Id"], + Start: start, + }) + } + } + + sort.Sort(activitys) + + var list []map[string]string = make([]map[string]string, 0) + + if len(activitys) > 0 { + for _, v := range activitys { + for _, old := range res_arr { + if v.Id == old["Id"] { + list = append(list, old) + break + } + } + } + } + + return list, nil +} + +/** + * 获取商品预热中活动 + * 2021/10/06 + * GZ + */ +func GetProductWarmingActivity(site_id, dbname, id string, url ...string) ([]map[string]string, error) { + + conn, err := rpc_server_conn(url...) + if err != nil { + return nil, err + } + defer conn.Close() + + arg := GetActivityParam{site_id, dbname, id, "0"} + + req, err := SetReqData(arg) + if err != nil { + return nil, err + } + + res := &Response{} + + err = conn.GetProductWarmingActivity(req, res) + + if err != nil { + return nil, err + } + + res_data_de, err := GetResData(res) + if err != nil { + return nil, err + } + if res_data_de == "" { + return nil, nil + } + var res_arr []map[string]string + + err = json.Unmarshal([]byte(res_data_de), &res_arr) + + if err != nil { + return nil, err + } + + var activitys Activitys + + if len(res_arr) > 0 { + var start int + + for _, item := range res_arr { + start, err = strconv.Atoi(item["Starttime"]) + if err != nil { + log.Println(err) + } + activitys = append(activitys, Activity{ + Id: item["Id"], + Start: start, + }) + } + } + + sort.Sort(activitys) + + var list []map[string]string = make([]map[string]string, 0) + + if len(activitys) > 0 { + for _, v := range activitys { + for _, old := range res_arr { + if v.Id == old["Id"] { + list = append(list, old) + break + } + } + } + } + + return list, nil +} + +/** + * 获取商品进行中活动 + * 2021/10/06 + * GZ + */ +func GetProductAllActivity(site_id, dbname, id string, url ...string) ([]map[string]interface{}, error) { + + conn, err := rpc_server_conn(url...) + if err != nil { + return nil, err + } + defer conn.Close() + + arg := GetActivityParam{site_id, dbname, id, "0"} + + req, err := SetReqData(arg) + if err != nil { + return nil, err + } + + res := &Response{} + + err = conn.GetProductAllActivity(req, res) + + if err != nil { + return nil, err + } + + res_data_de, err := GetResData(res) + + if err != nil { + return nil, err + } + if res_data_de == "" { + return nil, nil + } + var res_arr []map[string]interface{} + + err = json.Unmarshal([]byte(res_data_de), &res_arr) + + if err != nil { + log.Println(err) + return nil, err + } + + var activitys Activitys + + if len(res_arr) > 0 { + var start int + + for _, item := range res_arr { + start = helper.ToInt(item["starttime"]) + + activitys = append(activitys, Activity{ + Id: helper.ToStr(item["id"]), + Start: start, + }) + } + } + + sort.Sort(activitys) + + var list []map[string]interface{} = make([]map[string]interface{}, 0) + + if len(activitys) > 0 { + for _, v := range activitys { + for _, old := range res_arr { + if v.Id == helper.ToStr(old["id"]) { + list = append(list, old) + break + } + } + } + } + + return list, nil +} diff --git a/product_activity_test.go b/product_activity_test.go new file mode 100644 index 0000000..8fbd7c3 --- /dev/null +++ b/product_activity_test.go @@ -0,0 +1,17 @@ +package productrpc + +import ( + "testing" +) + +func Test_GetProductAllActivity(t *testing.T) { + dbname := "shop_v2" + id := "122" + site_id := "1058278" + + ret, err := GetAllActivity(site_id, dbname, id, "0") + + t.Log(ret) + t.Log(err) + +}