<?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.invest.mapper.AccountsMapper">

    <resultMap type="AccountsVo" id="AccountsResult">
        <result property="id"    column="id"    />
        <result property="name"    column="name"    />
        <result property="type"    column="type"    />
        <result property="code"    column="code"    />
        <result property="balance"    column="balance"    />
        <result property="creditLimit"    column="credit_limit"    />
        <result property="availableLimit"    column="available_limit"    />
        <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="delFlag"    column="del_flag"    />
        <result property="remark"    column="remark"    />
        <result property="accountId"    column="account_id"    />
        <result property="state"    column="state"    />
        <result property="billDate"    column="bill_date"    />
        <result property="payDate"    column="pay_date"    />
        <result property="creditLimit"    column="credit_limit"    />
        <result property="debitType"    column="debit_type"    />
    </resultMap>

    <sql id="selectAccountsVo">
        select
        a.id,
        a.name,
        a.type,
        a.code,
        a.balance,
        a.credit_limit,
        a.available_limit,
        a.create_by,
        a.create_time,
        a.update_by,
        a.update_time,
        a.del_flag,
        a.remark,
        a.account_id,
        a.state,
        bcl.bill_date ,
        bcl.pay_date ,
        bcl.credit_limit ,
        bcl.debit_type
        from
        accounts a
        left join bank_card_lend bcl on
        bcl.id = a.id
    </sql>

    <select id="selectAccountsList" parameterType="AccountsDto" resultMap="AccountsResult">
        <include refid="selectAccountsVo"/>
        <where>
            a.del_flag='0'
            <if test="name != null  and name != ''"> and a.name like '%'|| #{name}||'%'</if>
            <if test="type != null  and type != ''"> and a.type = #{type}</if>
            <if test="state != null  and state != ''"> and a.state = #{state}</if>
        </where>
        <!-- 数据范围过滤 -->
        ${params.dataScope}
        order by a.type asc,a.update_time desc
    </select>

    <select id="selectAccountsById" parameterType="Long" resultMap="AccountsResult">
        <include refid="selectAccountsVo"/>
        where a.id = #{id}
    </select>

    <insert id="insertAccounts" parameterType="Accounts">
        insert into accounts
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="id != null">id,</if>
            <if test="name != null and name != ''">name,</if>
            <if test="type != null and type != ''">type,</if>
            <if test="code != null and code != ''">code,</if>
            <if test="balance != null">balance,</if>
            <if test="creditLimit != null">credit_limit,</if>
            <if test="availableLimit != null">available_limit,</if>
            <if test="createBy != null">create_by,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="delFlag != null">del_flag,</if>
            <if test="remark != null">remark,</if>
            <if test="accountId != null and accountId != ''">account_id,</if>
            <if test="state != null">state,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="id != null">#{id},</if>
            <if test="name != null and name != ''">#{name},</if>
            <if test="type != null and type != ''">#{type},</if>
            <if test="code != null and code != ''">#{code},</if>
            <if test="balance != null">#{balance},</if>
            <if test="creditLimit != null">#{creditLimit},</if>
            <if test="availableLimit != null">#{availableLimit},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="delFlag != null">#{delFlag},</if>
            <if test="remark != null">#{remark},</if>
            <if test="accountId != null and accountId != ''">#{accountId},</if>
            <if test="state != null">#{state},</if>
         </trim>
    </insert>

    <update id="updateAccounts" parameterType="Accounts">
        update accounts
        <trim prefix="SET" suffixOverrides=",">
            <if test="name != null and name != ''">name = #{name},</if>
            <if test="type != null and type != ''">type = #{type},</if>
            <if test="code != null and code != ''">code = #{code},</if>
            <if test="balance != null">balance = #{balance},</if>
            <if test="creditLimit != null">credit_limit = #{creditLimit},</if>
            <if test="availableLimit != null">available_limit = #{availableLimit},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="delFlag != null">del_flag = #{delFlag},</if>
            <if test="remark != null">remark = #{remark},</if>
            <if test="accountId != null and accountId != ''">account_id = #{accountId},</if>
            <if test="state != null">state = #{state},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteAccountsById" parameterType="Long">
        delete from accounts where id = #{id}
    </delete>

    <delete id="deleteAccountsByIds" parameterType="String">
        delete from accounts where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>
    <update id="removeAccountsById" parameterType="Long">
         update accounts set del_flag='1' where id = #{id}
    </update>

    <update id="removeAccountsByIds" parameterType="String">
        update accounts set del_flag='1' where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </update>
</mapper>
