
-- 任务数
update wwyt_bp_investigate_task_summary_his summary
set task_count = (select count(*)
                  from wwyt_bp_investigate_task_his task
                           inner join wwyt_bp_investigate_task_assign_his assign on task.id = assign.task_id
                  where assign.summary_id = summary.id) +
                 (select count(*)
                  from wwyt_bp_investigate_task task
                           inner join wwyt_bp_investigate_task_assign assign on task.id = assign.task_id
                  where assign.summary_id = summary.id)
where source_type = 1;

-- 完成数
update wwyt_bp_investigate_task_summary_his summary
set task_done_count = (select count(*)
                       from wwyt_bp_investigate_task_his task
                                inner join wwyt_bp_investigate_task_assign_his assign on task.id = assign.task_id
                                inner join wwyt_bp_investigate_record_his record on record.task_id = task.id
                       where assign.summary_id = summary.id
                         and task.status = '已完成' and record.check_result != '未检') +
                      (select count(*)
                       from wwyt_bp_investigate_task task
                                inner join wwyt_bp_investigate_task_assign assign on task.id = assign.task_id
                                inner join wwyt_bp_investigate_record record on record.task_id = task.id
                       where assign.summary_id = summary.id
                         and task.status = '已完成' and record.check_result != '未检')
where source_type = 1;

-- 未检数
update wwyt_bp_investigate_task_summary_his summary
set uncheck_count = (select count(*)
                       from wwyt_bp_investigate_task_his task
                                inner join wwyt_bp_investigate_task_assign_his assign on task.id = assign.task_id
                                inner join wwyt_bp_investigate_record_his record on task.id = record.task_id
                       where assign.summary_id = summary.id
                         and record.check_result = '未检') +
                    (select count(*)
                       from wwyt_bp_investigate_task task
                                inner join wwyt_bp_investigate_task_assign assign on task.id = assign.task_id
                                inner join wwyt_bp_investigate_record record on task.id = record.task_id
                       where assign.summary_id = summary.id
                         and record.check_result = '未检')
where source_type = 1;

-- 发现隐患数
update wwyt_bp_investigate_task_summary_his summary
set exception_count = (select count(*)
                       from wwyt_bp_investigate_task_his task
                                inner join wwyt_bp_investigate_task_assign_his assign on task.id = assign.task_id
                                inner join wwyt_bp_investigate_record_his record on task.id = record.task_id
                       where assign.summary_id = summary.id
                         and record.check_result = '异常') +
                      (select count(*)
                       from wwyt_bp_investigate_task task
                                inner join wwyt_bp_investigate_task_assign assign on task.id = assign.task_id
                                inner join wwyt_bp_investigate_record record on task.id = record.task_id
                       where assign.summary_id = summary.id
                         and record.check_result = '异常')
where source_type = 1;


-- 任务数
update wwyt_bp_investigate_task_summary summary
set task_count = (select count(*)
                  from wwyt_bp_investigate_task_his task
                           inner join wwyt_bp_investigate_task_assign_his assign on task.id = assign.task_id
                  where assign.summary_id = summary.id) +
                 (select count(*)
                  from wwyt_bp_investigate_task task
                           inner join wwyt_bp_investigate_task_assign assign on task.id = assign.task_id
                  where assign.summary_id = summary.id)
where source_type = 1;

-- 完成数
update wwyt_bp_investigate_task_summary summary
set task_done_count = (select count(*)
                       from wwyt_bp_investigate_task_his task
                                inner join wwyt_bp_investigate_task_assign_his assign on task.id = assign.task_id
                                inner join wwyt_bp_investigate_record_his record on record.task_id = task.id
                       where assign.summary_id = summary.id
                         and task.status = '已完成' and record.check_result != '未检') +
                      (select count(*)
                       from wwyt_bp_investigate_task task
                                inner join wwyt_bp_investigate_task_assign assign on task.id = assign.task_id
                                inner join wwyt_bp_investigate_record record on record.task_id = task.id
                       where assign.summary_id = summary.id
                         and task.status = '已完成' and record.check_result != '未检')
where source_type = 1;

-- 未检数
update wwyt_bp_investigate_task_summary summary
set uncheck_count = (select count(*)
                       from wwyt_bp_investigate_task_his task
                                inner join wwyt_bp_investigate_task_assign_his assign on task.id = assign.task_id
                                inner join wwyt_bp_investigate_record_his record on task.id = record.task_id
                       where assign.summary_id = summary.id
                         and record.check_result = '未检') +
                    (select count(*)
                       from wwyt_bp_investigate_task task
                                inner join wwyt_bp_investigate_task_assign assign on task.id = assign.task_id
                                inner join wwyt_bp_investigate_record record on task.id = record.task_id
                       where assign.summary_id = summary.id
                         and record.check_result = '未检')
where source_type = 1;

-- 发现隐患数
update wwyt_bp_investigate_task_summary summary
set exception_count = (select count(*)
                       from wwyt_bp_investigate_task_his task
                                inner join wwyt_bp_investigate_task_assign_his assign on task.id = assign.task_id
                                inner join wwyt_bp_investigate_record_his record on task.id = record.task_id
                       where assign.summary_id = summary.id
                         and record.check_result = '异常') +
                      (select count(*)
                       from wwyt_bp_investigate_task task
                                inner join wwyt_bp_investigate_task_assign assign on task.id = assign.task_id
                                inner join wwyt_bp_investigate_record record on task.id = record.task_id
                       where assign.summary_id = summary.id
                         and record.check_result = '异常')
where source_type = 1;