-- =================================================
-- EasyCode front_project incremental upgrade
-- Run once on existing front workbench databases created before the current
-- front_project schema. Safe to run repeatedly.
-- =================================================

set @front_project_requirement_keyword_exists := (
  select count(1)
  from information_schema.columns
  where table_schema = database()
    and table_name = 'front_project'
    and column_name = 'requirement_keyword'
);

set @front_project_requirement_keyword_sql := if(
  @front_project_requirement_keyword_exists = 0,
  'alter table front_project add column requirement_keyword varchar(1000) default '''' comment ''需求关键词'' after project_desc',
  'select ''front_project.requirement_keyword already exists'''
);

prepare front_project_requirement_keyword_stmt from @front_project_requirement_keyword_sql;
execute front_project_requirement_keyword_stmt;
deallocate prepare front_project_requirement_keyword_stmt;

set @front_project_industry_template_exists := (
  select count(1)
  from information_schema.columns
  where table_schema = database()
    and table_name = 'front_project'
    and column_name = 'industry_template'
);

set @front_project_industry_template_sql := if(
  @front_project_industry_template_exists = 0,
  'alter table front_project add column industry_template varchar(50) default '''' comment ''行业模板'' after requirement_keyword',
  'select ''front_project.industry_template already exists'''
);

prepare front_project_industry_template_stmt from @front_project_industry_template_sql;
execute front_project_industry_template_stmt;
deallocate prepare front_project_industry_template_stmt;

set @front_project_app_blueprint_exists := (
  select count(1)
  from information_schema.columns
  where table_schema = database()
    and table_name = 'front_project'
    and column_name = 'app_blueprint'
);

set @front_project_app_blueprint_sql := if(
  @front_project_app_blueprint_exists = 0,
  'alter table front_project add column app_blueprint longtext comment ''应用蓝图草稿JSON'' after preview_status',
  'select ''front_project.app_blueprint already exists'''
);

prepare front_project_app_blueprint_stmt from @front_project_app_blueprint_sql;
execute front_project_app_blueprint_stmt;
deallocate prepare front_project_app_blueprint_stmt;

set @front_project_er_diagram_exists := (
  select count(1)
  from information_schema.columns
  where table_schema = database()
    and table_name = 'front_project'
    and column_name = 'er_diagram'
);

set @front_project_er_diagram_sql := if(
  @front_project_er_diagram_exists = 0,
  'alter table front_project add column er_diagram longtext comment ''ER图草稿JSON'' after app_blueprint',
  'select ''front_project.er_diagram already exists'''
);

prepare front_project_er_diagram_stmt from @front_project_er_diagram_sql;
execute front_project_er_diagram_stmt;
deallocate prepare front_project_er_diagram_stmt;

set @front_project_business_blueprint_exists := (
  select count(1)
  from information_schema.columns
  where table_schema = database()
    and table_name = 'front_project'
    and column_name = 'business_blueprint'
);

set @front_project_business_blueprint_sql := if(
  @front_project_business_blueprint_exists = 0,
  'alter table front_project add column business_blueprint longtext comment ''业务蓝图草稿JSON'' after er_diagram',
  'select ''front_project.business_blueprint already exists'''
);

prepare front_project_business_blueprint_stmt from @front_project_business_blueprint_sql;
execute front_project_business_blueprint_stmt;
deallocate prepare front_project_business_blueprint_stmt;

set @front_project_frontend_enabled_exists := (
  select count(1)
  from information_schema.columns
  where table_schema = database()
    and table_name = 'front_project'
    and column_name = 'frontend_enabled'
);

set @front_project_frontend_enabled_sql := if(
  @front_project_frontend_enabled_exists = 0,
  'alter table front_project add column frontend_enabled char(1) default ''1'' comment ''是否生成前台前端（1是 0否）'' after back_framework',
  'select ''front_project.frontend_enabled already exists'''
);

prepare front_project_frontend_enabled_stmt from @front_project_frontend_enabled_sql;
execute front_project_frontend_enabled_stmt;
deallocate prepare front_project_frontend_enabled_stmt;

create table if not exists front_project_module (
  id                   bigint(20)      not null auto_increment comment 'id',
  project_id           bigint(20)      not null comment '前台项目ID',
  module_id            bigint(20)      not null comment '功能模块ID',
  status               char(1)         default '0' comment '状态（0正常 1停用）',
  create_time          datetime        default null comment '创建时间',
  update_time          datetime        default null comment '更新时间',
  remark               varchar(500)    default null comment '备注',
  primary key (id),
  unique key uk_front_project_module (project_id, module_id),
  key idx_front_project_module_project_id (project_id),
  key idx_front_project_module_module_id (module_id),
  constraint fk_front_project_module_project foreign key (project_id) references front_project(project_id),
  constraint fk_front_project_module_module foreign key (module_id) references sys_module(module_id)
) engine=innodb auto_increment=100 default charset=utf8mb4 comment='前台项目功能模块关系';
