Advertisement
Pandaaaa906

Untitled

Feb 16th, 2023 (edited)
1,176
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.07 KB | None | 0 0
  1. from datetime import datetime
  2. from typing import List
  3.  
  4. from pydantic import BaseModel
  5.  
  6.  
  7. class SuppInqOrder(BaseModel):
  8.     """供应商单据头"""
  9.     code: str
  10.     create_date: datetime
  11.  
  12.  
  13. class SuppInqOrderDetail:
  14.     parent_id: int
  15.     supp_id: int
  16.  
  17.  
  18. class SummaryHeader(BaseModel):
  19.     """汇总头信息"""
  20.     supp_id: int
  21.     total: int
  22.     sent: int
  23.     status: str
  24.  
  25.  
  26. class SummaryData(BaseModel):
  27.     """汇总数据体"""
  28.     # 产品信息
  29.     prd_id: int
  30.     prd_info: str
  31.     # ... 其他产品信息
  32.  
  33.     # 一下供应商报价明细信息
  34.     # 动态key,内容为供应商报价
  35.     # 如:
  36.     # 1324: {price: 6534, tax: 13}
  37.     # 1123: {price: 2000, tax: 13}
  38.     # 3454: {price: 1000, tax: 13}
  39.  
  40.  
  41. class SummaryResponse(SuppInqOrder):
  42.     summary_headers: List[SummaryHeader]
  43.     summary_data: List[SummaryData]
  44.  
  45.  
  46. code = 'XXX20210216'
  47.  
  48.  
  49. def get_summary(code: str, limit: int = 500, offset: int = 0):
  50.     # 查询数据库,获取供应商询价单头
  51.     supp_inq_order = SuppInqOrder.select().filter(code=code).first()
  52.     # 查询数据库,取供应商询价单明细表,可能需要窗口函数去重,某产品,某供应商最新有效报价
  53.     supp_inq_details = SuppInqOrderDetail.select().filter(
  54.         parent_id=supp_inq_order.id
  55.     ).orderby("""没想好""").limit(limit).offset(offset)  # 估计不能用简单的limit offset需要用窗口函数在where选择需要的明细条目
  56.  
  57.     d_headers = dict()  # 汇总头信息, dict类似java的hashmap,此为 Map<供应商ID, 汇总头>
  58.     d_data = dict()  # 汇总明细, 此为 Map<产品ID, 汇总体>
  59.     for item in supp_inq_details:
  60.         # 处理汇总头
  61.         if item.supp_id not in d_headers:
  62.             d_headers[item.supp_id] = SummaryHeader(supp_id=item.supp_id, total=0, sent=0)
  63.         d_headers[item.supp_id].total += 1
  64.         if item.sent:
  65.             d_headers[item.supp_id].sent += 1
  66.  
  67.         # 处理汇总体
  68.         if item.prd_id not in d_data:
  69.             d_data[item.supp_id] = SummaryData(prd_id=item.prd_id, **{item.supp_id: item})
  70.  
  71.     # 将hashmap的headers和data处理为列表, 可能需要分别考虑排序问题
  72.     headers = to_list(d_headers)
  73.     data = to_list(d_data)
  74.     return SummaryResponse(
  75.         code=supp_inq_order.code,
  76.         summary_headers=headers,
  77.         summary_data=data
  78.     )
  79.  
  80.  
  81. """
  82. 返回样例
  83. {
  84. code: "XXX202302160005",
  85. date: "2023-02-16 15:10:32.4531",
  86. summary_headers: [
  87. {supp_id: 1234, supp_name: 公司1, total: 3, sent: 1},
  88. {supp_id: 1235, supp_name: 公司2, total: 3, sent: 3},
  89. {supp_id: 1236, supp_name: 公司3, total: 4, sent: 3},
  90. ],
  91. summary_data: [
  92.    {prd_id: 132, prd_info: ..., supp_1234: {price: 5666, tax: 13}, supp_1235: {price: 5666, tax: 13}, supp_1236: {price: 5666, tax: 13}},
  93.    {prd_id: 118, prd_info: ..., supp_1234: {price: 5666, tax: 13}, supp_1236: {price: 5666, tax: 13}},
  94.    {prd_id: 128, prd_info: ..., supp_1234: {price: 5666, tax: 13}, supp_1235: {price: 5666, tax: 13}, },
  95. ]
  96. }
  97. """
  98.  
  99. if __name__ == '__main__':
  100.     print(SummaryResponse.schema())
  101.  
Advertisement
Comments
  • texunltd
    1 year
    # C++ 0.19 KB | 0 0
    1. Textile Unlimited Inc. with over 3 decades of experience is a quality promised <a href="https://www.texunltd.com">Textile Buying House</a> professionally managing the sourcing of textiles.
    2.  
Add Comment
Please, Sign In to add comment
Advertisement