Administrator
2022-09-14 58d006e05dcf2a20d0ec5367dd03d66a61db6849
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.mandi.fendan.mapper.Fd_XSTDMapper">
    <sql id="column">
    a.td_bmbh,a.td_bmmc,a.td_bz,a.td_djrq,a.td_fpbh, a.td_jylx, a.td_khbh,a.td_cqNo,
    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
    </sql>
    <!--查询地区的厂区 -->
    <sql id="sqlDq">
        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,'' ) ),'')
    </sql>
    <!--查询物流公司的厂区 -->
    <sql id="mxWlgsDq">
    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,'' ) ),'')
    </sql>
 
    <!-- 根据地区来决定是否显示 -->
    <sql id="changqu">
        <if test=" cqV !=null and cqV !='' ">
            <if test="companyNo!=null and companyNo!=''">  <!--要确保是管理员或者是fdAdmin的时候companyNo是空-->
                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}
                <if test=" cqV !=null and cqV !='' ">
                    <choose>
                        <when test="cqV=='102' || cqV=='111' || cqV=='114' ">
                            and b.ywdyNo=#{cqV}
                        </when>
                        <otherwise>
                            and b.ywdyNo!='102' and b.ywdyNo!='111' and b.ywdyNo!='114'
                        </otherwise>
                    </choose>
                </if>
                ) )
                )
            </if>
            <choose>
                <when test="cqV=='102' || cqV=='111' || cqV=='114'">
                    and a.td_cqNo=#{cqV}
                </when>
                <otherwise>
                    and a.td_cqNo !='102' and a.td_cqNo !='111' and a.td_cqNo !='114'
                </otherwise>
            </choose>
        </if>
    </sql>
   <sql id="condition">
       <if test="companyNo !=null and companyNo !='' ">
           <choose>
               <when test="ztbz==0">
                   AND a.td_ztbz  in (1,3)   <!--非自提-->
               </when>
               <otherwise>
                   and a.td_ztbz not in (1,3) <!-- 自提 -->
               </otherwise>
           </choose>
       </if>
       <if test="sdate !=null and sdate !='' ">
           and a.td_djrq &gt;= #{sdate}+' 00:00:00'
       </if>
       <if test="edate !=null and edate !='' ">
           and a.td_djrq &lt;= #{edate}+' 23:59:59'
       </if>
       <if test="fdAdmin == null || fdAdmin == '' ">
           <choose>
               <when test="wcSign==0">
                   and a.td_wfdsl &gt; 0
               </when>
               <otherwise>
                   and a.td_wfdsl &lt;= 0
               </otherwise>
           </choose>
       </if>
       <if test="fdAdmin != null and fdAdmin != '' "> <!-- 在车辆分单页面时 -->
        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 &gt; 0  <!-- 有未分单显示 -->
        )
       </if>
       <if test="tdbh !=null and tdbh !='' ">
           and a.td_tdbh like '%'+#{tdbh}+'%'
       </if>
       <if test="shdd !=null and shdd !='' ">
            and c.name = #{shdd}
       </if>
       <if test="zdbm !=null and zdbm !='' ">
           and ( a.td_bmmc like '%'+#{zdbm}+'%')
       </if>
       <if test="fpbh !=null and fpbh !='' ">
           and ( a.td_fpbh like '%'+#{fpbh}+'%')
       </if>
       <if test="zdr !=null and zdr !='' ">
           and ( a.td_zdr like '%'+#{zdr}+'%')
       </if>
       <!-- 关联流水号中的可见物流公司(全部为空,显示地区;不全部为空,显示地区+可见;全部不为空,显示可见物流公司) -->
       <if test=" cqV !=null and cqV !='' ">
          and case when not exists (select * from fd_mx_wlgs where td_tdls=a.td_tdls)
           <include refid="changqu"/> <!--  厂区可见 -->
           then 1
           <if test="companyNo!=null and companyNo!=''">   <!--全部不为空,即提单中的每一项都手动分了-->
               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<include refid="changqu"/>)   <!-- 区域物流公司 + 手动分配的物流公司 可见  -->
               or exists(select * from fd_mx_wlgs where td_tdls=a.td_tdls and wlgs_no=#{companyNo}) then 1
           </if>
           else 0 end > 0
       </if>
   </sql>
 
 
    <select id="getList" parameterType="java.util.Map" resultType="com.mandi.fendan.util.persist.WaitXSTD">
        <choose>
            <when test="pagesize &gt; 0 ">
                select top ${pagesize} *
            </when>
            <otherwise>
                select *
            </otherwise>
        </choose>
        from ( select row_number()
        <choose>
            <when test="sort!=null and sort!='' ">
                over (order by ${sort} )
            </when>
            <otherwise>
                over (order by td_djrq desc )
            </otherwise>
        </choose>
        as rownumber, a.* from
        (select case when a.td_ztbz not in (1,3) then ''  <!-- 自提不显示物流公司  -->
        when not exists (select * from fd_mx_wlgs where td_tdls=a.td_tdls)
        then  <include refid="sqlDq"/>      <!-- 未人为分配,显示区域物流公司  -->
        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 <include refid="mxWlgsDq"/>   <!-- 全部分配,显示分配的物流公司  -->
        else <include refid="sqlDq"/> + ' ' + <include refid="mxWlgsDq"/>  <!-- 其他即“未全部人为分配”,则显示合集 -->
        end as  companyName,
        DATEDIFF (mi,a.td_djrq,getdate())  timeMinus,
        <include refid="column"/> from fd_xstd a
        LEFT JOIN dbo.company b ON a.td_khbh= b.companyNo
        LEFT JOIN dbo.nc_dq c ON a.td_shddbh= c.code
        LEFT JOIN dbo.nc_ywdy d ON a.td_ywdybh= d.code
        left join dbo.nc_dq  e on a.td_shddbh =  e.code
        <where>
            NOT EXISTS ( SELECT 1 FROM dbo.fd_gbxstd g WHERE a.td_tdbh= g.xstdNo )
            <include refid="condition"/>
        </where>
        ) a
        <where>
         <if test="khmc !=null and khmc !='' ">
           and ( a.td_khmc like '%'+#{khmc}+'%')
         </if>
       </where>
        ) as a where rownumber > #{pagesize}*#{page}
 
    </select>
 
    <select id="countPage" parameterType="Map" resultType="int">
        select count(*) from fd_xstd a
        LEFT JOIN dbo.company b ON a.td_khbh= b.companyNo
        LEFT JOIN dbo.nc_dq c ON a.td_shddbh= c.code
        LEFT JOIN dbo.nc_ywdy d ON a.td_ywdybh= d.code
        left join dbo.nc_dq  e on a.td_shddbh =  e.code
        <where>
            NOT EXISTS ( SELECT 1 FROM dbo.fd_gbxstd g WHERE a.td_tdbh= g.xstdNo )
            <include refid="condition"/>
        </where>
    </select>
</mapper>