<?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.ym.mec.biz.dal.dao.SysAccountDao">
    <resultMap id="SysAccount" type="com.ym.mec.biz.dal.entity.SysAccount">
        <!--@Table sys_account-->
        <id column="id_" jdbcType="INTEGER" property="id"/>
        <result column="company_" jdbcType="VARCHAR" property="company"/>
        <result column="mer_no_" jdbcType="VARCHAR" property="merNo"/>
        <result column="routing_mer_no_" jdbcType="VARCHAR" property="routingMerNo"/>
        <result column="routing_organ_id_" jdbcType="INTEGER" property="routingOrganId"/>
        <result column="max_receipt_" jdbcType="DECIMAL" property="maxReceipt"/>
        <result column="has_receipt_" jdbcType="DECIMAL" property="hasReceipt"/>
        <result column="channel_" jdbcType="VARCHAR" property="channel"/>
        <result column="type_" jdbcType="CHAR" property="type"/>
        <result column="status_" jdbcType="TINYINT" property="status"/>
        <result column="version" jdbcType="INTEGER" property="version"/>
    </resultMap>
    <sql id="Base_Column_List">
            id_,
            company_,
            mer_no_,
            routing_mer_no_,
            routing_organ_id_,
            max_receipt_,
            has_receipt_,
            channel_,
            type_,
            status_,
            version
    </sql>
    <select id="get" parameterType="java.lang.Integer" resultMap="SysAccount">
        <!--@mbg.generated-->
        select
        <include refid="Base_Column_List"/>
        from sys_account
        where id_ = #{id,jdbcType=INTEGER}
    </select>
    <delete id="delete" parameterType="java.lang.Integer">
        <!--@mbg.generated-->
        delete from sys_account
        where id_ = #{id,jdbcType=INTEGER}
    </delete>
    <insert id="insert" keyColumn="id_" keyProperty="id" parameterType="com.ym.mec.biz.dal.entity.SysAccount"
            useGeneratedKeys="true">
        <!--@mbg.generated-->
        insert into sys_account
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="company != null">
                company_,
            </if>
            <if test="merNo != null">
                mer_no_,
            </if>
            <if test="merNo != null">
                routing_mer_no_,
            </if>
            <if test="maxReceipt != null">
                max_receipt_,
            </if>
            <if test="hasReceipt != null">
                has_receipt_,
            </if>
            <if test="channel != null">
                channel_,
            </if>
            <if test="type != null">
                type_,
            </if>
            <if test="status != null">
                status_,
            </if>
            <if test="version != null">
                version,
            </if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="company != null">
                #{company,jdbcType=VARCHAR},
            </if>
            <if test="merNo != null">
                #{merNo,jdbcType=VARCHAR},
            </if>
            <if test="routingMerNo != null">
                #{routingMerNo,jdbcType=VARCHAR},
            </if>
            <if test="maxReceipt != null">
                #{maxReceipt,jdbcType=DECIMAL},
            </if>
            <if test="hasReceipt != null">
                #{hasReceipt,jdbcType=DECIMAL},
            </if>
            <if test="channel != null">
                #{channel,jdbcType=VARCHAR},
            </if>
            <if test="type != null">
                #{type,jdbcType=CHAR},
            </if>
            <if test="status != null">
                #{status,jdbcType=TINYINT},
            </if>
            <if test="version != null">
                #{version,jdbcType=INTEGER},
            </if>
        </trim>
    </insert>
    <update id="update" parameterType="com.ym.mec.biz.dal.entity.SysAccount">
        <!--@mbg.generated-->
        update sys_account
        <set>
            <if test="company != null">
                company_ = #{company,jdbcType=VARCHAR},
            </if>
            <if test="merNo != null">
                mer_no_ = #{merNo,jdbcType=VARCHAR},
            </if>
            <if test="routingMerNo != null">
                routing_mer_no_ = #{routingMerNo,jdbcType=VARCHAR},
            </if>
            <if test="maxReceipt != null">
                max_receipt_ = #{maxReceipt,jdbcType=DECIMAL},
            </if>
            <if test="hasReceipt != null">
                has_receipt_ = #{hasReceipt,jdbcType=DECIMAL},
            </if>
            <if test="channel != null">
                channel_ = #{channel,jdbcType=VARCHAR},
            </if>
            <if test="type != null">
                type_ = #{type,jdbcType=CHAR},
            </if>
            <if test="status != null">
                status_ = #{status,jdbcType=TINYINT},
            </if>
            <if test="version != null">
                version = version+1,
            </if>
        </set>
        where id_ = #{id,jdbcType=INTEGER} AND version =#{version}
    </update>

    <select id="getAccount" resultMap="SysAccount">
        SELECT * FROM sys_account WHERE status_=1 AND company_=#{company}
        <if test="channel != null">
            AND channel_ = #{channel}
        </if>
        <if test="type != null">
            AND FIND_IN_SET(type_,#{type})
        </if>
        <if test="amount != null">
            <![CDATA[AND max_receipt_ < has_receipt_+ #{amount}]]>
        </if>
    </select>

    <select id="getOneAccount" resultMap="SysAccount">
        SELECT * FROM sys_account WHERE status_ =1 AND company_=#{company}
        <if test="channel != null">
            AND channel_ = #{channel}
        </if>
        <if test="type != null">
            AND type_ = #{type}
        </if>
        <if test="amount != null">
            <![CDATA[AND max_receipt_ < has_receipt_+ #{amount}]]>
        </if>
    </select>

    <select id="getPerAccount" resultMap="SysAccount">
        SELECT * FROM sys_account WHERE status_=1 AND type_='PER'
        <if test="channel != null">
            AND channel_ = #{channel}
        </if>
    </select>
</mapper>