商品rpc数据格式及调用方法
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

261 lines
4.2 KiB

package productrpc
import (
"encoding/json"
"log"
"sort"
"strconv"
// "strconv"
// "time"
)
/**
* 获取商品进行中活动
* 2021/10/06
* GZ
*/
func GetActivity(site_id, dbname, id string, sku_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, sku_id}
req, err := SetReqData(arg)
if err != nil {
return nil, err
}
res := &Response{}
err = conn.GetActivity(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 GetWarmingActivity(site_id, dbname, id string, sku_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, sku_id}
req, err := SetReqData(arg)
if err != nil {
return nil, err
}
res := &Response{}
err = conn.GetWarmingActivity(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 GetAllActivity(site_id, dbname, id string, sku_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, sku_id}
req, err := SetReqData(arg)
if err != nil {
return nil, err
}
res := &Response{}
err = conn.GetAllActivity(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
}
type Activity struct {
Id string
Start int
}
type Activitys []Activity
func (s Activitys) Len() int {
return len(s)
}
func (s Activitys) Swap(i, j int) {
s[i], s[j] = s[j], s[i]
}
func (s Activitys) Less(i, j int) bool {
return s[i].Start < s[j].Start
}