nishiwei
6 days ago 3f43e40ed1a1e9f66b3ad2862da9dbc4b710c712
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
<?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.ard.data.region.mapper.ArdDeptRegionMapper">
 
    <resultMap type="ArdDeptRegion" id="ArdDeptRegionResult">
        <id     property="id"          column="id"          />
        <result property="deptId"      column="dept_id"     />
        <result property="regionName"  column="region_name" />
        <result property="regionColor" column="region_color" />
        <result property="centerLat"   column="center_lat"  />
        <result property="centerLng"   column="center_lng"  />
        <result property="areaSize"    column="area_size"   />
        <result property="status"      column="status"      />
        <result property="createBy"    column="create_by"   />
        <result property="createTime"  column="create_time" />
        <result property="updateBy"    column="update_by"   />
        <result property="updateTime"  column="update_time" />
        <result property="remark"      column="remark"      />
    </resultMap>
 
    <sql id="selectArdDeptRegionVo">
        SELECT r.id, r.dept_id, r.region_name, r.region_color,
               ST_AsText(r.region_boundary) as boundary_wkt,
               r.center_lat, r.center_lng, r.area_size,
               r.status, r.create_by, r.create_time, r.update_by, r.update_time, r.remark,
               d.dept_name
        FROM ard_dept_region r
                 LEFT JOIN sys_dept d ON r.dept_id = d.dept_id
    </sql>
 
    <!-- 查询部门区域列表(查询所有,不过滤状态) -->
    <select id="selectArdDeptRegionList" parameterType="ArdDeptRegion" resultMap="ArdDeptRegionResult">
        <include refid="selectArdDeptRegionVo"/>
        <where>
            <if test="deptId != null"> AND r.dept_id = #{deptId}</if>
            <if test="regionName != null and regionName != ''"> AND r.region_name like concat('%', #{regionName}, '%')</if>
            <if test="status != null and status != ''"> AND r.status = #{status}</if>
        </where>
        ORDER BY r.create_time DESC
    </select>
 
    <!-- 查询部门区域(根据ID,查询所有) -->
    <select id="selectArdDeptRegionById" parameterType="Long" resultMap="ArdDeptRegionResult">
        <include refid="selectArdDeptRegionVo"/>
        WHERE r.id = #{id}
    </select>
 
    <!-- 获取边界WKT -->
    <select id="selectBoundaryWkt" parameterType="Long" resultType="String">
        SELECT ST_AsText(region_boundary)
        FROM ard_dept_region
        WHERE id = #{id}
    </select>
 
    <!-- 根据区域名称查询区域(用于唯一性校验,查询所有) -->
    <select id="checkRegionNameUnique" parameterType="String" resultMap="ArdDeptRegionResult">
        SELECT id, dept_id, region_name, region_color,
               center_lat, center_lng, area_size,
               status, create_by, create_time, update_by, update_time, remark
        FROM ard_dept_region
        WHERE region_name = #{regionName}
            LIMIT 1
    </select>
 
    <!-- 检查点冲突(只查启用的区域) -->
    <select id="checkPointConflict" resultType="ConflictRegion">
        SELECT
        id,
        region_name as regionName,
        region_color as regionColor,
        CASE
        WHEN ST_Contains(region_boundary, ST_GeomFromText(#{wkt}, 4326)) THEN '包含该点'
        WHEN ST_Intersects(region_boundary, ST_GeomFromText(#{wkt}, 4326)) THEN '相交'
        ELSE '无冲突'
        END as relationship
        FROM ard_dept_region
        WHERE status = '0'
        <if test="deptId != null">
            AND dept_id = #{deptId}
        </if>
        <if test="excludeId != null">
            AND id != #{excludeId}
        </if>
        AND ST_Intersects(region_boundary, ST_GeomFromText(#{wkt}, 4326))
    </select>
 
    <!-- 检查线冲突(只查启用的区域) -->
    <select id="checkLineConflict" resultType="ConflictRegion">
        SELECT
        id,
        region_name as regionName,
        region_color as regionColor,
        CASE
        WHEN ST_Intersects(region_boundary, ST_GeomFromText(#{wkt}, 4326)) THEN '相交'
        WHEN ST_Contains(region_boundary, ST_GeomFromText(#{wkt}, 4326)) THEN '包含该线'
        ELSE '无冲突'
        END as relationship
        FROM ard_dept_region
        WHERE status = '0'
        <if test="deptId != null">
            AND dept_id = #{deptId}
        </if>
        <if test="excludeId != null">
            AND id != #{excludeId}
        </if>
        AND ST_Intersects(region_boundary, ST_GeomFromText(#{wkt}, 4326))
    </select>
 
    <!-- 检查面冲突(只查启用的区域) -->
    <select id="checkPolygonConflict" resultType="ConflictRegion">
        SELECT
        id,
        region_name as regionName,
        region_color as regionColor,
        CASE
        WHEN ST_Contains(region_boundary, ST_GeomFromText(#{wkt}, 4326)) THEN '包含该面'
        WHEN ST_Within(ST_GeomFromText(#{wkt}, 4326), region_boundary) THEN '在该面内部'
        WHEN ST_Overlaps(region_boundary, ST_GeomFromText(#{wkt}, 4326)) THEN '重叠'
        WHEN ST_Intersects(region_boundary, ST_GeomFromText(#{wkt}, 4326)) THEN '相交'
        ELSE '无冲突'
        END as relationship
        FROM ard_dept_region
        WHERE status = '0'
        <if test="deptId != null">
            AND dept_id = #{deptId}
        </if>
        <if test="excludeId != null">
            AND id != #{excludeId}
        </if>
        AND ST_Intersects(region_boundary, ST_GeomFromText(#{wkt}, 4326))
    </select>
 
    <!-- 根据坐标查询所属区域(查询所有区域,包括禁用的) -->
    <select id="selectRegionByPoint" resultMap="ArdDeptRegionResult">
        <include refid="selectArdDeptRegionVo"/>
        WHERE ST_Contains(r.region_boundary, ST_GeomFromText(#{pointWkt}, 4326))
        LIMIT 1
    </select>
 
    <!-- 新增部门区域(默认状态为启用0) -->
    <insert id="insertArdDeptRegion" parameterType="ArdDeptRegion" useGeneratedKeys="true" keyProperty="id">
        INSERT INTO ard_dept_region
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="deptId != null">dept_id,</if>
            <if test="regionName != null">region_name,</if>
            <if test="regionColor != null">region_color,</if>
            <if test="boundaryWkt != null">region_boundary,</if>
            <if test="centerLat != null">center_lat,</if>
            <if test="centerLng != null">center_lng,</if>
            <if test="areaSize != null">area_size,</if>
            <if test="createBy != null">create_by,</if>
            <if test="remark != null">remark,</if>
            status, create_time
        </trim>
        VALUES
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="deptId != null">#{deptId},</if>
            <if test="regionName != null">#{regionName},</if>
            <if test="regionColor != null">#{regionColor},</if>
            <if test="boundaryWkt != null">ST_GeomFromText(#{boundaryWkt}, 4326),</if>
            <if test="centerLat != null">#{centerLat},</if>
            <if test="centerLng != null">#{centerLng},</if>
            <if test="areaSize != null">#{areaSize},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="remark != null">#{remark},</if>
            '0', NOW()
        </trim>
    </insert>
 
    <!-- 修改部门区域 -->
    <update id="updateArdDeptRegion" parameterType="ArdDeptRegion">
        UPDATE ard_dept_region
        <set>
            <if test="deptId != null">dept_id = #{deptId},</if>
            <if test="regionName != null">region_name = #{regionName},</if>
            <if test="regionColor != null">region_color = #{regionColor},</if>
            <if test="boundaryWkt != null">region_boundary = ST_GeomFromText(#{boundaryWkt}, 4326),</if>
            <if test="centerLat != null">center_lat = #{centerLat},</if>
            <if test="centerLng != null">center_lng = #{centerLng},</if>
            <if test="areaSize != null">area_size = #{areaSize},</if>
            <if test="status != null">status = #{status},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="remark != null">remark = #{remark},</if>
            update_time = NOW()
        </set>
        WHERE id = #{id}
    </update>
 
    <!-- 启用/禁用区域 -->
    <update id="updateStatus">
        UPDATE ard_dept_region
        SET status = #{status}, update_time = NOW()
        WHERE id = #{id}
    </update>
 
    <!-- 物理删除区域 -->
    <delete id="deleteArdDeptRegionById" parameterType="Long">
        DELETE FROM ard_dept_region WHERE id = #{id}
    </delete>
 
    <!-- 批量物理删除区域 -->
    <delete id="deleteArdDeptRegionByIds" parameterType="String">
        DELETE FROM ard_dept_region WHERE id IN
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>
 
</mapper>