create procedure PRO_update_Goods_group_stock is v_min_stock NUMBER(10, 2); v_gg_stock NUMBER(10, 2); v_goods_no number; v_sum_cost NUMBER(10, 2); begin for g_goods_no in (select ggi.goods_no from goods_group_item ggi where ggi.is_delete = 'N' GROUP by ggi.goods_no) loop select count(*) into v_goods_no from goods_no_stock gn where gn.goods_no = g_goods_no.goods_no; if v_goods_no > 0 then continue; end if; --查詢組合子商品的庫存再除以綁定的數量 取最小值 gg主商品 g子商品 select min(TRUNC(g.available_stock / decode(gg1.group_goods_amount, 0, 1, gg1.group_goods_amount), 0)) into v_min_stock from goods_group_item gg1, goods g, goods gg where gg1.goods_no = g_goods_no.goods_no and gg1.is_delete = 'N' and gg.goods_id = gg1.goods_id and gg.is_delete = 'N' and g.goods_id = gg1.GROUP_GOODS_ID; --計算成本價格 gg主商品 g子商品 select trunc(sum(g.cost_price * gg1.group_goods_amount), 2) into v_sum_cost from goods_group_item gg1, goods g, goods gg where gg1.goods_no = g_goods_no.goods_no and gg1.is_delete = 'N' and gg.goods_id = gg1.goods_id and gg.is_delete = 'N' and g.goods_id = gg1.GROUP_GOODS_ID; update goods set available_stock = v_min_stock, COST_PRICE = v_sum_cost where goods_no = g_goods_no.goods_no and is_delete = 'N'; commit; end loop; --下架問題組合碼 insert into sys_user_message (MESSAGE_ID, PRODUCT_CODE, TARGET_MODUL_ID, MESSAGE_BODY, MESSAGE_STAGE, IS_DELETE) select sys_user_message_seq.nextval, p.product_code, 254, '商品:'||p.product_code||' 沒有維護組合碼', '0', 'N' from product p where not exists (select 1 from goods_group_item ggi where ggi.goods_no = p.product_code) and p.is_delete = 'N' and p.is_onsale = 'Y' and p.product_code like 'A%'; update product p set p.is_onsale = 'N' where not exists (select 1 from goods_group_item ggi where ggi.goods_no = p.product_code) and p.is_delete = 'N' and p.is_onsale = 'Y' and p.product_code like 'A%'; commit; end; select * from goods_no_stock gn where gn.goods_no in (select ggi.goods_no from goods_group_item ggi where ggi.is_delete = 'N' GROUP by ggi.goods_no); select * from goods g where g.GOODS_NO='02-00056'; select * from goods g where g.GOODS_NO='70261507'; select * from goods g where g.goods_no='70261506'; select gn.goods_no from goods_no_stock gn;
goods_no_stock:庫存同步黑名單表,在這張表中的數據,都不會進行相關的更新操做