drop procedure if exists UpdateEqpStatusLogs;
create
    definer = admin@`%` procedure UpdateEqpStatusLogs(in begindate datetime, in enddate datetime)
BEGIN
    ##1 报料，2 加料，3 开副室，4 晶棒产出，5 拆清
    if begindate = '2000-01-01 01:00:00' then
        set begindate = dATE_FORMAT(
                date_add(DATE_SUB(dATE_FORMAT(now() - 8.5 / 24, '%Y-%m-%d'), interval 1 day), interval 510 minute),
                '%Y-%m-%d %k:%i:%s');
    end if;

    set @logmaxtdate =
            (select dATE_FORMAT(date_add(dATE_FORMAT(max(eventtime) - 8.5 / 24, '%Y-%m-%d'), interval 510 minute),
                                '%Y-%m-%d %k:%i:%s')
             from eqponstatuslogs);

    if @logmaxtdate < begindate then
        set begindate = @logmaxtdate;
    end if;

    if enddate = '2000-01-01 01:00:00' then
        set enddate = dATE_FORMAT(date_add(dATE_FORMAT(now() - 8.5 / 24, '%Y-%m-%d'), interval 510 minute),
                                  '%Y-%m-%d %k:%i:%s');
    end if;

###报料记录
    insert into eqponstatuslogs(eqpname, event, eventtime, eventid, eqpid, eventtype)
    select substring(crystal_code, 5, 5) eqpname,
           case
               when report_type = '1' then '初装报料'
               when report_type = '2' then concat(substring(task_code, 13, 1), '次加料报料')
               else '' end               event,
           update_time,
           task_code,
           eq.eqpid,
           '1'
    from mes_sync.mes_report_task mrt
             left join reportdata.equipment eq on (substring(mrt.crystal_code, 5, 5) = eq.eqpname)
    where update_time between begindate and enddate
    order by create_time desc;

###加料记录
    insert into eqponstatuslogs(eqpname, eqpid, event, eventtime, eventid, eventtype)
    SELECT substring(order_no, 5, 5)                                                             eqpname,
           stove_no,
           case
               when feeding_task_code like '%CZ%' then '初装加料'
               when feeding_task_code like '%JL%' then concat(substring(feeding_task_code, 13, 1), '加次第',
                                                              right(task_code, 1), '桶加料') end event,
           ft.update_time,
           feeding_task_code,
           '2'
    from mes_sync.mes_furnace_task ft
             right join mes_sync.mes_feeding_detail fd
                        on (ft.feeding_task_id = fd.feeding_task_id and fd.material_type = '2')
    where ft.`status` = '3'
      and ft.update_time between begindate and enddate;

###开副室记录
    insert into eqponstatuslogs(eqpname, eqpid, event, eventtime, eventid, eventtype)
    select substring(order_no, 5, 5) eqpname,
           stove_no,
           '开副室'                  event,
           opening_time,
           code,
           '3'
    from mes_sync.mes_sub_chamber_task
    where opening_time between begindate and enddate;

###晶棒产出
    insert into eqponstatuslogs(eqpname, eqpid, event, eventtime, eventid, eventtype)
    select substring(order_no, 5, 5) eqpname,
           stove_no,
           concat(right(code, 3), '晶棒产出'),
           update_time,
           code,
           '4'
    from mes_sync.mes_production
    where update_time between begindate and enddate;

###拆清
    insert into eqponstatuslogs(eqpname, eqpid, event, eventtime, eventid, eventtype, eventstatus)
    select substring(order_no, 5, 5) eqpname,
           stove_no,
           '拆清',
           update_time,
           order_no,
           '5',
           status
    from mes_sync.mes_dismantle_task
    where 1 = 1
      and update_time between begindate and enddate
      and status = '3';

    ##更新归档时间
    update eqponstatuslogs
    set savedate = now()
    where eventtime between begindate and enddate;
end
