-- ================================================================
-- 대구광역시 공식 종량제·음식물 납부필증 가격(2024.7.1~) 및 포장단위 현실값 반영
-- 출처: https://www.daegu.go.kr/env/index.do?menu_id=00001325 (최근수정 2026.01.27)
--
-- bp_consumer  : 시 판매가(장/매)
-- bp_wholesale : 지정판매소 매입가 ≈ 판매가의 93% (도매 마진)
-- bp_order_price: 제작 발주단가(장) ≈ 판매가의 20% (제조 원가 수준)
--
-- 포장: 대구 동구청 가격표(묶음·박스 매수) — dong.daegu.kr/reserve/contents.do?mid=0302000000
--   일반 3L:  20매/팩 × 150팩/박스 = 3,000매
--   일반 5L:  20매/팩 × 150팩/박스 = 3,000매 (표 미기재, 3L 동일 계열)
--   일반 10L: 10매/팩 × 200팩/박스 = 2,000매
--   일반 20L: 50매/팩 × 20팩/박스  = 1,000매
--   일반 30L: 50매/팩 × 6팩/박스   = 300매 (표 미기재)
--   일반 50L: 20매/팩 × 25팩/박스  = 500매
--   일반 75L: 10매/팩 × 30팩/박스  = 300매
--   음식물·스티커: 필증/스티커는 100매/팩 등 별도 (본 스크립트 일반·공공·공동주택·재사용만)
--
-- 실행: mysql ... jongryangje < writable/database/fix_bag_price_packaging_daegu_realistic.sql
-- 이어서 O종 전체(27코드): fix_bag_price_packaging_daegu_realistic_all_codes.sql
-- ================================================================

SET NAMES utf8mb4;

-- ---------- 현행 단가 (적용 중인 행) ----------
UPDATE `bag_price` SET
  `bp_order_price` = CASE `bp_bag_code`
    WHEN '10112' THEN 24.00
    WHEN '10122' THEN 36.00
    WHEN '10132' THEN 68.00
    WHEN '10152' THEN 134.00
    WHEN '10162' THEN 216.00
    WHEN '10172' THEN 332.00
    WHEN '10192' THEN 494.00
    WHEN '10202' THEN 0.00
    WHEN '40152' THEN 128.00
    WHEN '20172' THEN 332.00
    WHEN '60102' THEN 22.00
    WHEN '60132' THEN 116.00
    ELSE `bp_order_price`
  END,
  `bp_wholesale` = CASE `bp_bag_code`
    WHEN '10112' THEN 112.00
    WHEN '10122' THEN 167.00
    WHEN '10132' THEN 316.00
    WHEN '10152' THEN 623.00
    WHEN '10162' THEN 1004.00
    WHEN '10172' THEN 1544.00
    WHEN '10192' THEN 2297.00
    WHEN '10202' THEN 0.00
    WHEN '40152' THEN 596.00
    WHEN '20172' THEN 1544.00
    WHEN '60102' THEN 102.00
    WHEN '60132' THEN 539.00
    ELSE `bp_wholesale`
  END,
  `bp_consumer` = CASE `bp_bag_code`
    WHEN '10112' THEN 120.00
    WHEN '10122' THEN 180.00
    WHEN '10132' THEN 340.00
    WHEN '10152' THEN 670.00
    WHEN '10162' THEN 1080.00
    WHEN '10172' THEN 1660.00
    WHEN '10192' THEN 2470.00
    WHEN '10202' THEN 0.00
    WHEN '40152' THEN 640.00
    WHEN '20172' THEN 1660.00
    WHEN '60102' THEN 110.00
    WHEN '60132' THEN 580.00
    ELSE `bp_consumer`
  END
WHERE `bp_bag_code` IN (
  '10112','10122','10132','10152','10162','10172','10192','10202',
  '40152','20172','60102','60132'
)
  AND `bp_state` = 1
  AND (`bp_end_date` IS NULL OR `bp_end_date` >= CURDATE());

-- ---------- 포장 단위 ----------
UPDATE `packaging_unit` SET
  `pu_pack_per_sheet` = CASE `pu_bag_code`
    WHEN '10112' THEN 20
    WHEN '10122' THEN 20
    WHEN '10132' THEN 10
    WHEN '10152' THEN 50
    WHEN '10162' THEN 50
    WHEN '10172' THEN 20
    WHEN '10192' THEN 10
    WHEN '10202' THEN 10
    WHEN '40152' THEN 50
    WHEN '20172' THEN 20
    WHEN '60102' THEN 100
    WHEN '60132' THEN 50
    ELSE `pu_pack_per_sheet`
  END,
  `pu_box_per_pack` = CASE `pu_bag_code`
    WHEN '10112' THEN 150
    WHEN '10122' THEN 150
    WHEN '10132' THEN 200
    WHEN '10152' THEN 20
    WHEN '10162' THEN 6
    WHEN '10172' THEN 25
    WHEN '10192' THEN 30
    WHEN '10202' THEN 5
    WHEN '40152' THEN 20
    WHEN '20172' THEN 25
    WHEN '60102' THEN 10
    WHEN '60132' THEN 10
    ELSE `pu_box_per_pack`
  END,
  `pu_total_per_box` = CASE `pu_bag_code`
    WHEN '10112' THEN 3000
    WHEN '10122' THEN 3000
    WHEN '10132' THEN 2000
    WHEN '10152' THEN 1000
    WHEN '10162' THEN 300
    WHEN '10172' THEN 500
    WHEN '10192' THEN 300
    WHEN '10202' THEN 50
    WHEN '40152' THEN 1000
    WHEN '20172' THEN 500
    WHEN '60102' THEN 1000
    WHEN '60132' THEN 500
    ELSE `pu_total_per_box`
  END
WHERE `pu_bag_code` IN (
  '10112','10122','10132','10152','10162','10172','10192','10202',
  '40152','20172','60102','60132'
)
  AND `pu_state` = 1
  AND (`pu_end_date` IS NULL OR `pu_end_date` >= CURDATE());

-- ---------- 판매·주문 단가를 현행 소비자가에 맞춤(선택적 정합) ----------
UPDATE `bag_sale` bs
INNER JOIN `bag_price` bp
  ON bp.`bp_lg_idx` = bs.`bs_lg_idx`
 AND bp.`bp_bag_code` = bs.`bs_bag_code`
 AND bp.`bp_state` = 1
 AND (bp.`bp_end_date` IS NULL OR bp.`bp_end_date` >= CURDATE())
SET
  bs.`bs_unit_price` = bp.`bp_consumer`,
  bs.`bs_amount` = bs.`bs_qty` * bp.`bp_consumer`
WHERE bs.`bs_type` = 'sale'
  AND bp.`bp_consumer` > 0;

UPDATE `shop_order_item` soi
INNER JOIN `shop_order` so ON so.`so_idx` = soi.`soi_so_idx`
INNER JOIN `bag_price` bp
  ON bp.`bp_lg_idx` = so.`so_lg_idx`
 AND bp.`bp_bag_code` = soi.`soi_bag_code`
 AND bp.`bp_state` = 1
 AND (bp.`bp_end_date` IS NULL OR bp.`bp_end_date` >= CURDATE())
SET
  soi.`soi_unit_price` = bp.`bp_consumer`,
  soi.`soi_amount` = soi.`soi_qty` * bp.`bp_consumer`
WHERE bp.`bp_consumer` > 0;
