<?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.fs.baidu.mapper.BdAccountMapper">
    
    <resultMap type="BdAccount" id="BdAccountResult">
        <result property="id"    column="id"    />
        <result property="accountId"    column="account_id"    />
        <result property="accountName"    column="account_name"    />
        <result property="userId"    column="user_id"    />
        <result property="createTime"    column="create_time"    />
        <result property="createBy"    column="create_by"    />
        <result property="updateBy"    column="update_by"    />
        <result property="updateTime"    column="update_time"    />
        <result property="remark"    column="remark"    />
    </resultMap>

    <sql id="selectBdAccountVo">
        select * from bd_account
    </sql>

    <select id="selectBdAccountList" parameterType="BdAccount" resultMap="BdAccountResult">
        select a.*,b.nick_name userName from bd_account a
        left join company_user b on a.user_id = b.user_id
        <where>  
            <if test="accountId != null "> and a.account_id = #{accountId}</if>
            <if test="accountName != null  and accountName != ''"> and a.account_name like concat('%', #{accountName}, '%')</if>
            <if test="userId != null "> and a.user_id = #{userId}</if>
        </where>
        order by a.create_time desc
    </select>
    
    <select id="selectBdAccountById" parameterType="Long" resultMap="BdAccountResult">
        <include refid="selectBdAccountVo"/>
        where id = #{id}
    </select>
        
    <insert id="insertBdAccount" parameterType="BdAccount" useGeneratedKeys="true" keyProperty="id">
        insert into bd_account
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="accountId != null">account_id,</if>
            <if test="accountName != null">account_name,</if>
            <if test="userId != null">user_id,</if>
            <if test="createTime != null">create_time,</if>
            <if test="createBy != null">create_by,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="remark != null">remark,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="accountId != null">#{accountId},</if>
            <if test="accountName != null">#{accountName},</if>
            <if test="userId != null">#{userId},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="remark != null">#{remark},</if>
         </trim>
    </insert>

    <update id="updateBdAccount" parameterType="BdAccount">
        update bd_account
        <trim prefix="SET" suffixOverrides=",">
            <if test="accountId != null">account_id = #{accountId},</if>
            <if test="accountName != null">account_name = #{accountName},</if>
            <if test="userId != null">user_id = #{userId},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="remark != null">remark = #{remark},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteBdAccountById" parameterType="Long">
        delete from bd_account where id = #{id}
    </delete>

    <delete id="deleteBdAccountByIds" parameterType="String">
        delete from bd_account where id in 
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>
</mapper>