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

    <resultMap type="AccountVO" id="AccountResult" autoMapping="true"/>

    <sql id="selectAccountVo">
        select
            ba.id,
            ba.user_id,
            ba.type,
            ba.no,
            ba.name,
            ba.id_card,
            ba.mobile,
            ba.bank_icon,
            ba.bank_name,
            ba.bank_card_name,
            ba.bank_type,
            ba.create_time,
            ba.deleted,
            su.nick_name as user_name,
            su.is_real as user_is_real,
            su.real_phone as user_real_phone,
            su.real_id_card as user_real_id_card,
            su.real_name as user_real_name
        from bst_account ba
            left join sys_user su on su.user_id = ba.user_id
    </sql>

    <sql id="searchCondition">
        <if test="query.id != null "> and ba.id = #{query.id}</if>
        <if test="query.userId != null "> and ba.user_id = #{query.userId}</if>
        <if test="query.type != null  and query.type != ''"> and ba.type = #{query.type}</if>
        <if test="query.no != null  and query.no != ''"> and ba.no like concat('%', #{query.no}, '%')</if>
        <if test="query.name != null  and query.name != ''"> and ba.name like concat('%', #{query.name}, '%')</if>
        <if test="query.idCard != null  and query.idCard != ''"> and ba.id_card like concat('%', #{query.idCard}, '%')</if>
        <if test="query.mobile != null  and query.mobile != ''"> and ba.mobile like concat('%', #{query.mobile}, '%')</if>
        <if test="query.bankName != null  and query.bankName != ''"> and ba.bank_name like concat('%', #{query.bankName}, '%')</if>
        <if test="query.bankCardName != null  and query.bankCardName != ''"> and ba.bank_card_name like concat('%', #{query.bankCardName}, '%')</if>
        <if test="query.bankType != null  and query.bankType != ''"> and ba.bank_type like concat('%', #{query.bankType}, '%')</if>
        <if test="query.deleted != null"> and ba.deleted = #{query.deleted}</if>
        <if test="query.deleted == null"> and ba.deleted = false</if>
        <if test="query.excludeId != null"> and ba.id != #{query.excludeId}</if>
        <if test="query.ids != null and query.ids.size() > 0">
            and ba.id in
            <foreach item="item" index="index" collection="query.ids" open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>
        ${@com.ruoyi.framework.util.DataScopeUtil@dataScope(
            null,
            "ba.user_id",
            null,
            null,
            query.scope
        )}
        ${query.params.dataScope}
    </sql>

    <select id="selectAccountList" parameterType="AccountQuery" resultMap="AccountResult">
        <include refid="selectAccountVo"/>
        <where>
            <include refid="searchCondition"/>
        </where>
    </select>

    <select id="selectAccountById" parameterType="Long" resultMap="AccountResult">
        <include refid="selectAccountVo"/>
        where ba.id = #{id}
    </select>

    <insert id="insertAccount" parameterType="Account" useGeneratedKeys="true" keyProperty="id">
        insert into bst_account
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="userId != null">user_id,</if>
            <if test="type != null and type != ''">type,</if>
            <if test="no != null and no != ''">no,</if>
            <if test="name != null and name != ''">name,</if>
            <if test="idCard != null and idCard != ''">id_card,</if>
            <if test="mobile != null and mobile != ''">mobile,</if>
            <if test="bankIcon != null">bank_icon,</if>
            <if test="bankName != null">bank_name,</if>
            <if test="bankCardName != null">bank_card_name,</if>
            <if test="bankType != null">bank_type,</if>
            <if test="createTime != null">create_time,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="userId != null">#{userId},</if>
            <if test="type != null and type != ''">#{type},</if>
            <if test="no != null and no != ''">#{no},</if>
            <if test="name != null and name != ''">#{name},</if>
            <if test="idCard != null and idCard != ''">#{idCard},</if>
            <if test="mobile != null and mobile != ''">#{mobile},</if>
            <if test="bankIcon != null">#{bankIcon},</if>
            <if test="bankName != null">#{bankName},</if>
            <if test="bankCardName != null">#{bankCardName},</if>
            <if test="bankType != null">#{bankType},</if>
            <if test="createTime != null">#{createTime},</if>
         </trim>
    </insert>

    <update id="updateAccount" parameterType="Account">
        update bst_account
        <trim prefix="SET" suffixOverrides=",">
            <include refid="updateColumns"/>
        </trim>
        where id = #{data.id}
    </update>

    <sql id="updateColumns">
        <if test="data.userId != null">user_id = #{data.userId},</if>
        <if test="data.type != null and data.type != ''">type = #{data.type},</if>
        <if test="data.no != null and data.no != ''">no = #{data.no},</if>
        <if test="data.name != null and data.name != ''">name = #{data.name},</if>
        <if test="data.idCard != null and data.idCard != ''">id_card = #{data.idCard},</if>
        <if test="data.mobile != null and data.mobile != ''">mobile = #{data.mobile},</if>
        <if test="data.bankIcon != null">bank_icon = #{data.bankIcon},</if>
        <if test="data.bankName != null">bank_name = #{data.bankName},</if>
        <if test="data.bankCardName != null">bank_card_name = #{data.bankCardName},</if>
        <if test="data.bankType != null">bank_type = #{data.bankType},</if>
        <if test="data.createTime != null">create_time = #{data.createTime},</if>
    </sql>

    <delete id="deleteAccountById" parameterType="Long">
        delete from bst_account where id = #{id}
    </delete>

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

    <!-- logicDel -->

    <update id="logicDel">
        update bst_account
        set deleted = true
        where id in
        <foreach item="id" collection="ids" open="(" separator="," close=")">
            #{id}
        </foreach>
        and deleted = false
    </update>

    <!-- selectIdList -->

    <select id="selectIdList" parameterType="AccountQuery" resultType="Long">
        select ba.id
        from bst_account ba
        <where>
            <include refid="searchCondition"/>
        </where>
    </select>

    <select id="selectCount" resultType="Integer">
        select count(ba.id)
        from bst_account ba
        <where>
            <include refid="searchCondition"/>
        </where>
    </select>
</mapper>
