ALTER TABLE `is_training`
ADD COLUMN `recommend` int(5) DEFAULT '0' COMMENT '推荐系数';

ALTER TABLE `is_training_material`
ADD column `recommend` int(5) DEFAULT '0' COMMENT '推荐系数';

ALTER TABLE `is_training_resource`
ADD column `recommend` int(5) DEFAULT '0' COMMENT '推荐系数';



# 备份课程分类
alter table is_training_material_type
    add column bak_module_type tinyint not null default '0' comment '备份moduleType数据字段';

update is_training_material_type set is_training_material_type.bak_module_type = module_type
where module_type in (1,5)
and bak_module_type = 0;

update is_training_material_type set module_type = 1
where module_type = 5
and bak_module_type = 5;


alter table is_training_material_permission
    add column resource_id int not null default '0' comment '资源id' after id;

alter table is_training_material_type
add column option_cover tinyint not null default '0' comment '选项-是否覆盖子类权限：1-是，0-否',
add column option_extend tinyint not null default '0' comment '选项-是否继承父类权限：1-是，0-否',
add column extend tinyint not null default '0' comment '是否继承父类权限：1-是，0-否',
add column extend_pass tinyint not null default '0' comment '继承权限是否传递给下级：1-是，0-否',
add column id_path varchar(128) not null default '' comment '分类id层级 eg: 1,12,33' after origin_id;

CREATE TABLE `is_training_material_type_permission`
(
    `id`               int(11) PRIMARY KEY AUTO_INCREMENT,
    `material_type_id` int(11)    NOT null default '0' comment '分类id',
    `permission_type`  tinyint(8) NOT null default '0' comment '权限类型 {1:查看,4:下载}',
    `unit_id`          bigint(20) not null DEFAULT '0' COMMENT '单元id',
    `enterprise_id`    INT(11)    not null DEFAULT '0' COMMENT '企业id',
    `source_type`      tinyint    not null DEFAULT '0' COMMENT '权限来源 {0:普通权限,1:创建人,2:继承父级}',
    `deleted`          tinyint    not null DEFAULT '0' COMMENT '逻辑删除',
    `create_time`      DATETIME            DEFAULT CURRENT_TIMESTAMP comment '创建时间',
    `update_time`      DATETIME            DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP comment '更新时间',
    KEY `idx_unit_id` (`unit_id`),
    KEY `idx_material_type_id` (`material_type_id`),
    KEY `idx_create_time` (`create_time`),
    KEY `idx_update_time` (`update_time`)
) ENGINE = InnoDB
  AUTO_INCREMENT = 1
  DEFAULT CHARSET = utf8mb4 COMMENT ='素材分类权限配置表';


# 清洗permission底表旧数据写入resource_id
update is_training_material_permission p
left join is_training_resource r on r.object_id = p.material_id and r.file_type in (1,2,3,4,5,6,7)
set p.resource_id = r.id
where r.id is not null;

# 检查permission底表数据都写入了resource_id
select * from is_training_material_permission
where resource_id = 0;

# 检查idPath为空数据
select * from is_training_material_type
where id_path = ''
and module_type = 1
and material_type_level != 1
order by id desc;


# 课程权限清洗
select * from is_training
where is_private = 1;

# 课程所有权限都改为单独设置
update is_training set read_type = 2
where id > 0;
select * from is_training
where read_type != 2;
update is_training_resource
set read_type = 2
where file_type = 8;
update is_training_resource
set download_type = 0
where file_type = 8;
select * from is_training_resource
where file_type = 8
and download_type != 0;

# 课程全部洗为单独权限
select * from is_training_resource
where file_type = 8
and read_type != 2;

# 素材type为0的数据全部重置为1 课程没有
update is_training_material set download_type = 1
where download_type = 0;
update is_training_material set read_type = 1
where read_type = 0;
update is_training_resource set read_type = 1
where read_type = 0;
update is_training_resource set download_type = 1
where download_type = 0
and file_type in (1,2,3,4,5,6,7);

select * from is_training_resource;

