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

    <resultMap type="com.ruoyi.system.domain.BerthingPoint" id="BerthingPointMap">
        <result property="berthingId" column="berthing_id" jdbcType="INTEGER"/>
        <result property="id" column="id" jdbcType="VARCHAR"/>
        <result property="siteId" column="site_id" jdbcType="INTEGER"/>
        <result property="berthingName" column="berthing_name" jdbcType="VARCHAR"/>
        <result property="positions" column="positions" jdbcType="VARCHAR"/>
        <result property="createTime" column="create_time" jdbcType="TIMESTAMP"/>
        <result property="status" column="status" jdbcType="INTEGER"/>
        <result property="isDel" column="is_del" jdbcType="INTEGER"/>
        <result property="factors" column="factors" jdbcType="VARCHAR"/>
        <result property="siteName" column="site_name" jdbcType="VARCHAR"/>
    </resultMap>

    <!--查询单个-->
    <select id="queryById" resultMap="BerthingPointMap">
        select
            berthing_id, id, site_id, berthing_name, positions, create_time, status, is_del, factors
        from berthing_point
        where berthing_id = #{berthingId}
    </select>

    <!--查询单个-->
    <select id="queryBySiteId" resultMap="BerthingPointMap">
        select
            berthing_id, id, site_id, berthing_name, positions, create_time, status, is_del, factors
        from berthing_point
        where site_id = #{siteId} and is_del = 0 and status = 1
        order by create_time desc
        limit 1
    </select>


    <!--设置表状态-->
    <update id="setBerthingStatus">
        update berthing_point
        <set>
            status = 0
        </set>
            where site_id = #{siteId}
    </update>

    <!--查询指定行数据-->
    <select id="queryAllByLimit" resultMap="BerthingPointMap">
        select
            a.berthing_id, a.id, a.site_id, a.berthing_name, a.positions, a.create_time, a.status, a.is_del, a.factors, b.site_name
        from berthing_point as a
        join site_info as b on a.site_id = b.site_id
        <where>
            <if test="berthingPointQuery.berthingId != null">
                and a.berthing_id = #{berthingPointQuery.berthingId}
            </if>
            <if test="berthingPointQuery.siteId != null">
                and a.site_id= #{berthingPointQuery.siteId}
            </if>
        </where>
        and a.is_del = 0
        ORDER BY create_time DESC
    </select>

    <!--统计总行数-->
    <select id="count" resultType="java.lang.Long">
        select count(1)
        from berthing_point
        <where>
            <if test="berthingId != null">
                and berthing_id = #{berthingId}
            </if>
            <if test="id != null and id != ''">
                and id = #{id}
            </if>
            <if test="siteId != null">
                and site_id = #{siteId}
            </if>
            <if test="berthingName != null and berthingName != ''">
                and berthing_name = #{berthingName}
            </if>
            <if test="positions != null and positions != ''">
                and positions = #{positions}
            </if>
            <if test="createTime != null">
                and create_time = #{createTime}
            </if>
            <if test="status != null">
                and status = #{status}
            </if>
            <if test="isDel != null">
                and is_del = #{isDel}
            </if>
            <if test="factors != null and factors != ''">
                and factors = #{factors}
            </if>
        </where>
    </select>

    <!--新增所有列-->
    <insert id="insert" keyProperty="berthingId" useGeneratedKeys="true">
        insert into berthing_point(id, site_id, berthing_name, positions, create_time, status, is_del, factors)
        values (#{id}, #{siteId}, #{berthingName}, #{positions}, #{createTime}, #{status}, #{isDel}, #{factors})
    </insert>

    <insert id="insertBatch" keyProperty="berthingId" useGeneratedKeys="true">
        insert into berthing_point(id, site_id, berthing_name, positions, create_time, status, is_del, factors)
        values
        <foreach collection="entities" item="entity" separator=",">
        (#{entity.id}, #{entity.siteId}, #{entity.berthingName}, #{entity.positions}, #{entity.createTime}, #{entity.status}, #{entity.isDel}, #{entity.factors})
        </foreach>
    </insert>

    <insert id="insertOrUpdateBatch" keyProperty="berthingId" useGeneratedKeys="true">
        insert into berthing_point(id, site_id, berthing_name, positions, create_time, status, is_del, factors)
        values
        <foreach collection="entities" item="entity" separator=",">
            (#{entity.id}, #{entity.siteId}, #{entity.berthingName}, #{entity.positions}, #{entity.createTime}, #{entity.status}, #{entity.isDel}, #{entity.factors})
        </foreach>
        on duplicate key update
id = values(id),
site_id = values(site_id),
berthing_name = values(berthing_name),
positions = values(positions),
create_time = values(create_time),
status = values(status),
is_del = values(is_del),
factors = values(factors)
    </insert>

    <!--通过主键修改数据-->
    <update id="update">
        update berthing_point
        <set>
            <if test="id != null and id != ''">
                id = #{id},
            </if>
            <if test="siteId != null">
                site_id = #{siteId},
            </if>
            <if test="berthingName != null and berthingName != ''">
                berthing_name = #{berthingName},
            </if>
            <if test="positions != null and positions != ''">
                positions = #{positions},
            </if>
            <if test="createTime != null">
                create_time = #{createTime},
            </if>
            <if test="status != null">
                status = #{status},
            </if>
            <if test="isDel != null">
                is_del = #{isDel},
            </if>
            <if test="factors != null and factors != ''">
                factors = #{factors},
            </if>
        </set>
        where berthing_id = #{berthingId}
    </update>

    <!--通过主键删除-->
    <update id="deleteById">
        update berthing_point set is_del=1 where berthing_id = #{berthingId}
    </update>

</mapper>

