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

    <resultMap type="SysReport" id="SysReportResult">
        <result property="id"    column="id"    />
        <result property="planId"    column="plan_id"    />
        <result property="lane"    column="lane"    />
        <result property="dept"    column="dept"    />
        <result property="startTime"    column="start_time"    />
        <result property="endTime"    column="end_time"    />
        <result property="manageUser"    column="manage_user"    />
        <result property="managePhone"    column="manage_phone"    />
        <result property="saveUser"    column="save_user"    />
        <result property="savePhone"    column="save_phone"    />
        <result property="createUser"    column="create_user"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateUser"    column="update_user"    />
        <result property="updateTime"    column="update_time"    />
        <result property="isMerge"    column="is_merge"    />
        <result property="section"    column="section"    />
        <result property="direction"    column="direction"    />
        <result property="clashId"    column="clash_id"    />
        <result property="startKm"    column="start_km"    />
        <result property="startHectometer"    column="start_hectometer"    />
        <result property="endKm"    column="end_km"    />
        <result property="endHectometer"    column="end_hectometer"    />
        <result property="contentOne"    column="content_one"    />
        <result property="contentTwo"    column="content_two"    />
        <result property="contentDes"    column="content_des"    />
        <result property="auditDept"    column="audit_dept"    />
        <result property="status"    column="status"    />
        <result property="cids"    column="cids"    />
        <result property="ext1"    column="ext1"    />
    </resultMap>

    <sql id="selectSysReportVo">
        select a.id, a.plan_id, a.lane,a.dept, a.start_time, a.end_time, a.manage_user, a.manage_phone, a.save_user, a.save_phone,
               a.create_user, a.create_time, a.update_user, a.update_time, a.is_merge,a.status,a.audit_dept,a.section,a.direction,a.start_km,a.start_hectometer,
               a.end_km,a.end_hectometer,a.content_one,a.content_two,a.content_des,a.ext1,a.clash_id
        from
        sys_report a
    </sql>

    <select id="selectSysReportList" parameterType="SysReport" resultMap="SysReportResult">
        <include refid="selectSysReportVo"/>
        <where>
            <if test="planId != null "> and plan_id = #{planId}</if>
            <if test="lane != null  and lane != ''"> and lane = #{lane}</if>
            <if test="params.beginTime != null "> and date_format(start_time,'%y%m%d')  &gt;= date_format(#{params.beginTime},'%y%m%d')</if>
            <if test="params.endTime != null "> and date_format(end_time,'%y%m%d')   &lt;= date_format(#{params.endTime},'%y%m%d') </if>
            <if test="manageUser != null  and manageUser != ''"> and manage_user = #{manageUser}</if>
            <if test="managePhone != null  and managePhone != ''"> and manage_phone = #{managePhone}</if>
            <if test="saveUser != null  and saveUser != ''"> and save_user = #{saveUser}</if>
            <if test="savePhone != null  and savePhone != ''"> and save_phone = #{savePhone}</if>
            <if test="createUser != null  and createUser != ''"> and create_user = #{createUser}</if>
            <if test="updateUser != null  and updateUser != ''"> and update_user = #{updateUser}</if>
            <if test="isMerge != null  and isMerge != ''"> and is_merge = #{isMerge}</if>
            <if test="status != null  and status != ''"> and status = #{status}</if>
            <if test="clashId != null  and clashId != ''"> and clash_id like '%${clashId}%'</if>
            <if test="params.isMerge != null  and params.isMerge != ''"> and is_merge != #{params.isMerge}</if>
        </where>
    </select>

    <select id="selectSysReportById" parameterType="Long" resultMap="SysReportResult">
        <include refid="selectSysReportVo"/>
        where id = #{id}
    </select>

    <select id="selectSysReportByIds" parameterType="String" resultMap="SysReportResult">
        <include refid="selectSysReportVo"/>
        where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </select>

    <insert id="insertSysReport" parameterType="SysReport" useGeneratedKeys="true"  keyProperty="id" keyColumn="id">
        insert into sys_report
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="planId != null">plan_id,</if>
            <if test="lane != null">lane,</if>
            <if test="startTime != null">start_time,</if>
            <if test="endTime != null">end_time,</if>
            <if test="manageUser != null">manage_user,</if>
            <if test="managePhone != null">manage_phone,</if>
            <if test="saveUser != null">save_user,</if>
            <if test="savePhone != null">save_phone,</if>
            <if test="createUser != null">create_user,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateUser != null">update_user,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="isMerge != null">is_merge,</if>
            <if test="status != null">status,</if>
            <if test="auditDept != null">audit_dept,</if>
            <if test="dept != null">dept,</if>
            <if test="section != null">section,</if>
            <if test="direction != null">direction,</if>
            <if test="startKm != null">start_km,</if>
            <if test="startHectometer != null">start_hectometer,</if>
            <if test="endKm != null">end_km,</if>
            <if test="endHectometer != null">end_hectometer,</if>
            <if test="contentOne != null">content_one,</if>
            <if test="contentTwo != null">content_two,</if>
            <if test="contentDes != null">content_des,</if>
            <if test="ext1 != null">ext1,</if>
            <if test="clashId != null">clash_id,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="planId != null">#{planId},</if>
            <if test="lane != null">#{lane},</if>
            <if test="startTime != null">#{startTime},</if>
            <if test="endTime != null">#{endTime},</if>
            <if test="manageUser != null">#{manageUser},</if>
            <if test="managePhone != null">#{managePhone},</if>
            <if test="saveUser != null">#{saveUser},</if>
            <if test="savePhone != null">#{savePhone},</if>
            <if test="createUser != null">#{createUser},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateUser != null">#{updateUser},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="isMerge != null">#{isMerge},</if>
            <if test="status != null">#{status},</if>
            <if test="auditDept != null">#{auditDept},</if>
            <if test="dept != null">#{dept},</if>
            <if test="section != null">#{section},</if>
            <if test="direction != null">#{direction},</if>
            <if test="startKm != null">#{startKm},</if>
            <if test="startHectometer != null">#{startHectometer},</if>
            <if test="endKm != null">#{endKm},</if>
            <if test="endHectometer != null">#{endHectometer},</if>
            <if test="contentOne != null">#{contentOne},</if>
            <if test="contentTwo != null">#{contentTwo},</if>
            <if test="contentDes != null">#{contentDes},</if>
            <if test="ext1 != null">#{ext1},</if>
            <if test="clashId != null">#{clashId},</if>
         </trim>
    </insert>

    <update id="updateSysReport" parameterType="SysReport">
        update sys_report
        <trim prefix="SET" suffixOverrides=",">
            <if test="planId != null">plan_id = #{planId},</if>
            <if test="lane != null">lane = #{lane},</if>
            <if test="startTime != null">start_time = #{startTime},</if>
            <if test="endTime != null">end_time = #{endTime},</if>
            <if test="manageUser != null">manage_user = #{manageUser},</if>
            <if test="managePhone != null">manage_phone = #{managePhone},</if>
            <if test="saveUser != null">save_user = #{saveUser},</if>
            <if test="savePhone != null">save_phone = #{savePhone},</if>
            <if test="createUser != null">create_user = #{createUser},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateUser != null">update_user = #{updateUser},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="isMerge != null">is_merge = #{isMerge},</if>
            <if test="status != null">status = #{status},</if>
            <if test="auditDept != null">audit_dept = #{auditDept},</if>
            <if test="section != null">section = #{section},</if>
            <if test="direction != null">direction = #{direction},</if>
            <if test="startKm != null">start_km = #{startKm},</if>
            <if test="startHectometer != null">start_hectometer = #{startHectometer},</if>
            <if test="endKm != null">end_km = #{endKm},</if>
            <if test="endHectometer != null">end_hectometer = #{endHectometer},</if>
            <if test="contentOne != null">content_one = #{contentOne},</if>
            <if test="contentTwo != null">content_two = #{contentTwo},</if>
            <if test="contentDes != null">content_des = #{contentDes},</if>
            <if test="dept != null">dept = #{dept},</if>
            <if test="clashId != null">clash_id = #{clashId},</if>
            <if test="ext1 != null">ext1 = #{ext1},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteSysReportById" parameterType="Long">
        delete from sys_report where id = #{id}
    </delete>

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

    <select id="collisionDetection" parameterType="String" resultMap="SysReportResult">

    <![CDATA[
        select a.id,(select GROUP_CONCAT(b.id SEPARATOR ',') from sys_report b where b.id in
           ]]>
        <foreach item="id" collection="array" open="(" separator="," close=")">
                #{id}
        </foreach>
        <![CDATA[
         and a.direction = b.direction and a.section = b.section and a.id <> b.id and
        ( (UNIX_TIMESTAMP(a.start_time)>=UNIX_TIMESTAMP(b.start_time) and  UNIX_TIMESTAMP(b.end_time)-UNIX_TIMESTAMP(a.start_time)> 3600 ) or
        (UNIX_TIMESTAMP(b.end_time)-UNIX_TIMESTAMP(b.start_time)>3600 and  UNIX_TIMESTAMP(a.end_time)>=UNIX_TIMESTAMP(b.end_time) and  UNIX_TIMESTAMP(b.start_time)>=UNIX_TIMESTAMP(a.start_time)  ) or
        (UNIX_TIMESTAMP(b.end_time)>=UNIX_TIMESTAMP(a.end_time) and  UNIX_TIMESTAMP(a.end_time)-UNIX_TIMESTAMP(b.start_time)> 3600 ) OR
        (UNIX_TIMESTAMP(b.start_time)<=UNIX_TIMESTAMP(a.start_time) and  UNIX_TIMESTAMP(a.end_time)<=UNIX_TIMESTAMP(b.end_time) and UNIX_TIMESTAMP(a.end_time)-UNIX_TIMESTAMP(a.start_time)> 3600   ) )
        and	NOT
        (   ( (CONVERT(b.end_km, UNSIGNED)+CONVERT(b.end_hectometer, UNSIGNED)/1000) < (CONVERT(a.start_km, UNSIGNED)+CONVERT(a.start_hectometer, UNSIGNED)/1000) and (CONVERT(a.start_km, UNSIGNED)+CONVERT(a.start_hectometer, UNSIGNED)/1000) - (CONVERT(b.end_km, UNSIGNED)+CONVERT(b.end_hectometer, UNSIGNED)/1000)>5  ) or
        ( (CONVERT(b.start_km, UNSIGNED)+CONVERT(b.start_hectometer, UNSIGNED)/1000) > (CONVERT(a.end_km, UNSIGNED)+CONVERT(a.end_hectometer, UNSIGNED)/1000) and (CONVERT(b.start_km, UNSIGNED)+CONVERT(b.start_hectometer, UNSIGNED)/1000) - (CONVERT(a.end_km, UNSIGNED)+CONVERT(a.end_hectometer, UNSIGNED)/1000)>5 )  )

        ) as cids from sys_report a where a.id in
]]>
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>


    </select>

    <select id="mergeQuery" parameterType="String" resultMap="SysReportResult">
        <![CDATA[
        SELECT a.id,(SELECT GROUP_CONCAT(b.id SEPARATOR ',') FROM sys_report b WHERE b.id IN
           ]]>
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
        <![CDATA[
         AND a.id<> b.id and a.section = b.section AND a.direction=b.direction AND (ABS(UNIX_TIMESTAMP(a.end_time)-UNIX_TIMESTAMP(b.end_time))<=3600) AND NOT ((locate('1',b.lane)=0 AND locate('2',b.lane)=0 AND locate('3',a.lane)=0 AND locate('4',a.lane)=0) OR (locate('1',a.lane)=0 AND locate('2',a.lane)=0 AND locate('3',b.lane)=0 AND locate('4',b.lane)=0)) AND ( (
             ABS(CONVERT ( b.end_km, UNSIGNED )+ CONVERT ( b.end_hectometer, UNSIGNED )/ 1000-CONVERT ( a.start_km, UNSIGNED )+ CONVERT ( a.start_hectometer, UNSIGNED )/ 1000 )
				)<= 2.5  and (
				ABS(CONVERT ( a.end_km, UNSIGNED )+ CONVERT ( a.end_hectometer, UNSIGNED )/ 1000-CONVERT ( b.start_km, UNSIGNED )+ CONVERT ( b.start_hectometer, UNSIGNED )/ 1000 )
				)<= 2.5 AND (((CONVERT (b.end_km,UNSIGNED)+CONVERT (b.end_hectometer,UNSIGNED)/1000)>=(CONVERT (a.end_km,UNSIGNED)+CONVERT (a.end_hectometer,UNSIGNED)/1000) AND (CONVERT (a.start_km,UNSIGNED)+CONVERT (a.start_hectometer,UNSIGNED)/1000)>=(CONVERT (b.start_km,UNSIGNED)+CONVERT (b.start_hectometer,UNSIGNED)/1000)) OR ((CONVERT (b.end_km,UNSIGNED)+CONVERT (b.end_hectometer,UNSIGNED)/1000)<=(CONVERT (a.end_km,UNSIGNED)+CONVERT (a.end_hectometer,UNSIGNED)/1000) AND (CONVERT (a.start_km,UNSIGNED)+CONVERT (a.start_hectometer,UNSIGNED)/1000)<=(CONVERT (b.start_km,UNSIGNED)+CONVERT (b.start_hectometer,UNSIGNED)/1000))))) AS cids FROM sys_report a WHERE a.id IN   ]]>
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>

    </select>

<!--    <select id="selectSysReportById" parameterType="Long" resultMap="SysReportResult">-->
<!--        SELECT-->
<!--        *-->
<!--        FROM-->
<!--        v_report_plan-->
<!--        WHERE-->
<!--        `status` = 2-->
<!--        AND section = #{section}-->
<!--        AND direction = #{direction}-->
<!--        AND ( start_km * 1000+start_hectometer > #{endKm}*1000+#{endHectometer} OR end_km * 1000+end_hectometer &lt; #{startKm}*1000+#{startHectometer})-->
<!--        AND ( UNIX_TIMESTAMP( start_time ) &lt; end_time - 3600 OR UNIX_TIMESTAMP( end_time ) &lt; start_time + 3600 )-->
<!--    </select>-->

</mapper>
