
/*步骤:
  1.将检测结果表 hc_check_result_collect_data， hc_check_onizing_radiation_result,hc_check_plan表的数据从企业端先导入过来
  2.清空表：
       delete from check_result;
       delete from ray_device;
       delete from ray_source_device;
       delete from check_select_item;
       delete  from check_plan;
       delete from check_factor;
       delete from check_history_data;
       delete from check_over_point;
       delete from com_over_rate;
  3.执行导入程序


  4.获取每个检测选择的检测项：
     insert into check_select_item (check_id, cid, item_id, create_date, update_date)  select t1.check_id,t1.cid, t2.item_id,min(t2.create_date) as create_date, max(t2.update_date) as update_date from check_result t1
             left join check_target t2 on t1.target_id=t2.id group by t1.check_id, t2.item_id;

  5. 导入检测数据：
    1)导入 hc_check_plan表的数据到check_plan表

insert into check_plan (
       id, cid,
       check_work_no,
       check_work_name,
       check_date_begin,
       check_date_end,
       check_type,
       manager,
       check_service_type,
       org_ids,
       third_org_id,
       dept_id,
       check_report_name,
       check_report_file_ids,
       check_report_location,
       contract_file_ids,
       contract_location,
       check_point_map_file_ids,
        check_result_certificate_file_ids,
        edit_status,
        check_plan_type,
       create_by,
       create_date,
       update_by,
       update_date,
       check_pids,
       project_area_id,
       project_address,
       contact_name,
       contact_phone,
      check_from,
      report_status,
    report_date,give_report_date,is_valid
)
select id, cid,
       check_work_no,
       check_work_name,
       check_time_begin as check_date_begin,
       check_time_end as check_date_end,
       check_type,
       manager,
       check_service_type,
       org_id as org_ids,
       third_party_or_check_org_id as third_org_id, -- 要更正check_service_type 为0时有效
       third_party_or_check_org_id as dept_id,  -- 要更正 check_service_type 为1时有效
       check_report_name,  -- 要更正：报告名中文
       check_report_name as check_report_file_ids,  -- 要更正：报告文件id
       check_report_location,
       evaluate_or_check_file as contract_file_ids,
       evaluate_or_check_location as contract_location,
       check_point_map_file_ids as check_point_map_file_ids,
        check_result_certificate_file_ids as check_result_certificate_file_ids,
        `status` as edit_status, -- 要更正 0->1, 1->2
        check_plan_type,
       ifnull(create_by,0) as create_by,
       ifnull(create_date, '2000-01-01 00:00:00') as create_date,
       ifnull(update_by,0) as update_by,
       ifnull(update_date, '2000-01-01 00:00:00') as update_date,
       check_pids,
       proj_area_id as project_area_id,
       proj_area_detail as project_address,
       link_man as contact_name,
       link_tel as contact_phone,
        check_data_source as check_from, -- 更正: 1,4->1 , 2,3->2
proj_appear_status as report_status,
       org_report_time as report_date,
give_report_time as give_report_date,is_valid
       from hc_check_plan;



2)更正字段值：

update check_plan set third_org_id= null where check_service_type=1;-- 自行检测，第三方机构设为nul
update check_plan set dept_id = null where check_service_type=0; -- 委外检测，部门id设为null
update check_plan set check_report_name = null where locate(',',check_report_name) !=0; -- 去掉文件id
update check_plan set check_report_file_ids = null where locate(',',check_report_name) = 0; -- 去掉文件名
update check_plan set edit_status = 2 where edit_status=1;
update check_plan set edit_status = 1 where edit_status=0; -- 编辑状态
update check_plan set check_from = 1 where check_from in (1,4);
update check_plan set check_from = 2 where check_from in (2,3);

update check_plan set project_name = check_work_name where check_service_type=0;

update check_plan set target_standard_id=1 where check_date_begin<'2020-04-01 00:00:00';
update check_plan set target_standard_id=2 where check_date_begin>='2020-04-01 00:00:00';
*/



6. 值类型改为范围类型
update check_result set check_value_range_type=1, check_value_min=check_value, check_value_max=check_value
where target_id in (select id from check_target where check_value_type=2)
  and (check_value_range_type is null);


7. 设置检测的评价id
企业端执行：
    select t1.id, t2.checkplan_id from evaluate t1 left join evaluate_third t2 on t1.id=t2.eid
  where t1.is_valid=true and t2.is_valid=true;// 查出结果，id为评价id， checkplan_id为检测id
  导出结果，修改sql语句为 ：update check_plan set evaluate_id=？ where id=？; //评价id和检测id为上面查询出来的结果

  到检测服务执行


8.更新manager_name

select distinct  manager from check_plan where manager is not null; //检测服务执行 查出id

select t2.real_name, t1.id from db_hse_hdyy.staff_info t1 left join staff_basic_info t2 on t1.staff_basic_id=t2.id

where t1.id in (

    ) //企业端执行 查询名字。

    //更新到检测端


9. 关联工作已完成检测id 及关联工作完成时间
//数据关联已完成检测id
select update_date, hccplan_id from harmfactor_hccheckplanunderway where is_finish=true and is_valid=true;
//数据关联进行中检测id
select update_date, hccplan_id from harmfactor_hccheckplanunderway where is_finish=false and is_valid=true;

