feat: 月度模式全面参数化 - 移除硬编码日期,前后端按月动态查询

This commit is contained in:
freedakgmail
2026-08-01 13:10:22 +08:00
parent a90c133578
commit dab06ba109
43 changed files with 9135 additions and 309 deletions
+51
View File
@@ -0,0 +1,51 @@
-- Step 2: 填充 dim_material 从配送流水+盘点数据合并去重
-- 配送流水有1835种物料,盘点数据有6133种物料,合并去重
WITH merged AS (
-- 配送流水中的物料
SELECT item_code AS material_code, item_name AS material_name, specification,
major_category, minor_category, finance_category, unit AS base_unit,
NULL::text AS supplier_code,
row_number() OVER (PARTITION BY item_code ORDER BY
(minor_category IS NOT NULL AND minor_category != '')::int DESC,
(finance_category IS NOT NULL AND finance_category != '')::int DESC,
(specification IS NOT NULL AND specification != '')::int DESC
) AS rn
FROM distribution_detail_records
WHERE item_code IS NOT NULL AND item_code != ''
UNION ALL
-- 盘点倒挤数据中的物料
SELECT item_code AS material_code, item_name AS material_name, specification,
major_category, minor_category, finance_category, unit AS base_unit,
NULL::text AS supplier_code,
row_number() OVER (PARTITION BY item_code ORDER BY
(minor_category IS NOT NULL AND minor_category != '')::int DESC,
(finance_category IS NOT NULL AND finance_category != '')::int DESC,
(specification IS NOT NULL AND specification != '')::int DESC
) AS rn
FROM inventory_cost_records
WHERE item_code IS NOT NULL AND item_code != ''
),
best AS (
SELECT DISTINCT ON (material_code) material_code, material_name, specification,
major_category, minor_category, finance_category, base_unit, supplier_code
FROM merged
ORDER BY material_code,
(minor_category IS NOT NULL AND minor_category != '') DESC,
(finance_category IS NOT NULL AND finance_category != '') DESC,
(specification IS NOT NULL AND specification != '') DESC,
(major_category IS NOT NULL AND major_category != '') DESC
)
INSERT INTO analytics.dim_material (material_code, material_name, specification, major_category, minor_category, finance_category, base_unit, supplier_code, status, created_at, updated_at)
SELECT material_code, material_name, specification, major_category, minor_category, finance_category, base_unit, supplier_code, '启用', now(), now()
FROM best
ON CONFLICT (material_code) DO UPDATE SET
material_name = EXCLUDED.material_name,
specification = COALESCE(NULLIF(EXCLUDED.specification, ''), dim_material.specification),
major_category = COALESCE(NULLIF(EXCLUDED.major_category, ''), dim_material.major_category),
minor_category = COALESCE(NULLIF(EXCLUDED.minor_category, ''), dim_material.minor_category),
finance_category = COALESCE(NULLIF(EXCLUDED.finance_category, ''), dim_material.finance_category),
base_unit = COALESCE(NULLIF(EXCLUDED.base_unit, ''), dim_material.base_unit),
updated_at = now();
+19
View File
@@ -0,0 +1,19 @@
-- Step 1: 填充 dim_supplier 从配送流水去重
INSERT INTO analytics.dim_supplier (supplier_code, supplier_name, supplier_type, contact_person, contact_phone, region, status, created_at, updated_at)
SELECT d.supplier_code, d.supplier_name, d.supplier_category, d.supplier_contact, d.supplier_phone,
CASE WHEN d.supplier_address ~ '北京' THEN '北京' WHEN d.supplier_address ~ '福建' THEN '福建'
WHEN d.supplier_address ~ '四川' THEN '四川' WHEN d.supplier_address ~ '山东' THEN '山东'
WHEN d.supplier_address ~ '新疆' THEN '新疆' WHEN d.supplier_address ~ '河北' THEN '河北'
WHEN d.supplier_address ~ '天津' THEN '天津' WHEN d.supplier_address ~ '内蒙古' THEN '内蒙古'
WHEN d.supplier_address ~ '甘肃' THEN '甘肃' WHEN d.supplier_address ~ '宁夏' THEN '宁夏'
ELSE '其他' END,
'启用', now(), now()
FROM (
SELECT DISTINCT ON (supplier_code) supplier_code, supplier_name, supplier_category, supplier_contact, supplier_phone, supplier_address
FROM distribution_detail_records WHERE supplier_code IS NOT NULL
ORDER BY supplier_code, (supplier_phone IS NOT NULL AND supplier_phone != '') DESC, (supplier_address IS NOT NULL AND supplier_address != '') DESC
) d
ON CONFLICT (supplier_code) DO UPDATE SET
supplier_name = EXCLUDED.supplier_name, supplier_type = EXCLUDED.supplier_type,
contact_person = EXCLUDED.contact_person, contact_phone = EXCLUDED.contact_phone,
region = EXCLUDED.region, updated_at = now();
+34
View File
@@ -0,0 +1,34 @@
-- Step 3: 填充 fact_inventory_snapshot 从盘点倒挤数据ETL
-- 需要将 cost_unit_code 映射为 store_code,盘点数据中有 sales_store_code 可用
INSERT INTO analytics.fact_inventory_snapshot (
store_code, material_code, snapshot_date,
opening_quantity, opening_amount,
purchase_quantity, purchase_amount,
consumption_quantity, consumption_amount,
ending_quantity, ending_amount,
waste_quantity, waste_amount,
transfer_in_quantity, transfer_out_quantity,
inventory_days, is_negative, created_at
)
SELECT
COALESCE(s.sales_store_code, i.cost_unit_code) AS store_code,
i.item_code AS material_code,
i.report_month AS snapshot_date,
i.opening_quantity, i.opening_amount,
i.purchase_quantity, i.purchase_amount,
i.consumption_quantity,
i.consumption_amount,
i.ending_quantity, i.ending_amount,
NULL::numeric AS waste_quantity,
COALESCE(i.return_loss_amount, 0) AS waste_amount,
NULL::numeric AS transfer_in_quantity,
NULL::numeric AS transfer_out_quantity,
NULL::numeric AS inventory_days,
i.is_negative_consumption AS is_negative,
now() AS created_at
FROM inventory_cost_records i
LEFT JOIN inventory_store_mapping s ON i.cost_unit_code = s.cost_unit_code
WHERE i.report_month = DATE '2026-04-01'
AND i.item_code IS NOT NULL AND i.item_code != ''
ON CONFLICT DO NOTHING;