wlzboy
2025-11-10 1bc97562ccf38d0b6235f2efdba35eb68f1f208b
ruoyi-system/src/main/resources/mapper/system/VehicleInfoMapper.xml
@@ -21,6 +21,13 @@
        <result property="updateBy"       column="update_by"       />
        <result property="updateTime"     column="update_time"     />
        <result property="remark"         column="remark"          />
        <!-- 多个分公司关联 -->
        <collection property="deptIds" ofType="Long"
                    select="selectVehicleDeptIds"
                    column="vehicle_id"/>
        <collection property="deptNames" ofType="String"
                    select="selectVehicleDeptNames"
                    column="vehicle_id"/>
    </resultMap>
    <sql id="selectVehicleInfoVo">
@@ -28,6 +35,19 @@
        from tb_vehicle_info v
        left join sys_dept d on v.dept_id = d.dept_id
    </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="selectVehicleInfoList" parameterType="VehicleInfo" resultMap="VehicleInfoResult">
        <include refid="selectVehicleInfoVo"/>
@@ -39,33 +59,19 @@
            <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>
<<<<<<< HEAD
            <if test="deptId != null"> and v.dept_id = #{deptId}</if>
=======
            <!-- 部门过滤:自动查找传入部门所属的分公司(parent_id=100) -->
            <!-- 部门过滤:根据分公司ID查询车辆(通过关联表) -->
            <if test="deptId != null">
                and v.dept_id = (
                    <!-- 如果传入的就是分公司(parent_id=100),直接返回 -->
                    select case
                        when exists(select 1 from sys_dept where dept_id = #{deptId} and parent_id = 100) then #{deptId}
                        else (
                            <!-- 否则从 ancestors 中查找分公司ID -->
                            select d.dept_id
                            from sys_dept d
                            where d.parent_id = 100
                            and FIND_IN_SET(d.dept_id, (
                                select ancestors from sys_dept where dept_id = #{deptId}
                            ))
                            limit 1
                        )
                    end
                and EXISTS (
                    SELECT 1 FROM tb_vehicle_dept vd
                    WHERE vd.vehicle_id = v.vehicle_id
                    AND vd.dept_id = #{deptId}
                )
            </if>
            <!-- 任务车辆选择必须过滤:只显示car_id和dept_id都不为空的车辆 -->
            and v.car_id is not null and v.car_id != ''
            and v.dept_id is not null
            and v.status=0
>>>>>>> feature-task
        </where>
    </select>
    
@@ -77,14 +83,11 @@
    <select id="selectVehicleInfoByPlateNumber" parameterType="String" resultMap="VehicleInfoResult">
        <include refid="selectVehicleInfoVo"/>
        where v.vehicle_no = #{plateNumber}
<<<<<<< HEAD
    </select>
    <select id="selectVehicleInfoByVehicleNo" parameterType="String" resultMap="VehicleInfoResult">
        <include refid="selectVehicleInfoVo"/>
        where v.vehicle_no = #{vehicleNo}
=======
>>>>>>> feature-task
    </select>
        
    <insert id="insertVehicleInfo" parameterType="VehicleInfo" useGeneratedKeys="true" keyProperty="vehicleId">
@@ -182,4 +185,22 @@
        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>