<?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.leromro.core.mapper.ClientAccountChangeMapper">

    <resultMap type="ClientAccountChange" id="ClientAccountChangeResult">
        <result property="clientAccountChangeId"    column="client_account_change_id"    />
        <result property="userId"    column="user_id"    />
        <result property="changeType"    column="change_type"    />
        <result property="sourceType"    column="source_type"    />
        <result property="changeMoney"    column="change_money"    />
        <result property="beforeBalance"    column="before_balance"    />
        <result property="afterBalance"    column="after_balance"    />
        <result property="mainOrderId"    column="main_order_id"    />
        <result property="createBy"    column="create_by"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateBy"    column="update_by"    />
        <result property="remark"    column="remark"    />
    </resultMap>

    <sql id="selectLClientAccountChangeVo">
        select client_account_change_id, user_id, change_type, source_type, change_money, before_balance, after_balance,main_order_id, create_by, create_time, update_by, remark from l_client_account_change
    </sql>

    <select id="selectClientAccountChangebyUserId" resultType="com.leromro.core.domain.vo.ClientAccountChangeVO">
        select lcac.user_id,
               lcac.create_time,
               lcac.source_type,
               lcac.change_type,
               lcac.change_money,
               lcac.main_order_id,
               lmo.business_management_id,
               (select business_tier_name
                from l_business_management lbm
                where lmo.business_management_id = lbm.business_management_id) as business_tier_name
        from l_client_account_change lcac
                 left join l_main_orders lmo
                           on lcac.main_order_id = lmo.main_order_id
        <where>
            <if test="userId != null and userId != ''">and lcac.user_id = #{userId}</if>
            <if test="createTime != null ">
                AND lcac.create_time &gt;= #{createTime}
                AND lcac.create_time &lt; DATE_ADD(#{createTime}, INTERVAL 1 DAY)
            </if>
        </where>
        order by create_time desc

    </select>


    <select id="selectLClientAccountChangeList" parameterType="ClientAccountChange" resultMap="ClientAccountChangeResult">
        <include refid="selectLClientAccountChangeVo"/>
        <where>
            <if test="userId != null "> and user_id = #{userId}</if>
            <if test="changeType != null  and changeType != ''"> and change_type = #{changeType}</if>
            <if test="sourceType != null  and sourceType != ''"> and source_type = #{sourceType}</if>
            <if test="changeMoney != null "> and change_money = #{changeMoney}</if>
            <if test="beforeBalance != null "> and before_balance = #{beforeBalance}</if>
            <if test="afterBalance != null "> and after_balance = #{afterBalance}</if>
        </where>
    </select>

    <select id="selectLClientAccountChangeByClientAccountChangeId" parameterType="Long" resultMap="ClientAccountChangeResult">
        <include refid="selectLClientAccountChangeVo"/>
        where client_account_change_id = #{clientAccountChangeId}
    </select>




    <insert id="insertLClientAccountChange" parameterType="ClientAccountChange" useGeneratedKeys="true" keyProperty="clientAccountChangeId">
        insert into l_client_account_change
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="userId != null">user_id,</if>
            <if test="changeType != null">change_type,</if>
            <if test="sourceType != null">source_type,</if>
            <if test="changeMoney != null">change_money,</if>
            <if test="beforeBalance != null">before_balance,</if>
            <if test="afterBalance != null">after_balance,</if>
            <if test="mainOrderId != null">main_order_id,</if>
            <if test="createBy != null">create_by,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="remark != null">remark,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="userId != null">#{userId},</if>
            <if test="changeType != null">#{changeType},</if>
            <if test="sourceType != null">#{sourceType},</if>
            <if test="changeMoney != null">#{changeMoney},</if>
            <if test="beforeBalance != null">#{beforeBalance},</if>
            <if test="afterBalance != null">#{afterBalance},</if>
            <if test="mainOrderId != null">#{mainOrderId},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="remark != null">#{remark},</if>
         </trim>
    </insert>

    <update id="updateLClientAccountChange" parameterType="ClientAccountChange">
        update l_client_account_change
        <trim prefix="SET" suffixOverrides=",">
            <if test="userId != null">user_id = #{userId},</if>
            <if test="changeType != null">change_type = #{changeType},</if>
            <if test="sourceType != null">source_type = #{sourceType},</if>
            <if test="changeMoney != null">change_money = #{changeMoney},</if>
            <if test="beforeBalance != null">before_balance = #{beforeBalance},</if>
            <if test="afterBalance != null">after_balance = #{afterBalance},</if>
            <if test="mainOrderId != null">main_order_id = #{mainOrderId},</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="remark != null">remark = #{remark},</if>
        </trim>
        where client_account_change_id = #{clientAccountChangeId}
    </update>

    <delete id="deleteLClientAccountChangeByClientAccountChangeId" parameterType="Long">
        delete from l_client_account_change where client_account_change_id = #{clientAccountChangeId}
    </delete>

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