<?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.KuaishouSettlementRulesMapper">

    <resultMap type="com.ruixuan.isc.entity.KuaishouSettlementRules" id="KuaishouSettlementRulesResult">
        <result property="id" column="id"/>
        <result property="ruleName" column="rule_name"/>
        <result property="ruleType" column="rule_type"/>
        <result property="userId" column="user_id"/>
        <result property="userName" column="user_name"/>
        <result property="createTime" column="create_time"/>
        <result property="updateTime" column="update_time"/>
    </resultMap>

    <sql id="selectKuaishouSettlementRulesVo">
        select id, rule_name, rule_type, user_id, user_name, create_time, update_time from kuaishou_settlement_rules
    </sql>

    <select id="selectKuaishouSettlementRulesList" parameterType="com.ruixuan.isc.entity.KuaishouSettlementRules"
            resultMap="KuaishouSettlementRulesResult">
        <include refid="selectKuaishouSettlementRulesVo"/>
        <where>
            <if test="ruleName != null  and ruleName != ''">and rule_name like concat('%', #{ruleName}, '%')</if>
            <if test="ruleType != null ">and rule_type = #{ruleType}</if>
            <if test="userId != null ">and user_id = #{userId}</if>
            <if test="userName != null  and userName != ''">and user_name like concat('%', #{userName}, '%')</if>
        </where>
    </select>

    <select id="selectKuaishouSettlementRulesById" parameterType="java.lang.Long"
            resultMap="KuaishouSettlementRulesResult">
        <include refid="selectKuaishouSettlementRulesVo"/>
        where id = #{id}
    </select>




    <select id="getRuleList" 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>
        and rule_type in(1,2)
        order by createTime desc
        ) t group by t.id

    </select>
    <select id="getMaxRuleId" resultType="java.lang.Long">
        select  max(id) from kuaishou_settlement_rules
    </select>

    <insert id="insertKuaishouSettlementRules" parameterType="com.ruixuan.isc.entity.KuaishouSettlementRules">
        insert into kuaishou_settlement_rules
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="id != null">id,</if>
            <if test="ruleName != null">rule_name,</if>
            <if test="ruleType != null">rule_type,</if>
            <if test="userId != null">user_id,</if>
            <if test="userName != null">user_name,</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="ruleName != null">#{ruleName},</if>
            <if test="ruleType != null">#{ruleType},</if>
            <if test="userId != null">#{userId},</if>
            <if test="userName != null">#{userName},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateTime != null">#{updateTime},</if>
        </trim>
    </insert>

    <update id="updateKuaishouSettlementRules" parameterType="com.ruixuan.isc.entity.KuaishouSettlementRules">
        update kuaishou_settlement_rules
        <trim prefix="SET" suffixOverrides=",">
            <if test="ruleName != null">rule_name = #{ruleName},</if>
            <if test="ruleType != null">rule_type = #{ruleType},</if>
            <if test="userId != null">user_id = #{userId},</if>
            <if test="userName != null">user_name = #{userName},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
        </trim>
        where id = #{id}
    </update>

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

    <delete id="deleteKuaishouSettlementRulesByIds" parameterType="java.lang.String">
        delete from kuaishou_settlement_rules where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>
</mapper>