linwei
2024-01-08 ad2ec97fe0c60eee4bb7c8cdacff7c876f280ea7
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
<?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="org.springblade.modules.house.mapper.HouseRentalMapper">
 
    <!-- 通用查询映射结果 -->
    <resultMap id="houseRentalResultMap" type="org.springblade.modules.house.entity.HouseRentalEntity">
        <result column="id" property="id"/>
        <result column="house_code" property="houseCode"/>
        <result column="house_name" property="houseName"/>
        <result column="tenant_relationship" property="tenantRelationship"/>
        <result column="rental_time" property="rentalTime"/>
        <result column="due_time" property="dueTime"/>
        <result column="house_status" property="houseStatus"/>
        <result column="rental_use" property="rentalUse"/>
        <result column="file_urls" property="fileUrls"/>
        <result column="audit_status" property="auditStatus"/>
        <result column="create_user" property="createUser"/>
        <result column="create_time" property="createTime"/>
        <result column="update_user" property="updateUser"/>
        <result column="update_time" property="updateTime"/>
        <result column="remark" property="remark"/>
        <result column="is_deleted" property="isDeleted"/>
    </resultMap>
 
    <resultMap id="houseRentalTenant" type="org.springblade.modules.house.vo.HouseRentalVO"
               autoMapping="true">
        <id property="id" column="id"/>
        <collection property="householdVOList" javaType="java.util.List"
                    ofType="org.springblade.modules.house.vo.HouseholdVO" autoMapping="true">
            <id property="id" column="tenantId"/>
        </collection>
    </resultMap>
 
 
    <sql id="selectHouseRental">
        select
            id,
            house_code,
            tenant_relationship,
            rental_time,
            due_time,
            termination_time,
            house_status,
            rental_use,
            audit_status,
            file_urls,
            create_user,
            create_time,
            update_user,
            update_time,
            remark,
            is_deleted
        from
            jczz_house_rental
    </sql>
 
    <!--自定义列表查询-->
    <select id="selectHouseRentalPage" resultType="org.springblade.modules.house.vo.HouseRentalTenantVO">
        SELECT jhr.*,jda.address_name as houseName,b.tenantName,b.phone,
        concat(jh.district_name," ",jh.building," ",unit," ",room) as address,
        case when TIMESTAMPDIFF( MONTH, jhr.rental_time, jhr.due_time )&gt;= 8 then 1
             when TIMESTAMPDIFF( MONTH, jhr.rental_time, jhr.due_time )&lt;4 then 3
             else 2 end as dldType,
        if(jhr.termination_time is null,if(date_format(jhr.due_time,'%Y-%m-%d') >= date_format(now(),'%Y-%m-%d'),0,1),2) as status
        FROM jczz_house_rental jhr
        JOIN jczz_doorplate_address jda ON jda.address_code = jhr.house_code
        JOIN jczz_house jh ON jh.house_code = jhr.house_code
        LEFT JOIN jczz_grid_range jgr on jgr.house_code=jda.address_code
        LEFT JOIN jczz_grid jg on jg.id = jgr.grid_id and jg.is_deleted = 0
        LEFT JOIN (
            SELECT jht.housing_rental_id,jht.name as tenantName,jht.phone_number as phone
            FROM jczz_household jht RIGHT JOIN (
                SELECT MAX(ID) as id,housing_rental_id
                FROM jczz_household
                WHERE is_deleted = 0
                <if test="vo.tenantName != null and vo.tenantName != ''">
                    AND name LIKE CONCAT('%',#{vo.tenantName},'%')
                </if>
                and housing_rental_id is not null
                GROUP BY  housing_rental_id
            ) a ON a.id = jht.id and a.housing_rental_id is not null
        ) b ON b.housing_rental_id = jhr.id
        WHERE jhr.is_deleted = 0
        <if test="vo.auditStatus != null and vo.auditStatus != ''">
            <if test="vo.auditStatus ==1">
                AND jhr.audit_status  = 1
            </if>
            <if test="vo.auditStatus ==2">
                AND jhr.audit_status  = 0
            </if>
            <if test="vo.auditStatus ==10">
                AND date_format(jhr.due_time,'%Y-%m-%d')&gt;= date_format(now(),'%Y-%m-%d')
            </if>
            <if test="vo.auditStatus ==20">
                AND TIMESTAMPDIFF( day, now(), jhr.due_time )&lt;30
                AND TIMESTAMPDIFF( day, now(), jhr.due_time )&gt;= 0
            </if>
            <if test="vo.auditStatus ==30">
                AND date_format(jhr.due_time,'%Y-%m-%d')&lt; date_format(now(),'%Y-%m-%d')
            </if>
        </if>
        <if test="vo.tenantName != null and vo.tenantName != ''">
            AND b.tenantName LIKE CONCAT('%',#{vo.tenantName},'%')
        </if>
        <if test="vo.tenantRelationship != null">
            AND jhr.tenant_relationship = #{vo.tenantRelationship}
        </if>
        <if test="vo.houseStatus != null">
            AND jhr.house_status = #{vo.houseStatus}
        </if>
        <if test="vo.rentalUse != null">
            AND jhr.rental_use = #{vo.rentalUse}
        </if>
        <if test="vo.dldType != null">
            <if test="vo.dldType ==1 ">
                AND TIMESTAMPDIFF( MONTH, jhr.rental_time, jhr.due_time )&gt;= 8
            </if>
            <if test="vo.dldType ==2 ">
                AND  4 &lt;= TIMESTAMPDIFF( MONTH, jhr.rental_time, jhr.due_time )
                AND TIMESTAMPDIFF( MONTH, jhr.rental_time, jhr.due_time )&lt;=8
            </if>
            <if test="vo.dldType ==3 ">
                AND TIMESTAMPDIFF( MONTH, jhr.rental_time, jhr.due_time )&lt;4
            </if>
        </if>
        <if test="vo.startTime != null and vo.startTime != '' and vo.endTime != null and vo.endTime != '' ">
            AND jhr.create_time BETWEEN #{vo.startTime} and #{vo.endTime}
        </if>
        <if test="vo.regionCode!=null and vo.regionCode!=''">
            and jg.community_code like concat('%',#{vo.regionCode},'%')
        </if>
        <include refid="filterHouseGrid"/>
    </select>
 
    <!--查询房屋出租情况-->
    <select id="getHouseRentalListByCode" resultMap="houseRentalTenant">
        select
            jhr.*,
            if(termination_time is null,if(date_format(jhr.due_time,'%Y-%m-%d') >= date_format(now(),'%Y-%m-%d'),0,1),2) as status,
            jht.id as tenantId,
            jht.*
        from jczz_house_rental jhr
        left join jczz_household jht on jhr.id = jht.housing_rental_id and jht.is_deleted = 0
        where 1 = 1
        and jhr.is_deleted = 0
        and jhr.house_code = #{code}
    </select>
 
    <sql id="filterHouseGrid">
        <if test="vo.roleName!=null and vo.roleName!=''">
            <if test="vo.roleName=='网格员'">
                <choose>
                    <when test="list != null and list.size()>0">
                        and jda.address_code in
                        <foreach collection="list" item="houseCode" separator ="," open="("  close=")">
                            #{houseCode}
                        </foreach>
                    </when>
                    <otherwise>
                        and jda.address_code in ('')
                    </otherwise>
                </choose>
            </if>
        </if>
    </sql>
 
    <select id="getStatistics" resultType="org.springblade.modules.house.vo.HouseRentalStatistics">
        SELECT
        'longTerm' as term,count(1) total,ifnull(sum(num),0) as personNum
        FROM jczz_house_rental jhr
        LEFT JOIN
        (
        select housing_rental_id,count(*) num from jczz_household
        where is_deleted = 0 and housing_rental_id is not null
        GROUP BY housing_rental_id
        ) jht
        ON jht.housing_rental_id = jhr.id
        LEFT JOIN jczz_doorplate_address jda ON jda.address_code = jhr.house_code
        WHERE jhr.is_deleted = 0
        AND TIMESTAMPDIFF( MONTH, jhr.rental_time, jhr.due_time )>= 8
        <if test="vo.auditStatus != null and vo.auditStatus != '' or vo.auditStatus == 0 ">
            AND jhr.audit_status = #{vo.auditStatus}
        </if>
        <include refid="filterHouseGrid"/>
 
        UNION ALL
 
        SELECT 'middleTerm' AS term,count(1) total,ifnull(sum(num),0) as personNum
        FROM jczz_house_rental jhr
        LEFT JOIN
        (
        select housing_rental_id,count(*) num from jczz_household
        where is_deleted = 0 and housing_rental_id is not null
        GROUP BY housing_rental_id
        ) jht
        ON jht.housing_rental_id = jhr.id
        LEFT JOIN jczz_doorplate_address jda ON jda.address_code = jhr.house_code
        WHERE jhr.is_deleted = 0
        AND 4 &lt;= TIMESTAMPDIFF( MONTH, rental_time, due_time ) AND TIMESTAMPDIFF( MONTH, rental_time, due_time )&lt;=8
        <if test="vo.auditStatus != null and vo.auditStatus != '' or vo.auditStatus == 0 ">
            AND jhr.audit_status = #{vo.auditStatus}
        </if>
        <include refid="filterHouseGrid"/>
 
        UNION ALL
 
        SELECT 'shortTerm' AS term,count(1) total,ifnull(sum(num),0) as personNum
        FROM jczz_house_rental jhr
        LEFT JOIN
        (
        select housing_rental_id,count(*) num from jczz_household
        where is_deleted = 0 and housing_rental_id is not null
        GROUP BY housing_rental_id
        ) jht
        ON jht.housing_rental_id = jhr.id
        LEFT JOIN jczz_doorplate_address jda ON jda.address_code = jhr.house_code
        WHERE jhr.is_deleted = 0
        AND TIMESTAMPDIFF( MONTH, rental_time, due_time )&lt;4
        <if test="vo.auditStatus != null and vo.auditStatus != '' or vo.auditStatus == 0 ">
                AND jhr.audit_status  = #{vo.auditStatus}
            </if>
            <include refid="filterHouseGrid"/>
    </select>
 
    <!--导出租赁信息-->
    <select id="export" resultType="org.springblade.modules.house.excel.HouseRentalExcel">
        SELECT
        jhr.tenant_relationship,jhr.rental_time,jhr.due_time,jhr.house_status,
        jhr.rental_use,jhr.audit_status,jhr.remark,
        jda.address_name as houseName,b.tenantName,b.phone,
        concat(jh.district_name," ",jh.building," ",unit," ",room) as address,
        case when TIMESTAMPDIFF( MONTH, jhr.rental_time, jhr.due_time )>= 8 then 1
        when TIMESTAMPDIFF( MONTH, jhr.rental_time, jhr.due_time )&lt;4 then 3
        else 2 end as dldType
        FROM jczz_house_rental jhr
        JOIN jczz_doorplate_address jda ON jda.address_code = jhr.house_code
        JOIN jczz_house jh ON jh.house_code = jhr.house_code and jh.is_deleted = 0
        LEFT JOIN (
        SELECT jht.housing_rental_id,jht.name as tenantName,jht.phoneNumber phone
        FROM jczz_household jht RIGHT JOIN (
        SELECT MAX(ID) as id,housing_rental_id
        FROM jczz_household
        WHERE is_deleted = 0 and house_rental_id is not null
        <if test="vo.tenantName != null and vo.tenantName != ''">
            AND name LIKE CONCAT('%',#{vo.tenantName},'%')
        </if>
        GROUP BY  housing_rental_id
        ) a ON a.id = jht.id
        ) b ON b.housing_rental_id = jhr.id
        WHERE jhr.is_deleted = 0
        <if test="vo.auditStatus != null and vo.auditStatus != ''">
            <if test="vo.auditStatus ==1">
                AND jhr.audit_status  = 1
            </if>
            <if test="vo.auditStatus ==2">
                AND jhr.audit_status  = 0
            </if>
            <if test="vo.auditStatus ==10">
                AND date_format(jhr.due_time,'%Y-%m-%d')&gt;= date_format(now(),'%Y-%m-%d')
            </if>
            <if test="vo.auditStatus ==20">
                AND TIMESTAMPDIFF( day, now(), jhr.due_time )&lt;30
            </if>
            <if test="vo.auditStatus ==30">
                AND date_format(jhr.due_time,'%Y-%m-%d')&lt; date_format(now(),'%Y-%m-%d')
            </if>
        </if>
        <if test="vo.tenantName != null and vo.tenantName != ''">
            AND b.tenantName LIKE CONCAT('%',#{vo.tenantName},'%')
        </if>
        <if test="vo.tenantRelationship != null">
            AND jhr.tenant_relationship = #{vo.tenantRelationship}
        </if>
        <if test="vo.houseStatus != null">
            AND jhr.house_status = #{vo.houseStatus}
        </if>
        <if test="vo.rentalUse != null">
            AND jhr.rental_use = #{vo.rentalUse}
        </if>
        <if test="vo.dldType != null">
            <if test="vo.dldType ==1 ">
                AND TIMESTAMPDIFF( MONTH, jhr.rental_time, jhr.due_time )>= 8
            </if>
            <if test="vo.dldType ==2 ">
                AND  4 &lt;= TIMESTAMPDIFF( MONTH, jhr.rental_time, jhr.due_time )
                AND TIMESTAMPDIFF( MONTH, jhr.rental_time, jhr.due_time )&lt;=8
            </if>
            <if test="vo.dldType ==3 ">
                AND TIMESTAMPDIFF( MONTH, jhr.rental_time, jhr.due_time )&lt;4
            </if>
        </if>
        <include refid="filterHouseGrid"/>
    </select>
 
 
    <select id="getStatisticsCount" resultType="java.lang.Integer">
        SELECT
        count( 1 )
        FROM
        jczz_house_rental jhr
        LEFT JOIN jczz_doorplate_address jda ON jhr.house_code = jda.address_code
        <where>
            <if test="neiCode != null and neiCode != ''">
                and jda.nei_code = #{neiCode}
            </if>
            <if test="userId != null">
                AND jhr.house_code IN (
                SELECT
                jgr.house_code
                FROM
                jczz_grid_range jgr
                LEFT JOIN jczz_grid jg ON jg.id = jgr.grid_id
                LEFT JOIN jczz_gridman jgm ON jg.id = jgm.grid_id
                WHERE
                jg.is_deleted = 0
                AND jgm.user_id = #{userId} )
            </if>
            and jhr.is_deleted = 0
            and jhr.audit_status = 0
        </where>
 
    </select>
 
 
</mapper>