增加楼栋订单数查询。
This commit is contained in:
parent
0fb3a28070
commit
3fa45a738f
@ -25,11 +25,12 @@ from app.core.config import settings
|
|||||||
from app.models.address import AddressDB
|
from app.models.address import AddressDB
|
||||||
from sqlalchemy.orm import joinedload
|
from sqlalchemy.orm import joinedload
|
||||||
from app.models.community import CommunityDB
|
from app.models.community import CommunityDB
|
||||||
from app.models.community_building import CommunityBuildingDB
|
from app.models.community_building import CommunityBuildingDB, CommunityBuildingInfo
|
||||||
from app.models.station import StationDB
|
from app.models.station import StationDB
|
||||||
from app.models.point import PointRecordDB, PointRecordType
|
from app.models.point import PointRecordDB, PointRecordType
|
||||||
from app.core.utils import CommonUtils
|
from app.core.utils import CommonUtils
|
||||||
import logging
|
import logging
|
||||||
|
from sqlalchemy import func
|
||||||
|
|
||||||
router = APIRouter()
|
router = APIRouter()
|
||||||
|
|
||||||
@ -338,6 +339,57 @@ async def get_order_detail(
|
|||||||
"packages": package_list
|
"packages": package_list
|
||||||
})
|
})
|
||||||
|
|
||||||
|
# 提供一个接口,传入 community_id 返回每栋楼栋的订单数量
|
||||||
|
@router.get("/community_building/count", response_model=ResponseModel)
|
||||||
|
async def get_community_building_order_count(
|
||||||
|
community_id: int,
|
||||||
|
db: Session = Depends(get_db),
|
||||||
|
current_user: UserDB = Depends(get_current_user)
|
||||||
|
):
|
||||||
|
"""获取社区每栋楼栋的订单数量"""
|
||||||
|
# 查询当前社区所有楼栋
|
||||||
|
community_buildings = db.query(
|
||||||
|
CommunityBuildingDB
|
||||||
|
).filter(
|
||||||
|
CommunityBuildingDB.community_id == community_id
|
||||||
|
).all()
|
||||||
|
|
||||||
|
if not community_buildings:
|
||||||
|
return error_response(code=404, message="社区不存在")
|
||||||
|
|
||||||
|
# 查询每个楼栋的订单数量
|
||||||
|
building_order_count = db.query(
|
||||||
|
ShippingOrderDB.address_community_building_name,
|
||||||
|
func.count(ShippingOrderDB.orderid)
|
||||||
|
).filter(
|
||||||
|
ShippingOrderDB.address_community_building_name.in_(
|
||||||
|
[building.building_name for building in community_buildings]
|
||||||
|
)
|
||||||
|
).group_by(
|
||||||
|
ShippingOrderDB.address_community_building_name
|
||||||
|
).all()
|
||||||
|
|
||||||
|
|
||||||
|
# 没有订单的楼栋,订单数量为0
|
||||||
|
result = []
|
||||||
|
building_order_count_dict = dict(building_order_count)
|
||||||
|
|
||||||
|
for building in community_buildings:
|
||||||
|
if building.building_name not in building_order_count_dict:
|
||||||
|
result.append({
|
||||||
|
"building_name": building.building_name,
|
||||||
|
"order_count": 0
|
||||||
|
})
|
||||||
|
else:
|
||||||
|
result.append({
|
||||||
|
"building_name": building.building_name,
|
||||||
|
"order_count": building_order_count_dict[building.building_name]
|
||||||
|
})
|
||||||
|
|
||||||
|
|
||||||
|
return success_response(data=result)
|
||||||
|
|
||||||
|
|
||||||
@router.get("/user/list", response_model=ResponseModel)
|
@router.get("/user/list", response_model=ResponseModel)
|
||||||
async def get_user_orders(
|
async def get_user_orders(
|
||||||
status: Optional[OrderStatus] = None,
|
status: Optional[OrderStatus] = None,
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user