<?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.frequency.givewallerecord.mapper.GiveWalletRecordMapper">

    <resultMap type="com.ruoyi.frequency.givewallerecord.entity.GiveWalletRecord" id="GiveWalletRecordResult">
        <result property="id"    column="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="delFlag"    column="del_flag"    />
        <result property="userType"    column="user_type"    />
        <result property="recordType"    column="record_type"    />
        <result property="price"    column="price"    />
        <result property="userId"    column="user_id"    />
        <result property="objectId"    column="object_id"    />
        <result property="status"    column="status"    />
        <result property="canWithTime"    column="can_with_time"    />
        <result property="noWithTime"    column="no_with_time"    />
        <result property="cancelTime"    column="cancel_time"    />
        <result property="currentBalance"    column="current_balance"    />
        <result property="remark"    column="remark"    />
        <association property="userInfo"    column="user_id" javaType="com.ruoyi.vo.UserInfo" resultMap="userInfo" />
    </resultMap>

    <resultMap id="userInfo" type="com.ruoyi.vo.UserInfo">
        <id     property="userId"    column="user_id"     />
        <result property="userType"  column="user_type"   />
        <result property="idNo"  column="id_no"   />
        <result property="nickname"  column="nickname"   />
        <result property="avatar" column="avatar"   />
        <result property="mobile" column="mobile"   />
    </resultMap>
    
    
    <sql id="selectGiveWalletRecordVo">
        select
            t.id ,
            t.create_by ,
            t.create_time ,
            t.update_by ,
            t.update_time ,
            t.del_flag ,
            t.user_type ,
            t.record_type ,
            t.price ,
            t.user_id ,
            t.object_id ,
            t.status ,
            t.can_with_time ,
            t.no_with_time ,
            t.cancel_time ,
            t.current_balance ,
            t.remark ,
            u.user_type,
            u.id_no,
            u.mobile,
            u.avatar,
            u.nickname
        from t_give_wallet_record AS t
                LEFT JOIN t_user AS u ON t.user_id = u.id
    </sql>

    <select id="selectGiveWalletRecordList" parameterType="com.ruoyi.frequency.givewallerecord.entity.GiveWalletRecord" resultMap="GiveWalletRecordResult">
        <include refid="selectGiveWalletRecordVo"/>
        <where> AND t.del_flag != 2
            <if test="userType != null "> and t.user_type = #{userType}</if>
            <if test="recordType != null "> and t.record_type = #{recordType}</if>
            <if test="price != null "> and t.price = #{price}</if>
            <if test="userId != null "> and t.user_id = #{userId}</if>
            <if test="objectId != null "> and t.object_id = #{objectId}</if>
            <if test="status != null "> and t.status = #{status}</if>
            <if test="canWithTime != null "> and t.can_with_time = #{canWithTime}</if>
            <if test="noWithTime != null "> and t.no_with_time = #{noWithTime}</if>
            <if test="cancelTime != null "> and t.cancel_time = #{cancelTime}</if>
            <if test="currentBalance != null "> and t.current_balance = #{currentBalance}</if>
            <if test="delFlag != null and delFlag != ''"> and t.del_flag = #{delFlag}</if>
        </where>
        order by t.create_time desc
    </select>

    <select id="selectGiveWalletRecordById" parameterType="Long" resultMap="GiveWalletRecordResult">
        <include refid="selectGiveWalletRecordVo"/>
        where t.id = #{id}
    </select>
    <select id="giveWalletPage" resultType="com.ruoyi.controller.resp.WalletPageResp">
        SELECT
        t.id,
        t.object_id,
        t.create_time,
        t.price,
        t.record_type,
        t.current_balance,
        CASE
        t.record_type
        WHEN 1 THEN
        '' ELSE ''
        END record,
        o.order_no
        FROM
        t_give_wallet_record t
        left join t_order o on t.object_id = o.id
        WHERE
        t.del_flag = 1
        AND t.user_id = #{userVo.userId}
        AND t.user_type = #{userVo.userType}
        <if test="req.type != null and req.type != '' ">

            <if test="req.type == 1 ">
                and t.price > 0
            </if>
            <if test="req.type == 2 ">
                and t.price &lt; 0
            </if>
        </if>
        <if test="req.beginTime != null ">AND t.create_time >=
            #{req.beginTime}
        </if>
        <if test="req.endTime != null ">AND t.create_time &lt;
            #{req.endTime}
        </if>
        ORDER BY
        t.create_time DESC
    </select>

    <insert id="insertGiveWalletRecord" parameterType="com.ruoyi.frequency.givewallerecord.entity.GiveWalletRecord">
        insert into t_give_wallet_record
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="id != null">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="delFlag != null">del_flag,</if>
            <if test="userType != null">user_type,</if>
            <if test="recordType != null">record_type,</if>
            <if test="price != null">price,</if>
            <if test="userId != null">user_id,</if>
            <if test="objectId != null">object_id,</if>
            <if test="status != null">status,</if>
            <if test="canWithTime != null">can_with_time,</if>
            <if test="noWithTime != null">no_with_time,</if>
            <if test="cancelTime != null">cancel_time,</if>
            <if test="currentBalance != null">current_balance,</if>
            <if test="remark != null">remark,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="id != null">#{id},</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="userType != null">#{userType},</if>
            <if test="recordType != null">#{recordType},</if>
            <if test="price != null">#{price},</if>
            <if test="userId != null">#{userId},</if>
            <if test="objectId != null">#{objectId},</if>
            <if test="status != null">#{status},</if>
            <if test="canWithTime != null">#{canWithTime},</if>
            <if test="noWithTime != null">#{noWithTime},</if>
            <if test="cancelTime != null">#{cancelTime},</if>
            <if test="currentBalance != null">#{currentBalance},</if>
            <if test="remark != null">#{remark},</if>
        </trim>
    </insert>

    <update id="updateGiveWalletRecord" parameterType="com.ruoyi.frequency.givewallerecord.entity.GiveWalletRecord">
        update t_give_wallet_record
        <trim prefix="SET" suffixOverrides=",">
            <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="userType != null">user_type = #{userType},</if>
            <if test="recordType != null">record_type = #{recordType},</if>
            <if test="price != null">price = #{price},</if>
            <if test="userId != null">user_id = #{userId},</if>
            <if test="objectId != null">object_id = #{objectId},</if>
            <if test="status != null">status = #{status},</if>
            <if test="canWithTime != null">can_with_time = #{canWithTime},</if>
            <if test="noWithTime != null">no_with_time = #{noWithTime},</if>
            <if test="cancelTime != null">cancel_time = #{cancelTime},</if>
            <if test="currentBalance != null">current_balance = #{currentBalance},</if>
            <if test="remark != null">remark = #{remark},</if>
        </trim>
        where id = #{id}
    </update>


</mapper>