puzhibing
刚刚 8ea02ca30907216aece38fda1ba918e5a5246abe
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
-- VIN码充电改造(account库):VIN 账户锁定金额 + 分配流水扩展(消费流水)
-- 目的:
--   1) t_vin_management 新增 locked_amount:分配模式下 VIN 充电申请时锁定 VIN 余额
--   2) t_company_allocate_record 新增 change_type / charging_order_code:区分「分配」与「消费」流水,并按订单号幂等
-- 创建日期: 2026-09-29
-- 目标库:charging_pile_account(可直接经 Mycat 8066 执行,非分片表)
-- 幂等说明:ALTER 重复执行会报 Duplicate column / Duplicate key name,忽略即可(以小节 B 回查为准)
-- 兼容说明:历史流水 change_type 默认 1(分配)、charging_order_code 为 NULL;唯一键 (charging_order_code, change_type)
--           在 MySQL 中允许多个 NULL 共存,历史数据不受影响
 
USE `charging_pile_account`;
 
-- ============ 小节 A:VIN 管理表新增锁定金额 ============
ALTER TABLE `t_vin_management`
  ADD COLUMN `locked_amount` decimal(12,2) NOT NULL DEFAULT '0.00' COMMENT '锁定金额(VIN码充电申请时冻结,结算后释放)';
 
-- ============ 小节 B:分配记录表扩展(分配/消费区分 + 订单号幂等键) ============
ALTER TABLE `t_company_allocate_record`
  ADD COLUMN `change_type` tinyint NOT NULL DEFAULT '1' COMMENT '变更类型:1=分配(历史默认),2=消费(充电结算)';
 
ALTER TABLE `t_company_allocate_record`
  ADD COLUMN `charging_order_code` varchar(64) DEFAULT NULL COMMENT '充电订单号(消费流水写入,用于幂等与追溯)';
 
ALTER TABLE `t_company_allocate_record`
  ADD UNIQUE KEY `uk_order_change` (`charging_order_code`, `change_type`);
 
ALTER TABLE `t_company_allocate_record`
  ADD KEY `idx_change_type` (`change_type`);
 
-- ============ 小节 C:回查校验 ============
SELECT t.table_name, c.column_name, c.column_type, c.column_comment
FROM information_schema.tables t
JOIN information_schema.columns c
  ON c.table_schema = t.table_schema
 AND c.table_name = t.table_name
WHERE t.table_schema = DATABASE()
  AND (
        (t.table_name = 't_vin_management' AND c.column_name = 'locked_amount')
     OR (t.table_name = 't_company_allocate_record' AND c.column_name IN ('change_type', 'charging_order_code'))
      );
 
SELECT index_name, GROUP_CONCAT(column_name ORDER BY seq_in_index) AS columns_in_index
FROM information_schema.statistics
WHERE table_schema = DATABASE()
  AND table_name = 't_company_allocate_record'
  AND index_name IN ('uk_order_change', 'idx_change_type')
GROUP BY index_name;