/*
龙创生产计划模块（SQL Server）建表脚本
*/

if object_id('dbo.lc_production_order', 'U') is null
begin
    create table dbo.lc_production_order (
        id bigint identity(1,1) primary key,
        order_type nvarchar(30) not null,
        task_no nvarchar(64) null,
        task_type nvarchar(32) null,
        source_project_no nvarchar(64) null,
        project_no nvarchar(64) null,
        model_no nvarchar(64) null,
        color nvarchar(64) null,
        floor_count int null,
        special_requirement nvarchar(1000) null,
        task_qty decimal(18,2) null,
        report_qty decimal(18,2) not null default(0),
        plan_delivery_date date null,
        plan_finish_date date null,
        status nvarchar(20) not null default('已排产'),
        finish_date date null,
        create_time datetime not null default(getdate()),
        create_by bigint null,
        update_time datetime not null default(getdate()),
        update_by bigint null,
        is_deleted bit not null default(0)
    );
end;
go

if object_id('dbo.lc_production_order_node', 'U') is null
begin
    create table dbo.lc_production_order_node (
        id bigint identity(1,1) primary key,
        order_id bigint not null,
        node_code nvarchar(50) not null,
        node_name nvarchar(100) not null,
        sort_no int not null,
        status nvarchar(20) not null default('未开始'),
        last_report_time datetime null,
        last_report_user_id bigint null,
        last_report_remark nvarchar(500) null,
        create_time datetime not null default(getdate()),
        update_time datetime not null default(getdate())
    );
end;
go

if object_id('dbo.lc_production_node_role', 'U') is null
begin
    create table dbo.lc_production_node_role (
        id bigint identity(1,1) primary key,
        order_type nvarchar(30) not null,
        node_code nvarchar(50) not null,
        node_name nvarchar(100) not null,
        role_id bigint not null,
        role_name nvarchar(100) null,
        enabled bit not null default(1),
        create_time datetime not null default(getdate()),
        create_by bigint null
    );
end;
go

if object_id('dbo.lc_production_node_report_log', 'U') is null
begin
    create table dbo.lc_production_node_report_log (
        id bigint identity(1,1) primary key,
        order_id bigint not null,
        order_type nvarchar(30) not null,
        node_code nvarchar(50) not null,
        node_name nvarchar(100) not null,
        report_qty decimal(18,2) null,
        remark nvarchar(500) null,
        report_user_id bigint null,
        report_user_name nvarchar(100) null,
        report_time datetime not null default(getdate())
    );
end;
go

if object_id('dbo.lc_production_node_assignee', 'U') is null
begin
    create table dbo.lc_production_node_assignee (
        id bigint identity(1,1) primary key,
        order_id bigint not null,
        order_type nvarchar(30) not null,
        node_code nvarchar(50) not null,
        assignee_user_id bigint not null,
        assignee_user_name nvarchar(100) null,
        create_time datetime not null default(getdate()),
        create_by bigint null
    );
end;
go

if not exists (select 1 from sys.indexes where name = 'ix_lc_order_type_status' and object_id = object_id('dbo.lc_production_order'))
begin
    create index ix_lc_order_type_status on dbo.lc_production_order(order_type, status, is_deleted, create_time);
end;
go

if not exists (select 1 from sys.indexes where name = 'ux_lc_node_order_code' and object_id = object_id('dbo.lc_production_order_node'))
begin
    create unique index ux_lc_node_order_code on dbo.lc_production_order_node(order_id, node_code);
end;
go

if not exists (select 1 from sys.indexes where name = 'ux_lc_node_role_unique' and object_id = object_id('dbo.lc_production_node_role'))
begin
    create unique index ux_lc_node_role_unique on dbo.lc_production_node_role(order_type, node_code, role_id);
end;
go

if exists (select 1 from sys.indexes where name = 'ux_lc_node_assignee_unique' and object_id = object_id('dbo.lc_production_node_assignee'))
begin
    drop index ux_lc_node_assignee_unique on dbo.lc_production_node_assignee;
end;
go

if not exists (select 1 from sys.indexes where name = 'ux_lc_node_assignee_user_unique' and object_id = object_id('dbo.lc_production_node_assignee'))
begin
    create unique index ux_lc_node_assignee_user_unique on dbo.lc_production_node_assignee(order_id, node_code, assignee_user_id);
end;
go

/*
可选：在 sys_role 增加 role_type 以区分角色类别（推荐）
*/
if col_length('dbo.sys_role', 'role_type') is null
begin
    alter table dbo.sys_role add role_type nvarchar(50) null;
end;
go

/*
推荐：role_type 细化到“订单类型 + 节点”
家用电梯:
  LC_HOME_LIFT_STOCKING
  LC_HOME_LIFT_PLATFORM_DEBUG
  LC_HOME_LIFT_BG_CEILING
  LC_HOME_LIFT_DOOR_ASSY
  LC_HOME_LIFT_PACK
线缆/COP:
  LC_CABLE_COP_LINE_PRODUCTION
  LC_CABLE_COP_COP_PRODUCTION
改造项目:
  LC_RENOVATION_STOCKING
  LC_RENOVATION_ASSY
  LC_RENOVATION_INSPECT
  LC_RENOVATION_PACK
以下为按角色名称的示例初始化，请按你们实际角色名调整
*/
update dbo.sys_role set role_type = 'LC_HOME_LIFT_STOCKING' where role_name like '%家用电梯%' and role_name like '%仓库配料%';
update dbo.sys_role set role_type = 'LC_HOME_LIFT_PLATFORM_DEBUG' where role_name like '%家用电梯%' and role_name like '%平台组装%' ;
update dbo.sys_role set role_type = 'LC_HOME_LIFT_BG_CEILING' where role_name like '%家用电梯%' and (role_name like '%背景墙%' or role_name like '%吊顶%');
update dbo.sys_role set role_type = 'LC_HOME_LIFT_DOOR_ASSY' where role_name like '%家用电梯%' and role_name like '%门组装%';
update dbo.sys_role set role_type = 'LC_HOME_LIFT_PACK' where role_name like '%家用电梯%' and role_name like '%打包%';

update dbo.sys_role set role_type = 'LC_CABLE_COP_LINE_PRODUCTION' where role_name like '%线缆生产%';
update dbo.sys_role set role_type = 'LC_CABLE_COP_COP_PRODUCTION' where role_name like '%COP生产%';

update dbo.sys_role set role_type = 'LC_RENOVATION_STOCKING' where role_name like '%改造%' and role_name like '%仓库配料%';
update dbo.sys_role set role_type = 'LC_RENOVATION_ASSY' where role_name like '%改造%' and role_name like '%组装%';
update dbo.sys_role set role_type = 'LC_RENOVATION_INSPECT' where role_name like '%改造%' and role_name like '%检验%';
update dbo.sys_role set role_type = 'LC_RENOVATION_PACK' where role_name like '%改造%' and role_name like '%打包%';
go
