ALTER TABLE system_operate_log MODIFY COLUMN `result_data` TEXT COMMENT '结果数据';
ALTER TABLE system_operate_log MODIFY COLUMN `java_method_args` TEXT COMMENT 'Java 方法的参数';
ALTER TABLE system_operate_log MODIFY COLUMN `content` TEXT COMMENT '操作内容';

ALTER TABLE system_operate_log ADD COLUMN `business_id` bigint(20) DEFAULT NULL COMMENT '业务id' AFTER trace_id;
ALTER TABLE system_operate_log ADD COLUMN `business_type` varchar(32) COMMENT '业务类型 用于业务关联' AFTER trace_id;

ALTER TABLE system_operate_log add INDEX `idx_business`(`business_type`,`business_id`);

ALTER TABLE infra_file ADD COLUMN business_file_type varchar(32) COMMENT '业务文件类型 用于区分不同业务类型文件' after business_id;
ALTER TABLE infra_file ADD INDEX `idx_business`(`business_type`,`business_id`,`business_file_type`);

DROP TABLE IF EXISTS `wms_storage`;
CREATE TABLE `wms_storage`  (
                                `id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '主键id',
                                `stock_type` tinyint(1) NOT NULL COMMENT '入库出库大类型：1为入库，2为出库',
                                `stock_in_type` tinyint(1) NULL COMMENT '入库类型(字典)：1为采购入库，2为盘盈入库，3为其他入库',
                                `stock_out_type` tinyint(1) NULL COMMENT '出库类型(字典)：1为领料出库，2为销售出库，3为盘亏出库，4为其他出库',
                                `stock_no` varchar(20)  NOT NULL COMMENT '入/出库单号',
                                `header_no` bigint(20) NULL DEFAULT NULL COMMENT '上游单号',
                                `description` varchar(255)  NULL DEFAULT NULL COMMENT '备注',
                                `status` tinyint(1) NULL DEFAULT NULL COMMENT '状态：1为保存；2为提交；3为作废',
                                `wh_id` bigint(19) NOT NULL COMMENT '仓库Id，对应 wms_wh 表中的Id',
                                `deleted` bit(1) NOT NULL DEFAULT b'0' COMMENT '逻辑删除 1.是,0.否',
                                `creator` varchar(64)  NOT NULL COMMENT '记录的创建人,对应员工表中的 Id',
                                `create_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
                                `updater` varchar(64)  NULL DEFAULT NULL COMMENT '记录的修改人,对应员工表中的 Id',
                                `update_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
                                `tenant_id` bigint(20) NOT NULL COMMENT '租户编号',
                                `keeper` bigint(20) NULL DEFAULT NULL COMMENT '入库人',
                                `keeper_time` datetime NULL DEFAULT NULL COMMENT '入库时间',
                                `outbound` bigint(20) NULL DEFAULT NULL COMMENT '出库人',
                                `outbound_time` datetime NULL DEFAULT NULL COMMENT '出库时间',
                                `cancel` bigint(20) NULL DEFAULT NULL COMMENT '作废人',
                                `cancel_time` datetime NULL DEFAULT NULL COMMENT '作废时间',
                                PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 1 COMMENT = '入/出库表' ROW_FORMAT = DYNAMIC;

DROP TABLE IF EXISTS `wms_storage_mat`;
CREATE TABLE `wms_storage_mat`  (
                                    `id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '主键id',
                                    `stock_id` bigint(20) NOT NULL COMMENT '入/出库Id',
                                    `mat_id` bigint(20) NOT NULL COMMENT '物料 Id,对应 base_material表中的 Id 列',
                                    `wh_id` bigint(20) NOT NULL COMMENT '仓库 Id，对应 wms_wh 表中的Id',
                                    `rg_id` bigint(20) NOT NULL COMMENT '库区 Id，对应 wms_rg 表中的Id',
                                    `pn_id` bigint(20) NOT NULL COMMENT '库区 Id，对应 wms_rg 表中的Id',
                                    `storage_ok_qty` decimal(11, 2) NULL DEFAULT NULL COMMENT '库存良品数量',
                                    `lot_no` varchar(50)  NULL DEFAULT NULL COMMENT '批次号',
                                    `description` varchar(255)  NULL DEFAULT NULL COMMENT '备注',
                                    `deleted` bit(1) NOT NULL DEFAULT b'0' COMMENT '逻辑删除 1.是,0.否',
                                    `creator` varchar(64)  NOT NULL COMMENT '记录的创建人,对应员工表中的 Id',
                                    `create_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
                                    `updater` varchar(64)  NULL DEFAULT NULL COMMENT '记录的修改人,对应员工表中的 Id',
                                    `update_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
                                    `tenant_id` bigint(20) NOT NULL COMMENT '租户编号',
                                    PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 1 COMMENT = '入/出库物料表' ROW_FORMAT = DYNAMIC;

DROP TABLE IF EXISTS `wms_storage_log`;
CREATE TABLE `wms_storage_log`  (
                                    `id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '主键id',
                                    `stock_id` bigint(20) NOT NULL COMMENT '入/出库Id',
                                    `mat_id` bigint(20) NOT NULL COMMENT '物料 Id,对应 base_material表中的 Id 列',
                                    `wh_id` bigint(20) NOT NULL COMMENT '仓库 Id，对应 wms_wh 表中的Id',
                                    `rg_id` bigint(20) NOT NULL COMMENT '库区 Id，对应 wms_rg 表中的Id',
                                    `pn_id` bigint(20) NOT NULL COMMENT '库区 Id，对应 wms_rg 表中的Id',
                                    `storage_ok_qty` decimal(11, 2) NULL DEFAULT NULL COMMENT '库存良品数量',
                                    `lot_no` varchar(50)  NULL DEFAULT NULL COMMENT '批次号',
                                    `description` varchar(255)  NULL DEFAULT NULL COMMENT '备注',
                                    `deleted` bit(1) NOT NULL DEFAULT b'0' COMMENT '逻辑删除 1.是,0.否',
                                    `creator` varchar(64)  NOT NULL COMMENT '记录的创建人,对应员工表中的 Id',
                                    `create_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
                                    `updater` varchar(64)  NULL DEFAULT NULL COMMENT '记录的修改人,对应员工表中的 Id',
                                    `update_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
                                    `tenant_id` bigint(20) NOT NULL COMMENT '租户编号',
                                    `status` tinyint(1) NULL DEFAULT NULL COMMENT '状态：1为保存；2为提交；3为作废',
                                    PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 1 COMMENT = '入/出库日志表' ROW_FORMAT = DYNAMIC;

DROP TABLE IF EXISTS project_order;
CREATE TABLE `project_order` (
                                 `id` BIGINT ( 20 ) NOT NULL AUTO_INCREMENT COMMENT '自增字段，唯一',
                                 `code` VARCHAR ( 64 ) NOT NULL COMMENT '项目编号，唯一',
                                 `order_status` INT ( 11 ) NOT NULL DEFAULT '0' COMMENT '单据状态 已保存/已送审/已审核/已启动/已打回/已终止',
                                 `delivery_status` TINYINT ( 1 ) NOT NULL COMMENT '发货状态 未发货/部分发货/全部发货',
                                 `business_man` BIGINT ( 20 ) NOT NULL COMMENT '业务员',
                                 `business_dept_id` BIGINT ( 20 ) NOT NULL COMMENT '提出部门id',
                                 `customer_id` BIGINT ( 20 ) NOT NULL COMMENT '客户id',
                                 `mould_name` VARCHAR ( 255 ) NOT NULL COMMENT '模具名称',
                                 `business_line` CHAR ( 1 ) NOT NULL COMMENT '业务线',
                                 `blueprint_no` VARCHAR ( 32 ) COMMENT '图号',
                                 `has_price` TINYINT ( 1 ) NOT NULL COMMENT '是否有价格',
                                 `price` DECIMAL ( 20, 6 ) COMMENT '价格 保留四位小数',
                                 `currency` char(1) COMMENT '币种',
                                 `project_start_time` DATETIME NOT NULL COMMENT '项目开始时间',
                                 `project_end_time` DATETIME NOT NULL COMMENT '项目结束时间',
                                 `is_urgency` TINYINT ( 1 ) NOT NULL COMMENT '是否紧急',
                                 `property` TINYINT ( 1 ) NOT NULL COMMENT '性质 新制|维修',
                                 `reference_technology` VARCHAR ( 255 ) COMMENT '可引用的原有技术',
                                 `has_alter` TINYINT ( 1 ) NOT NULL COMMENT '是否已变更',
                                 `last_alter_time` DATETIME COMMENT '上一次变更时间',
                                 `quality_requirement` VARCHAR ( 255 ) COMMENT '检验要求',
                                 `remark` VARCHAR ( 255 ) COMMENT '备注',
                                 `has_contract` TINYINT ( 1 ) NOT NULL COMMENT '有无合同',
                                 `has_technology_protocol` TINYINT ( 1 ) NOT NULL COMMENT '有无技术协议',
                                 `has_blueprint` TINYINT ( 1 ) NOT NULL COMMENT '有无图纸/数模',
                                 `blueprint_remark` VARCHAR ( 255 ) 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`) USING BTREE
) ENGINE = InnoDB COMMENT = '项目订单表';

DROP TABLE IF EXISTS project_order_sub;
CREATE TABLE `project_order_sub` (
                                     `id` BIGINT ( 20 ) NOT NULL AUTO_INCREMENT COMMENT '自增字段，唯一',
                                     `project_order_id` BIGINT ( 20 ) NOT NULL COMMENT '项目订单id',
                                     `name` VARCHAR ( 128 ) NOT NULL COMMENT '子项目名称，唯一',
                                     `device_model` VARCHAR ( 64 ) COMMENT '设备型号',
                                     `amount` INT ( 11 ) NOT NULL COMMENT '数量',
                                     `composition_id` BIGINT(20) NOT NULL COMMENT '材质',
                                     `remark` VARCHAR ( 255 ) 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_project_name` (`project_order_id`,`name`) USING BTREE
) ENGINE = InnoDB COMMENT = '项目订单子项目表';

ALTER TABLE project_order ADD COLUMN `contract_no` VARCHAR ( 128 ) COMMENT '模具名称' AFTER customer_id;
ALTER TABLE project_order_sub ADD COLUMN `unit` CHAR ( 1 ) NOT NULL COMMENT '单位' AFTER amount;
ALTER TABLE project_order ADD COLUMN `order_time` datetime NOT NULL COMMENT '单据日期' AFTER order_status;
ALTER TABLE project_order ADD COLUMN `snapshot_order_time` datetime COMMENT '快照原始单据日期' AFTER blueprint_remark;
ALTER TABLE project_order ADD COLUMN `snapshot_code` VARCHAR ( 64 ) COMMENT '快照原始单据号' AFTER blueprint_remark;
ALTER TABLE project_order ADD COLUMN `snapshot_id` BIGINT ( 20 ) COMMENT '快照原始id' AFTER blueprint_remark;
ALTER TABLE project_order ADD COLUMN `is_snapshot` TINYINT ( 1 ) NOT NULL DEFAULT '0' COMMENT '是否快照' AFTER blueprint_remark;

DROP TABLE IF EXISTS `base_serial_number`;
CREATE TABLE `base_serial_number` (
                                      `id` BIGINT ( 20 ) NOT NULL AUTO_INCREMENT COMMENT '自增字段，唯一',
                                      `business_type` VARCHAR ( 32 ) NOT NULL COMMENT '业务类型 用于业务关联',
                                      `segment` VARCHAR ( 64 ) NOT NULL COMMENT '序列参考段',
                                      `serial_number` BIGINT ( 20 ) NOT NULL DEFAULT '0' 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_business_segment` ( `business_type`, `segment` ) USING BTREE
) ENGINE = InnoDB COMMENT = '序列号记录表';

CREATE ALGORITHM = UNDEFINED DEFINER = `root` @`%` SQL SECURITY DEFINER VIEW `v_storage_material_amount` AS
select `t`.`tenant_id` AS `tenant_id`,`t`.`mat_id` AS `mat_id`,`t`.`mat_name` AS `mat_name`,`t`.`mat_code` AS `mat_code`,
       `t`.`wh_id` AS `wh_id`,`t`.`rg_id` AS `rg_id`,`t`.`pn_id` AS `pn_id`,`t`.`spec` AS `spec`,`t`.`unit` AS `unit`,
       `t`.`material_type` AS `material_type`,`t`.`lot_no` AS `lot_no`,'' AS `storage_ok_qty`,sum(`t`.`storage_ok_qty`) AS `mat_rest`
from (
    select `st`.`stock_type` AS `stock_type`,`mat`.`tenant_id` AS `tenant_id`,`mat`.`mat_id` AS `mat_id`,`bm`.`code` AS `mat_code`,`bm`.`name` AS `mat_name`,
           `mat`.`wh_id` AS `wh_id`,`mat`.`rg_id` AS `rg_id`,`mat`.`pn_id` AS `pn_id`,`mat`.`lot_no` AS `lot_no`,`bm`.`spec` AS `spec`,`bm`.`unit` AS `unit`,`bm`.`material_type` AS `material_type`,
           (case when (`st`.`stock_type` = 1) then `mat`.`storage_ok_qty` else (`mat`.`storage_ok_qty` * -(1)) end) AS `storage_ok_qty`
    from ((`mes-heli`.`wms_storage_mat` `mat`
        left join `mes-heli`.`wms_storage` `st` on((`mat`.`stock_id` = `st`.`id`)))
        left join `mes-heli`.`base_material` `bm` on((`mat`.`mat_id` = `bm`.`id`)))
    where ((`mat`.`deleted` = 0) and (`st`.`status` <> 3) and (`st`.`deleted` = 0) and (`bm`.`deleted` = 0) and (`bm`.`status` = 1))) `t` group by `t`.`mat_id`;
