diff --git a/wwwroot/discount_setting/index.ui b/wwwroot/discount_setting/index.ui index f7901e7..531e75e 100644 --- a/wwwroot/discount_setting/index.ui +++ b/wwwroot/discount_setting/index.ui @@ -1,6 +1,4 @@ {% set products = get_discount_setting_products(request) %} -{% set save_url = entire_url('./save_discount.dspy') %} -{% set disc_id = params_kw.discountid %} { "widgettype": "VBox", "options": { @@ -63,23 +61,31 @@ {"widgettype": "Text", "options": {"text": "{{p.product_name or ''}}", "width": "34%", "padding": "0 12px", "fontSize": "12px", "color": "#F1F5F9"}}, {"widgettype": "Text", "options": {"text": "{{p.product_code or ''}}", "width": "22%", "padding": "0 12px", "fontSize": "12px", "color": "#E2E8F0"}}, { - "widgettype": "VBox", + "widgettype": "UiFloat", "options": { "width": "22%", "padding": "0 12px", - "data-pid": "{{p.productid}}", - "data-did": "{{p.detail_id or ''}}" + "name": "discount", + "value": "{{p.discount or ''}}", + "placeholder": "0~1", + "dec_len": 2, + "fontSize": "12px" }, - "subwidgets": [ + "binds": [ { - "widgettype": "UiFloat", + "wid": "self", + "event": "blur", + "actiontype": "urlwidget", + "datawidget": "self", + "target": "self", "options": { - "width": "100%", - "name": "discount", - "value": "{{p.discount or ''}}", - "placeholder": "0~1", - "dec_len": 2, - "fontSize": "12px" + "url": "{{entire_url('./save_discount.dspy')}}", + "params": { + "discountid": "{{params_kw.discountid}}", + "old_discount": "{{p.discount or ''}}", + "productid": "{{p.productid}}", + "detail_id": "{{p.detail_id or ''}}" + } } } ] @@ -90,14 +96,6 @@ ] } ] - ], - "binds": [ - { - "wid": "self", - "event": "focusin", - "actiontype": "script", - "target": "self", - "script": "if(window._discSetupDone)return;window._discSetupDone=true;setInterval(function(){document.querySelectorAll('[data-pid] input[type=number]').forEach(function(inp){if(inp._db)return;inp._db=true;inp.addEventListener('blur',function(){var nv=inp.value;var w=inp.closest('[data-pid]');fetch('{{save_url}}',{method:'POST',headers:{'Content-Type':'application/json'},body:JSON.stringify({discountid:'{{disc_id}}',productid:w.getAttribute('data-pid'),detail_id:w.getAttribute('data-did')||'',discount:nv})}).then(function(r){return r.json()}).then(function(r){if(r.widgettype==='Error')bricks.show_error(r.options)}).catch(function(e){console.error(e)})})})},500)" - } - ] } + ] +} diff --git a/wwwroot/discount_setting/save_discount.dspy b/wwwroot/discount_setting/save_discount.dspy index be12f87..fca532e 100644 --- a/wwwroot/discount_setting/save_discount.dspy +++ b/wwwroot/discount_setting/save_discount.dspy @@ -1,54 +1,60 @@ -# Upsert折扣明细:有detail_id则更新,无则新增 -# 参数: discountid, productid, category_id, discount, detail_id(可选) +# Upsert折扣明细:对比old_discount和discount,不同才更新 +# 参数: discountid, productid, discount, old_discount, detail_id(可选) result = {'widgettype': 'Message', 'options': {'title': 'Error', 'message': 'Invalid', 'type': 'error'}} try: discountid = params_kw.get('discountid') productid = params_kw.get('productid') - category_id = params_kw.get('category_id') 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') - + + # 对比新旧值,相同则跳过 + 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'}} + 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: - dbname = get_module_dbname('discount') async with DBPools().sqlorContext(dbname) as sor: await sor.D('discount_detail', {'id': detail_id}) result = {'widgettype': 'Message', 'options': {'title': 'Success', 'message': '折扣已清除', 'type': 'success'}} else: result = {'widgettype': 'Message', 'options': {'title': 'Success', 'message': 'ok', 'type': 'success'}} return result - - dbname = get_module_dbname('discount') - user_orgid = await get_userorgid() - + async with DBPools().sqlorContext(dbname) as sor: 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': category_id, + 'prodtypeid': None, 'productid': productid, 'discount': float(discount_val) }) - + result = {'widgettype': 'Message', 'options': {'title': 'Success', 'message': '折扣保存成功', 'type': 'success'}} except Exception as e: - debug(f'save_discount error: {e}') + debug(f'save_discount error: {format_exc()}') result['options'] = {'title': 'Error', 'message': f'保存失败: {str(e)}', 'type': 'error'} return result