<?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.plan.FaultMapper">

    <resultMap id="faultMap" type="com.tofly.pojo.plan.Fault">
        <id property="id" column="id"/>
        <result property="category" column="CATEGORY"/>
        <result property="faultType" column="FAULT_TYPE"/>
        <result property="belongStationId" column="BELONG_STATION_ID"/>
        <result property="belongPumpId" column="BELONG_PUMP_ID"/>
        <result property="occurrenceDate" column="OCCURRENCE_DATE"/>
        <result property="occurrenceTime" column="OCCURRENCE_TIME"/>
        <result property="failureDuration" column="FAILURE_DURATION"/>
        <result property="overDate" column="OVER_DATE"/>
        <result property="overTime" column="OVER_TIME"/>
        <result property="deviceId" column="DEVICE_ID"/>
        <result property="faultPart" column="FAULT_PART"/>
        <result property="faultCondition" column="FAULT_CONDITION"/>
        <result property="faultCause" column="FAULT_CAUSE"/>
        <result property="disposeCondition" column="DISPOSE_CONDITION"/>
        <result property="disposeUserId" column="DISPOSE_USER_ID"/>
        <result property="createUser" column="CREATE_USER"/>
        <result property="createTime" column="CREATE_TIME"/>
        <result property="note" column="NOTE"/>
        <result property="fileIds" column="FILE_IDS"/>
        <association property="categoryName" column="CATEGORY" select="getCategoryName"/>
        <association property="faultTypeName" column="FAULT_TYPE" select="getFaultTypeName"/>
        <association property="belongStationName" column="BELONG_STATION_ID" select="getBelongStationName"/>
        <association property="belongPumpName" column="BELONG_PUMP_ID" select="getBelongPumpName"/>
        <association property="deviceName" column="DEVICE_ID" select="getDeviceName"/>
        <association property="disposeUserName" column="DISPOSE_USER_ID" select="com.tofly.gpsboot.mapper.plan.HydrantrecordMapper.getRealName"/>
        <association property="createUserName" column="CREATE_USER" select="com.tofly.gpsboot.mapper.plan.HydrantrecordMapper.getRealName"/>
        <association property="filePathList" column="FILE_IDS"
                     select="com.tofly.gpsboot.mapper.plan.HydrantrecordMapper.getFilePathList"/>
    </resultMap>

    <select id="getCategoryName" resultType="string">
        select NOTES categoryName
        from tf_smpn_code_x
        where ID = #{category}
    </select>
    <select id="getFaultTypeName" resultType="string">
        select NOTES faultTypeName
        from tf_smpn_code_x
        where convert(varchar(100),ID) = #{faultType}
    </select>
    <select id="getDeviceName" resultType="string">
        select name deviceName
        from tf_ywpn_monitor_device_w
        where ID = #{deviceId}
    </select>

    <select id="getBelongPumpName" resultType="string">
        select name
        from tf_monitor_pump_station_w
        where id = #{belongPumpId}
    </select>
    <select id="getBelongStationName" resultType="string">
        select name
        from tf_ywpn_monitor_stationsiteinfo_w
        where id = #{belongStationId}
    </select>

    <select id="getFaultPage" resultMap="faultMap">
        select * from tf_ywpn_fault_w
        where  1=1
        <if test="dto.category != null">
            and CATEGORY = #{dto.category}
        </if>
        <if test="dto.startTime != null and dto.endTime != null ">
            and CONVERT(datetime,OCCURRENCE_DATE,20) >= #{dto.startTime}
            and CONVERT(datetime,OCCURRENCE_DATE,20) &lt;= #{dto.endTime}
        </if>
        <if test="dto.likStr != null and dto.likStr != '' ">
            and FAULT_TYPE like CONCAT ('%',#{dto.likStr},'%')
            or FAULT_PART like CONCAT ('%',#{dto.likStr},'%')
        </if>
    </select>
</mapper>
