<?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.mall.mapper.MallMemberIntegralMapper">

    <resultMap type="MallMemberIntegral" id="MallMemberIntegralResult">
        <result property="integralId"    column="integral_id"    />
        <result property="userId"    column="user_id"    />
        <result property="integral"    column="integral"    />
        <result property="mark"    column="mark"    />
        <result property="type"    column="type"    />
        <result property="sendType"    column="send_type"    />
        <result property="sendId"    column="send_id"    />
        <result property="resId"    column="res_id"    />
        <result property="resName"    column="res_name"    />
        <result property="createAt"    column="create_at"    />
        <result property="updateAt"    column="update_at"    />
        <result property="delFlag"    column="del_flag"    />
    </resultMap>

    <sql id="selectMallMemberIntegralVo">
        select integral_id, user_id, integral, mark, type, send_type, send_id, res_id, res_name, create_at, update_at, del_flag from mall_member_integral
    </sql>

    <select id="selectMallMemberIntegralList" parameterType="MallMemberIntegral" resultMap="MallMemberIntegralResult">
        <include refid="selectMallMemberIntegralVo"/>
        <where>
            <if test="userId != null "> and user_id = #{userId}</if>
            <if test="integral != null "> and integral = #{integral}</if>
            <if test="mark != null "> and mark = #{mark}</if>
            <if test="type != null "> and type = #{type}</if>
            <if test="sendType != null "> and send_type = #{sendType}</if>
            <if test="sendId != null "> and send_id = #{sendId}</if>
            <if test="resId != null "> and res_id = #{resId}</if>
            <if test="resName != null  and resName != ''"> and res_name like concat('%', #{resName}, '%')</if>
            <if test="createAt != null "> and create_at = #{createAt}</if>
            <if test="updateAt != null "> and update_at = #{updateAt}</if>
            <if test="delFlag != null "> and del_flag = #{delFlag}</if>
        </where>
    </select>

    <select id="selectMallMemberIntegralByIntegralId" parameterType="Long" resultMap="MallMemberIntegralResult">
        <include refid="selectMallMemberIntegralVo"/>
        where integral_id = #{integralId}
    </select>

    <insert id="insertMallMemberIntegral" parameterType="MallMemberIntegral" useGeneratedKeys="true" keyProperty="integralId">
        insert into mall_member_integral
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="userId != null">user_id,</if>
            <if test="integral != null">integral,</if>
            <if test="mark != null">mark,</if>
            <if test="type != null">type,</if>
            <if test="sendType != null">send_type,</if>
            <if test="sendId != null">send_id,</if>
            <if test="resId != null">res_id,</if>
            <if test="resName != null">res_name,</if>
            <if test="createAt != null">create_at,</if>
            <if test="updateAt != null">update_at,</if>
            <if test="delFlag != null">del_flag,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="userId != null">#{userId},</if>
            <if test="integral != null">#{integral},</if>
            <if test="mark != null">#{mark},</if>
            <if test="type != null">#{type},</if>
            <if test="sendType != null">#{sendType},</if>
            <if test="sendId != null">#{sendId},</if>
            <if test="resId != null">#{resId},</if>
            <if test="resName != null">#{resName},</if>
            <if test="createAt != null">#{createAt},</if>
            <if test="updateAt != null">#{updateAt},</if>
            <if test="delFlag != null">#{delFlag},</if>
         </trim>
    </insert>

    <update id="updateMallMemberIntegral" parameterType="MallMemberIntegral">
        update mall_member_integral
        <trim prefix="SET" suffixOverrides=",">
            <if test="userId != null">user_id = #{userId},</if>
            <if test="integral != null">integral = #{integral},</if>
            <if test="mark != null">mark = #{mark},</if>
            <if test="type != null">type = #{type},</if>
            <if test="sendType != null">send_type = #{sendType},</if>
            <if test="sendId != null">send_id = #{sendId},</if>
            <if test="resId != null">res_id = #{resId},</if>
            <if test="resName != null">res_name = #{resName},</if>
            <if test="createAt != null">create_at = #{createAt},</if>
            <if test="updateAt != null">update_at = #{updateAt},</if>
            <if test="delFlag != null">del_flag = #{delFlag},</if>
        </trim>
        where integral_id = #{integralId}
    </update>

    <delete id="deleteMallMemberIntegralByIntegralId" parameterType="Long">
        delete from mall_member_integral where integral_id = #{integralId}
    </delete>

    <delete id="deleteMallMemberIntegralByIntegralIds" parameterType="String">
        delete from mall_member_integral where integral_id in
        <foreach item="integralId" collection="array" open="(" separator="," close=")">
            #{integralId}
        </foreach>
    </delete>

    <insert id="bachInsertMallMemberIntegral">
        insert into mall_member_integral(user_id, integral, mark, type, send_type, send_id, res_id, create_at, del_flag) values
        <foreach item="item" index="index" collection="list" separator=",">
            (#{item.userId},#{item.integral},#{item.mark},#{item.type},#{item.sendType},#{item.sendId},#{item.resId}},#{item.createAt}, 0)
        </foreach>
        where item.resId NOT IN (select res_id from mall_member_integral)
    </insert>

    <select id="selectMallMemberIntegralByUserId" resultType="java.math.BigDecimal">
        select ifnull(sum(integral), 0) from mall_member_integral
        where user_id = #{userId}
        and type = #{type}
        and del_flag =0
    </select>

    <select id="getUserAllIntegral" resultType="java.math.BigDecimal">
        select ifnull(sum(payment), 0)
        from mall_order_order
        where user_id = #{userId}
            and status = '4'
            and del_flag = '0'
    </select>
</mapper>
