<?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.ruoyi.ss.account.mapper.AccountMapper">

    <resultMap type="AccountVO" id="SmAccountResult" autoMapping="true">
        <result property="cardInfo"    column="card_info"   typeHandler="com.ruoyi.system.mapper.typehandler.BankCardInfoTypeHandler" />
    </resultMap>

    <sql id="selectSmAccountVo">
        select
            sa.account_id,
            sa.user_id,
            sa.account_type,
            sa.name,
            sa.id_card,
            sa.account_no,
            sa.mobile,
            sa.create_time,
            sa.create_by,
            sa.card_info,
            sa.is_default,
            if(su.is_real, su.real_name, su.user_name) as user_name
        from sm_account sa
            left join sm_user su on su.user_id = sa.user_id
    </sql>

    <sql id="searchCondition">
        <if test="userId != null "> and sa.user_id = #{userId}</if>
        <if test="accountType != null  and accountType != ''"> and sa.account_type = #{accountType}</if>
        <if test="name != null  and name != ''"> and sa.name = #{name}</if>
        <if test="idCard != null  and idCard != ''"> and sa.id_card = #{idCard}</if>
        <if test="accountNo != null  and accountNo != ''"> and sa.account_no = #{accountNo}</if>
        <if test="mobile != null  and mobile != ''"> and sa.mobile = #{mobile}</if>
        <if test="isDefault != null"> and sa.is_default = #{isDefault}</if>
        <if test="userName != null  and userName != ''"> and if(su.is_real, su.real_name, su.user_name) like CONCAT('%',#{userName},'%')</if>
        <if test="excludeIds != null and excludeIds.size() > 0">
            and sa.account_id not in
            <foreach item="item" collection="excludeIds" open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>
    </sql>

    <select id="selectSmAccountList" parameterType="AccountQuery" resultMap="SmAccountResult">
        <include refid="selectSmAccountVo"/>
        <where>
            <include refid="searchCondition"/>
        </where>
    </select>

    <select id="selectSmAccountByAccountId" parameterType="Long" resultMap="SmAccountResult">
        <include refid="selectSmAccountVo"/>
        where sa.account_id = #{accountId}
    </select>

    <select id="selectByAccountNo" resultMap="SmAccountResult">
        select *
        from sm_account
        <where>
            account_no = #{accountNo}
            <if test="accountType != null">
                and account_type = #{accountType}
            </if>
        </where>
        limit 1
    </select>

    <select id="selectOne" resultMap="SmAccountResult">
        <include refid="selectSmAccountVo"/>
        <where>
            <include refid="searchCondition"/>
        </where>
        limit 1
    </select>

    <insert id="insertSmAccount" parameterType="Account">
        insert into sm_account
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="accountId != null">account_id,</if>
            <if test="userId != null">user_id,</if>
            <if test="accountType != null">account_type,</if>
            <if test="name != null">`name`,</if>
            <if test="idCard != null">id_card,</if>
            <if test="accountNo != null">account_no,</if>
            <if test="mobile != null">mobile,</if>
            <if test="createTime != null">create_time,</if>
            <if test="createBy != null">create_by,</if>
            <if test="cardInfo != null">card_info,</if>
            <if test="isDefault != null">is_default,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="accountId != null">#{accountId},</if>
            <if test="userId != null">#{userId},</if>
            <if test="accountType != null">#{accountType},</if>
            <if test="name != null">#{name},</if>
            <if test="idCard != null">#{idCard},</if>
            <if test="accountNo != null">#{accountNo},</if>
            <if test="mobile != null">#{mobile},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="cardInfo != null">#{cardInfo,typeHandler=com.ruoyi.system.mapper.typehandler.BankCardInfoTypeHandler},</if>
            <if test="isDefault != null">#{isDefault},</if>
         </trim>
    </insert>

    <update id="updateSmAccount" parameterType="Account">
        update sm_account
        <trim prefix="SET" suffixOverrides=",">
            <if test="userId != null">user_id = #{userId},</if>
            <if test="accountType != null">account_type = #{accountType},</if>
            <if test="name != null">name = #{name},</if>
            <if test="idCard != null">id_card = #{idCard},</if>
            <if test="accountNo != null">account_no = #{accountNo},</if>
            <if test="mobile != null">mobile = #{mobile},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="cardInfo != null">card_info = #{cardInfo,typeHandler=com.ruoyi.system.mapper.typehandler.BankCardInfoTypeHandler},</if>
            <if test="isDefault != null">is_default = #{isDefault},</if>
        </trim>
        where account_id = #{accountId}
    </update>

    <update id="setDefault">
        update sm_account
        set is_default = if(account_id = #{accountId}, true, false)
        where user_id = #{userId}
    </update>

    <delete id="deleteSmAccountByAccountId" parameterType="Long">
        delete from sm_account where account_id = #{accountId}
    </delete>

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