From 5814b27af4acc372fdb1fef01cd27bd67a9f866b Mon Sep 17 00:00:00 2001 From: gazebo Date: Thu, 6 Sep 2018 15:14:11 +0800 Subject: [PATCH] - return totalCount for GetStores. --- business/jxstore/cms/store.go | 38 ++++++++++++++++++++++++----------- 1 file changed, 26 insertions(+), 12 deletions(-) diff --git a/business/jxstore/cms/store.go b/business/jxstore/cms/store.go index ce1b2e513..cedfae093 100644 --- a/business/jxstore/cms/store.go +++ b/business/jxstore/cms/store.go @@ -19,6 +19,11 @@ type StoreExt struct { DistrictName string `json:"districtName"` } +type StoresInfo struct { + TotalCount int `json:"totalCount"` + Stores []*StoreExt `json:"stores"` +} + var ( ErrMissingInput = errors.New("没有有效的输入参数") ErrCanNotVendor = errors.New("vendorID参数不合法") @@ -60,9 +65,8 @@ func UpdatePlaces(places []*model.Place, userName string) (err error) { return err } -func GetStores(keyword string, params map[string]interface{}, offset, pageSize int) (retVal []*StoreExt, err error) { +func GetStores(keyword string, params map[string]interface{}, offset, pageSize int) (retVal *StoresInfo, err error) { sql := ` - SELECT t1.*, city.name city_name, district.name district_name FROM store t1 LEFT JOIN place city ON t1.city_code = city.code AND city.level = 2 LEFT JOIN place district ON t1.district_code = district.code AND district.level = 3 @@ -121,19 +125,29 @@ func GetStores(keyword string, params map[string]interface{}, offset, pageSize i sql += " AND t1.status >= ? AND t1.status <= ?" params2 = append(params2, fromStatus, toStatus) } - sql += ` + sqlCount := "SELECT COUNT(*) ct\n" + sql + type tcount struct { + Ct int + } + countInfo := []tcount{} + if err = dao.GetRows(nil, &countInfo, sqlCount, params2...); err == nil { + sqlData := "SELECT t1.*, city.name city_name, district.name district_name\n" + sql + ` ORDER BY id LIMIT ? OFFSET ?` - if pageSize == 0 { - pageSize = model.DefPageSize + if pageSize == 0 { + pageSize = model.DefPageSize + } + if offset < 0 { + offset = 0 + } + params2 = append(params2, pageSize, offset) + retVal := &StoresInfo{ + TotalCount: countInfo[0].Ct, + } + err = dao.GetRows(nil, &retVal.Stores, sqlData, params2...) + return retVal, err } - if offset < 0 { - offset = 0 - } - params2 = append(params2, pageSize, offset) - - err = dao.GetRows(nil, &retVal, sql, params2...) - return retVal, err + return nil, err } func GetVendorStore(vendorStoreID string, vendorID int) (retVal *StoreExt, err error) {