Pandaaaa906

Untitled

May 5th, 2021
586
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. from datetime import datetime
  2. from typing import List
  3.  
  4. from loguru import logger
  5. from sqlalchemy import func, literal
  6. from sqlalchemy.orm import Session
  7.  
  8. from ..models.base import Inventory, BasPart, InventorySub, InventoryExtraDefine
  9. from ..models.product import Product, ProductPackage
  10.  
  11. cls_code_d = {
  12.     '0302': '0102',
  13.     '0301': '0101',
  14. }
  15.  
  16.  
  17. def validate(text: str):
  18.     return text.replace('\'', '’')
  19.  
  20.  
  21. def upsert_inventory(db: Session, product: Product, packages: List[ProductPackage] = None, cat_no_prefix=''):
  22.     inv_code = f'{cat_no_prefix}{product.cat_no}'
  23.     inventory = Inventory(
  24.         cInvCode=inv_code,
  25.         cInvName=validate(product.cn_name),
  26.         cEnglishName=validate(product.en_name),
  27.         dSDate=datetime.today(),
  28.         dModifyDate=datetime.now(),
  29.         iMassDate=product.mass_day,
  30.         cInvDefine4=product.mf,
  31.         cInvDefine5=product.mw,
  32.         cInvDefine6=product.cas,
  33.         cInvDefine8=product.purity,
  34.     )
  35.     if not cat_no_prefix:
  36.         inventory.cInvCCode = product.cls_code
  37.         # cInvDefine1=product.cls_name  # 网站分类名称
  38.         inventory.cInvDefine10 = f'M-{product.cat_no}'
  39.         inventory.bSelf = 1
  40.         inventory.bFree1 = 1
  41.         inventory.iGroupType = 0
  42.         inventory.cGroupCode = '01'
  43.         inventory.cComUnitCode = '01'
  44.         # PictureGUID=?
  45.         inventory.cProductUnit = None
  46.         inventory.cMassUnit = 1  # TODO hardcode
  47.         inventory.bConfigFree1 = 1
  48.     else:
  49.         inventory.cInvCCode = cls_code_d.get(product.cls_code, '0101')
  50.         inventory.cInvDefine10 = inv_code
  51.         inventory.bSelf = 0
  52.         inventory.bFree1 = 0
  53.         inventory.iGroupType = 2
  54.         inventory.cGroupCode = '03'
  55.         inventory.cComUnitCode = '0301'
  56.         inventory.cAssComUnitCode = '0302'
  57.         inventory.cSAComUnitCode = '0302'
  58.         inventory.cPUComUnitCode = '0302'
  59.         inventory.cSTComUnitCode = '0302'
  60.         inventory.cCAComUnitCode = '0302'
  61.         inventory.cProductUnit = '0301'
  62.         inventory.cMassUnit = 1  # TODO hardcode
  63.         inventory.bConfigFree1 = 0
  64.         inventory.iPlanDefault = 3
  65.         inventory.cShopUnit = '0301'
  66.  
  67.     origin_inventory = db.query(
  68.         Inventory
  69.     ).with_hint(Inventory, 'WITH(NOLOCK)').filter(
  70.         Inventory.cInvCode == inv_code
  71.     ).limit(1).first()
  72.     if not origin_inventory:
  73.         # add
  74.         db.add(inventory)
  75.         db.add(InventorySub(cInvSubCode=inv_code))
  76.         db.add(InventoryExtraDefine(
  77.             cInvCode=inv_code,
  78.             cidefine1=product.smiles,
  79.             cidefine2=product.ghs_icon01,
  80.             cidefine3=product.ghs_icon02,
  81.             cidefine4=product.ghs_icon03,
  82.             cidefine5=product.remark,
  83.             cidefine6=product.handler,
  84.             # TODO packages(cidefine12)?
  85.         ))
  86.         logger.debug(f'inserting inventory {inventory.__dict__}')
  87.     else:
  88.         diff = {
  89.             field: getattr(inventory, field)
  90.             for field in inventory.__dict__
  91.             if not field.startswith('_sa_')
  92.                and field not in {'dSDate', 'dModifyDate'}
  93.                and getattr(origin_inventory, field) != getattr(inventory, field)
  94.         }
  95.         if diff:
  96.             db.query(
  97.                 Inventory
  98.             ).filter(
  99.                 Inventory.cInvCode == inv_code
  100.             ).update(diff)
  101.             logger.debug(f'updating inventory {inv_code} from {origin_inventory} to {diff}')
  102.         # TODO update inventory extra define
  103.     db.commit()
  104.  
  105.     upsert_packages(db, product, cat_no_prefix, packages)
  106.  
  107.  
  108. def upsert_packages(db: Session, product: Product, cat_no_prefix='', packages: List[ProductPackage] = None):
  109.     if cat_no_prefix:
  110.         inv_code = f'{cat_no_prefix}{product.cat_no}'
  111.         bas_part = db.query(
  112.             BasPart
  113.         ).with_hint(BasPart, 'WITH(NOLOCK)').filter(
  114.             BasPart.InvCode == inv_code
  115.         ).limit(1).first()
  116.         if bas_part:
  117.             return
  118.         max_id = db.query(func.max(BasPart.PartId)).scalar()
  119.         db.add(BasPart(
  120.             PartId=max_id + 1,
  121.             InvCode=inv_code,
  122.             bVirtual=1,
  123.             LLC=1
  124.         ))
  125.         db.commit()
  126.     elif packages is not None:
  127.         tmp = {
  128.             package.package: package for package in packages
  129.         }
  130.         for each in db.query(
  131.                 BasPart
  132.         ).filter(
  133.             BasPart.InvCode == product.cat_no,
  134.             BasPart.Free1.in_(tmp.keys())
  135.         ).all():
  136.             tmp.pop(each.Free1)
  137.         for package in tmp.values():
  138.             db.add(BasPart(
  139.                 PartId=db.query(func.max(BasPart.PartId)).scalar() + 1,
  140.                 InvCode=product.cat_no,
  141.                 Free1=package.package,
  142.                 bVirtual=0
  143.             ))
  144.             db.commit()
  145.         if not db.query(literal(True)).filter(
  146.                 db.query(BasPart.PartId).filter(BasPart.InvCode == product.cat_no, BasPart.Free1 == 'ETS').exists()
  147.         ).scalar():
  148.             db.add(BasPart(
  149.                 PartId=db.query(func.max(BasPart.PartId)).scalar() + 1,
  150.                 InvCode=product.cat_no,
  151.                 Free1='ETS',
  152.                 bVirtual=0
  153.             ))
  154.             db.commit()
  155.  
RAW Paste Data