use `sys-icss`;

ALTER TABLE `icss_template_info`
ADD COLUMN `spell` varchar(100) DEFAULT NULL COMMENT '拼音' AFTER `name`;

DROP TABLE IF EXISTS `icss_template_quote`;
CREATE TABLE `icss_template_quote` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '主键',
  `is_deleted` char(3) DEFAULT 'N' COMMENT '是否删除,N:未删除，Y:删除',
  `gmt_create` datetime DEFAULT '1970-01-01 12:00:00' COMMENT '记录创建时间',
  `gmt_modified` datetime DEFAULT '1970-01-01 12:00:00' COMMENT '记录修改时间,如果时间是1970年则表示纪录未修改',
  `creator` varchar(60) DEFAULT '0' COMMENT '创建人，0表示无创建人值',
  `modifier` varchar(60) DEFAULT '0' COMMENT '修改人,如果为0则表示纪录未修改',
  `template_id` bigint(20) DEFAULT NULL COMMENT '模板id',
  `doctor_id` bigint(20) DEFAULT NULL COMMENT '医生id',
  `remark` varchar(255) DEFAULT NULL COMMENT '备注',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='模板引用表';


DROP TABLE IF EXISTS `icss_template_folder`;
CREATE TABLE `icss_template_folder` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '主键',
  `is_deleted` char(1)  NOT NULL DEFAULT 'N' COMMENT '是否删除,N:未删除，Y:删除',
  `gmt_create` datetime NOT NULL DEFAULT '1970-01-01 12:00:00' COMMENT '记录创建时间',
  `gmt_modified` datetime NOT NULL DEFAULT '1970-01-01 12:00:00' COMMENT '记录修改时间,如果时间是1970年则表示纪录未修改',
  `creator` varchar(20)  NOT NULL DEFAULT '' COMMENT '创建人姓名',
  `modifier` varchar(20)  NOT NULL DEFAULT '' COMMENT '修改人姓名',
  `name` varchar(300) NOT NULL DEFAULT '' COMMENT '名称',
  `hospital_id` bigint(20) DEFAULT NULL COMMENT '医院id',
  `doctor_id` bigint(20) DEFAULT NULL COMMENT '医生id',
  `template_type` int(11) NOT NULL DEFAULT '1' COMMENT '模板类型（个人1，管理员2）',
  `order_no` int(11) NOT NULL DEFAULT '1' COMMENT '排序号',
  `remark` varchar(255)  NOT NULL DEFAULT '' COMMENT '备注',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8  COMMENT='模板文件夹表';


DROP TABLE IF EXISTS `icss_folder_mapping`;
CREATE TABLE `icss_folder_mapping` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '主键',
  `is_deleted` char(1)  NOT NULL DEFAULT 'N' COMMENT '是否删除,N:未删除，Y:删除',
  `gmt_create` datetime NOT NULL DEFAULT '1970-01-01 12:00:00' COMMENT '记录创建时间',
  `gmt_modified` datetime NOT NULL DEFAULT '1970-01-01 12:00:00' COMMENT '记录修改时间,如果时间是1970年则表示纪录未修改',
  `creator` varchar(20)  NOT NULL DEFAULT '' COMMENT '创建人姓名',
  `modifier` varchar(20)  NOT NULL DEFAULT '' COMMENT '修改人姓名',
  `template_id` bigint(20) DEFAULT NULL COMMENT '模板id',
  `folder_id` bigint(20) DEFAULT NULL COMMENT '文件夹id',
  `order_no` int(11) NOT NULL DEFAULT '1' COMMENT '排序号',
  `remark` varchar(255)  NOT NULL DEFAULT '' COMMENT '备注',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8  COMMENT='模板文件夹映射表';

insert into `icss_template_folder`(`creator`,`modifier`,`name`,`hospital_id`,`doctor_id`,`template_type`,`order_no`) values (-7,-7,'未分类文件夹','-9','-7',2,99999);

-- 更新icss_template_info的doctor_id
update `sys-icss`.icss_template_info set doctor_id = CONCAT('-',doctor_id)
where doctor_id in
	(SELECT id FROM `sys-tran`.`tran_doctor_info` where hospital_code = 'LT');

-- 更新icss_template_info的hospital_id
update `sys-icss`.icss_template_info set hospital_id = CONCAT('-',hospital_id)
where hospital_id in
	(SELECT id FROM `sys-tran`.`tran_hospital_info` where code = 'LT');

-- 更新icss_template_info的hospital_dept_id
update `sys-icss`.icss_template_info set hospital_dept_id = CONCAT('-',hospital_dept_id)
where hospital_dept_id in
	(SELECT id FROM `sys-tran`.`tran_hospital_dept` where hospital_code = 'LT');

-- 将标准模板放入“未分类文件夹”下
insert into icss_folder_mapping(`template_id`,`creator`,`modifier`,`folder_id`)
(SELECT id,doctor_id,doctor_id, (select id from icss_template_folder where name = '未分类文件夹') from icss_template_info where template_type = 2 and is_deleted = 'N');


update `sys-tran`.`tran_doctor_info` set id=CONCAT('-',id)  where hospital_code = 'LT';
update `sys-tran`.`tran_hospital_info` set id=CONCAT('-',id)  where code = 'LT';
update `sys-tran`.`tran_hospital_dept` set id=CONCAT('-',id)  where hospital_code = 'LT';
update `sys-tran`.`tran_patient_info` set id=CONCAT('-',id)  where hospital_code = 'LT';
update `sys-tran`.`tran_sys_set` set id=-1  where `code` = 'template_admin';