<?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.gx.mapper.GxExchangeApplyMapper">

    <resultMap type="com.goldenwater.gx.domain.GxExchangeApply" id="GxExchangeApplyResult">
        <id     property="id"            column="id"             />
        <result property="unitName"      column="unit_name"      />
        <result property="userId"        column="user_id"        />
        <result property="bizCode"       column="biz_code"       />
        <result property="applyTime"     column="apply_time"     />
        <result property="applyExpire"   column="apply_expire"   />
        <result property="applyState"    column="apply_state"    />
        <result property="approveTime"   column="approve_time"   />
        <result property="approveUser"   column="approve_user"   />
        <result property="approveRemark" column="approve_remark" />
        <result property="createBy"      column="create_by"      />
        <result property="createTime"    column="create_time"    />
        <result property="updateBy"      column="update_by"      />
        <result property="updateTime"    column="update_time"    />
        <result property="remark"        column="remark"         />
        <result property="delFlag"       column="del_flag"       />
        <result property="bizName"       column="biz_name"       />
    </resultMap>

    <sql id="selectApplyVo">
        select a.id, a.unit_name, a.user_id, a.biz_code, a.apply_time, a.apply_expire,
               a.apply_state, a.approve_time, a.approve_user, a.approve_remark,
               a.create_by, a.create_time, a.update_by, a.update_time, a.remark, a.del_flag,
               b.biz_name
        from gx_exchange_apply a
        left join gx_exchange_biz b on a.biz_code = b.biz_code and b.del_flag = '0'
    </sql>

    <select id="selectGxExchangeApplyList" parameterType="com.goldenwater.gx.domain.GxExchangeApply" resultMap="GxExchangeApplyResult">
        <include refid="selectApplyVo"/>
        where a.del_flag = '0'
        <if test="unitName != null and unitName != ''">
            AND a.unit_name like '%' || #{unitName} || '%'
        </if>
        <if test="bizCode != null and bizCode != ''">
            AND a.biz_code = #{bizCode}
        </if>
        <if test="applyState != null and applyState != ''">
            AND a.apply_state = #{applyState}
        </if>
        <if test="params.beginTime != null and params.beginTime != ''">
            AND a.apply_time &gt;= TO_DATE(#{params.beginTime}, 'YYYY-MM-DD')
        </if>
        <if test="params.endTime != null and params.endTime != ''">
            AND a.apply_time &lt;= TO_DATE(#{params.endTime}, 'YYYY-MM-DD')
        </if>
        order by a.create_time desc
    </select>

    <select id="selectGxExchangeApplyById" parameterType="Long" resultMap="GxExchangeApplyResult">
        <include refid="selectApplyVo"/>
        where a.id = #{id} and a.del_flag = '0'
    </select>

    <insert id="insertGxExchangeApply" parameterType="com.goldenwater.gx.domain.GxExchangeApply" useGeneratedKeys="true" keyProperty="id">
        insert into gx_exchange_apply
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="unitName != null and unitName != ''">unit_name,</if>
            <if test="userId != null">user_id,</if>
            <if test="bizCode != null and bizCode != ''">biz_code,</if>
            <if test="applyExpire != null">apply_expire,</if>
            <if test="createBy != null">create_by,</if>
            <if test="remark != null">remark,</if>
            apply_time, create_time
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="unitName != null and unitName != ''">#{unitName},</if>
            <if test="userId != null">#{userId},</if>
            <if test="bizCode != null and bizCode != ''">#{bizCode},</if>
            <if test="applyExpire != null">#{applyExpire},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="remark != null">#{remark},</if>
            SYSDATE, SYSDATE
        </trim>
    </insert>

    <update id="updateGxExchangeApply" parameterType="com.goldenwater.gx.domain.GxExchangeApply">
        update gx_exchange_apply
        <set>
            <if test="unitName != null and unitName != ''">unit_name = #{unitName},</if>
            <if test="bizCode != null and bizCode != ''">biz_code = #{bizCode},</if>
            <if test="applyExpire != null">apply_expire = #{applyExpire},</if>
            <if test="applyState != null">apply_state = #{applyState},</if>
            <if test="approveTime != null">approve_time = #{approveTime},</if>
            <if test="approveUser != null">approve_user = #{approveUser},</if>
            <if test="approveRemark != null">approve_remark = #{approveRemark},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="remark != null">remark = #{remark},</if>
            update_time = SYSDATE
        </set>
        where id = #{id}
    </update>

    <update id="deleteGxExchangeApplyById" parameterType="Long">
        update gx_exchange_apply set del_flag = '1' where id = #{id}
    </update>

    <update id="deleteGxExchangeApplyByIds" parameterType="Long">
        update gx_exchange_apply set del_flag = '1' where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </update>

</mapper>
