<?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.watermeter.mapper.BwrepairMapper">

    <resultMap id="bwrepairMap" type="com.tofly.watermeter.entity.Bwrepair">
        <id property="id" column="ID"/>
        <result property="sbbh" column="SBBH"/>
        <result property="repairResult" column="REPAIR_RESULT"/>
        <result property="repairDescription" column="REPAIR_DESCRIPTION"/>
        <result property="repairUser" column="REPAIR_USER"/>
        <result property="repairTime" column="REPAIR_TIME"/>
        <result property="createUser" column="CREATE_USER"/>
        <result property="createTime" column="CREATE_TIME"/>
        <association property="sbcjName" column="BW_SBCJ" select="com.tofly.watermeter.mapper.BwrksbdatailMapper.getCodeById"/>
        <association property="sbkjName" column="BW_SBKJ" select="com.tofly.watermeter.mapper.BwrksbdatailMapper.getCodeById"/>
        <association property="sblxName" column="BW_SBLX" select="com.tofly.watermeter.mapper.BwrksbdatailMapper.getCodeById"/>
        <association property="repairResultName" column="REPAIR_RESULT" select="getRepairResultName"/>
        <association property="repairUserName" column="REPAIR_USER"
                     select="com.tofly.baseapi.mapper.ToflyCodeCommonMapper.getUserName"/>
        <association property="createUserName" column="CREATE_USER"
                     select="com.tofly.baseapi.mapper.ToflyCodeCommonMapper.getUserName"/>
    </resultMap>

    <select id="getRepairResultName" resultType="string">
        select decode(#{repairResult} , '0','维修不合格','1','维修合格')
        from dual
    </select>

    <select id="getRepair" resultMap="bwrepairMap">
        SELECT a.BW_SBCJ , a.BW_SBLX , a.BW_SBKJ , b.* FROM TF_YWPN_BWRKSBDATAIL_W a
        RIGHT JOIN TF_YWPN_BWREPAIR_W b ON a.BW_SBBH=b.SBBH
        <where>
            <if test="sb.bwSbkj != null">
                AND a.BW_SBKJ = #{sb.bwSbkj}
            </if>
            <if test="sb.bwSbcj != null">
                AND a.BW_SBCJ = #{sb.bwSbcj}
            </if>
            <if test="sb.bwSblx != null">
                AND a.BW_SBLX = #{sb.bwSblx}
            </if>
            <if test="sb.sbbh != null">
                AND a.BW_SBBH LIKE '%' || #{sb.sbbh} || '%'
            </if>
            <if test="sb.time != null and sb.endTime != null ">
                AND b.REPAIR_TIME BETWEEN #{sb.time} AND #{sb.endTime}
            </if>
        </where>
        order by b.REPAIR_TIME DESC
    </select>


</mapper>
