<?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.VolunteerAccountChangeMapper">
    
    <resultMap type="VolunteerAccountChange" id="VolunteerAccountChangeResult">
        <result property="volunteerAccountChangeId"    column="volunteer_account_change_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="sourceId"    column="source_id"    />
        <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="remark"    column="remark"    />
        <result property="sourceId"    column="source_id"    />
    </resultMap>

    <sql id="selectVolunteerAccountChangeVo">
        select volunteer_account_change_id, change_type, source_type, change_money, before_balance, after_balance, source_id, create_by, create_time, update_by, update_time, remark from l_volunteer_account_change
    </sql>

    <select id="selectVolunteerAccountChangeList" parameterType="VolunteerAccountChange" resultMap="VolunteerAccountChangeResult">
        <include refid="selectVolunteerAccountChangeVo"/>
        <where>  
            <if test="changeType != null "> and change_type = #{changeType}</if>
            <if test="sourceType != null "> 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="selectVolunteerAccountChangeByVolunteerAccountChangeId" parameterType="Long" resultMap="VolunteerAccountChangeResult">
        <include refid="selectVolunteerAccountChangeVo"/>
        where volunteer_account_change_id = #{volunteerAccountChangeId}
    </select>


    <select id="selectVolunteerAccountChangeListAndServiceType"
            resultType="com.leromro.core.domain.vo.VolunteerAccountChangeVO"
            parameterType="com.leromro.core.domain.VolunteerAccountChange">
        select lvac.*,
        case
            when lvac.source_type = 10 then
                (select is_pay from l_volunteer_take_record where volunteer_take_record_id = lvac.source_id)
            else
                "99"
        end as isPay
        from l_volunteer_account_change lvac
        <where>
            <if test="volunteerId != null and volunteerId != ''">and lvac.volunteer_id = #{volunteerId}</if>
            <if test="createTime != null ">
                AND lvac.create_time &gt;= #{createTime}
                AND lvac.create_time &lt; DATE_ADD(#{createTime}, INTERVAL 1 DAY)
            </if>
        </where>
    </select>

    <insert id="insertVolunteerAccountChange" parameterType="VolunteerAccountChange" useGeneratedKeys="true" keyProperty="volunteerAccountChangeId">
        insert into l_volunteer_account_change
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <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="sourceId != null">source_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="updateTime != null">update_time,</if>
            <if test="remark != null">remark,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <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="sourceId != null">#{sourceId},</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="remark != null">#{remark},</if>
         </trim>
    </insert>


    <update id="updateVolunteerAccountChange" parameterType="VolunteerAccountChange">
        update l_volunteer_account_change
        <trim prefix="SET" suffixOverrides=",">
            <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="secondOrderId != null">source_id = #{sourceId},</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="remark != null">remark = #{remark},</if>
        </trim>
        where volunteer_account_change_id = #{volunteerAccountChangeId}
    </update>

    <delete id="deleteVolunteerAccountChangeByVolunteerAccountChangeId" parameterType="Long">
        delete from l_volunteer_account_change where volunteer_account_change_id = #{volunteerAccountChangeId}
    </delete>

    <delete id="deleteVolunteerAccountChangeByVolunteerAccountChangeIds" parameterType="String">
        delete from l_volunteer_account_change where volunteer_account_change_id in 
        <foreach item="volunteerAccountChangeId" collection="array" open="(" separator="," close=")">
            #{volunteerAccountChangeId}
        </foreach>
    </delete>
    <insert id="insertList" parameterType="java.util.List">
        INSERT INTO l_volunteer_account_change
        (volunteer_id, change_type, source_type, change_money, before_balance, after_balance,source_id, create_time)
        VALUES
        <foreach collection="list" item="item" separator=",">
            (
            #{item.volunteerId},
            #{item.changeType},
            #{item.sourceType},
            #{item.changeMoney},
            #{item.beforeBalance},
            #{item.afterBalance},
            #{item.sourceId},
            #{item.createTime}
            )
        </foreach>
    </insert>
</mapper>