a.td_bmbh,a.td_bmmc,a.td_bz,a.td_djrq,a.td_fpbh, a.td_jylx, a.td_khbh,a.td_cqNo,a.committime,
CASE WHEN a.td_fpbh IS NULL THEN b.name WHEN a.td_fpbh= '' THEN b.name
WHEN a.td_ywdybh= '101' THEN '河南报关' WHEN a.td_ywdybh= '102' THEN '郑州报关' WHEN a.td_ywdybh= '111' THEN '明晟报关' WHEN a.td_ywdybh= '114' THEN '义瑞报关' ELSE '科技报关'
END td_khmc,
a.td_shddbh,
c.name as td_shdd,
a.td_tdbh,
a.td_tdls,
a.td_wfdsl,
a.td_wfdzl,
a.td_zcwcbz,
a.td_zdrmc td_zdr,
a.td_zdr as td_zdrbh,
CASE a.td_ztbz WHEN '1' THEN '含运费送货' WHEN '3' THEN '不含运费送货' ELSE '自提' END td_ztbz,
a.td_shsj td_shsj,
a.td_ywdybh,
d.name as td_ywdyName,
e.name as dq_name
ISNULL((select STUFF((select distinct ','+b.name from fd_wlgs_fhdq f left join fd_company b on f.wlgsNo=b.companyNo where f.fhdqNo=a.td_shddbh
and f.ywdyNo=(case when a.td_ywdybh in ('102','111','114') then a.td_ywdybh else '103' end)
and EXISTS (SELECT 1 from fd_company_ywdy c where b.companyNo=c.companyNo and c.ywdyNo=a.td_ywdybh )
for xml path('')
),1,1,'' ) ),'')
ISNULL(( select STUFF( (
select ','+name from fd_company fdc where EXISTS (
select * from fd_mx_wlgs where wlgs_no = fdc.companyNo and td_tdls=a.td_tdls
) for xml path('')
),1,1,'' ) ),'')
and ( (a.td_ztbz not in (1,3) and EXISTS ( select * from fd_company_ywdy cy left join fd_company fc on
cy.companyNo = fc.companyNo where hasSPR=1 and cy.companyNo=#{companyNo} ) )
or
(a.td_ztbz in (1,3) and EXISTS (SELECT 1 from fd_wlgs_fhdq b LEFT JOIN nc_dq c on b.fhdqNo=c.code
where c.code=a.td_shddbh and b.wlgsNo=#{companyNo}
and b.ywdyNo=#{cqV}
and b.ywdyNo!='102' and b.ywdyNo!='111' and b.ywdyNo!='114'
) )
)
and a.td_cqNo=#{cqV}
and a.td_cqNo !='102' and a.td_cqNo !='111' and a.td_cqNo !='114'
AND a.td_ztbz in (1,3)
and a.td_ztbz not in (1,3)
and a.td_djrq >= #{sdate}+' 00:00:00'
and a.td_djrq <= #{edate}+' 23:59:59'
and a.td_wfdsl > 0
and a.td_wfdsl <= 0
and (
(a.td_wfdsl=0 and exists (select 1 from fd_xstdmx tdmx where tdmx.tdmx_tdls = a.td_tdls and
tdmx.tdmx_gbbz=0 and exists(select 1 from bussinesssheet bs where bussinessNo=tdmx.tdmx_fdywbh
and cardno is not null and cardno!='') )
)
or a.td_wfdsl > 0
)
and a.td_tdbh like '%'+#{tdbh}+'%'
and c.name = #{shdd}
and ( a.td_bmmc like '%'+#{zdbm}+'%')
and ( a.td_fpbh like '%'+#{fpbh}+'%')
and ( a.td_zdr like '%'+#{zdr}+'%')
and case when not exists (select * from fd_mx_wlgs where td_tdls=a.td_tdls)
then 1
when exists (select * from fd_xstdmx mx where exists (
select * from fd_mx_wlgs where tdmx_tdfl=mx.tdmx_Tdfl) and tdmx_tdls=a.td_tdls
) then (case when exists (select * from fd_mx_wlgs where td_tdls=a.td_tdls and wlgs_no=#{companyNo} ) then 1 else 0 end)
when (1=1)
or exists(select * from fd_mx_wlgs where td_tdls=a.td_tdls and wlgs_no=#{companyNo}) then 1
else 0 end > 0