wlzboy
7 小时以前 5f2ee03958a1a16dc27195c76ea7cffb422c95d1
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
<?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.ruoyi.system.mapper.VehicleInfoMapper">
    
    <resultMap type="com.ruoyi.system.domain.VehicleInfo" id="VehicleInfoResult">
        <id     property="vehicleId"      column="vehicle_id"      />
        <result property="carId"          column="car_id"          />
        <result property="deviceId"       column="device_id"       />
        <result property="vehicleNo"      column="vehicle_no"      />
        <result property="vehicleType"    column="vehicle_type"    />
        <result property="vehicleBrand"   column="vehicle_brand"   />
        <result property="vehicleModel"   column="vehicle_model"   />
        <result property="status"         column="status"          />
        <result property="platformCode"   column="platform_code"   />
        <result property="deptName"       column="dept_name"       />
        <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>
    
    <!-- 包含多分公司关联的完整结果映射(仅在需要时使用) -->
    <resultMap type="com.ruoyi.system.domain.VehicleInfo" id="VehicleInfoWithDeptsResult" extends="VehicleInfoResult">
        <!-- 多个分公司关联(立即加载,避免延迟加载导致的序列化问题) -->
        <collection property="deptIds" ofType="Long" 
                    select="selectVehicleDeptIds" 
                    column="vehicle_id"
                    fetchType="eager"/>
        <collection property="deptNames" ofType="String" 
                    select="selectVehicleDeptNames" 
                    column="vehicle_id"
                    fetchType="eager"/>
    </resultMap>
 
    <sql id="selectVehicleInfoVo">
        select v.vehicle_id, v.car_id, v.device_id, v.vehicle_no, v.vehicle_type, v.vehicle_brand, v.vehicle_model, v.status, v.platform_code, v.create_by, v.create_time, v.update_by, v.update_time, v.remark
        from tb_vehicle_info v
    </sql>
    
    <!-- 查询车辆关联的所有分公司ID -->
    <select id="selectVehicleDeptIds" resultType="Long">
        SELECT dept_id FROM tb_vehicle_dept WHERE vehicle_id = #{vehicle_id}
    </select>
    
    <!-- 查询车辆关联的所有分公司名称 -->
    <select id="selectVehicleDeptNames" resultType="String">
        SELECT d.dept_name 
        FROM tb_vehicle_dept vd
        LEFT JOIN sys_dept d ON vd.dept_id = d.dept_id
        WHERE vd.vehicle_id = #{vehicle_id}
    </select>
 
    <select id="selectVehicleInfoListWithDepts" parameterType="VehicleInfo" resultMap="VehicleInfoWithDeptsResult">
        select v.vehicle_id, v.car_id, v.device_id, v.vehicle_no, v.vehicle_type, v.vehicle_brand, 
               v.vehicle_model, v.status, v.platform_code, v.create_by, v.create_time, 
               v.update_by, v.update_time, v.remark
        from tb_vehicle_info v
        <where>  
            <if test="vehicleNo != null  and vehicleNo != ''"> and v.vehicle_no LIKE concat('%', #{vehicleNo}, '%')</if>
            <if test="deviceId != null  and deviceId != ''"> and v.device_id = #{deviceId}</if>
            <if test="vehicleType != null  and vehicleType != ''"> and v.vehicle_type = #{vehicleType}</if>
            <if test="vehicleBrand != null  and vehicleBrand != ''"> and v.vehicle_brand = #{vehicleBrand}</if>
            <if test="vehicleModel != null  and vehicleModel != ''"> and v.vehicle_model = #{vehicleModel}</if>
            <if test="status != null  and status != ''"> and v.status = #{status}</if>
            <if test="platformCode != null  and platformCode != ''"> and v.platform_code = #{platformCode}</if>
            <!-- 部门过滤:根据分公司ID查询车辆(通过关联表) -->
            <if test="deptId != null">
                and EXISTS (
                    SELECT 1 FROM tb_vehicle_dept vd2 
                    WHERE vd2.vehicle_id = v.vehicle_id 
                    AND vd2.dept_id = #{deptId}
                )
            </if>
            and v.status=0
        </where>
        group by v.vehicle_id, v.car_id, v.device_id, v.vehicle_no, v.vehicle_type, v.vehicle_brand,
                 v.vehicle_model, v.status, v.platform_code, v.create_by, v.create_time,
                 v.update_by, v.update_time, v.remark
        order by v.create_time desc
    </select>
    
    <select id="selectVehicleInfoList" parameterType="VehicleInfo" resultMap="VehicleInfoResult">
        select v.vehicle_id, v.car_id, v.device_id, v.vehicle_no, v.vehicle_type, v.vehicle_brand, 
               v.vehicle_model, v.status, v.platform_code, v.create_by, v.create_time, 
               v.update_by, v.update_time, v.remark,
               GROUP_CONCAT(DISTINCT d.dept_name ORDER BY d.dept_name SEPARATOR ',') as dept_name
        from tb_vehicle_info v
        left join tb_vehicle_dept vd on v.vehicle_id = vd.vehicle_id
        left join sys_dept d on vd.dept_id = d.dept_id
        <where>  
            <if test="vehicleNo != null  and vehicleNo != ''"> and v.vehicle_no LIKE concat('%', #{vehicleNo}, '%')</if>
            <if test="deviceId != null  and deviceId != ''"> and v.device_id = #{deviceId}</if>
            <if test="vehicleType != null  and vehicleType != ''"> and v.vehicle_type = #{vehicleType}</if>
            <if test="vehicleBrand != null  and vehicleBrand != ''"> and v.vehicle_brand = #{vehicleBrand}</if>
            <if test="vehicleModel != null  and vehicleModel != ''"> and v.vehicle_model = #{vehicleModel}</if>
            <if test="status != null  and status != ''"> and v.status = #{status}</if>
            <if test="platformCode != null  and platformCode != ''"> and v.platform_code = #{platformCode}</if>
            <!-- 部门过滤:根据分公司ID查询车辆(通过关联表) -->
            <if test="deptId != null">
                and EXISTS (
                    SELECT 1 FROM tb_vehicle_dept vd2 
                    WHERE vd2.vehicle_id = v.vehicle_id 
                    AND vd2.dept_id = #{deptId}
                )
            </if>
            <!-- 任务车辆选择必须过滤:只显示car_id不为空且已关联分公司的车辆 -->
<!--            and v.car_id is not null and v.car_id != ''-->
<!--            and EXISTS (SELECT 1 FROM tb_vehicle_dept vd WHERE vd.vehicle_id = v.vehicle_id)-->
            and v.status=0
 
        </where>
        group by v.vehicle_id, v.car_id, v.device_id, v.vehicle_no, v.vehicle_type, v.vehicle_brand,
                 v.vehicle_model, v.status, v.platform_code, v.create_by, v.create_time,
                 v.update_by, v.update_time, v.remark
    </select>
    
    <select id="selectVehicleInfoById" parameterType="Long" resultMap="VehicleInfoWithDeptsResult">
        <include refid="selectVehicleInfoVo"/>
        where v.vehicle_id = #{vehicleId}
    </select>
    
    <!-- 查询车辆信息(包含多分公司关联) -->
    <select id="selectVehicleInfoWithDeptsById" parameterType="Long" resultMap="VehicleInfoWithDeptsResult">
        <include refid="selectVehicleInfoVo"/>
        where v.vehicle_id = #{vehicleId}
    </select>
 
    <select id="selectVehicleInfoByPlateNumber" parameterType="String" resultMap="VehicleInfoResult">
        <include refid="selectVehicleInfoVo"/>
        where v.vehicle_no LIKE concat('%', #{plateNumber}, '%')
        limit 1
    </select>
 
    <select id="selectVehicleInfoByVehicleNo" parameterType="String" resultMap="VehicleInfoResult">
        <include refid="selectVehicleInfoVo"/>
        where v.vehicle_no LIKE concat('%', #{vehicleNo}, '%')
    </select>
    
    <!-- 根据旧系统车辆ID(CarID)查询车辆信息 -->
    <select id="selectVehicleInfoByCarId" parameterType="Integer" resultMap="VehicleInfoResult">
        <include refid="selectVehicleInfoVo"/>
        where v.car_id = #{carId}
    </select>
    
    <!-- 根据旧系统车辆ID查询车辆信息 -->
    <select id="selectVehicleInfoByCarID" resultType="java.util.HashMap">
        SELECT 
            vehicle_id,
            car_id,
            vehicle_no
        FROM tb_vehicle_info 
        WHERE car_id = #{carID}
    </select>
        
    <insert id="insertVehicleInfo" parameterType="VehicleInfo" useGeneratedKeys="true" keyProperty="vehicleId">
        insert into tb_vehicle_info
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="carId != null">car_id,</if>
            <if test="deviceId != null">device_id,</if>
            <if test="vehicleNo != null">vehicle_no,</if>
            <if test="vehicleType != null">vehicle_type,</if>
            <if test="vehicleBrand != null">vehicle_brand,</if>
            <if test="vehicleModel != null">vehicle_model,</if>
            <if test="status != null">status,</if>
            <if test="platformCode != null">platform_code,</if>
            <if test="createBy != null">create_by,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="remark != null">remark,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="carId != null">#{carId},</if>
            <if test="deviceId != null">#{deviceId},</if>
            <if test="vehicleNo != null">#{vehicleNo},</if>
            <if test="vehicleType != null">#{vehicleType},</if>
            <if test="vehicleBrand != null">#{vehicleBrand},</if>
            <if test="vehicleModel != null">#{vehicleModel},</if>
            <if test="status != null">#{status},</if>
            <if test="platformCode != null">#{platformCode},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="remark != null">#{remark},</if>
         </trim>
    </insert>
 
    <update id="updateVehicleInfo" parameterType="VehicleInfo">
        update tb_vehicle_info
        <trim prefix="SET" suffixOverrides=",">
            <if test="carId != null">car_id = #{carId},</if>
            <if test="deviceId != null">device_id = #{deviceId},</if>
            <if test="vehicleNo != null">vehicle_no = #{vehicleNo},</if>
            <if test="vehicleType != null">vehicle_type = #{vehicleType},</if>
            <if test="vehicleBrand != null">vehicle_brand = #{vehicleBrand},</if>
            <if test="vehicleModel != null">vehicle_model = #{vehicleModel},</if>
            <if test="status != null">status = #{status},</if>
            <if test="platformCode != null">platform_code = #{platformCode},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="remark != null">remark = #{remark},</if>
        </trim>
        where vehicle_id = #{vehicleId}
    </update>
 
    <delete id="deleteVehicleInfoById" parameterType="Long">
        delete from tb_vehicle_info where vehicle_id = #{vehicleId}
    </delete>
 
    <delete id="deleteVehicleInfoByIds" parameterType="String">
        delete from tb_vehicle_info where vehicle_id in 
        <foreach item="vehicleId" collection="array" open="(" separator="," close=")">
            #{vehicleId}
        </foreach>
    </delete>
    
    <!-- 绑定车辆到用户 -->
    <insert id="bindVehicleToUser">
        INSERT INTO sys_user_vehicle (user_id, vehicle_id, bind_time, bind_by, status, create_by, create_time)
        VALUES (#{userId}, #{vehicleId}, NOW(), #{bindBy}, '0', #{bindBy}, NOW())
    </insert>
    
    <!-- 解绑用户车辆 -->
    <update id="unbindVehicleFromUser">
        UPDATE sys_user_vehicle 
        SET status = '1', update_time = NOW()
        WHERE user_id = #{userId} AND vehicle_id = #{vehicleId} AND status = '0'
    </update>
    
    <!-- 解绑用户的所有车辆 -->
    <update id="unbindAllVehiclesFromUser">
        UPDATE sys_user_vehicle 
        SET status = '1', update_time = NOW()
        WHERE user_id = #{userId} AND status = '0'
    </update>
    
    <!-- 获取用户当前绑定的车辆 -->
    <select id="getUserBoundVehicle" resultMap="VehicleInfoResult">
        SELECT v.* 
        FROM tb_vehicle_info v
        INNER JOIN sys_user_vehicle uv ON v.vehicle_id = uv.vehicle_id
        WHERE uv.user_id = #{userId} AND uv.status = '0'
        ORDER BY uv.bind_time DESC
        LIMIT 1
    </select>
    
    <!-- 批量插入车辆-分公司关联 -->
    <insert id="batchInsertVehicleDept">
        INSERT INTO tb_vehicle_dept (vehicle_id, dept_id, order_class, create_by, create_time)
        VALUES
        <foreach collection="list" item="item" separator=",">
            (#{item.vehicleId}, #{item.deptId}, #{item.orderClass}, #{item.createBy}, NOW())
        </foreach>
        ON DUPLICATE KEY UPDATE
        order_class = VALUES(order_class),
        update_by = VALUES(create_by),
        update_time = NOW()
    </insert>
    
    <!-- 删除车辆的所有分公司关联 -->
    <delete id="deleteVehicleDeptByVehicleId">
        DELETE FROM tb_vehicle_dept WHERE vehicle_id = #{vehicleId}
    </delete>
</mapper>