<?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.system.mapper.UserBillingMapper">

    <resultMap type="UserBilling" id="UserBillingResult">
        <result property="id"    column="id"    />
        <result property="userId"    column="user_id"    />
        <result property="type"    column="type"    />
        <result property="ddId"    column="dd_id"    />
        <result property="amount"    column="amount"    />
        <result property="state"    column="state"    />
        <result property="illustrate"    column="illustrate"    />
        <result property="paymentMethod"    column="payment_method"    />
        <result property="accountNumber"    column="account_number"    />
        <result property="mdId"    column="md_id"    />
        <result property="divvy"    column="divvy"    />
        <result property="balancePay"    column="balance_pay"    />
        <result property="userType"    column="user_type"    />
        <result property="cretim" column="cretim" />
        <result property="divvyRate"    column="divvy_rate"    />
        <result property="behalfAmount"    column="behalf_amount"    />
        <result property="walletBalance"    column="wallet_balance"    />
        <result property="tax"    column="tax"    />
        <result property="taxRate"    column="tax_rate"    />
    </resultMap>

    <sql id="selectUserBillingVo">
        select b.id, b.user_id, b.type, b.dd_id, b.md_id,b.amount, b.divvy,b.state, b.illustrate, b.payment_method, b.account_number,b.balance_pay, b.user_type,b.cretim, b.divvy_rate, b.behalf_amount, b.wallet_balance, b.tax, b.tax_rate from user_billing b
    </sql>

    <select id="selectUserBillingList" parameterType="UserBilling" resultMap="UserBillingResult">
        <include refid="selectUserBillingVo"/>
        <if test="(userName !=null and userName!='') or (userType!=null and userType!='') ">
            left join info_user u on b.user_id=u.user_id
        </if>
        <where>
            <if test="userId != null "> and b.user_id = #{userId}</if>
            <if test="type != null  and type != ''"> and b.type = #{type}</if>
            <if test="ddId != null  and ddId != ''"> and b.dd_id = #{ddId}</if>
            <if test="state != null  and state != ''"> and b.state = #{state}</if>
            <if test="mdId != null  and mdId != ''"> and b.md_id = #{mdId}</if>
            <if test="paymentMethod != null  and paymentMethod != ''"> and b.payment_method like concat('%', #{paymentMethod}, '%')</if>
            <if test="accountNumber != null  and accountNumber != ''"> and b.account_number like concat('%', #{accountNumber}, '%')</if>
            <if test="balancePay != null  and balancePay != ''"> and b.balance_pay = #{balancePay}</if>
            <if test="userName != null and userName != ''"> and (u.user_name like concat('%', #{userName}, '%') or u.phone like concat('%', #{userName}, '%'))</if>
            <if test="userType != null and userType != ''"> and u.user_type = #{userType}</if>
        </where>
        order by id desc
    </select>

    <select id="selectUserWithdrawList" parameterType="UserBilling" resultMap="UserBillingResult">
        <include refid="selectUserBillingVo"/>
        <where>
            type='2'
            <if test="userId != null "> and b.user_id = #{userId}</if>
            <if test="ddId != null  and ddId != ''"> and b.dd_id = #{ddId}</if>
            <if test="state != null  and state != ''"> and b.state = #{state}</if>
            <if test="mdId != null  and mdId != ''"> and b.md_id = #{mdId}</if>
            <if test="paymentMethod != null  and paymentMethod != ''"> and b.payment_method like concat('%', #{paymentMethod}, '%')</if>
            <if test="accountNumber != null  and accountNumber != ''"> and b.account_number like concat('%', #{accountNumber}, '%')</if>
            <if test="balancePay != null  and balancePay != ''"> and b.balance_pay = #{balancePay}</if>
            <if test="userType != null  and userType != ''"> and b.user_type = #{userType}</if>
        </where>
        order by b.cretim desc
    </select>

    <select id="selectUserBillingById" parameterType="Long" resultMap="UserBillingResult">
        <include refid="selectUserBillingVo"/>
        where b.id = #{id}
    </select>

    <insert id="insertUserBilling" parameterType="UserBilling" useGeneratedKeys="true" keyProperty="id">
        insert into user_billing
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="userId != null">user_id,</if>
            <if test="type != null">type,</if>
            <if test="ddId != null">dd_id,</if>
            <if test="amount != null">amount,</if>
            <if test="state != null">state,</if>
            <if test="illustrate != null">illustrate,</if>
            <if test="paymentMethod != null">payment_method,</if>
            <if test="accountNumber != null">account_number,</if>
            <if test="mdId != null">md_id,</if>
            <if test="divvy != null">divvy,</if>
            <if test="balancePay != null">balance_pay,</if>
            <if test="userType != null">user_type,</if>
            <if test="divvyRate != null">divvy_rate,</if>
            <if test="behalfAmount != null">behalf_amount,</if>
            <if test="walletBalance != null">wallet_balance,</if>
            <if test="tax != null">tax,</if>
            <if test="taxRate != null and taxRate != ''">tax_rate,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="userId != null">#{userId},</if>
            <if test="type != null">#{type},</if>
            <if test="ddId != null">#{ddId},</if>
            <if test="amount != null">#{amount},</if>
            <if test="state != null">#{state},</if>
            <if test="illustrate != null">#{illustrate},</if>
            <if test="paymentMethod != null">#{paymentMethod},</if>
            <if test="accountNumber != null">#{accountNumber},</if>
            <if test="mdId != null">#{mdId},</if>
            <if test="divvy != null">#{divvy},</if>
            <if test="balancePay != null">#{balancePay},</if>
            <if test="userType != null">#{userType},</if>
            <if test="divvyRate != null">#{divvyRate},</if>
            <if test="behalfAmount != null">#{behalfAmount},</if>
            <if test="walletBalance != null">#{walletBalance},</if>
            <if test="tax != null">#{tax},</if>
            <if test="taxRate != null and taxRate != ''">#{taxRate},</if>
        </trim>
    </insert>

    <update id="updateUserBilling" parameterType="UserBilling">
        update user_billing
        <trim prefix="SET" suffixOverrides=",">
            <if test="userId != null">user_id = #{userId},</if>
            <if test="type != null">type = #{type},</if>
            <if test="ddId != null">dd_id = #{ddId},</if>
            <if test="amount != null">amount = #{amount},</if>
            <if test="state != null">state = #{state},</if>
            <if test="illustrate != null">illustrate = #{illustrate},</if>
            <if test="paymentMethod != null">payment_method = #{paymentMethod},</if>
            <if test="accountNumber != null">account_number = #{accountNumber},</if>
            <if test="mdId != null">md_id = #{mdId},</if>
            <if test="divvy != null">divvy = #{divvy},</if>
            <if test="balancePay != null">balance_pay = #{balancePay},</if>
            <if test="userType != null">user_type = #{userType},</if>
            <if test="divvyRate != null">divvy_rate = #{divvyRate},</if>
            <if test="behalfAmount != null">behalf_amount = #{behalfAmount},</if>
            <if test="walletBalance != null">wallet_balance = #{walletBalance},</if>
            <if test="tax != null">tax = #{tax},</if>
            <if test="taxRate != null and taxRate != ''">tax_rate = #{taxRate},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteUserBillingById" parameterType="Long">
        delete from user_billing where id = #{id}
    </delete>

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