<?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.integralrecord.mapper.IntegralRecordMapper">

    <resultMap type="com.ruoyi.frequency.integralrecord.entity.IntegralRecord" id="IntegralRecordResult">
        <result property="id"    column="id"    />
        <result property="delFlag"    column="del_flag"    />
        <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="integral"    column="integral"    />
        <result property="currentBalance"    column="current_balance"    />
        <result property="record"    column="record"    />
        <result property="userType"    column="user_type"    />
        <result property="remark"    column="remark"    />
        <result property="userId"    column="user_id"    />
        <result property="objectId"    column="object_id"    />
        <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="selectIntegralRecordVo">
        SELECT
            t.id,
            t.del_flag,
            t.create_by,
            t.create_time,
            t.update_by,
            t.update_time,
            t.integral,
            t.current_balance,
            t.record,
            t.user_type,
            t.user_id,
            t.object_id,
            t.remark,
            u.id,
            u.mobile,
            u.avatar,
            u.nickname
        FROM
            t_integral_record AS t
                LEFT JOIN t_customer AS u ON t.user_id = u.id
    </sql>

    <select id="selectIntegralRecordList" parameterType="com.ruoyi.frequency.integralrecord.entity.IntegralRecord" resultMap="IntegralRecordResult">
        <include refid="selectIntegralRecordVo"/>
        <where> AND t.del_flag != 2
            <if test="integral != null "> and t.integral = #{integral}</if>
            <if test="currentBalance != null "> and t.current_balance = #{currentBalance}</if>
            <if test="record != null "> and t.record = #{record}</if>
            <if test="record != null "> and t.record = #{record}</if>
            <if test="userType != null "> and t.user_type = #{userType}</if>
            <if test="userId != null "> and t.user_id = #{userId}</if>
            <if test="objectId != null "> and t.object_id = #{objectId}</if>
            <if test="delFlag != null and delFlag != ''"> and t.del_flag = #{delFlag}</if>
            <if test="remark != null "> and t.remark like concat('%', #{remark}, '%')</if>
            <if test="beginTime != null ">
                and  t.create_time >= #{beginTime}

            </if>
            <if test="endTime != null ">
                and  t.create_time &lt; #{endTime}
            </if>
            <if test="recordTypeList!=null and recordTypeList.size()>0">
                and t.record in
                <foreach item="recordType" collection="recordTypeList" open="(" separator="," close=")">
                    #{recordType}
                </foreach>
            </if>
        </where>
        order by t.create_time desc
    </select>

    <select id="selectIntegralRecordById" parameterType="Long" resultMap="IntegralRecordResult">
        <include refid="selectIntegralRecordVo"/>
        where t.id = #{id}
    </select>

    <insert id="insertIntegralRecord" parameterType="com.ruoyi.frequency.integralrecord.entity.IntegralRecord">
        insert into t_integral_record
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="id != null">id,</if>
            <if test="delFlag != null">del_flag,</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="integral != null">integral,</if>
            <if test="currentBalance != null">current_balance,</if>
            <if test="record != null">record,</if>
            <if test="userType != null">user_type,</if>
            <if test="userId != null">user_id,</if>
            <if test="objectId != null">object_id,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="id != null">#{id},</if>
            <if test="delFlag != null">#{delFlag},</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="integral != null">#{integral},</if>
            <if test="currentBalance != null">#{currentBalance},</if>
            <if test="record != null">#{record},</if>
            <if test="userType != null">#{userType},</if>
            <if test="userId != null">#{userId},</if>
            <if test="objectId != null">#{objectId},</if>
        </trim>
    </insert>

    <update id="updateIntegralRecord" parameterType="com.ruoyi.frequency.integralrecord.entity.IntegralRecord">
        update t_integral_record
        <trim prefix="SET" suffixOverrides=",">
            <if test="delFlag != null">del_flag = #{delFlag},</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="integral != null">integral = #{integral},</if>
            <if test="currentBalance != null">current_balance = #{currentBalance},</if>
            <if test="record != null">record = #{record},</if>
            <if test="userType != null">user_type = #{userType},</if>
            <if test="userId != null">user_id = #{userId},</if>
            <if test="objectId != null">object_id = #{objectId},</if>
        </trim>
        where id = #{id}
    </update>

    <select id="integralPage" resultType="com.ruoyi.controller.resp.IntegralPageResp">
        SELECT
            t.id,
            t.current_balance,
            t.integral,
            t.record,
            t.create_time
        FROM
            t_integral_record t
        WHERE
            t.del_flag = 1
          AND t.user_id = #{userVo.userId}
          AND t.user_type = #{userVo.userType}
        ORDER BY t.create_time DESC
    </select>
</mapper>
