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

    <resultMap type="SlgcWaterUnitRecord" id="SlgcWaterUnitRecordResult">
        <result property="id" column="ID"/>
        <result property="approvalNumber" column="APPROVAL_NUMBER"/>
        <result property="approvalDate" column="APPROVAL_DATE"/>
        <result property="unitId" column="UNIT_ID"/>
        <result property="approvalContent" column="APPROVAL_CONTENT"/>
        <result property="createTime" column="CREATE_TIME"/>
        <result property="updateTime" column="UPDATE_TIME"/>
        <result property="isDelete" column="IS_DELETE"/>
    </resultMap>

    <sql id="selectSlgcWaterUnitRecordVo">
        select ID, APPROVAL_NUMBER, APPROVAL_DATE, UNIT_ID, APPROVAL_CONTENT, CREATE_TIME, UPDATE_TIME, IS_DELETE
        from SLGC_WATER_UNIT_RECORD
    </sql>

    <select id="selectSlgcWaterUnitRecordList" parameterType="String" resultMap="SlgcWaterUnitRecordResult">
        <include refid="selectSlgcWaterUnitRecordVo"/>
        where IS_DELETE = '0' and UNIT_ID = #{unitId}
        order by UPDATE_TIME DESC
    </select>

    <select id="selectSlgcWaterUnitRecordById" parameterType="String" resultMap="SlgcWaterUnitRecordResult">
        <include refid="selectSlgcWaterUnitRecordVo"/>
        where ID = #{id}
    </select>

    <insert id="insertSlgcWaterUnitRecord" parameterType="SlgcWaterUnitRecord">
        insert into SLGC_WATER_UNIT_RECORD
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="id != null">ID,</if>
            <if test="approvalNumber != null">APPROVAL_NUMBER,</if>
            <if test="approvalDate != null">APPROVAL_DATE,</if>
            <if test="unitId != null">UNIT_ID,</if>
            <if test="approvalContent != null">APPROVAL_CONTENT,</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="approvalNumber != null">#{approvalNumber},</if>
            <if test="approvalDate != null">#{approvalDate},</if>
            <if test="unitId != null">#{unitId},</if>
            <if test="approvalContent != null">#{approvalContent},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="isDelete != null">#{isDelete},</if>
        </trim>
    </insert>

    <update id="updateSlgcWaterUnitRecord" parameterType="SlgcWaterUnitRecord">
        update SLGC_WATER_UNIT_RECORD
        <trim prefix="SET" suffixOverrides=",">
            <if test="approvalNumber != null">APPROVAL_NUMBER = #{approvalNumber},</if>
            <if test="approvalDate != null">APPROVAL_DATE = #{approvalDate},</if>
            <if test="unitId != null">UNIT_ID = #{unitId},</if>
            <if test="approvalContent != null">APPROVAL_CONTENT = #{approvalContent},</if>
            <if test="updateTime != null">UPDATE_TIME = #{updateTime},</if>
        </trim>
        where ID = #{id}
    </update>

    <delete id="deleteSlgcWaterUnitRecordByIds" parameterType="String">
        update SLGC_WATER_UNIT_RECORD 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_RECORD
    </select>

</mapper>
