select c.* from
        (
        select s.id,s.org_id, s.org_name,s.org_path, s.device_name, dvs.device_name as host_name,
        (select mission_id from iot_device_diagnose_mission_ch ch where ch.iot_token = s.iot_token
        and ch.device_product=s.device_product and ch.dvs = s.host_code and ch.channel = s.device_code limit 1) mission_id
        from iot_device_info s
        inner join iot_device_info dvs on dvs.iot_token = s.iot_token
        and dvs.device_product = s.device_product and dvs.device_code = s.host_code
        and dvs.deleted = 0 and s.enable = 0 and dvs.device_type = 1
        where s.deleted=0 and s.enable=0 and s.device_type = 2
        
            and s.org_id=#{request.orgId}
         
        
            and s.org_path like CONCAT(#{request.orgPath}, '%')
         
        
            and s.device_name like CONCAT('%',#{request.deviceName},'%')
         
        order by s.host_code ,cast(s.device_code as signed)
        ) c where 1=1
        
            
             
            
                and c.mission_id is null
             
            
                and c.mission_id is not null
             
         
        
            
                and c.mission_id = #{request.missionCode}
             
            
             
         
     
    
        select count(1) from
        (
        select s.iot_token,s.device_product,s.host_code dvs,s.device_code channel,
        (select mission_id from iot_device_diagnose_mission_ch ch where ch.iot_token = s.iot_token
        and ch.device_product=s.device_product and ch.dvs = s.host_code and ch.channel = s.device_code limit 1) mission_id
        from iot_device_info s
        inner join iot_device_info dvs on dvs.iot_token = s.iot_token
        and dvs.device_product = s.device_product and dvs.device_code = s.host_code
        and dvs.deleted = 0 and s.enable = 0 and dvs.device_type = 1
        where s.deleted=0 and s.enable=0 and s.device_type = 2
        
            and s.org_id=#{request.orgId}
         
        
            and s.org_path like CONCAT(#{request.orgPath}, '%')
         
        
            and s.device_name like CONCAT('%',#{request.deviceName},'%')
         
        order by s.host_code ,cast(s.device_code as signed)
        ) c where 1=1
        
            
             
            
                and c.mission_id is null
             
            
                and c.mission_id is not null
             
         
        
            
                and c.mission_id = #{request.missionCode}
             
            
             
         
     
    
        select c.* from
        (
        select s.iot_token,s.device_product,s.host_code dvs,s.device_code channel,ch.mission_id,ch.id,
        (select group_concat(threshold_code) from iot_device_diagnose_threshold_ch ch where ch.iot_token = s.iot_token
        and ch.device_product=s.device_product and ch.dvs = s.host_code and ch.channel = s.device_code) threshold_codes
        from iot_device_info s
        inner join iot_device_info dvs on dvs.iot_token = s.iot_token
        and dvs.device_product = s.device_product and dvs.device_code = s.host_code
        and dvs.deleted = 0 and s.enable = 0 and dvs.device_type = 1
        left join iot_device_diagnose_mission_ch ch on ch.iot_token = s.iot_token
        and ch.device_product=s.device_product and ch.dvs = s.host_code and ch.channel = s.device_code
        where s.deleted=0 and s.enable=0 and s.device_type = 2
        
            and s.org_id=#{request.orgId}
         
        
            and s.org_path like CONCAT(#{request.orgPath}, '%')
         
        
            and s.device_name like CONCAT('%',#{request.deviceName},'%')
         
        order by s.host_code ,cast(s.device_code as signed)
        ) c where 1=1
        
            
             
            
                and c.mission_id is null
             
            
                and c.mission_id is not null
             
         
        
            
                and c.mission_id = #{request.missionCode}
             
            
             
         
     
    
        insert into iot_device_diagnose_mission_ch (id,mission_id,iot_token,device_product,dvs,channel)
        values
        
            (#{item.id},#{item.missionId},#{item.iotToken},#{item.deviceProduct},#{item.dvs}, #{item.channel})
         
     
    
        
            update iot_device_diagnose_mission_ch
            
                
                    mission_id = #{item.missionId},
                 
             
            where iot_token = #{item.iotToken} and device_product = #{item.deviceProduct}
            and dvs = #{item.dvs} and channel = #{item.channel}
         
     
    
        insert into iot_device_diagnose_threshold_ch (id,threshold_code, iot_token,device_product,dvs,channel)
        values
        
            (#{item.id},#{item.thresholdCode},#{item.iotToken},#{item.deviceProduct},#{item.dvs}, #{item.channel})
         
     
    
        delete from iot_device_diagnose_mission_ch where id in
        #{item}
         
     
    
        delete from iot_device_diagnose_mission_ch where mission_id = #{missionId}
     
    
        select * from iot_device_diagnose_mission