Merge remote-tracking branch 'origin/test' into test
This commit is contained in:
@@ -42,17 +42,12 @@ BEGIN
|
||||
SELECT COUNT(*) INTO n FROM information_schema.columns
|
||||
WHERE table_schema = DATABASE() AND table_name = 'ar_bill' AND column_name = 'profit_sales_user_name';
|
||||
IF n = 0 THEN ALTER TABLE ar_bill ADD COLUMN profit_sales_user_name VARCHAR(64) NULL COMMENT '账单销售员姓名快照'; END IF;
|
||||
SELECT COUNT(*) INTO n FROM information_schema.columns
|
||||
WHERE table_schema = DATABASE() AND table_name = 'ar_bill' AND column_name = 'profit_snapshot_source';
|
||||
IF n = 0 THEN ALTER TABLE ar_bill ADD COLUMN profit_snapshot_source VARCHAR(16) NULL COMMENT 'NEW生成时快照/BACKFILLED历史补录'; END IF;
|
||||
|
||||
SELECT COUNT(*) INTO n FROM information_schema.columns WHERE table_schema = DATABASE() AND table_name = 'ar_bill'
|
||||
AND is_nullable = 'YES'
|
||||
AND ((column_name = 'profit_customer_area' AND data_type = 'varchar' AND character_maximum_length = 50)
|
||||
OR (column_name = 'profit_sales_user_id' AND data_type = 'bigint')
|
||||
OR (column_name IN ('profit_sales_user_code','profit_sales_user_name') AND data_type = 'varchar' AND character_maximum_length = 64)
|
||||
OR (column_name = 'profit_snapshot_source' AND data_type = 'varchar' AND character_maximum_length = 16));
|
||||
IF n <> 5 THEN SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = '账单快照字段结构冲突'; END IF;
|
||||
OR (column_name IN ('profit_sales_user_code','profit_sales_user_name') AND data_type = 'varchar' AND character_maximum_length = 64));
|
||||
IF n <> 4 THEN SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = '账单快照字段结构冲突'; END IF;
|
||||
SELECT COUNT(*) INTO n FROM information_schema.statistics WHERE table_schema = DATABASE()
|
||||
AND table_name = 'ar_bill' AND index_name = 'idx_ar_profit_sales_date';
|
||||
IF n = 0 THEN ALTER TABLE ar_bill ADD INDEX idx_ar_profit_sales_date (profit_sales_user_id, bill_date, ar_bill_id); END IF;
|
||||
@@ -256,7 +251,7 @@ JOIN ctr_customer c ON c.customer_id = b.customer_id
|
||||
JOIN sys_user u ON CONVERT(CAST(u.user_id AS CHAR) USING utf8mb4) COLLATE utf8mb4_unicode_ci = c.sales_user_id AND u.del_flag = '0'
|
||||
JOIN ar_bill_detail d ON d.ar_bill_id = b.ar_bill_id
|
||||
JOIN prc_order_bill p ON p.bill_id = d.prc_order_bill_id
|
||||
WHERE b.profit_sales_user_id IS NULL AND (b.profit_snapshot_source IS NULL OR b.profit_snapshot_source = 'BACKFILLED')
|
||||
WHERE b.profit_sales_user_id IS NULL
|
||||
AND p.belong_saler_code IS NOT NULL
|
||||
AND TRIM(p.belong_saler_code) <> ''
|
||||
AND TRIM(p.belong_saler_code) <> u.user_name COLLATE utf8mb4_unicode_ci
|
||||
@@ -265,7 +260,7 @@ WHERE b.profit_sales_user_id IS NULL AND (b.profit_snapshot_source IS NULL OR b.
|
||||
-- 此处仅补空值。客户区域来自客户当前资料,并非出账时的历史事实。
|
||||
UPDATE ar_bill b JOIN ctr_customer c ON c.customer_id = b.customer_id
|
||||
SET b.profit_customer_area = c.customer_area
|
||||
WHERE b.profit_customer_area IS NULL AND (b.profit_snapshot_source IS NULL OR b.profit_snapshot_source = 'BACKFILLED')
|
||||
WHERE b.profit_customer_area IS NULL
|
||||
AND c.customer_area IS NOT NULL AND TRIM(c.customer_area) <> '';
|
||||
|
||||
UPDATE ar_bill b
|
||||
@@ -274,7 +269,7 @@ JOIN sys_user u ON CONVERT(CAST(u.user_id AS CHAR) USING utf8mb4) COLLATE utf8mb
|
||||
SET b.profit_sales_user_id = u.user_id,
|
||||
b.profit_sales_user_code = u.user_name,
|
||||
b.profit_sales_user_name = u.nick_name
|
||||
WHERE b.profit_sales_user_id IS NULL AND (b.profit_snapshot_source IS NULL OR b.profit_snapshot_source = 'BACKFILLED')
|
||||
WHERE b.profit_sales_user_id IS NULL
|
||||
AND NOT EXISTS (
|
||||
SELECT 1 FROM ar_bill_detail d JOIN prc_order_bill p ON p.bill_id = d.prc_order_bill_id
|
||||
WHERE d.ar_bill_id = b.ar_bill_id AND p.belong_saler_code IS NOT NULL
|
||||
@@ -283,9 +278,6 @@ WHERE b.profit_sales_user_id IS NULL AND (b.profit_snapshot_source IS NULL OR b.
|
||||
AND TRIM(p.belong_saler_code) <> CONVERT(CAST(u.user_id AS CHAR) USING utf8mb4) COLLATE utf8mb4_unicode_ci
|
||||
);
|
||||
|
||||
UPDATE ar_bill SET profit_snapshot_source = 'BACKFILLED'
|
||||
WHERE profit_snapshot_source IS NULL AND (profit_customer_area IS NOT NULL OR profit_sales_user_id IS NOT NULL);
|
||||
|
||||
-- 历史核销比例不可追溯,不能使用当前比例替代。
|
||||
INSERT INTO ar_profit_writeoff_rate (writeoff_id, sales_user_id, rate_id, rate, create_time)
|
||||
SELECT w.writeoff_id, b.profit_sales_user_id, NULL, NULL, NOW()
|
||||
@@ -337,3 +329,36 @@ END$$
|
||||
DELIMITER ;
|
||||
CALL bms_ar_profit_upgrade_20261105_v1();
|
||||
DROP PROCEDURE IF EXISTS bms_ar_profit_upgrade_20261105_v1;
|
||||
|
||||
-- 删除已废弃的来源标记,并把保留的四个快照字段放在 customer_name 后;顺序正确时跳过表变更。
|
||||
SELECT COUNT(*) INTO @bms_ar_profit_source_column_count
|
||||
FROM information_schema.columns
|
||||
WHERE table_schema = DATABASE() AND table_name = 'ar_bill' AND column_name = 'profit_snapshot_source';
|
||||
SELECT COUNT(*) INTO @bms_ar_profit_column_order_count
|
||||
FROM information_schema.columns c
|
||||
JOIN information_schema.columns base_column
|
||||
ON base_column.table_schema = c.table_schema
|
||||
AND base_column.table_name = c.table_name
|
||||
AND base_column.column_name = 'customer_name'
|
||||
WHERE c.table_schema = DATABASE() AND c.table_name = 'ar_bill'
|
||||
AND ((c.column_name = 'profit_customer_area' AND c.ordinal_position = base_column.ordinal_position + 1)
|
||||
OR (c.column_name = 'profit_sales_user_id' AND c.ordinal_position = base_column.ordinal_position + 2)
|
||||
OR (c.column_name = 'profit_sales_user_code' AND c.ordinal_position = base_column.ordinal_position + 3)
|
||||
OR (c.column_name = 'profit_sales_user_name' AND c.ordinal_position = base_column.ordinal_position + 4));
|
||||
SET @bms_ar_profit_snapshot_column_definitions =
|
||||
'MODIFY COLUMN profit_customer_area VARCHAR(50) COLLATE utf8mb4_unicode_ci NULL COMMENT ''账单客户区域快照'' AFTER customer_name,
|
||||
MODIFY COLUMN profit_sales_user_id BIGINT(20) NULL COMMENT ''账单销售员用户ID快照'' AFTER profit_customer_area,
|
||||
MODIFY COLUMN profit_sales_user_code VARCHAR(64) COLLATE utf8mb4_unicode_ci NULL COMMENT ''账单销售员工号快照'' AFTER profit_sales_user_id,
|
||||
MODIFY COLUMN profit_sales_user_name VARCHAR(64) COLLATE utf8mb4_unicode_ci NULL COMMENT ''账单销售员姓名快照'' AFTER profit_sales_user_code';
|
||||
SET @bms_ar_profit_column_order_sql = CASE
|
||||
WHEN @bms_ar_profit_source_column_count > 0 AND @bms_ar_profit_column_order_count = 4
|
||||
THEN 'ALTER TABLE ar_bill DROP COLUMN profit_snapshot_source'
|
||||
WHEN @bms_ar_profit_source_column_count > 0
|
||||
THEN CONCAT('ALTER TABLE ar_bill DROP COLUMN profit_snapshot_source, ', @bms_ar_profit_snapshot_column_definitions)
|
||||
WHEN @bms_ar_profit_column_order_count <> 4
|
||||
THEN CONCAT('ALTER TABLE ar_bill ', @bms_ar_profit_snapshot_column_definitions)
|
||||
ELSE 'SELECT 1 WHERE 1 = 0'
|
||||
END;
|
||||
PREPARE bms_ar_profit_column_order_stmt FROM @bms_ar_profit_column_order_sql;
|
||||
EXECUTE bms_ar_profit_column_order_stmt;
|
||||
DEALLOCATE PREPARE bms_ar_profit_column_order_stmt;
|
||||
|
||||
Reference in New Issue
Block a user