<?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.zc.business.mapper.DcRoadSectionMapper">
    
    <resultMap type="DcRoadSection" id="DcRoadSectionResult">
        <result property="id"    column="id"    />
        <result property="deptId"    column="dept_id"    />
        <result property="roadId"    column="road_id"    />
        <result property="startStakeMark"    column="start_stake_mark"    />
        <result property="endStakeMark"    column="end_stake_mark"    />
        <result property="sectionName"    column="section_name"    />
        <result property="roadCode"    column="road_code"    />
        <result property="mileage"    column="mileage"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateTime"    column="update_time"    />
        <result property="deptName"    column="dept_name"    />
        <result property="roadName"    column="road_name"    />
    </resultMap>

    <sql id="selectDcRoadSectionVo">
        select section.id, section.dept_id, section.road_id,
               dept.dept_name,road.road_name,
               section.start_stake_mark, section.end_stake_mark,
               section.section_name, section.road_code, section.mileage,
               section.create_time, section.update_time from dc_road_section as section
        left join sys_dept as dept on section.dept_id=dept.dept_id
        left join dc_road as road on  section.road_id=road.id
    </sql>

    <select id="selectDcRoadSectionList" parameterType="DcRoadSection" resultMap="DcRoadSectionResult">
        <include refid="selectDcRoadSectionVo"/>
        <where>
            <if test="id != null "> and section.id = #{id}</if>
            <if test="deptId != null "> and section.dept_id = #{deptId}</if>
            <if test="roadId != null "> and section.road_id = #{roadId}</if>
            <if test="startStakeMark != null  and startStakeMark != ''
             and endStakeMark != null  and endStakeMark != ''">
                and   CAST(SUBSTRING(SUBSTRING_INDEX(start_stake_mark,'+',1),2)AS UNSIGNED)*1000
                +CAST(SUBSTRING_INDEX(start_stake_mark,  '+', -1) AS UNSIGNED)&gt;#{startStakeMark}
                and
                CAST(SUBSTRING(SUBSTRING_INDEX(end_stake_mark,'+',1),2)AS UNSIGNED)*1000
                +CAST(SUBSTRING_INDEX(end_stake_mark,  '+', -1) AS UNSIGNED)&lt;#{endStakeMark}
             </if>
            <if test="startStakeMark != null  and startStakeMark != ''
             and endStakeMark == null ">
                and   CAST(SUBSTRING(SUBSTRING_INDEX(start_stake_mark,'+',1),2)AS UNSIGNED)*1000
                +CAST(SUBSTRING_INDEX(start_stake_mark,  '+', -1) AS UNSIGNED)&gt;#{startStakeMark}
            </if>
            <if test=" endStakeMark != null  and endStakeMark != ''
             and startStakeMark == null ">
                and   CAST(SUBSTRING(SUBSTRING_INDEX(end_stake_mark,'+',1),2)AS UNSIGNED)*1000
                +CAST(SUBSTRING_INDEX(end_stake_mark,  '+', -1) AS UNSIGNED)&lt;#{endStakeMark}
            </if>
            <if test="sectionName != null  and sectionName != ''"> and section.section_name like concat('%', #{sectionName}, '%')</if>
            <if test="roadCode != null  and roadCode != ''"> and section.road_code = #{roadCode}</if>
        </where>
    </select>
    <select id="selectName" resultType="com.zc.business.domain.DcRoadSection">
        select id,section_name from dc_road_section
    </select>
    <select id="selectDcRoadSectionById" parameterType="Long" resultMap="DcRoadSectionResult">
        <include refid="selectDcRoadSectionVo"/>
        where section.id = #{id}
    </select>
    <select id="selectRoadList" resultType="java.util.HashMap">
        select id,road_name roadName from dc_road
    </select>
    <select id="selectMileage" resultType="java.util.HashMap">
        select id ,CAST(SUBSTRING(SUBSTRING_INDEX(start_stake_mark,'+',1),2)AS UNSIGNED)*1000
            +CAST(SUBSTRING_INDEX(start_stake_mark,  '+', -1) AS UNSIGNED) startStakeMark,
               CAST(SUBSTRING(SUBSTRING_INDEX(end_stake_mark,'+',1),2)AS UNSIGNED)*1000
                   +CAST(SUBSTRING_INDEX(end_stake_mark,  '+', -1) AS UNSIGNED) endStakeMark
        from dc_road_section
    </select>
    <select id="selectStakeMarkMileage" resultType="java.util.HashMap">
        select   stake_mark startStakeMark,
        CAST(SUBSTRING(SUBSTRING_INDEX(stake_mark,'+',1),2)AS UNSIGNED)*1000
        +CAST(SUBSTRING_INDEX(stake_mark,  '+', -1) AS UNSIGNED) stakeMark  from  dc_stake_mark where stake_mark!='222'

    </select>

    <update id="updateStakeMarkRecord">
        update dc_stake_mark set section_id=#{sectionId} where stake_mark=#{stakeMark}
    </update>

    <insert id="insertDcRoadSection" parameterType="DcRoadSection">
        insert into dc_road_section
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="id != null">id,</if>
            <if test="deptId != null">dept_id,</if>
            <if test="roadId != null">road_id,</if>
            <if test="startStakeMark != null">start_stake_mark,</if>
            <if test="endStakeMark != null">end_stake_mark,</if>
            <if test="sectionName != null and sectionName != ''">section_name,</if>
            <if test="roadCode != null">road_code,</if>
            <if test="mileage != null">mileage,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateTime != null">update_time,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="id != null">#{id},</if>
            <if test="deptId != null">#{deptId},</if>
            <if test="roadId != null">#{roadId},</if>
            <if test="startStakeMark != null">#{startStakeMark},</if>
            <if test="endStakeMark != null">#{endStakeMark},</if>
            <if test="sectionName != null and sectionName != ''">#{sectionName},</if>
            <if test="roadCode != null">#{roadCode},</if>
            <if test="mileage != null">#{mileage},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateTime != null">#{updateTime},</if>
         </trim>
    </insert>

    <update id="updateDcRoadSection" parameterType="DcRoadSection">
        update dc_road_section
        <trim prefix="SET" suffixOverrides=",">
            <if test="deptId != null">dept_id = #{deptId},</if>
            <if test="roadId != null">road_id = #{roadId},</if>
            <if test="startStakeMark != null">start_stake_mark = #{startStakeMark},</if>
            <if test="endStakeMark != null">end_stake_mark = #{endStakeMark},</if>
            <if test="sectionName != null and sectionName != ''">section_name = #{sectionName},</if>
            <if test="roadCode != null">road_code = #{roadCode},</if>
            <if test="mileage != null">mileage = #{mileage},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteDcRoadSectionById" parameterType="Long">
        delete from dc_road_section where id = #{id}
    </delete>

    <delete id="deleteDcRoadSectionByIds" parameterType="String">
        delete from dc_road_section where id in 
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>
</mapper>