ns = params_kw.copy() for k,v in ns.items(): if v == 'NaN' or v == 'null': ns[k] = None id = params_kw.id if not id or len(id) > 32: id = getID() ns['id'] = id user_id = await get_user() userorgid = await get_userorgid() if not userorgid: return {'widgettype':'Error','options':{'title':'Authorization Error','message':'Please login'}} ns['resellerid'] = userorgid ns['created_by'] = user_id ns['created_at'] = timestampstr() ns['updated_at'] = timestampstr() # Auto-generate contract_code if not ns.get('contract_code'): today = curDateString().replace('-', '') prefix = f'SC-{today}' db = DBPools() dbname = get_module_dbname('supplychain') async with db.sqlorContext(dbname) as sor: recs = await sor.sqlExe( "SELECT COUNT(*) as cnt FROM supply_contracts WHERE resellerid = ${rid}$ AND contract_code LIKE ${p}$", {"rid": userorgid, "p": prefix + "%"} ) seq = (recs[0].cnt if recs else 0) + 1 ns['contract_code'] = f'{prefix}-{seq:04d}' db = DBPools() dbname = get_module_dbname('supplychain') async with db.sqlorContext(dbname) as sor: await sor.C('supply_contracts', ns.copy()) return {'widgettype':'Message','options':{'title':'Add Success','message':'供应合同创建成功'}} return {'widgettype':'Error','options':{'title':'Add Error','message':'failed'}}