diff --git a/business/jxcallback/orderman/orderman_ext.go b/business/jxcallback/orderman/orderman_ext.go index 472fa50ef..bcf35c88e 100644 --- a/business/jxcallback/orderman/orderman_ext.go +++ b/business/jxcallback/orderman/orderman_ext.go @@ -752,37 +752,8 @@ func (c *OrderManager) GetOrdersFinancial(ctx *jxcontext.Context, fromDateStr, t return pagedInfo, err } -func (c *OrderManager) GetStoresOrderSaleInfo(ctx *jxcontext.Context, storeIDList []int, fromTime time.Time, toTime time.Time, statusList []int) (saleInfoList []*StoresOrderSaleInfo, err error) { - if toTime.Sub(fromTime) > time.Hour*24*60 { - return nil, fmt.Errorf("查询时间范围不能超过60天") - } - // 用int64类型去取float型的数据库返回值,会取不到 - sql := fmt.Sprintf(` - SELECT IF(t1.jx_store_id > 0, t1.jx_store_id, t1.store_id) store_id, t1.vendor_id, IF(t1.status < ?, 0, t1.status) status, - COUNT(*) count, SUM(t1.shop_price) shop_price, SUM(t1.vendor_price) vendor_price, SUM(t1.sale_price) sale_price, SUM(t1.actual_pay_price) actual_pay_price, - CAST(SUM(IF(t1.earning_price <> 0, t1.earning_price, IF(t1.shop_price <> 0 && t1.shop_price < t1.sale_price, t1.shop_price, t1.sale_price) * IF(t5.pay_percentage > 0, t5.pay_percentage, %d) / 100)) AS SIGNED) earning_price - FROM goods_order t1 - LEFT JOIN store t5 ON t5.id = IF(t1.jx_store_id <> 0, t1.jx_store_id, t1.store_id) - WHERE t1.order_created_at >= ? AND t1.order_created_at <= ? - `, model.DefaultEarningPricePercentage) - sqlParams := []interface{}{ - model.OrderStatusEndBegin, - fromTime, - toTime, - } - if len(storeIDList) > 0 { - sql += " AND IF(t1.jx_store_id > 0, t1.jx_store_id, t1.store_id) IN (" + dao.GenQuestionMarks(len(storeIDList)) + ")" - sqlParams = append(sqlParams, storeIDList) - } - if len(statusList) > 0 { - sql += " AND t1.status IN (" + dao.GenQuestionMarks(len(statusList)) + ")" - sqlParams = append(sqlParams, statusList) - } - sql += ` - GROUP BY 1,2,3 - ORDER BY 1,2,3` - err = dao.GetRows(dao.GetDB(), &saleInfoList, sql, sqlParams...) - return saleInfoList, err +func (c *OrderManager) GetStoresOrderSaleInfo(ctx *jxcontext.Context, storeIDList []int, fromTime time.Time, toTime time.Time, statusList []int) (saleInfoList []*dao.StoresOrderSaleInfo, err error) { + return dao.GetStoresOrderSaleInfo(dao.GetDB(), storeIDList, fromTime, toTime, statusList) } func (c *OrderManager) GetAfsOrders(ctx *jxcontext.Context, keyword, afsOrderID, vendorOrderID string, vendorIDList, appealTypeList, storeIDList, statusList []int, fromTime, toTime time.Time, offset, pageSize int) (pagedInfo *model.PagedInfo, err error) { diff --git a/business/jxstore/cms/store.go b/business/jxstore/cms/store.go index b768ee17d..461d898bf 100644 --- a/business/jxstore/cms/store.go +++ b/business/jxstore/cms/store.go @@ -104,7 +104,7 @@ var ( ) // todo 门店绑定信息可以考虑以数组形式返回,而不是现在这样 -func GetStores(ctx *jxcontext.Context, keyword string, params map[string]interface{}, offset, pageSize int) (retVal *StoresInfo, err error) { +func GetStores(ctx *jxcontext.Context, keyword string, params map[string]interface{}, offset, pageSize int, orderTimeFrom, orderTimeTo time.Time, orderCountFrom, orderCountTo int) (retVal *StoresInfo, err error) { sql := ` SELECT SQL_CALC_FOUND_ROWS CAST(t1.lng AS DECIMAL(15,6))/1000000 float_lng, @@ -324,6 +324,8 @@ func GetStores(ctx *jxcontext.Context, keyword string, params map[string]interfa var storeList []*StoreExt mapLimit := false if err = dao.GetRows(db, &storeList, sql, sqlParams...); err == nil { + retVal.TotalCount = dao.GetLastTotalRowCount(db) + dao.Commit(db) // globals.SugarLogger.Debugf("GetStores, len(storeList):%d", len(storeList)) var ( mapLatitude, mapLongitude float64 @@ -343,32 +345,57 @@ func GetStores(ctx *jxcontext.Context, keyword string, params map[string]interfa if valid { if v.StoreMapStr != "" { if err = utils.UnmarshalUseNumber([]byte(v.StoreMapStr), &v.StoreMaps); err != nil { - dao.Rollback(db) return nil, err } } if v.CourierMapStr != "" { if err = utils.UnmarshalUseNumber([]byte(v.CourierMapStr), &v.CourierMaps); err != nil { - dao.Rollback(db) return nil, err } } retVal.Stores = append(retVal.Stores, v) } } + retVal.Stores, err = filterStoreByOrderInfo(db, retVal.Stores, orderTimeFrom, orderTimeTo, orderCountFrom, orderCountTo) if mapLimit { retVal.TotalCount = len(retVal.Stores) - } else { - retVal.TotalCount = dao.GetLastTotalRowCount(db) } + } else { + dao.Rollback(db) } - dao.Commit(db) - if mapLimit { + if mapLimit && len(retVal.Stores) > 0 { retVal.MapCenterLng, retVal.MapCenterLat = getMapCenter(retVal.Stores) } return retVal, err } +func filterStoreByOrderInfo(db *dao.DaoDB, inStores []*StoreExt, orderTimeFrom, orderTimeTo time.Time, orderCountFrom, orderCountTo int) (outStores []*StoreExt, err error) { + if len(inStores) > 0 && !utils.IsTimeZero(orderTimeFrom) { + storeIDs := make([]int, len(inStores)) + for k, v := range inStores { + storeIDs[k] = v.ID + } + orderSaleList, err2 := dao.GetStoresOrderSaleInfo(dao.GetDB(), storeIDs, orderTimeFrom, orderTimeTo, []int{model.OrderStatusFinished}) + if err = err2; err != nil { + return nil, err + } + storeOrderCountMap := make(map[int]int) + for _, v := range orderSaleList { + storeOrderCountMap[v.StoreID] += v.Count + } + + for _, v := range inStores { + orderCount := storeOrderCountMap[v.ID] + if orderCount >= orderCountFrom && orderCount <= orderCountTo { + outStores = append(outStores, v) + } + } + } else { + outStores = inStores + } + return outStores, err +} + func getMapCenter(storeList []*StoreExt) (lng, lat float64) { globals.SugarLogger.Debugf("getMapCenter len(storeList):%d", len(storeList)) if len(storeList) == 0 { @@ -573,7 +600,7 @@ func SetStoreStatus(ctx *jxcontext.Context, storeID, status int) (err error) { func EnableHaveRestStores(ctx *jxcontext.Context, isAsync, isContinueWhenError bool) (hint string, err error) { storeInfo, err := GetStores(ctx, "", map[string]interface{}{ "statuss": string(utils.MustMarshal([]int{model.StoreStatusHaveRest})), - }, 0, model.UnlimitedPageSize) + }, 0, model.UnlimitedPageSize, utils.ZeroTimeValue, utils.ZeroTimeValue, 0, 0) if err != nil { return "", err } diff --git a/business/model/dao/dao_order.go b/business/model/dao/dao_order.go index 9d3855fc9..f9ba426ca 100644 --- a/business/model/dao/dao_order.go +++ b/business/model/dao/dao_order.go @@ -1,11 +1,26 @@ package dao import ( + "fmt" "time" + "git.rosy.net.cn/baseapi/utils" "git.rosy.net.cn/jx-callback/business/model" ) +type StoresOrderSaleInfo struct { + StoreID int `orm:"column(store_id)" json:"storeID"` + VendorID int `orm:"column(vendor_id)" json:"vendorID"` + Status int `json:"status"` + Count int `json:"count"` + ShopPrice int64 `json:"shopPrice"` + VendorPrice int64 `json:"vendorPrice"` + SalePrice int64 `json:"salePrice"` + ActualPayPrice int64 `json:"actualPayPrice"` + + EarningPrice int64 `json:"earningPrice"` // 预估结算给门店老板的钱 +} + func GetStoreOrderAfterTime(db *DaoDB, storeID int, orderTime time.Time, lastOrderSeqID int64) (orderList []*model.GoodsOrderExt, err error) { sql := ` SELECT t1.*, @@ -82,3 +97,43 @@ func GetAfsOrders(db *DaoDB, vendorID int, vendorOrderID, afsOrderID string) (af err = GetRows(db, &afsOrderList, sql, sqlParams...) return afsOrderList, err } + +func GetStoresOrderSaleInfo(db *DaoDB, storeIDList []int, fromTime time.Time, toTime time.Time, statusList []int) (saleInfoList []*StoresOrderSaleInfo, err error) { + if utils.IsTimeZero(fromTime) { + return nil, fmt.Errorf("查询订单信息必须指定起始时间") + } + if utils.IsTimeZero(toTime) { + toTime = time.Now() + } + if toTime.Sub(fromTime) > time.Hour*24*60 { + return nil, fmt.Errorf("查询时间范围不能超过60天") + } + + // 用int64类型去取float型的数据库返回值,会取不到 + sql := fmt.Sprintf(` + SELECT IF(t1.jx_store_id > 0, t1.jx_store_id, t1.store_id) store_id, t1.vendor_id, IF(t1.status < ?, 0, t1.status) status, + COUNT(*) count, SUM(t1.shop_price) shop_price, SUM(t1.vendor_price) vendor_price, SUM(t1.sale_price) sale_price, SUM(t1.actual_pay_price) actual_pay_price, + CAST(SUM(IF(t1.earning_price <> 0, t1.earning_price, IF(t1.shop_price <> 0 && t1.shop_price < t1.sale_price, t1.shop_price, t1.sale_price) * IF(t5.pay_percentage > 0, t5.pay_percentage, %d) / 100)) AS SIGNED) earning_price + FROM goods_order t1 + LEFT JOIN store t5 ON t5.id = IF(t1.jx_store_id <> 0, t1.jx_store_id, t1.store_id) + WHERE t1.order_created_at >= ? AND t1.order_created_at <= ? + `, model.DefaultEarningPricePercentage) + sqlParams := []interface{}{ + model.OrderStatusEndBegin, + fromTime, + toTime, + } + if len(storeIDList) > 0 { + sql += " AND IF(t1.jx_store_id > 0, t1.jx_store_id, t1.store_id) IN (" + GenQuestionMarks(len(storeIDList)) + ")" + sqlParams = append(sqlParams, storeIDList) + } + if len(statusList) > 0 { + sql += " AND t1.status IN (" + GenQuestionMarks(len(statusList)) + ")" + sqlParams = append(sqlParams, statusList) + } + sql += ` + GROUP BY 1,2,3 + ORDER BY 1,2,3` + err = GetRows(db, &saleInfoList, sql, sqlParams...) + return saleInfoList, err +} diff --git a/controllers/cms_store.go b/controllers/cms_store.go index 8f5d181dd..82b4106c8 100644 --- a/controllers/cms_store.go +++ b/controllers/cms_store.go @@ -30,6 +30,10 @@ type StoreController struct { // @Param mapLongitude query string false "地图中心经度" // @Param mapLatitude query string false "地图中心纬度" // @Param mapRadius query int false "地图半径(单位为米)" +// @Param orderTimeFrom query string false "订单创建起始时间" +// @Param orderTimeTo query string false "订单创建结束时间" +// @Param orderCountFrom query int false "订单量起始" +// @Param orderCountTo query int false "订单量结束" // @Param offset query int false "门店列表起始序号(以0开始,缺省为0)" // @Param pageSize query int false "门店列表页大小(缺省为50,-1表示全部)" // @Success 200 {object} controllers.CallResult @@ -37,7 +41,10 @@ type StoreController struct { // @router /GetStores [get] func (c *StoreController) GetStores() { c.callGetStores(func(params *tStoreGetStoresParams) (retVal interface{}, errCode string, err error) { - retVal, err = cms.GetStores(params.Ctx, params.Keyword, params.MapData, params.Offset, params.PageSize) + timeList, err := jxutils.BatchStr2Time(params.OrderTimeFrom, params.OrderTimeTo) + if err == nil { + retVal, err = cms.GetStores(params.Ctx, params.Keyword, params.MapData, params.Offset, params.PageSize, timeList[0], timeList[1], params.OrderCountFrom, params.OrderCountTo) + } return retVal, "", err }) }