<?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.platformrecord.mapper.PlatformRecordMapper">

    <resultMap type="com.ruoyi.frequency.platformrecord.entity.PlatformRecord" id="PlatformRecordResult">
        <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="price"    column="price"    />
        <result property="objectId"    column="object_id"    />
        <result property="objectType"    column="object_type"    />
        <result property="userId"    column="user_id"    />
        <result property="userType"    column="user_type"    />
        <result property="balance"    column="balance"    />
        <result property="beforeBalance"    column="before_balance"    />
    </resultMap>

    <sql id="selectPlatformRecordVo">
        select
            t.id ,
            t.create_by ,
            t.create_time ,
            t.update_by ,
            t.update_time ,
            t.del_flag ,
            t.price ,
            t.object_id ,
            t.object_type ,
            t.user_id ,
            t.user_type ,
            t.balance ,
            t.before_balance
        from t_platform_record AS t
                 LEFT JOIN t_store s on t.user_id = s.id
                 LEFT JOIN t_customer c on t.user_id = c.id
                 LEFT JOIN t_sale sa on t.user_id = sa.id
    </sql>

    <select id="selectPlatformRecordList" parameterType="com.ruoyi.frequency.platformrecord.entity.PlatformRecord" resultMap="PlatformRecordResult">
        <include refid="selectPlatformRecordVo"/>
        <where> AND t.del_flag != 2
            <if test="price != null "> and t.price = #{price}</if>
            <if test="objectId != null "> and t.object_id = #{objectId}</if>
            <if test="objectType != null "> and t.object_type = #{objectType}</if>
            <if test="userId != null "> and t.user_id = #{userId}</if>
            <if test="userType != null "> and t.user_type = #{userType}</if>
            <if test="balance != null "> and t.balance = #{balance}</if>
            <if test="beforeBalance != null "> and t.before_balance = #{beforeBalance}</if>
            <if test="delFlag != null and delFlag != ''"> and t.del_flag = #{delFlag}</if>
            <if test="type != null and type != ''">
                <if test="type == 1">
                    and t.price > 0
                </if>
            </if>
            <if test="userInfo != null and userInfo.nickname != null  and userInfo.nickname != '' ">
                and (
                s.nickname like concat('%', #{userInfo.nickname}, '%')
                or
                c.nickname like concat('%', #{userInfo.nickname}, '%')
                or
                sa.nickname like concat('%', #{userInfo.nickname}, '%')
                )

            </if>
            <if test="userInfo != null and  userInfo.mobile != null and  userInfo.mobile != '' ">
                and (
                s.mobile like concat('%', #{userInfo.mobile}, '%')
                or
                c.mobile like concat('%', #{userInfo.mobile}, '%')
                or
                sa.mobile like concat('%', #{userInfo.mobile}, '%')
                )
            </if>
            <if test="beginTime != null ">
                and  t.create_time >= #{beginTime}

            </if>
            <if test="endTime != null ">
                and  t.create_time &lt; #{endTime}
            </if>
        </where>
        order by t.create_time desc
    </select>

    <select id="statistics" parameterType="com.ruoyi.frequency.platformrecord.entity.PlatformRecord" resultType="com.ruoyi.frequency.platformrecord.model.result.PlatformRecordCount">
        select
        IFNULL(SUM(t.price),0) price
        from t_platform_record AS t
        where
        t.id in
        <foreach item="id" collection="list" open="(" separator="," close=")">
            #{id}
        </foreach>
    </select>

    <select id="selectPlatformRecordById" parameterType="Long" resultMap="PlatformRecordResult">
        <include refid="selectPlatformRecordVo"/>
        where t.id = #{id}
    </select>

    <insert id="insertPlatformRecord" parameterType="com.ruoyi.frequency.platformrecord.entity.PlatformRecord">
        insert into t_platform_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="price != null">price,</if>
            <if test="objectId != null">object_id,</if>
            <if test="objectType != null">object_type,</if>
            <if test="userId != null">user_id,</if>
            <if test="userType != null">user_type,</if>
            <if test="balance != null">balance,</if>
            <if test="beforeBalance != null">before_balance,</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="price != null">#{price},</if>
            <if test="objectId != null">#{objectId},</if>
            <if test="objectType != null">#{objectType},</if>
            <if test="userId != null">#{userId},</if>
            <if test="userType != null">#{userType},</if>
            <if test="balance != null">#{balance},</if>
            <if test="beforeBalance != null">#{beforeBalance},</if>
        </trim>
    </insert>

    <update id="updatePlatformRecord" parameterType="com.ruoyi.frequency.platformrecord.entity.PlatformRecord">
        update t_platform_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="price != null">price = #{price},</if>
            <if test="objectId != null">object_id = #{objectId},</if>
            <if test="objectType != null">object_type = #{objectType},</if>
            <if test="userId != null">user_id = #{userId},</if>
            <if test="userType != null">user_type = #{userType},</if>
            <if test="balance != null">balance = #{balance},</if>
            <if test="beforeBalance != null">before_balance = #{beforeBalance},</if>
        </trim>
        where id = #{id}
    </update>


    <select id="platformAmountCount" resultType="java.math.BigDecimal">
        SELECT
            IFNULL( SUM( t.price ), 0 )
        FROM
            t_platform_record t
    </select>
</mapper>
