-- 项目图纸工作量（从 FTP「{令号}图纸数量统计表.xlsx」解析入库）

CREATE TABLE IF NOT EXISTS `drawing_workload` (
    `id`                    bigint        NOT NULL AUTO_INCREMENT,
    `order_pro_id`          bigint        NOT NULL COMMENT '项目令号ID process_order_pro.id',
    `production_order_no`   varchar(64)   NOT NULL COMMENT '生产令号',
    `a4_count`              int           DEFAULT NULL COMMENT 'A4图纸数量',
    `a3_count`              int           DEFAULT NULL COMMENT 'A3图纸数量',
    `a3_half_count`         int           DEFAULT NULL COMMENT 'A3+1/2图纸数量',
    `a3_plus1_count`        int           DEFAULT NULL COMMENT 'A3+1图纸数量',
    `a2_count`              int           DEFAULT NULL COMMENT 'A2图纸数量',
    `a2_half_count`         int           DEFAULT NULL COMMENT 'A2+1/2图纸数量',
    `a1_count`              int           DEFAULT NULL COMMENT 'A1图纸数量',
    `a1_half_count`         int           DEFAULT NULL COMMENT 'A1+1/2图纸数量',
    `a0_count`              int           DEFAULT NULL COMMENT 'A0图纸数量',
    `a0_half_count`         int           DEFAULT NULL COMMENT 'A0+1/2图纸数量',
    `equivalent_a4_total`   int           DEFAULT NULL COMMENT '等效A4图纸总数量',
    `item_count`            int           NOT NULL DEFAULT 0 COMMENT '明细行数',
    `sync_time`             datetime      NOT NULL COMMENT '最近同步时间',
    `create_by`             varchar(64)   DEFAULT '' COMMENT '创建者',
    `create_time`           datetime      DEFAULT NULL COMMENT '创建时间',
    `update_by`             varchar(64)   DEFAULT '' COMMENT '更新者',
    `update_time`           datetime      DEFAULT NULL COMMENT '更新时间',
    PRIMARY KEY (`id`),
    UNIQUE KEY `uk_order_pro_id` (`order_pro_id`),
    KEY `idx_production_order_no` (`production_order_no`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='项目图纸工作量汇总';

CREATE TABLE IF NOT EXISTS `drawing_workload_item` (
    `id`                    bigint        NOT NULL AUTO_INCREMENT,
    `workload_id`           bigint        NOT NULL COMMENT '汇总ID drawing_workload.id',
    `drawing_no`            varchar(128)  DEFAULT NULL COMMENT '图号',
    `drawing_name`          varchar(256)  DEFAULT NULL COMMENT '名称',
    `frame_name`            varchar(128)  DEFAULT NULL COMMENT '图框名称',
    `paper_size`            varchar(64)   DEFAULT NULL COMMENT '图纸幅面',
    `equivalent_a4`         int           DEFAULT NULL COMMENT '等效A4图纸数量',
    `sort_no`               int           DEFAULT NULL COMMENT '行序',
    PRIMARY KEY (`id`),
    KEY `idx_workload_id` (`workload_id`),
    KEY `idx_drawing_no` (`drawing_no`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='项目图纸工作量明细';

-- 按钮权限（挂到项目令号菜单下）
INSERT INTO sys_menu (menu_id, menu_name, parent_id, order_num, path, component, is_frame, is_cache, menu_type, visible, status, perms, icon, create_by, create_time, update_by, update_time, remark)
SELECT (SELECT IFNULL(MAX(menu_id), 0) + 1 FROM sys_menu),
       '读取图纸工作量',
       COALESCE(
           (SELECT menu_id FROM sys_menu WHERE perms = 'system:orderPro:list' AND menu_type = 'C' LIMIT 1),
           (SELECT parent_id FROM sys_menu WHERE perms = 'system:orderPro:exportRoute3' LIMIT 1),
           (SELECT menu_id FROM sys_menu WHERE path = 'orderPro' AND menu_type = 'C' LIMIT 1)
       ),
       30,
       '#',
       '',
       1, 0, 'F', '0', '0',
       'system:orderPro:drawingWorkload',
       '#',
       'admin', sysdate(), '', NULL,
       '从FTP读取图纸数量统计表并解析入库'
FROM DUAL
WHERE NOT EXISTS (SELECT 1 FROM sys_menu WHERE perms = 'system:orderPro:drawingWorkload');
