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

    <resultMap type="IntegralRule" id="IntegralRuleResult">
        <result property="id"    column="id"    />
        <result property="stationId"    column="station_id"    />
        <result property="stationName"    column="station_name"    />
        <result property="ruleType"    column="rule_type"    />
        <result property="termDateManage"    column="term_date_manage"    />
        <result property="emptyDate"    column="empty_date"    />
        <result property="integralActivity"    column="integral_activity"    />
        <result property="datePicker"    column="date_picker"    />
        <result property="integralProportion"    column="integral_proportion"    />
        <result property="datePickerTime"    column="date_picker_time"    />
        <result property="integralTermSetting"    column="integral_term_setting"    />
        <result property="integralEmptyTime"    column="integral_empty_time"    />
        <result property="ruleTerms"    column="rule_terms"    />
        <result property="grade"    column="grade"    />
        <result property="gradeName"    column="grade_name"    />
        <result property="oilName"    column="oil_name"    />
        <result property="saleAmt"    column="sale_amt"    />
        <result property="integral"    column="integral"    />
        <result property="oilType"    column="oil_type"    />
        <result property="integralFlag"    column="integral_flag"    />
        <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="integralFlag"    column="integral_flag"    />
        <result property="image1"    column="image1"    />
        <result property="image2"    column="image2"    />
        <result property="image3"    column="image3"    />
        <result property="image4"    column="image4"    />
        <result property="image5"    column="image5"    />
    </resultMap>

    <sql id="selectIntegralRuleVo">
       select id, station_id,d.dept_name as station_name, term_date_manage, empty_date, integral_activity,
        date_picker, integral_proportion,date_picker_time,integral_term_setting,r.create_by,r.create_time,
        r.update_by,r.update_time,d.integral_flag,r.image1,r.image2,r.image3,r.image4,r.image5,r.integral_empty_time
        from integral_rule r join sys_dept d on r.station_id =d.dept_id
    </sql>

    <select id="selectIntegralRuleList" parameterType="IntegralRule" resultMap="IntegralRuleResult">
        <include refid="selectIntegralRuleVo"/>
        <where>
            <if test="stationId != null "> and station_id = #{stationId}</if>
            <if test="stationName != null  and stationName != ''"> and station_name like concat('%', #{stationName}, '%')</if>
            <if test="termDateManage != null  and termDateManage != ''"> and term_date_manage = #{termDateManage}</if>
            <if test="emptyDate != null "> and empty_date = #{emptyDate}</if>
            <if test="integralActivity != null  and integralActivity != ''"> and integral_activity = #{integralActivity}</if>
            <if test="datePicker != null"> and date_picker = #{datePicker}</if>
            <if test="integralProportion != null  "> and integral_proportion = #{integralProportion}</if>
            <if test="datePickerTime != null "> and date_picker_time = #{datePickerTime}</if>
            <if test="integralTermSetting != null  and integralTermSetting != ''"> and integral_term_setting = #{integralTermSetting}</if>
            <if test="stationIdList != null ">
                and station_id in
                <foreach item="item" index="index" collection="stationIdList"
                         open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
        </where>
        order by  id desc
    </select>

    <select id="selectIntegralRuleById" parameterType="Long" resultMap="IntegralRuleResult">
        <include refid="selectIntegralRuleVo"/>
        where id = #{id}
    </select>

    <insert id="insertIntegralRule" parameterType="IntegralRule" useGeneratedKeys="true" keyProperty="id">
        insert into integral_rule
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="stationId != null">station_id,</if>
            <if test="stationName != null">station_name,</if>
            <if test="termDateManage != null">term_date_manage,</if>
            <if test="emptyDate != null">empty_date,</if>
            <if test="integralActivity != null">integral_activity,</if>
            <if test="datePicker != null">date_picker,</if>
            <if test="integralProportion != null">integral_proportion,</if>
            <if test="datePickerTime != null">date_picker_time,</if>
            <if test="integralTermSetting != null">integral_term_setting,</if>
            <if test="integralEmptyTime != null">integral_empty_time,</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="image1 != null">image1,</if>
            <if test="image2 != null">image2,</if>
            <if test="image3 != null">image3,</if>
            <if test="image4 != null">image4,</if>
            <if test="image4 != null">image5,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="stationId != null">#{stationId},</if>
            <if test="stationName != null">#{stationName},</if>
            <if test="termDateManage != null">#{termDateManage},</if>
            <if test="emptyDate != null">#{emptyDate},</if>
            <if test="integralActivity != null">#{integralActivity},</if>
            <if test="datePicker != null">#{datePicker},</if>
            <if test="integralProportion != null">#{integralProportion},</if>
            <if test="datePickerTime != null">#{datePickerTime},</if>
            <if test="integralTermSetting != null">#{integralTermSetting},</if>
            <if test="integralEmptyTime != null">#{integralEmptyTime},</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>
         </trim>
    </insert>

    <update id="updateIntegralRule" parameterType="IntegralRule">
        update integral_rule
        <trim prefix="SET" suffixOverrides=",">
            <if test="stationId != null">station_id = #{stationId},</if>
            <if test="stationName != null">station_name = #{stationName},</if>
            <if test="termDateManage != null">term_date_manage = #{termDateManage},</if>
            <if test="emptyDate != null">empty_date = #{emptyDate},</if>
            <if test="integralActivity != null">integral_activity = #{integralActivity},</if>
            <if test="datePicker != null">date_picker = #{datePicker},</if>
            <if test="integralProportion != null">integral_proportion = #{integralProportion},</if>
            <if test="datePickerTime != null ">  date_picker_time = #{datePickerTime},</if>
            <if test="integralTermSetting != null  ">  integral_term_setting = #{integralTermSetting},</if>
            <if test="integralEmptyTime != null"> integral_empty_time=#{integralEmptyTime},</if>
            <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>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteIntegralRuleById" parameterType="Long">
        delete from integral_rule where id = #{id}
    </delete>

    <delete id="deleteIntegralRuleByIds" parameterType="String">
        delete from integral_rule where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>
    <select id="selectIntegralRule" parameterType="IntegralRule" resultMap="IntegralRuleResult">
        <include refid="selectIntegralRuleVo"/>
        <where>
            <if test="stationId != null "> and station_id = #{stationId}</if>
            <if test="stationName != null  and stationName != ''"> and station_name like concat('%', #{stationName}, '%')</if>
            <if test="termDateManage != null  and termDateManage != ''"> and term_date_manage = #{termDateManage}</if>
            <if test="emptyDate != null "> and empty_date = #{emptyDate}</if>
            <if test="integralActivity != null  and integralActivity != ''"> and integral_activity = #{integralActivity}</if>
            <if test="datePicker != null  and datePicker != ''"> and date_picker = #{datePicker}</if>
            <if test="integralProportion != null  and integralProportion != ''"> and integral_proportion = #{integralProportion}</if>
            <if test="datePickerTime != null  and datePickerTime != ''"> and date_picker_time = #{datePickerTime}</if>
            <if test="integralTermSetting != null  and integralTermSetting != ''"> and integral_term_setting = #{integralTermSetting}</if>
            <if test="integralEmptyTime != null  and integralEmptyTime != ''"> and integral_empty_time = #{integralEmptyTime}</if>
            <if test="stationIdList != null ">
                and station_id in
                <foreach item="item" index="index" collection="stationIdList"
                         open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
        </where>
        order by  id desc limit 1
    </select>

    <select id="listRuleInfo" parameterType="IntegralRule" resultMap="IntegralRuleResult">
        SELECT a.station_id,d.dept_name as station_name,a.term_date_manage,a.integral_activity,empty_date,date_picker,integral_proportion,s.grade as grade_name,
        b.rule_type,rule_terms,b.grade,b.oil_name,b.sale_amt,b.integral,b.oil_type,c.integral_flag,a.integral_empty_time
        from integral_rule a
        join integral_rule_detail b on a.id =b.parent_id
        join station_pay c on a.station_id =c.station_id
        join sys_dept d on a.station_id =d.dept_id
        left join customer_grade_setting s on s.id = b.grade
        <where>
            <if test="stationId != null "> and a.station_id = #{stationId}</if>
            <if test="stationName != null  and stationName != ''"> and d.dept_name like concat('%', #{stationName}, '%')</if>
            <if test="stationIdList != null ">
                and a.station_id in
                <foreach item="item" index="index" collection="stationIdList"
                         open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
        </where>
        order by  b.oil_name desc
    </select>
</mapper>
