# Upsert折扣明细:对比old_discount和discount,不同才更新 # 客户折扣不得高于全部用户折扣 result = {'widgettype': 'Message', 'options': {'title': 'Error', 'message': 'Invalid', 'type': 'error', 'timeout': 3}} try: discountid = params_kw.get('discountid') productid = params_kw.get('productid') discount_val = params_kw.get('discount') old_discount = params_kw.get('old_discount') detail_id = params_kw.get('detail_id') if not discountid or not productid: raise Exception('缺少必要参数: discountid, productid') # 过滤 NaN 和空值 if discount_val is not None: s = str(discount_val).strip().lower() if s in ('', 'nan', 'none', 'null'): discount_val = None if old_discount is not None: s = str(old_discount).strip().lower() if s in ('', 'nan', 'none', 'null'): old_discount = None # 对比新旧值,相同则跳过 if discount_val is not None and old_discount is not None: try: if float(discount_val) == float(old_discount): result = {'widgettype': 'Message', 'options': {'title': 'Success', 'message': '折扣未变化', 'type': 'success', 'timeout': 3}} return result except: pass user_orgid = await get_userorgid() dbname = get_module_dbname('discount') # discount为0或空时,如果已有记录则删除 if not discount_val or float(discount_val) == 0: if detail_id: async with DBPools().sqlorContext(dbname) as sor: await sor.D('discount_detail', {'id': detail_id}) result = {'widgettype': 'Message', 'options': {'title': 'Success', 'message': '折扣已清除', 'type': 'success', 'timeout': 3}} else: result = {'widgettype': 'Message', 'options': {'title': 'Success', 'message': 'ok', 'type': 'success', 'timeout': 3}} return result async with DBPools().sqlorContext(dbname) as sor: # 检查是否客户专属折扣方案:如果是,验证折扣不高于全部用户折扣 disc_sql = "SELECT customerid, resellerid FROM discount WHERE id = ${id}$" disc_recs = await sor.sqlExe(disc_sql, {'id': discountid}) if disc_recs and disc_recs[0].customerid: # 查找全部用户默认方案的同一产品折扣 default_sql = """ SELECT dd.discount FROM discount_detail dd JOIN discount d ON dd.discountid = d.id WHERE d.resellerid = ${resellerid}$ AND d.customerid IS NULL AND dd.productid = ${productid}$ LIMIT 1 """ default_recs = await sor.sqlExe(default_sql, {'resellerid': disc_recs[0].resellerid, 'productid': productid}) if default_recs and default_recs[0].discount is not None: default_d = float(default_recs[0].discount) customer_d = float(discount_val) if customer_d > default_d: result = {'widgettype': 'Error', 'options': {'title': '折扣无效', 'message': f'客户折扣({customer_d})不能高于全部用户折扣({default_d})', 'timeout': 5}} return result if detail_id: await sor.U('discount_detail', { 'id': detail_id, 'discount': float(discount_val) }) else: await sor.C('discount_detail', { 'id': getID(), 'discountid': discountid, 'resellerid': user_orgid, 'prodtypeid': None, 'productid': productid, 'discount': float(discount_val) }) result = {'widgettype': 'Message', 'options': {'title': 'Success', 'message': '折扣保存成功', 'type': 'success', 'timeout': 3}} except Exception as e: debug(f'save_discount error: {format_exc()}') result['options'] = {'title': 'Error', 'message': f'保存失败: {str(e)}', 'type': 'error', 'timeout': 5} return result