select p3.id,p3.dept_id, p3.vehicle_id,
p3.maint_items, p3.remark,
IF(v.plate_code is not null and v.plate_code!='',CONCAT_WS("---",v.plate_number,v.plate_code),v.plate_number) as
plate_number, v.plate_code,
case when m.repair_depot_id is null then p3.repair_depot_id else m.repair_depot_id end as repair_depot_id,
case when m.maint_type is null then p3.maint_type else m.maint_type end as maint_type,
case when m.apply_remark is null then p3.apply_remark else m.apply_remark end as apply_remark
from process_approval_info p1
left join process_info p2 on p1.process_id=p2.process_id and p1.relation_table='process_maint_apply_outside'
left join process_maint_apply_outside p3 on p3.id=p1.relation_id
left join vehicle_base_info v on v.id=p3.vehicle_id
left JOIN 
(SELECT a.apply_id, a.repair_depot_id, a.maint_type, a.apply_remark FROM process_maint_apply_outside_modify a ,
(select apply_id, MAX( gmt_create ) AS max_createdate from process_maint_apply_outside_modify group by apply_id ) b 
where a.apply_id = b.apply_id and a.gmt_create = b.max_createdate ) m on m.apply_id =p3.id
<where>
    <if test="map.maintType!=null and map.maintType!=''">
        and ((m.maint_type is not null and m.maint_type = #{map.maintType}) or (m.maint_type is null and p3.maint_type= #{map.maintType}))
    </if>
    <if test="map.repairDepotId!=null and map.repairDepotId!=''">
        and ((m.repair_depot_id is not null and m.repair_depot_id = #{map.repairDepotId}) or (m.repair_depot_id is null and p3.repair_depot_id= #{map.repairDepotId}))
    </if>
</where>
order by maint_apply_time