ALTER TABLE base_procedure ADD COLUMN salary_type VARCHAR ( 16 ) COMMENT '工资类型 HourWage|PieceRateWage|NoAccounting 计时工资/计件工资/不核算' AFTER `procedure_type`;
ALTER TABLE base_procedure ADD COLUMN standard_labour_price DECIMAL ( 20, 2 ) COMMENT '额定工价' AFTER `procedure_type`;
ALTER TABLE base_procedure ADD COLUMN piecework_basis CHAR ( 2 ) COMMENT '计件依据 01：合格数量02：报工数量' AFTER `procedure_type`;

ALTER TABLE project_sale_order ADD COLUMN `external_code` VARCHAR ( 64 ) COMMENT '销售订单号 来源用友 唯一' AFTER `code`;

ALTER TABLE system_dept ADD COLUMN `code` VARCHAR ( 16 ) COMMENT '部门编码' AFTER `name`;

ALTER TABLE project_sale_order_sub ADD COLUMN `material_id` BIGINT ( 20 ) NOT NULL COMMENT '物料id' AFTER project_order_id;
alter table project_sale_order_sub DROP INDEX uk_project_name;
alter table project_sale_order_sub ADD UNIQUE KEY `uk_project_material`(`project_order_id`, `material_id`);
alter table project_sale_order_sub MODIFY COLUMN `name` varchar(128) COMMENT '子项目名称，唯一';

DROP TABLE IF EXISTS `project_sale_order`;
CREATE TABLE `project_sale_order` (
                                      `id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '自增字段，唯一',
                                      `code` varchar(64) NOT NULL COMMENT '项目编号，唯一',
                                      `external_code` varchar(64) DEFAULT NULL COMMENT '销售订单号 来源用友 唯一',
                                      `order_status` int(11) NOT NULL DEFAULT '0' COMMENT '单据状态 已保存/已送审/已审核/已启动/已打回/已终止',
                                      `order_time` datetime NOT NULL COMMENT '单据日期',
                                      `delivery_status` tinyint(1) COMMENT '发货状态 未发货/部分发货/全部发货',
                                      `business_man` bigint(20) COMMENT '业务员',
                                      `business_dept_id` bigint(20) NOT NULL COMMENT '提出部门id',
                                      `customer_id` bigint(20) NOT NULL COMMENT '客户id',
                                      `contract_no` varchar(128) DEFAULT NULL COMMENT '合同编号',
                                      `project_name` varchar(255) COMMENT '项目名称',
                                      `business_line` char(1) NOT NULL COMMENT '业务线',
                                      `blueprint_no` varchar(32) DEFAULT NULL COMMENT '图号',
                                      `has_price` tinyint(1) NOT NULL DEFAULT '0' COMMENT '是否有价格',
                                      `price` decimal(20,6) DEFAULT NULL COMMENT '价格 保留四位小数',
                                      `currency` char(1) DEFAULT NULL COMMENT '币种',
                                      `project_start_time` datetime NOT NULL COMMENT '项目开始时间',
                                      `project_end_time` datetime NOT NULL COMMENT '项目结束时间',
                                      `is_urgency` tinyint(1) NOT NULL DEFAULT '0' COMMENT '是否紧急',
                                      `property` tinyint(1) COMMENT '性质 新制|维修',
                                      `reference_technology` varchar(255) DEFAULT NULL COMMENT '可引用的原有技术',
                                      `has_alter` tinyint(1) NOT NULL DEFAULT '0' COMMENT '是否已变更',
                                      `last_alter_time` datetime DEFAULT NULL COMMENT '上一次变更时间',
                                      `quality_requirement` varchar(255) DEFAULT NULL COMMENT '检验要求',
                                      `remark` varchar(255) DEFAULT NULL COMMENT '备注',
                                      `has_contract` tinyint(1) NOT NULL DEFAULT '0' COMMENT '有无合同',
                                      `has_technology_protocol` tinyint(1) NOT NULL DEFAULT '0' COMMENT '有无技术协议',
                                      `has_blueprint` tinyint(1) NOT NULL DEFAULT '0' COMMENT '有无图纸/数模',
                                      `blueprint_remark` varchar(255) DEFAULT NULL COMMENT '图纸/数模 说明',
                                      `is_snapshot` tinyint(1) NOT NULL DEFAULT '0' COMMENT '是否快照',
                                      `snapshot_order_time` datetime DEFAULT NULL COMMENT '快照原始单据日期',
                                      `snapshot_id` bigint(20) DEFAULT NULL COMMENT '快照原始id',
                                      `snapshot_code` varchar(64) DEFAULT NULL COMMENT '快照原始单据号',
                                      `status` tinyint(1) NOT NULL DEFAULT '1' COMMENT '状态,1表示正常，2表示禁用',
                                      `creator` varchar(64) DEFAULT '' COMMENT '创建者',
                                      `create_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
                                      `updater` varchar(64) DEFAULT '' COMMENT '更新者',
                                      `update_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
                                      `deleted` bit(1) NOT NULL DEFAULT b'0' COMMENT '是否删除',
                                      `tenant_id` bigint(20) NOT NULL DEFAULT '0' COMMENT '租户编号',
                                      PRIMARY KEY (`id`) USING BTREE,
                                      UNIQUE KEY `uk_code` (`code`,`tenant_id`) USING BTREE
) ENGINE=InnoDB ROW_FORMAT=DYNAMIC COMMENT='项目销售订单表';

ALTER TABLE base_workshop MODIFY COLUMN `dept_id` BIGINT ( 20 ) COMMENT '部门id';
ALTER TABLE base_workshop MODIFY COLUMN `status` tinyint(1) NOT NULL DEFAULT '1' COMMENT '状态,1表示正常，2表示禁用，默认是1';
ALTER TABLE base_customer MODIFY COLUMN `status` TINYINT ( 1 ) NOT NULL DEFAULT '1' COMMENT '状态,1 表示正常，2 表示禁用';
ALTER TABLE base_procedure MODIFY COLUMN `procedure_type` CHAR ( 1 ) COMMENT '工序类型';
ALTER TABLE base_material MODIFY COLUMN `material_type` VARCHAR ( 32 ) NOT NULL COMMENT '物料类型';
ALTER TABLE project_sale_order ADD COLUMN order_source TINYINT ( 1 ) NOT NULL DEFAULT '1' COMMENT '单据来源 1 默认手动新增 2 接口同步' AFTER `code`;

ALTER TABLE project_sale_order MODIFY COLUMN `business_dept_id` bigint(20) COMMENT '提出部门id';
ALTER TABLE project_sale_order MODIFY COLUMN `business_line` char(1) COMMENT '业务线';
ALTER TABLE project_sale_order MODIFY COLUMN `project_start_time` datetime COMMENT '项目开始时间';
ALTER TABLE project_sale_order MODIFY COLUMN `project_end_time` datetime COMMENT '项目结束时间';

ALTER TABLE project_sale_order_sub MODIFY COLUMN `composition_id` BIGINT ( 20 ) COMMENT '材质';
ALTER TABLE infra_file ADD COLUMN `business_file_remark` VARCHAR(255) COMMENT '业务文件说明' AFTER `business_file_type`;

drop table if exists project_plan_task;
drop table if exists project_plan_task_bom;


drop table if exists `project_plan_workshop`;
CREATE TABLE `project_plan_workshop` (
                                         `id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '自增字段，唯一',
                                         `project_plan_id` bigint(20) NOT NULL COMMENT '计划id',
                                         `project_id` bigint(20) NOT NULL COMMENT '项目id',
                                         `workshop_id` bigint(20) NOT NULL COMMENT '车间id',
                                         `start_time` datetime COMMENT '预计开始时间',
                                         `end_time` datetime COMMENT '预计结束时间',
                                         `creator` varchar(64) DEFAULT '' COMMENT '创建者',
                                         `create_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
                                         `updater` varchar(64) DEFAULT '' COMMENT '更新者',
                                         `update_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
                                         `deleted` bit(1) NOT NULL DEFAULT b'0' COMMENT '是否删除',
                                         `tenant_id` bigint(20) NOT NULL DEFAULT '0' COMMENT '租户编号',
                                         PRIMARY KEY (`id`) USING BTREE,
                                         UNIQUE KEY `uk_project_workshop` (`project_plan_id`, `workshop_id`,`tenant_id`) USING BTREE
) ENGINE=InnoDB COMMENT='生产计划车间计划';

ALTER TABLE pro_task_dispatch ADD COLUMN `workshop_id` BIGINT ( 20 ) NOT NULL COMMENT '车间id' AFTER `project_sub_id`;

drop table if exists pro_process_bom;
drop table if exists pro_process_bom_detail;

alter table project_sale_order_sub drop column unit;
ALTER TABLE project_plan_sub DROP COLUMN project_sub_code;

ALTER TABLE pro_task_dispatch MODIFY COLUMN `dispatch_type` VARCHAR ( 16 ) COMMENT '派工类型 生产任务|装配任务 PRODUCTION|ASSEMBLE';

ALTER TABLE `project_sale_order` ADD COLUMN `external_id` VARCHAR ( 32 ) NULL COMMENT '销售订单id 来源用友 唯一' AFTER `order_source`;

ALTER TABLE base_equip_manufacture ADD COLUMN `workshop_id` BIGINT ( 20 ) NOT NULL COMMENT '车间id' AFTER `model`;


drop table if exists `base_equip_maintenance_record`;
CREATE TABLE `base_equip_maintenance_record` (
                                                 `id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '自增字段，唯一',
                                                 `code` varchar(64) NOT NULL COMMENT '编码，唯一',
                                                 `type` char(1) NOT NULL COMMENT '名称 维修|保养',
                                                 `equip_id` bigint(20) NOT NULL COMMENT '设备id',
                                                 `submit_time` datetime COMMENT '上报日期',
                                                 `description` varchar(255) DEFAULT NULL COMMENT '描述',
                                                 `owner` bigint(20) NOT NULL COMMENT '维保人',
                                                 `progress` char(1) NOT NULL COMMENT '维保进度 未开始/进行中/已完成',
                                                 `time_consumed` decimal(20,2) COMMENT '耗时 单位小时',
                                                 `remark` varchar(255) DEFAULT NULL COMMENT '进度说明',
                                                 `status` tinyint(1) NOT NULL DEFAULT '1' COMMENT '状态,1表示正常，2表示禁用',
                                                 `creator` varchar(64) DEFAULT '' COMMENT '创建者',
                                                 `create_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
                                                 `updater` varchar(64) DEFAULT '' COMMENT '更新者',
                                                 `update_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
                                                 `deleted` bit(1) NOT NULL DEFAULT b'0' COMMENT '是否删除',
                                                 `tenant_id` bigint(20) NOT NULL DEFAULT '0' COMMENT '租户编号',
                                                 PRIMARY KEY (`id`) USING BTREE,
                                                 UNIQUE KEY `uk_code` (`code`,`tenant_id`) USING BTREE
) ENGINE=InnoDB COMMENT='设备维保记录表';

ALTER TABLE `project_sale_order_sub` ADD COLUMN production_detail_id VARCHAR ( 32 ) COMMENT '生产加工单明细id' AFTER project_order_id;
ALTER TABLE `project_sale_order_sub` ADD COLUMN production_id VARCHAR ( 32 ) COMMENT '生产加工单id' AFTER project_order_id;

ALTER TABLE pro_unqualified_notification DROP COLUMN `bom_detail_id`;
ALTER TABLE pro_unqualified_notification ADD COLUMN `workshop_id` BIGINT ( 20 ) NOT NULL COMMENT '车间id' AFTER `project_sub_id`;

ALTER TABLE pro_task_report MODIFY COLUMN `dispatch_detail_id` BIGINT ( 20 ) COMMENT '派工单任务明细id';

ALTER TABLE pro_task_report ADD COLUMN `dispatch_id` BIGINT ( 20 ) COMMENT '派工单id' AFTER `id`;
ALTER TABLE pro_task_report ADD COLUMN `procedure_id` bigint(20) COMMENT '工序id' AFTER `dispatch_detail_id`;
ALTER TABLE pro_task_report ADD COLUMN `type` VARCHAR(16) NOT NULL DEFAULT 'PRODUCTION' COMMENT '报工任务类型 生产|包装 PRODUCTION|ASSEMBLE' AFTER `dispatch_detail_id`;
ALTER TABLE pro_task_report ADD COLUMN `assemble_code` VARCHAR(32) COMMENT '包装码' AFTER `dispatch_detail_id`;

DROP TABLE IF EXISTS `sc_equipment`;
CREATE TABLE `sc_equipment`  (
                                 `Id` int(11) NOT NULL COMMENT '设备id',
                                 `Nmae` varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL DEFAULT NULL COMMENT '设备名称',
                                 `OEE` decimal(24, 2) NULL DEFAULT NULL COMMENT 'OEE',
                                 `UtilizationRate` decimal(24, 2) NULL DEFAULT NULL COMMENT '设备利用率',
                                 `QualificationRate` decimal(24, 2) NULL DEFAULT NULL COMMENT '产品合格率',
                                 `ShutdownTime` decimal(24, 2) NULL DEFAULT NULL COMMENT '当日停机次数',
                                 `Num` decimal(24, 2) NULL DEFAULT NULL COMMENT '当日合格数量',
                                 `Statuss` int(11) NULL DEFAULT NULL COMMENT '设备状态',
                                 PRIMARY KEY (`Id`) USING BTREE
) ENGINE = InnoDB comment='数采设备表';

alter table wms_storage_mat add column `procedure_id` bigint(20) COMMENT '工序id' AFTER `lot_no`;

ALTER TABLE sc_equipment ADD COLUMN `deleted` BIT ( 1 ) NOT NULL DEFAULT b '0' COMMENT '是否删除';
ALTER TABLE sc_equipment ADD COLUMN `creator` varchar(64) DEFAULT '' COMMENT '创建者';
ALTER TABLE sc_equipment ADD COLUMN `create_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间';
ALTER TABLE sc_equipment ADD COLUMN `updater` varchar(64) DEFAULT '' COMMENT '更新者';
ALTER TABLE sc_equipment ADD COLUMN `update_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间';

ALTER TABLE wms_wh MODIFY COLUMN `wh_status` TINYINT (1) NOT NULL DEFAULT '1' COMMENT '状态,1 表示正常，2 表示禁用';
ALTER TABLE project_sale_order_sub ADD COLUMN `warehouse_id` BIGINT ( 20 ) COMMENT '仓库id' AFTER `material_id`;

ALTER TABLE `project_sale_order` ADD COLUMN `warehouse_id` BIGINT ( 20 ) DEFAULT NULL COMMENT '仓库id' AFTER `order_status`;

ALTER TABLE base_material ADD COLUMN inventory_property INT ( 11 ) DEFAULT '0' COMMENT '物料属性' AFTER `description`;



drop table if exists `base_system_parameters`;
CREATE TABLE `base_system_parameters` (
                                          `id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '自增字段，唯一',
                                          `creator` varchar(64) COLLATE DEFAULT '' COMMENT '创建者',
                                          `create_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
                                          `updater` varchar(64) COLLATE DEFAULT '' COMMENT '更新者',
                                          `update_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
                                          `deleted` bit(1) NOT NULL DEFAULT b'0' COMMENT '是否删除',
                                          `tenant_id` bigint(20) NOT NULL DEFAULT '0' COMMENT '租户编号',
                                          `code` varchar(255) COLLATE DEFAULT NULL COMMENT '参数编码',
                                          `name` varchar(255) COLLATE DEFAULT NULL COMMENT '参数名称',
                                          `scope` varchar(255) COLLATE DEFAULT NULL COMMENT '值范围',
                                          `current_value` varchar(255) COLLATE DEFAULT NULL COMMENT '当前值',
                                          `description` varchar(255) COLLATE DEFAULT NULL COMMENT '备注',
                                          PRIMARY KEY (`id`)
) ENGINE=InnoDB COMMENT='系统参数设置表';

drop table if exists `base_process`;
CREATE TABLE `base_process` (
                                `id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '自增字段，唯一',
                                `code` varchar(64) COLLATE DEFAULT NULL COMMENT '工艺路线编号',
                                `name` varchar(64) COLLATE NOT NULL COMMENT '工艺路线名称,唯一',
                                `status` tinyint(1) NOT NULL COMMENT '状态,1表示正常，2表示禁用',
                                `creator` varchar(64) COLLATE DEFAULT '' COMMENT '创建者',
                                `create_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
                                `updater` varchar(64) COLLATE DEFAULT '' COMMENT '更新者',
                                `update_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
                                `deleted` bit(1) NOT NULL DEFAULT b'0' COMMENT '是否删除',
                                `tenant_id` bigint(20) NOT NULL DEFAULT '0' COMMENT '租户编号',
                                PRIMARY KEY (`id`)
) ENGINE=InnoDB COMMENT='工艺路线表';

drop table if exists `base_process_detail`;
CREATE TABLE `base_process_detail` (
                                       `id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '自增字段，唯一',
                                       `process_id` bigint(20) DEFAULT NULL COMMENT '工艺路线id',
                                       `procedure_id` bigint(20) DEFAULT NULL COMMENT '工序id',
                                       `procedure_sort` int(11) DEFAULT NULL COMMENT '工序顺序号',
                                       `procedure_equip_id` bigint(20) DEFAULT NULL COMMENT '生产设备id',
                                       `type` int(11) DEFAULT NULL COMMENT '明细类型：1为工序，2为产品',
                                       `material_id` bigint(20) DEFAULT NULL COMMENT '产品id（物料）',
                                       `creator` varchar(64) COLLATE DEFAULT '' COMMENT '创建者',
                                       `create_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
                                       `updater` varchar(64) COLLATE DEFAULT '' COMMENT '更新者',
                                       `update_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
                                       `deleted` bit(1) NOT NULL DEFAULT b'0' COMMENT '是否删除',
                                       `tenant_id` bigint(20) NOT NULL DEFAULT '0' COMMENT '租户编号',
                                       PRIMARY KEY (`id`)
) ENGINE=InnoDB COMMENT='工艺路线明细表';

alter table base_material add COLUMN `process_id` BIGINT null comment '工艺路线id';
alter table base_material add COLUMN `safe_amount` BIGINT null comment '安全库存量';

drop table if exists `base_process_setting`;
CREATE TABLE `base_process_setting` (
                                        `id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '自增字段，唯一',
                                        `process_id` bigint(20) DEFAULT NULL COMMENT '工艺路线id',
                                        `procedure_id` bigint(20) DEFAULT NULL COMMENT '工序id',
                                        `user_id` bigint(20) DEFAULT NULL COMMENT '用户id',
                                        `salary_type` int(11) DEFAULT NULL COMMENT '工资类型',
                                        `price` decimal(10,4) DEFAULT NULL COMMENT '工价',
                                        `work_time` decimal(10,4) DEFAULT NULL COMMENT '标准工时',
                                        `creator` varchar(64) DEFAULT '' COMMENT '创建者',
                                        `create_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
                                        `updater` varchar(64)  DEFAULT '' COMMENT '更新者',
                                        `update_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
                                        `deleted` bit(1) NOT NULL DEFAULT b'0' COMMENT '是否删除',
                                        `tenant_id` bigint(20) NOT NULL DEFAULT '0' COMMENT '租户编号',
                                        PRIMARY KEY (`id`)
) ENGINE=InnoDB COMMENT='工时工价表';


alter table pro_task_dispatch_detail add column `owner_ids` VARCHAR(128) null comment '多个负责人' after `owner`;
alter table pro_task_dispatch_detail modify column `owner` bigint(20) null comment '负责人' ;
alter table pro_task_report add COLUMN `has_synch` tinyint(1) default null comment '是否已同步用友';
