<?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.ruixuan.isc.mapper.KuaishouRuleDetailMapper">

    <resultMap type="com.ruixuan.isc.entity.KuaishouRuleDetail" id="KuaishouRuleDetailResult">
        <result property="id" column="id"/>
        <result property="ruleId" column="rule_id"/>
        <result property="startItemCount" column="start_item_count"/>
        <result property="endItemCount" column="end_item_count"/>
        <result property="anchorAmount" column="anchor_amount"/>
        <result property="referenceAmount" column="reference_amount"/>
        <result property="sortCount" column="sort_count"/>
        <result property="createTime" column="create_time"/>
        <result property="updateTime" column="update_time"/>
    </resultMap>

    <sql id="selectKuaishouRuleDetailVo">
        select id, rule_id, start_item_count, end_item_count, anchor_amount, reference_amount, sort_count, create_time, update_time from kuaishou_rule_detail
    </sql>

    <select id="selectKuaishouRuleDetailList" parameterType="com.ruixuan.isc.entity.KuaishouRuleDetail"
            resultMap="KuaishouRuleDetailResult">
        <include refid="selectKuaishouRuleDetailVo"/>
        <where>
            <if test="ruleId != null  and ruleId != ''">and rule_id = #{ruleId}</if>
            <if test="startItemCount != null ">and start_item_count = #{startItemCount}</if>
            <if test="endItemCount != null ">and end_item_count = #{endItemCount}</if>
            <if test="anchorAmount != null ">and anchor_amount = #{anchorAmount}</if>
            <if test="referenceAmount != null ">and reference_amount = #{referenceAmount}</if>
            <if test="sortCount != null ">and sort_count = #{sortCount}</if>
        </where>
    </select>

    <select id="selectKuaishouRuleDetailById" parameterType="java.lang.Long" resultMap="KuaishouRuleDetailResult">
        <include refid="selectKuaishouRuleDetailVo"/>
        where id = #{id}
    </select>
    <select id="getRuleDetailByRuleId" resultMap="KuaishouRuleDetailResult"
            parameterType="java.lang.Long">
        <include refid="selectKuaishouRuleDetailVo"/>
        where rule_id = #{ruleId}
        order by sort_count asc
    </select>
    <insert id="insertKuaishouRuleDetail" parameterType="com.ruixuan.isc.entity.KuaishouRuleDetail">
        insert into kuaishou_rule_detail
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="id != null">id,</if>
            <if test="ruleId != null">rule_id,</if>
            <if test="startItemCount != null">start_item_count,</if>
            <if test="endItemCount != null">end_item_count,</if>
            <if test="anchorAmount != null">anchor_amount,</if>
            <if test="referenceAmount != null">reference_amount,</if>
            <if test="sortCount != null">sort_count,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateTime != null">update_time,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="id != null">#{id},</if>
            <if test="ruleId != null">#{ruleId},</if>
            <if test="startItemCount != null">#{startItemCount},</if>
            <if test="endItemCount != null">#{endItemCount},</if>
            <if test="anchorAmount != null">#{anchorAmount},</if>
            <if test="referenceAmount != null">#{referenceAmount},</if>
            <if test="sortCount != null">#{sortCount},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateTime != null">#{updateTime},</if>
        </trim>
    </insert>
    <insert id="addAll">

        replace into kuaishou_rule_detail (
        rule_id,
        start_item_count,
        end_item_count,
        anchor_amount,
        reference_amount,
        sort_count
        ) values
        <foreach collection="adds" item="add" separator=",">
            (
            #{add.ruleId},
            #{add.startItemCount},
            #{add.endItemCount},
            #{add.anchorAmount},
            #{add.referenceAmount},
            #{add.sortCount}
            )
        </foreach>

    </insert>

    <update id="updateKuaishouRuleDetail" parameterType="com.ruixuan.isc.entity.KuaishouRuleDetail">
        update kuaishou_rule_detail
        <trim prefix="SET" suffixOverrides=",">
            <if test="ruleId != null">rule_id = #{ruleId},</if>
            <if test="startItemCount != null">start_item_count = #{startItemCount},</if>
            <if test="endItemCount != null">end_item_count = #{endItemCount},</if>
            <if test="anchorAmount != null">anchor_amount = #{anchorAmount},</if>
            <if test="referenceAmount != null">reference_amount = #{referenceAmount},</if>
            <if test="sortCount != null">sort_count = #{sortCount},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteKuaishouRuleDetailById" parameterType="java.lang.Long">
        delete from kuaishou_rule_detail where id = #{id}
    </delete>

    <delete id="deleteKuaishouRuleDetailByIds" parameterType="java.lang.String">
        delete from kuaishou_rule_detail where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>
    <delete id="deleteKuaishouRuleDetailByRuleId" parameterType="java.lang.Long">
        delete  from kuaishou_rule_detail where rule_id = #{id}
    </delete>

    <select id="getPromoterRuleList" resultType="com.alibaba.fastjson.JSONObject">
        select id as 'id',
        rule_name as 'ruleName',
        rule_type as 'ruleType',
        user_id as 'userId',
        user_name as 'userName',
        create_time as 'createTime'
        from kuaishou_settlement_rules
        where 1= 1
        <if test="userId != null and userId != ''">
            and user_id = #{userId}
        </if>
        <if test="ruleName != null and ruleName != ''">
            and rule_name like concat('%',#{ruleName}, '%')
        </if>
        and rule_type = 3
        order by create_time desc
    </select>
    <select id="getAllRuleList" resultType="com.alibaba.fastjson.JSONObject" parameterType="java.lang.Long">

        select * from (
        select id as 'id',
        rule_name as 'ruleName',
        rule_type as 'ruleType',
        user_id as 'userId',
        user_name as 'userName',
        create_time as 'createTime'
        from kuaishou_settlement_rules
        where id = 1
        <if test="ruleName != null and ruleName != ''">
            and rule_name like concat('%',#{ruleName}, '%')
        </if>
        union all
        select id as 'id',
        rule_name as 'ruleName',
        rule_type as 'ruleType',
        user_id as 'userId',
        user_name as 'userName',
        create_time as 'createTime'
        from kuaishou_settlement_rules
        where 1= 1
        <if test="userId != null and userId != ''">
            and user_id = #{userId}
        </if>
        <if test="ruleName != null and ruleName != ''">
            and rule_name like concat('%',#{ruleName}, '%')
        </if>
        order by createTime desc
        )
        t group by t.id
    </select>
</mapper>