From d5c365d226f5f6b77f66de14c051047e53a693fc Mon Sep 17 00:00:00 2001 From: RIceWqy <1840169763@qq.com> Date: Mon, 25 Dec 2023 22:59:39 +0800 Subject: [PATCH] goods --- module/goods/Goods.go | 17 ++++++----- module/goods/init.go | 71 +++++++++++++++++++++++++++++++++---------- 2 files changed, 65 insertions(+), 23 deletions(-) diff --git a/module/goods/Goods.go b/module/goods/Goods.go index b38b65e..1cb9321 100644 --- a/module/goods/Goods.go +++ b/module/goods/Goods.go @@ -75,12 +75,18 @@ func (good *Goods) AfterQuery(session basedboperat.DbTransactionSession) error { if len(skus) > 0 { var a activity.Activity - err := session.Get(&a, nil, "id = ?", skus[0].ActivityId) - if err != nil { - return err + for _, v := range skus { + if v.ActivityId != 0 { + err := session.Get(&a, nil, "id = ?", v.ActivityId) + if err != nil { + log.Println("@", err) + return err + } + break + } } - good.Activity = a + good.Activity = a } var serverList []service.Service @@ -105,9 +111,7 @@ func (good *Goods) AfterQuery(session basedboperat.DbTransactionSession) error { Value: good.ID, }) session.ListScan(&list2, &gcr, &gcrs) // 传递切片的指针 - log.Println("goodid", good.ID) - log.Println("gcrs", gcrs) var tempIds []string for _, v := range gcrs { @@ -136,6 +140,5 @@ func (good *Goods) AfterQuery(session basedboperat.DbTransactionSession) error { Value: "%" + strconv.FormatInt(good.ID, 10) + "%", }) session.ListScan(&list, &sku, &skus)*/ - return nil } diff --git a/module/goods/init.go b/module/goods/init.go index 00b9304..a94c449 100644 --- a/module/goods/init.go +++ b/module/goods/init.go @@ -15,9 +15,9 @@ func init() { } type List2Params struct { - CategoryId int64 `json:"category_id"` - Keywords string `json:"keywords"` - Page int64 `json:"page"` + CategoryId interface{} `json:"category_id"` + Keywords string `json:"keywords"` + Page int64 `json:"page"` } func List2(rpcConn towgo.JsonRpcConnection) { @@ -32,7 +32,9 @@ func List2(rpcConn towgo.JsonRpcConnection) { var gcr good_category_re.GoodCategoryRe var gcrs []good_category_re.GoodCategoryRe var listGcr basedboperat.List + listGcr.Limit = 10 var listGood basedboperat.List + listGood.Limit = 10 if params.CategoryId != 0 { listGcr.Where = append(listGcr.Where, basedboperat.Condition{ Field: "category_id", @@ -40,18 +42,37 @@ func List2(rpcConn towgo.JsonRpcConnection) { Value: params.CategoryId, }) } - /*tempMap := make(map[string][]string) - if params.Keywords != "" { - tempMap["title"] = "%" + params.Keywords + "%" - listGood.Like = tempMap - append(listGood.Where, basedboperat.Condition{ - Field: "title", - Operator: "like", - Value: "%" + params.Keywords + "%", + if params.CategoryId != "0" { + listGcr.Where = append(listGcr.Where, basedboperat.Condition{ + Field: "category_id", + Operator: "=", + Value: params.CategoryId, }) - }*/ + } + if params.Keywords != "" { + listGood.Like = make(map[string][]interface{}) + listGood.Like["title"] = []interface{}{"%" + params.Keywords + "%"} + } basedboperat.ListScan(&listGcr, &gcr, &gcrs) - basedboperat.ListScan(&listGood, &good, &goods) + //basedboperat.ListScan(&listGood, &good, &goods) + sql := "select * from " + good.TableName() + " where title like '%" + params.Keywords + "%'" + + err = basedboperat.SqlQueryScan(&goods, sql) + + if err != nil { + log.Println(err) + //rpcConn.WriteError(500, err.Error()) + //return + } + sql = "select count(id) as `count` from " + good.TableName() + var m []map[string]interface{} + err = basedboperat.SqlQueryScan(&m, sql) + + if err != nil { + log.Println(err) + } + count := int64(m[0]["count"].(float64)) + mapGcrs := make(map[int64]int64) for _, v := range gcrs { mapGcrs[v.GoodsId] = v.CategoryId @@ -60,10 +81,28 @@ func List2(rpcConn towgo.JsonRpcConnection) { for _, v := range goods { if mapGcrs[v.ID] != 0 { resultGoods = append(resultGoods, v) - } } - - rpcConn.WriteResult(resultGoods) + type data struct { + ResultGoods []Goods `json:"resultGoods"` + CurrentPage int64 `json:"current_Page"` + LastPage int64 `json:"last_page"` + PerPage int64 `json:"per_page"` + Total int64 `json:"total"` + } + var lastPage int64 + if count%10 == 0 { + lastPage = count / 10 + } else { + lastPage = count/10 + 1 + } + d := data{ + ResultGoods: resultGoods, + CurrentPage: params.Page, + LastPage: lastPage, + PerPage: 10, + Total: count, + } + rpcConn.WriteResult(d) }