INSERT INTO mall_store_inventory (store_id, product_id, available, on_hand, updated_by, updated_at) VALUES (#{storeId}, #{productId}, COALESCE(#{available},TRUE), COALESCE(#{onHand},0), #{updatedBy}, NOW()) ON CONFLICT (store_id, product_id) DO UPDATE SET available = EXCLUDED.available, on_hand = EXCLUDED.on_hand, updated_by = EXCLUDED.updated_by, updated_at = NOW() INSERT INTO mall_store_inventory (store_id, product_id, available, updated_by, updated_at) VALUES (#{storeId}, #{productId}, #{available}, #{updatedBy}, NOW()) ON CONFLICT (store_id, product_id) DO UPDATE SET available = #{available}, updated_by = #{updatedBy}, updated_at = NOW() UPDATE mall_store_inventory SET on_hand = GREATEST(COALESCE(on_hand,0) + #{delta}, 0), updated_at = NOW() WHERE store_id = #{storeId} AND product_id = #{productId}