<?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.yijia.integral.mapper.IntegralRuleDetailMapper">

    <resultMap type="IntegralRuleDetail" id="IntegralRuleDetailResult">
        <result property="id"    column="id"    />
        <result property="parentId"    column="parent_id"    />
        <result property="ruleTerms"    column="rule_terms"    />
        <result property="grade"    column="grade"    />
        <result property="oilName"    column="oil_name"    />
        <result property="saleAmt"    column="sale_amt"    />
        <result property="integral"    column="integral"    />
        <result property="ruleType"    column="rule_type"    />
        <result property="oilType"    column="oil_type"    />
    </resultMap>

    <sql id="selectIntegralRuleDetailVo">
        select id, parent_id, rule_terms, grade, oil_name, sale_amt, integral, rule_type,oil_type from integral_rule_detail
    </sql>

    <select id="selectIntegralRuleDetailList" parameterType="IntegralRuleDetail" resultMap="IntegralRuleDetailResult">
        select id, parent_id, rule_terms,grade, oil_name, sale_amt, integral, rule_type , d.dict_value as oil_type
        from integral_rule_detail r join sys_dict_data d on r.oil_name = d.dict_label and dict_type = "oil_name"
        <where>
            <if test="parentId != null "> and parent_id = #{parentId}</if>
            <if test="ruleTerms != null "> and rule_terms = #{ruleTerms}</if>
            <if test="grade != null  and grade != ''"> and grade = #{grade}</if>
            <if test="oilName != null  and oilName != ''"> and oil_name like concat('%', #{oilName}, '%')</if>
            <if test="saleAmt != null "> and sale_amt = #{saleAmt}</if>
            <if test="integral != null "> and integral = #{integral}</if>
            <if test="ruleType != null  and ruleType != ''"> and rule_type = #{ruleType}</if>
        </where>
    </select>
    <select id="selectIntegralRuleDetailById" parameterType="Long" resultMap="IntegralRuleDetailResult">
        <include refid="selectIntegralRuleDetailVo"/>
        where id = #{id}
    </select>

    <insert id="insertIntegralRuleDetail" parameterType="IntegralRuleDetail" useGeneratedKeys="true" keyProperty="id">
        insert into integral_rule_detail
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="parentId != null">parent_id,</if>
            <if test="ruleTerms != null">rule_terms,</if>
            <if test="grade != null">grade,</if>
            <if test="oilName != null">oil_name,</if>
            <if test="saleAmt != null">sale_amt,</if>
            <if test="integral != null">integral,</if>
            <if test="ruleType != null">rule_type,</if>
            <if test="oilType != null">oil_type,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="parentId != null">#{parentId},</if>
            <if test="ruleTerms != null">#{ruleTerms},</if>
            <if test="grade != null">#{grade},</if>
            <if test="oilName != null">#{oilName},</if>
            <if test="saleAmt != null">#{saleAmt},</if>
            <if test="integral != null">#{integral},</if>
            <if test="ruleType != null">#{ruleType},</if>
            <if test="oilType != null">#{oilType},</if>
         </trim>
    </insert>

    <update id="updateIntegralRuleDetail" parameterType="IntegralRuleDetail">
        update integral_rule_detail
        <trim prefix="SET" suffixOverrides=",">
            <if test="parentId != null">parent_id = #{parentId},</if>
            <if test="ruleTerms != null">rule_terms = #{ruleTerms},</if>
            <if test="grade!= null">grade = #{grade},</if>
            <if test="oilName != null">oil_name = #{oilName},</if>
            <if test="saleAmt != null">sale_amt = #{saleAmt},</if>
            <if test="integral != null">integral = #{integral},</if>
            <if test="ruleType != null">rule_type = #{ruleType},</if>
            <if test="oilType != null">oil_type = #{oilType},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteIntegralRuleDetailById" parameterType="Long">
        delete from integral_rule_detail where id = #{id}
    </delete>

    <delete id="deleteIntegralRuleDetailByIds" parameterType="String">
        delete from integral_rule_detail where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>
    <delete id="deleteIntegralRuleDetail" parameterType="Long">
        delete from integral_rule_detail
        <where>
            <if test="parentId != null "> and parent_id = #{parentId}</if>
            <if test="ruleTerms != null "> and rule_terms = #{ruleTerms}</if>
            <if test="grade != null  and grade != ''"> and grade = #{grade}</if>
            <if test="oilName != null  and oilName != ''"> and oil_name like concat('%', #{oilName}, '%')</if>
            <if test="saleAmt != null "> and sale_amt = #{saleAmt}</if>
            <if test="integral != null "> and integral = #{integral}</if>
            <if test="ruleType != null  and ruleType != ''"> and rule_type = #{ruleType}</if>
        </where>
    </delete>
</mapper>
