-- 初始化保单表
TRUNCATE TABLE dd_case_policy;

-- ---------保单表数据迁移------------------
INSERT INTO `dd_case_policy` (
	`regist_no`,
	`policy_no`,
	`insurance_code`,
	`insurance_name`,
	`insurance_type_code`,
	`insurance_type_name`,
	`insurance_org_code`,
	`is_enable`
) SELECT
	CASE_INFO_ID,
	POLICY_NUMBER,
	INSURANCE_CODE,
	INSURANCE_NAME,
	INSURANCE_TYPE_CODE,
	INSURANCE_TYPE_NAME,
	insurance_Org_Code,
	'1'
FROM
	sd_policy_item;
	
-- ------------------------------额外处理字段------------------------------

-- -----1-和案件表保持一致的字段
UPDATE dd_case_policy p,
 dd_case_info c
SET p.regist_no = c.regist_no

WHERE
	p.regist_no = c.case_info_id;

-- -----2-交强险-----
UPDATE dd_case_policy 
SET bi_policy_no = policy_no

WHERE
	insurance_name LIKE '%强制%';


-- -----3-商业险-----
UPDATE dd_case_policy 
SET ci_policy_no = policy_no

WHERE
	insurance_name LIKE '%商业%';
	

-- -----4-案件表保单号——交强险——bi_policy_no--------
UPDATE dd_case_info c,
 dd_case_policy p
SET c.bi_policy_no = p.bi_policy_no

WHERE
	1 = 1
AND c.regist_no = p.regist_no
AND p.bi_policy_no IS NOT NULL;

-- -----5-案件表保单号——商业险——ci_policy_no--------
UPDATE dd_case_info c,
 dd_case_policy p
SET c.ci_policy_no = p.ci_policy_no

WHERE
	1 = 1
AND c.regist_no = p.regist_no
AND p.ci_policy_no IS NOT NULL;



-- -----6-案件表保单号处理——policy_no--------（需要等交强、商业险保单有字段后再执行）-----------------------
-- --需要执行两个脚本，优先记录商业险、无商业险再记录交强险
UPDATE dd_case_info c
SET c.policy_no = c.ci_policy_no
WHERE
	1 = 1
AND c.ci_policy_no IS NOT NULL;

UPDATE dd_case_info c
SET c.policy_no = c.bi_policy_no
WHERE
	1 = 1
AND c.policy_no IS NULL;




