-- 外发流程字段补丁（可重复执行思路：逐条执行，若报 Duplicate column 说明该列已有，跳过即可）
-- 先查看已有列：SHOW COLUMNS FROM `purchase_order` LIKE 'wflow%';
--              SHOW COLUMNS FROM `purchase_order` LIKE 'pick%';
--              SHOW COLUMNS FROM `purchase_order` LIKE 'audit%';

-- 以下每条单独选中执行；1060 Duplicate column = 已存在，换下一行

-- ALTER TABLE `purchase_order` ADD COLUMN `wflow_status` tinyint(3) unsigned NOT NULL DEFAULT 0 COMMENT '0待初选1待审核2已下发' AFTER `status`;
-- ALTER TABLE `purchase_order` ADD COLUMN `pick_company_name` varchar(200) DEFAULT '' COMMENT '初选供应商' AFTER `wflow_status`;
ALTER TABLE `purchase_order` ADD COLUMN `pick_contact_name` varchar(64) DEFAULT '' COMMENT '初选联系人' AFTER `pick_company_name`;
ALTER TABLE `purchase_order` ADD COLUMN `pick_email` varchar(128) DEFAULT '' AFTER `pick_contact_name`;
ALTER TABLE `purchase_order` ADD COLUMN `pick_phone` varchar(32) DEFAULT '' AFTER `pick_email`;
ALTER TABLE `purchase_order` ADD COLUMN `pick_admin_id` int(10) unsigned NOT NULL DEFAULT 0 AFTER `pick_phone`;
ALTER TABLE `purchase_order` ADD COLUMN `pick_admin_name` varchar(64) DEFAULT '' AFTER `pick_admin_id`;
ALTER TABLE `purchase_order` ADD COLUMN `pick_time` datetime DEFAULT NULL AFTER `pick_admin_name`;
ALTER TABLE `purchase_order` ADD COLUMN `audit_admin_id` int(10) unsigned NOT NULL DEFAULT 0 AFTER `pick_time`;
ALTER TABLE `purchase_order` ADD COLUMN `audit_admin_name` varchar(64) DEFAULT '' AFTER `audit_admin_id`;
ALTER TABLE `purchase_order` ADD COLUMN `audit_time` datetime DEFAULT NULL AFTER `audit_admin_name`;
ALTER TABLE `purchase_order` ADD COLUMN `pick_companies_json` mediumtext COMMENT '初选候选供应商JSON' AFTER `pick_time`;

-- 已存在该列但类型过小（报 Data too long for column pick_companies_json）时执行：
ALTER TABLE `purchase_order` MODIFY COLUMN `pick_companies_json` MEDIUMTEXT COMMENT '初选候选供应商JSON';

-- 历史数据迁移（只需执行一次；若已跑过可跳过）
-- UPDATE `purchase_order` po SET po.`wflow_status` = 2
-- WHERE po.`status` = 0 AND po.`wflow_status` = 0
--   AND EXISTS (SELECT 1 FROM `purchase_order_detail` d WHERE d.`scydgy_id` = po.`scydgy_id` LIMIT 1);
-- UPDATE `purchase_order` SET `wflow_status` = 2 WHERE `status` = 1 AND `wflow_status` = 0;
