<?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.CarPositionMapper">

    <resultMap type="com.ruoyi.system.domain.CarPosition" id="CarPositionMap">
        <result property="carId" column="car_id" jdbcType="INTEGER"/>
        <result property="position" column="position" jdbcType="NUMERIC"/>
        <result property="createTime" column="create_time" jdbcType="TIMESTAMP"/>
        <result property="siteId" column="site_id" jdbcType="INTEGER"/>
        <result property="status" column="status" jdbcType="INTEGER"/>
        <result property="id" column="id" jdbcType="VARCHAR"/>
    </resultMap>

    <!--查询单个-->
    <select id="queryById" resultMap="CarPositionMap">
        select
            car_id, position, create_time, site_id, status, id
        from car_position
        where car_id = #{carId}
    </select>

    <!--查询单个-->
    <select id="queryBysiteId" resultMap="CarPositionMap">
        select
            car_id, position, create_time, site_id, status, id
        from car_position
        where site_id = #{siteId}
        order by create_time desc
        limit 1
    </select>

    <!--查询多个-->
    <select id="queryBysiteIdTime" resultMap="CarPositionMap">
        select
            car_id, position, create_time, site_id, status, id
        from car_position
        where site_id = #{siteId} and create_time between #{startTime} and #{endTime}
    </select>


    <!--查询指定行数据-->
    <select id="queryAllByLimit" resultMap="CarPositionMap">
        select
            car_id, position, create_time, site_id, status, id
        from car_position
        <where>
            <if test="carId != null">
                and car_id = #{carId}
            </if>
            <if test="position != null">
                and position = #{position}
            </if>
            <if test="createTime != null">
                and create_time = #{createTime}
            </if>
            <if test="siteId != null">
                and site_id = #{siteId}
            </if>
            <if test="status != null">
                and status = #{status}
            </if>
            <if test="id != null and id != ''">
                and id = #{id}
            </if>
        </where>
        limit #{pageable.offset}, #{pageable.pageSize}
    </select>

    <!--统计总行数-->
    <select id="count" resultType="java.lang.Long">
        select count(1)
        from car_position
        <where>
            <if test="carId != null">
                and car_id = #{carId}
            </if>
            <if test="position != null">
                and position = #{position}
            </if>
            <if test="createTime != null">
                and create_time = #{createTime}
            </if>
            <if test="siteId != null">
                and site_id = #{siteId}
            </if>
            <if test="status != null">
                and status = #{status}
            </if>
            <if test="id != null and id != ''">
                and id = #{id}
            </if>
        </where>
    </select>

    <!--新增所有列-->
    <insert id="insert" keyProperty="carId" useGeneratedKeys="true">
        insert into car_position(position, create_time, site_id, status, id)
        values (#{position}, #{createTime}, #{siteId}, #{status}, #{id})
    </insert>

    <insert id="insertBatch" keyProperty="carId" useGeneratedKeys="true">
        insert into car_position(position, create_time, site_id, status, id)
        values
        <foreach collection="entities" item="entity" separator=",">
        (#{entity.position}, #{entity.createTime}, #{entity.siteId}, #{entity.status}, #{entity.id})
        </foreach>
    </insert>

    <insert id="insertOrUpdateBatch" keyProperty="carId" useGeneratedKeys="true">
        insert into car_position(position, create_time, site_id, status,id)
        values
        <foreach collection="entities" item="entity" separator=",">
            (#{entity.position}, #{entity.createTime}, #{entity.siteId}, #{entity.status},#{entity.id})
        </foreach>
        on duplicate key update
position = values(position),
create_time = values(create_time),
site_id = values(site_id),
status = values(status)
    </insert>

    <!--通过主键修改数据-->
    <update id="update">
        update car_position
        <set>
            <if test="position != null">
                position = #{position},
            </if>
            <if test="createTime != null">
                create_time = #{createTime},
            </if>
            <if test="siteId != null">
                site_id = #{siteId},
            </if>
            <if test="status != null">
                status = #{status},
            </if>
            <if test="id != null">
                id = #{id},
            </if>
        </set>
        where car_id = #{carId}
    </update>

    <!--通过主键删除-->
    <delete id="deleteById">
        delete from car_position where car_id = #{carId}
    </delete>

</mapper>

