<?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.tofly.gpsboot.mapper.trouble.ToubleclearMapper">

    <resultMap id="toubleclearMap" type="com.tofly.pojo.trouble.Toubleclear">
        <id property="id" column="ID"/>
        <result property="auditTime" column="AUDIT_TIME"/>
        <result property="auditUser" column="AUDIT_USER"/>
        <result property="auditNotes" column="AUDIT_NOTES"/>
        <result property="createTime" column="CREATE_TIME"/>
        <result property="createUser" column="CREATE_USER"/>
        <result property="isSubmit" column="IS_SUBMIT"/>
        <result property="toubleId" column="TOUBLE_ID"/>
        <result property="notes" column="NOTES"/>
        <result property="clearFileids" column="CLEAR_FILEIDS"/>
        <result property="isread" column="ISREAD"/>
        <result property="state" column="STATE"/>
        <result property="submitTime" column="SUBMIT_TIME"/>
        <result property="submitUser" column="SUBMIT_USER"/>
        <result property="auditResult" column="AUDIT_RESULT"/>
        <association property="submitUserName" column="SUBMIT_USER" select="com.tofly.gpsboot.mapper.plan.HydrantrecordMapper.getRealName"/>
        <association property="auditUserName" column="AUDIT_USER" select="com.tofly.gpsboot.mapper.plan.HydrantrecordMapper.getRealName"/>
        <association property="createUserName" column="CREATE_USER" select="com.tofly.gpsboot.mapper.plan.HydrantrecordMapper.getRealName"/>
        <association property="isSubmitName" column="IS_SUBMIT" select="com.tofly.gpsboot.mapper.build.BuildMapper.getIsSubmitName"/>
        <association property="auditResultName" column="AUDIT_RESULT" select="com.tofly.gpsboot.mapper.build.BuildMapper.getAuditResultName"/>
        <association property="filePathList" column="CLEAR_FILEIDS" select="getFilePathList"/>
        <association property="isreadName" column="ISREAD" select="com.tofly.gpsboot.mapper.build.BuildMapper.getIsreadName"/>
        <association property="stateName" column="STATE" select="getStateName"/>
        <association property="pipeName" column="TOUBLE_ID" select="getPipeName"/>
        <association property="address" column="TOUBLE_ID" select="getAddress"/>
        <association property="tuobleState" column="TOUBLE_ID" select="getTuobleState"/>
        <association property="toubleRangeIdName" column="TOUBLE_ID" select="getToubleRangeIdName"/>
        <association property="typeIdName" column="TOUBLE_ID" select="getTypeIdName"/>
    </resultMap>
    <select id="getTypeIdName" resultType="string">
        select t3.name typeIdName
        from TF_YWPN_TROUBLE_W t1 left join TF_YWPN_TOUBLECLEAR_W t2
        on t1.id = t2.TOUBLE_ID left join TF_YWPN_TOUBLETYPE_D t3
        on t1.TYPE_ID = t3.id
        where t1.id = #{toubleId}
        GROUP BY t3.name
    </select>
    <select id="getFilePathList" resultType="string" databaseId="oracle">
        select FILE_PATH as filePath
        from TF_YWPN_FILE_W
        <if test="clearFileids != null and clearFileids != ''  ">where id in
            <foreach item="nodeCodestr" collection="clearFileids.split(',')" open="(" separator="," close=")">
                #{nodeCodestr}
            </foreach>
        </if>
        <if test="clearFileids == null ">
            where id = ''
        </if>

    </select>
    <select id="getToubleClearPage" resultMap="toubleclearMap">
        select t1.ID,t1.AUDIT_TIME,t1.AUDIT_USER,t1.AUDIT_NOTES,t1.CREATE_TIME,t1.CREATE_USER,
               t1.IS_SUBMIT,t1.TOUBLE_ID,t1.NOTES,t1.CLEAR_FILEIDS,t1.ISREAD,t1.STATE,t1.SUBMIT_TIME,t1.SUBMIT_USER,
               t1.AUDIT_RESULT
        from TF_YWPN_TOUBLECLEAR_W t1 left join  TF_YWPN_TROUBLE_W t2
        on t1.TOUBLE_ID = t2.id
        where 1=1
        <if test="dto.auditResult != null and dto.auditResult != ''  ">
            and t1.AUDIT_RESULT in
            <foreach item="nodeCodestr" collection="dto.auditResult.split(',')" open="(" separator="," close=")">
                #{nodeCodestr}
            </foreach>
        </if>
        <if test="dto.userIdList != null and dto.userIdList.size>0  ">
            and t1.CREATE_USER in
            <foreach collection="dto.userIdList" item="item" open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>
        <if test="dto.auditUser != null and dto.auditUser != ''  ">
            and t1.audit_user  = #{dto.auditUser}
        </if>
        <if test="dto.isSubmit != null and dto.isSubmit != ''  ">
            and t1.is_submit  = #{dto.isSubmit}
        </if>
        <if test="dto.pipeName != null and dto.pipeName != ''  ">
            and t2.pipe_Name  like '%' || #{dto.pipeName} || '%'
        </if>
        <if test="dto.address != null and dto.address != ''  ">
            and t2.address like '%' || #{dto.address} || '%'
        </if>
        order by t1.CREATE_TIME desc
    </select>

    <select id="getPipeName" resultType="string">
        select pipe_Name
        from  TF_YWPN_TROUBLE_W
        where id = #{toubleId}
    </select>
    <select id="getAddress" resultType="string">
        select address
        from  TF_YWPN_TROUBLE_W
        where id = #{toubleId}
    </select>
    <select id="getTuobleState" resultType="string">
        select decode(state,'0','无需处理','1','巡查','2','已消除','3','消除申请','4','已上报') tuobleState
        from  TF_YWPN_TROUBLE_W
        where id = #{toubleId}
    </select>
    <select id="getToubleRangeIdName" resultType="string">
        select t2.name toubleRangeIdName
        from  TF_YWPN_TROUBLE_W t1 left join TF_YWPN_TOUBLERANGE_D t2
        on t1.TOUBLE_RANGE_ID = t2.id
        where t1.id = #{toubleId}
    </select>
    <select id="getStateName" resultType="string">
        select decode(#{state},'0','未审核','1','同意','2','不同意') as stateName from dual
    </select>

    <update id="clearTroubleUserTaskPlan">
        update TF_YWPN_USERTASK_W
        set ishandle = '1'
        where table_id in(
                select PLAN_ID
                from TF_YWPN_TROUBLE_W t1 left join TF_YWPN_TOUBLEPLAN_W t2 on t1.id = t2.TROUBLE_ID
                where t1.id = #{toubleId}
            )
        and tkty = '4'
        and tknm = '隐患任务'
    </update>

</mapper>
