<?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.goldenwater.slgc.mapper.SlgcWaterUnitMapper">

    <resultMap type="SlgcWaterUnit" id="SlgcWaterUnitResult">
        <result property="id" column="ID"/>
        <result property="name" column="NAME"/>
        <result property="province" column="PROVINCE"/>
        <result property="city" column="CITY"/>
        <result property="fax" column="FAX"/>
        <result property="longitude" column="LONGITUDE"/>
        <result property="latitude" column="LATITUDE"/>
        <result property="address" column="ADDRESS"/>
        <result property="contacts" column="CONTACTS"/>
        <result property="telphone" column="TELPHONE"/>
        <result property="createTime" column="CREATE_TIME"/>
        <result property="updateTime" column="UPDATE_TIME"/>
        <result property="isDelete" column="IS_DELETE"/>
        <result property="approvalDate" column="APPROVAL_DATE"/>
    </resultMap>

    <sql id="selectSlgcWaterUnitVo">
        select swu.ID, swu.NAME, swu.PROVINCE, swu.CITY, swu.FAX, swu.LONGITUDE, swu.LATITUDE,
               swu.ADDRESS, swu.CONTACTS, swu.TELPHONE, swu.CREATE_TIME, swu.UPDATE_TIME, swu.IS_DELETE
        from SLGC_WATER_UNIT swu
    </sql>

    <select id="selectSlgcWaterUnitList" parameterType="SlgcWaterUnit" resultMap="SlgcWaterUnitResult">
        select swu.ID, swu.NAME, swu.PROVINCE, swu.CITY, swu.FAX, swu.LONGITUDE, swu.LATITUDE,
               swu.ADDRESS, swu.CONTACTS, swu.TELPHONE, swu.CREATE_TIME, swu.UPDATE_TIME, swu.IS_DELETE,
               SWUR.APPROVAL_DATE as APPROVAL_DATE
        from SLGC_WATER_UNIT swu
        LEFT JOIN (
            select UNIT_ID, MAX(APPROVAL_DATE) AS APPROVAL_DATE
            from SLGC_WATER_UNIT_RECORD GROUP BY UNIT_ID
        ) swur ON SWUR.UNIT_ID = swu.ID
        where swu.IS_DELETE = '0'
        <if test="name != null and name != ''">
            AND swu.NAME like concat('%', #{name}, '%')
        </if>
        <if test="address != null and address != ''">
            AND (swu.PROVINCE LIKE concat('%', #{address}, '%') OR swu.CITY LIKE concat('%', #{address}, '%') OR swu.ADDRESS like concat('%', #{address}, '%'))
        </if>
        ORDER BY SWUR.APPROVAL_DATE ASC
    </select>

    <select id="selectSlgcWaterUnitById" parameterType="String" resultMap="SlgcWaterUnitResult">
        <include refid="selectSlgcWaterUnitVo"/>
        where swu.ID = #{id}
    </select>

    <insert id="insertSlgcWaterUnit" parameterType="SlgcWaterUnit">
        insert into SLGC_WATER_UNIT
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="id != null">ID,</if>
            <if test="name != null">NAME,</if>
            <if test="province != null">PROVINCE,</if>
            <if test="city != null">CITY,</if>
            <if test="fax != null">FAX,</if>
            <if test="longitude != null">LONGITUDE,</if>
            <if test="latitude != null">LATITUDE,</if>
            <if test="address != null">ADDRESS,</if>
            <if test="contacts != null">CONTACTS,</if>
            <if test="telphone != null">TELPHONE,</if>
            <if test="createTime != null">CREATE_TIME,</if>
            <if test="updateTime != null">UPDATE_TIME,</if>
            <if test="isDelete != null">IS_DELETE,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="id != null">#{id},</if>
            <if test="name != null">#{name},</if>
            <if test="province != null">#{province},</if>
            <if test="city != null">#{city},</if>
            <if test="fax != null">#{fax},</if>
            <if test="longitude != null">#{longitude},</if>
            <if test="latitude != null">#{latitude},</if>
            <if test="address != null">#{address},</if>
            <if test="contacts != null">#{contacts},</if>
            <if test="telphone != null">#{telphone},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="isDelete != null">#{isDelete},</if>
        </trim>
    </insert>

    <update id="updateSlgcWaterUnit" parameterType="SlgcWaterUnit">
        update SLGC_WATER_UNIT
        <trim prefix="SET" suffixOverrides=",">
            <if test="name != null">NAME = #{name},</if>
            <if test="province != null">PROVINCE = #{province},</if>
            <if test="city != null">CITY = #{city},</if>
            <if test="fax != null">FAX = #{fax},</if>
            <if test="longitude != null">LONGITUDE = #{longitude},</if>
            <if test="latitude != null">LATITUDE = #{latitude},</if>
            <if test="address != null">ADDRESS = #{address},</if>
            <if test="contacts != null">CONTACTS = #{contacts},</if>
            <if test="telphone != null">TELPHONE = #{telphone},</if>
            <if test="updateTime != null">UPDATE_TIME = #{updateTime},</if>
        </trim>
        where ID = #{id}
    </update>

    <delete id="deleteSlgcWaterUnitByIds" parameterType="String">
        update SLGC_WATER_UNIT set IS_DELETE = '1' where ID in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>

    <select id="findMaxId" resultType="java.lang.Integer">
        select COALESCE(max(to_number(id)) + 1, 1) from SLGC_WATER_UNIT
    </select>

    <select id="selectByUnitName" parameterType="String" resultMap="SlgcWaterUnitResult">
        select * from SLGC_WATER_UNIT where NAME = #{unitName} and IS_DELETE = '0' fetch first 1 rows only
    </select>

</mapper>
