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.

109 lines
2.6 KiB

package goods
import (
"github.com/towgo/towgo/dao/basedboperat"
"github.com/towgo/towgo/dao/ormDriver/xormDriver"
"github.com/towgo/towgo/towgo"
"log"
"src/module/good_category_re"
)
func init() {
xormDriver.Sync2(new(Goods))
towgo.NewCRUDJsonrpcAPI("/goods", Goods{}, []Goods{}).RegAPI()
towgo.SetFunc("/goods/list2", List2)
}
type List2Params struct {
CategoryId interface{} `json:"category_id"`
Keywords string `json:"keywords"`
Page int64 `json:"page"`
}
func List2(rpcConn towgo.JsonRpcConnection) {
var params List2Params
err := rpcConn.ReadParams(&params)
if err != nil {
log.Println(err)
return
}
var good Goods
var goods []Goods
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",
Operator: "=",
Value: params.CategoryId,
})
}
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)
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
}
var resultGoods []Goods
for _, v := range goods {
if mapGcrs[v.ID] != 0 {
resultGoods = append(resultGoods, v)
}
}
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)
}