DROP TABLE IF EXISTS `pro_task_report`;
CREATE TABLE `pro_task_report` (
                                   `id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '自增字段，唯一',
                                   `dispatch_detail_id` bigint(20) NOT NULL COMMENT '派工单任务明细id',
                                   `owner` bigint(20) NOT NULL COMMENT '负责人',
                                   `amount` int(11) NOT NULL DEFAULT '0' COMMENT '数量',
                                   `start_time` datetime NOT NULL COMMENT '开始时间',
                                   `end_time` datetime NOT NULL COMMENT '结束时间',
                                   `work_time` DECIMAL(20,2) not null comment '报工工时',
                                   `report_time` datetime NOT NULL COMMENT '报工时间',
                                   `has_report` bit(1) NOT NULL DEFAULT b'0' COMMENT '是否已报工 0 默认否 1 是',
                                   `status` tinyint(1) NOT NULL DEFAULT '1' COMMENT '状态,1表示正常，2表示禁用',
                                   `creator` varchar(64) COLLATE utf8mb4_unicode_ci DEFAULT '' COMMENT '创建者',
                                   `create_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
                                   `updater` varchar(64) COLLATE utf8mb4_unicode_ci 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,
                                   KEY `idx_dispatch_id` (`dispatch_detail_id`) USING BTREE
) ENGINE=InnoDB COMMENT='任务报工表';

DROP TABLE IF EXISTS `pro_unqualified_notification`;
CREATE TABLE `pro_unqualified_notification` (
                                                `id` BIGINT ( 20 ) NOT NULL AUTO_INCREMENT COMMENT '自增字段，唯一',
                                                `code` VARCHAR ( 32 ) DEFAULT NULL COMMENT '编号，唯一',
                                                `type` CHAR ( 1 ) NOT NULL COMMENT '质检类型',
                                                `notification_status` INT ( 1 ) NOT NULL COMMENT '单据状态 已保存|已送审|已审核 1|2|3',
                                                `project_id` BIGINT ( 20 ) NOT NULL COMMENT '项目订单id',
                                                `project_sub_id` BIGINT ( 20 ) NOT NULL COMMENT '子项目id',
                                                `bom_detail_id` BIGINT ( 20 ) DEFAULT NULL COMMENT 'bom明细id',
                                                `procedure_id` BIGINT ( 20 ) NOT NULL COMMENT '工序id',
                                                `amount` INT ( 11 ) NOT NULL COMMENT '异常数量',
                                                `owner` BIGINT ( 11 ) NOT NULL COMMENT '责任人',
                                                `remark` VARCHAR ( 512 ) NOT NULL COMMENT '情况描述',
                                                `auditor` BIGINT ( 11 ) COMMENT '审核人',
                                                `audit_opinion` CHAR ( 1 ) COMMENT '审核意见',
                                                `audit_time` DATETIME COMMENT '审核时间',
                                                `status` TINYINT ( 1 ) NOT NULL DEFAULT '1' COMMENT '状态,1表示正常，2表示禁用',
                                                `creator` VARCHAR ( 64 ) COLLATE utf8mb4_unicode_ci DEFAULT '' COMMENT '创建者',
                                                `create_time` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
                                                `updater` VARCHAR ( 64 ) COLLATE utf8mb4_unicode_ci 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` ),
                                                KEY `idx_project` ( `project_id`, `project_sub_id` ) USING BTREE,
                                                KEY `idx_bom_detail` ( `bom_detail_id` ) USING BTREE,
                                                KEY `idx_procedure` ( `procedure_id` ) USING BTREE
) ENGINE = InnoDB COMMENT = '品质异常通知单审核表';

DROP TABLE IF EXISTS `pro_quality_feedback`;
CREATE TABLE `pro_quality_feedback` (
                                        `id` BIGINT ( 20 ) NOT NULL AUTO_INCREMENT COMMENT '自增字段，唯一',
                                        `project_id` BIGINT ( 20 ) NOT NULL COMMENT '项目订单id',
                                        `project_sub_id` BIGINT ( 20 ) NOT NULL COMMENT '子项目id',
                                        `feedbacker` VARCHAR ( 32 ) COMMENT '反馈人',
                                        `mobile` VARCHAR ( 32 ) COMMENT '联系电话',
                                        `remark` VARCHAR ( 512 ) COMMENT '反馈内容',
                                        `status` TINYINT ( 1 ) NOT NULL DEFAULT '1' COMMENT '状态,1表示正常，2表示禁用',
                                        `creator` VARCHAR ( 64 ) COLLATE utf8mb4_unicode_ci DEFAULT '' COMMENT '创建者',
                                        `create_time` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
                                        `updater` VARCHAR ( 64 ) COLLATE utf8mb4_unicode_ci 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,
                                        KEY `idx_project` ( `project_id`, `project_sub_id` ) USING BTREE
) ENGINE = InnoDB COMMENT = '客户反馈质量表';

DROP TABLE IF EXISTS `project_plan_task`;
CREATE TABLE `project_plan_task` (
                                     `id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '自增字段，唯一',
                                     `project_id` bigint(20) DEFAULT NULL COMMENT '项目id',
                                     `project_plan_id` bigint(20) NOT NULL COMMENT '生产计划id',
                                     `project_plan_sub_id` bigint(20) DEFAULT NULL COMMENT '生产计划子项目id',
                                     `process_bom_id` bigint(20) DEFAULT NULL COMMENT '工艺bomid',
                                     `task_no` varchar(13)  DEFAULT NULL COMMENT '生产计划编号，唯一',
                                     `bom_version` int(11) DEFAULT NULL COMMENT 'bom版本号',
                                     `description` varchar(255) DEFAULT NULL COMMENT '备注',
                                     `status` tinyint(1) DEFAULT '1' COMMENT '状态,1已保存，2已提交，3已终止 ，默认是1',
                                     `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='生产计划任务表';

DROP TABLE IF EXISTS `project_plan_task_bom`;
CREATE TABLE `project_plan_task_bom` (
                                         `id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '自增字段，唯一',
                                         `task_id` bigint(20) NOT NULL COMMENT '生产任务id',
                                         `bom_detail_id` bigint(20) NOT NULL COMMENT 'bom明细id',
                                         `owner` varchar(64)DEFAULT '' COMMENT '负责人',
                                         `require_end_date` datetime 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='生产计划任务Bom表';

ALTER TABLE pro_process_bom_detail ADD UNIQUE KEY `uk_material`(`bom_id`, `type`, `material_name`,`tenant_id`);

DROP TABLE IF EXISTS `base_equip_manufacture`;
CREATE TABLE `base_equip_manufacture` (
                                          `id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '自增字段，唯一',
                                          `code` varchar(64) NOT NULL COMMENT '编码 唯一',
                                          `name` varchar(128) NOT NULL COMMENT '名称',
                                          `type` varchar(64) COMMENT '类型',
                                          `model` varchar(64) COMMENT '型号',
                                          `status` tinyint(1) DEFAULT '1' COMMENT '状态,1表示正常，2表示禁用，默认是1',
                                          `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`),
                                          UNIQUE KEY `idx_code` (`code`, `tenant_id`)
) ENGINE=InnoDB COMMENT='制造设备表';


DROP TABLE IF EXISTS `finance_invoice`;
CREATE TABLE `finance_invoice` (
                                   `id` BIGINT ( 20 ) NOT NULL AUTO_INCREMENT COMMENT '自增字段，唯一',
                                   `business_type` VARCHAR ( 16 ) NOT NULL COMMENT '业务类型 FINANCE_MAKE_INVOICE|FINANCE_RECEIVE_INVOICE 开票|收票',
                                   `order_id` BIGINT ( 20 ) NOT NULL COMMENT '关联单据id',
                                   `number` VARCHAR ( 64 ) NOT NULL COMMENT '发票号码',
                                   `type` CHAR ( 1 ) NOT NULL COMMENT '发票类型',
                                   `billing_date` DATETIME NOT NULL COMMENT '开票日期',
                                   `amount` DECIMAL ( 20, 6 ) NOT NULL COMMENT '发票金额',
                                   `rate` DECIMAL ( 20, 2 ) NOT NULL COMMENT '税率',
                                   `remark` VARCHAR ( 255 ) DEFAULT NULL COMMENT '备注',
                                   `submitter` BIGINT ( 20 ) COMMENT '提交人',
                                   `submit_time` DATETIME COMMENT '提交时间',
                                   `canceller` BIGINT ( 20 ) COMMENT '作废人',
                                   `cancel_time` DATETIME COMMENT '作废时间',
                                   `status` TINYINT ( 1 ) NOT NULL COMMENT '单据状态 已保存|已提交|已作废 1|2|3',
                                   `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 `base_classes` DROP INDEX `idx_code`;
ALTER TABLE `base_classes` ADD UNIQUE KEY `uk_code`(`code`,`tenant_id`);

ALTER TABLE `base_composition` DROP INDEX `idx_name`;
ALTER TABLE `base_composition` ADD UNIQUE KEY `uk_name`(`name`,`tenant_id`);

ALTER TABLE `base_equip` DROP INDEX `idx_name`;
ALTER TABLE `base_equip` ADD UNIQUE KEY `uk_name`(`name`,`tenant_id`);

ALTER TABLE `base_mould_type` DROP INDEX `idx_name`;
ALTER TABLE `base_mould_type` ADD UNIQUE KEY `uk_name`(`name`,`tenant_id`);

ALTER TABLE `base_process_route` DROP INDEX `idx_code`;
ALTER TABLE `base_process_route` ADD UNIQUE KEY `uk_code`(`code`,`tenant_id`);
ALTER TABLE `base_process_route` DROP INDEX `idx_name`;
ALTER TABLE `base_process_route` ADD UNIQUE KEY `uk_name`(`name`,`tenant_id`);
ALTER TABLE `base_process_route` DROP INDEX `idx_material_id`;
ALTER TABLE `base_process_route` ADD UNIQUE KEY `uk_material_id`(`material_id`,`tenant_id`);

ALTER TABLE `base_workshop` DROP INDEX `idx_code`;
ALTER TABLE `base_workshop` ADD UNIQUE KEY `uk_code`(`code`,`tenant_id`);

ALTER TABLE `base_procedure` DROP INDEX `idx_code`;
ALTER TABLE `base_procedure` DROP INDEX `idx_name`;
ALTER TABLE `base_procedure` ADD UNIQUE KEY `uk_code`(`code`,`tenant_id`);
ALTER TABLE `base_procedure` ADD UNIQUE KEY `uk_name`(`name`,`tenant_id`);


CREATE OR REPLACE VIEW v_storage_material_all AS
select `t`.`deleted` AS `deleted`,`t`.`id` AS `id`,`t`.`creator` AS `creator`,`t`.`updater` AS `updater`,`t`.`create_time` AS `create_time`,`t`.`update_time` AS `update_time`,`t`.`tenant_id` AS `tenant_id`,
       `t`.`code` AS `mat_code`,`t`.`name` AS `mat_name`,
       (select `mes-heli`.`system_dict_data`.`label` from `mes-heli`.`system_dict_data` where ((`mes-heli`.`system_dict_data`.`dict_type` = 'heli_material_type') and (`mes-heli`.`system_dict_data`.`value` = `t`.`material_type`))) AS `mat_type`,
       (select `mes-heli`.`system_dict_data`.`label` from `mes-heli`.`system_dict_data` where ((`mes-heli`.`system_dict_data`.`dict_type` = 'heli_material_unit') and (`mes-heli`.`system_dict_data`.`value` = `t`.`unit`))) AS `mat_unit`,
       `t`.`spec` AS `mat_spec`,`t`.`brand` AS `mat_brand`,`vm`.`wh_id` AS `wh_id`,`vm`.`wh_name` AS `wh_name`,`vm`.`rg_id` AS `rg_id`,`vm`.`rg_name` AS `rg_name`,`vm`.`pn_id` AS `pn_id`,`vm`.`pn_name` AS `pn_name`,
       `vm`.`lot_no` AS `lot_no`,`t`.`short_name` AS `short_name`,`vm`.`storage_ok_qty` AS `storage_ok_qty`
from (`mes-heli`.`base_material` `t`
    left join `mes-heli`.`v_storage_material_now` `vm` on((`t`.`id` = `vm`.`id`)))
where ((`t`.`virtual_part` = 'N') and (`t`.`status` = 1));

ALTER TABLE pro_process_bom_detail DROP INDEX `uk_material`;
ALTER TABLE pro_process_bom_detail ADD UNIQUE KEY `uk_material`(`bom_id`,`type`,`material_id`,`material_name`,`tenant_id`);
