<?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.aucma.dms.mapper.DmsBaseStationProjectMapper">
    
    <resultMap type="DmsBaseStationProject" id="DmsBaseStationProjectResult">
        <result property="maintStationId"    column="maint_station_id"    />
        <result property="maintProjectId"    column="maint_project_id"    />
        <result property="maintProjectName"  column="maint_project_name"  />
        <result property="maintStationName" column="maint_station_name" />
        <result property="maintStationCode" column="maint_station_code" />


    </resultMap>

    <sql id="selectDmsBaseStationProjectVo">
        select dbsp.maint_station_id,
               dbsp.maint_project_id,
               dbmp.maint_project_name,
               dbms.maint_station_name,
               dbms.maint_station_code
        from dms_base_station_project dbsp
        left join dms_base_maint_project dbmp on dbsp.maint_project_id = dbmp.maint_project_id
        left join dms_base_maint_station dbms on dbsp.maint_station_id = dbms.maint_station_id

    </sql>

    <select id="selectDmsBaseStationProjectList" parameterType="DmsBaseStationProject" resultMap="DmsBaseStationProjectResult">
        <include refid="selectDmsBaseStationProjectVo"/>
        <where>  
            <if test="maintStationId != null "> and dbsp.maint_station_id = #{maintStationId}</if>
            <if test="maintProjectId != null "> and dbsp.maint_project_id = #{maintProjectId}</if>
             <if test="maintProjectName != null and maintProjectName != ''"> and dbmp.maint_project_name like concat('%', #{maintProjectName}, '%')</if>
              <if test="maintStationName != null and maintStationName != ''"> and dbms.maint_station_name like concat('%', #{maintStationName}, '%')</if>
               <if test="maintStationCode != null and maintStationCode != ''"> and dbms.maint_station_code like concat('%', #{maintStationCode}, '%')</if>
        </where>
    </select>
    
    <select id="selectDmsBaseStationProjectByMaintStationId" parameterType="Long" resultMap="DmsBaseStationProjectResult">
        <include refid="selectDmsBaseStationProjectVo"/>
        where dbsp.maint_station_id = #{maintStationId}
    </select>

    <select id="selectDmsBaseStationProjectBymaintProjectId" parameterType="Long" resultMap="DmsBaseStationProjectResult">
        <include refid="selectDmsBaseStationProjectVo"/>
        where dbsp.maint_project_id = #{maintProjectId}
    </select>


    <insert id="insertDmsBaseStationProject" parameterType="DmsBaseStationProject">
        insert into dms_base_station_project
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="maintStationId != null">maint_station_id,</if>
            <if test="maintProjectId != null">maint_project_id,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="maintStationId != null">#{maintStationId},</if>
            <if test="maintProjectId != null">#{maintProjectId},</if>
         </trim>
    </insert>

    <update id="updateDmsBaseStationProject" parameterType="DmsBaseStationProject">
        update dms_base_station_project
        <trim prefix="SET" suffixOverrides=",">
            <if test="maintProjectId != null">maint_project_id = #{maintProjectId},</if>
            <if test="maintProjectName != null and maintProjectName != ''">maint_project_name = #{maintProjectName},</if>
        </trim>
        where maint_station_id = #{maintStationId}
    </update>

    <delete id="deleteDmsBaseStationProjectByMaintStationId" parameterType="Long">
        delete from dms_base_station_project where maint_station_id = #{maintStationId}
    </delete>

    <delete id="deleteDmsBaseStationProjectByMaintStationIds" parameterType="String">
        delete from dms_base_station_project where maint_station_id in
        <foreach item="maintStationId" collection="array" open="(" separator="," close=")">
            #{maintStationId}
        </foreach>
    </delete>



    <update id="batchUpdateDmsBaseStationProject" parameterType="java.util.List">
        <foreach collection="list" item="item" index="index" open="" close="" separator=",">
        update  dms_base_station_project
            <set>
                maint_project_id = #{item.maintProjectId}
            </set>
            where maint_station_id = #{item.maintStationId}
        </foreach>
    </update>

    <insert id="batchInsertDmsBaseStationProject" parameterType="java.util.List">
            insert into dms_base_station_project(maint_station_id,maint_project_id)
            values
            <foreach collection="list" item="item" index="index" open="" close="" separator=",">
            (#{item.maintStationId},#{item.maintProjectId})
            </foreach>
    </insert>

</mapper>