<?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.market.mapper.MarkertPlanMapper">

    <resultMap type="MarkertPlan" id="MarkertPlanResult">
        <result property="id"    column="id"    />
        <result property="grade"    column="grade"    />
        <result property="discountTerm"    column="discount_term"    />
        <result property="discountAmt"    column="discount_amt"    />
        <result property="gasoilDiscountAmt"    column="gasoil_discount_amt"    />
        <result property="vipDiscountyPlus"    column="vip_discounty_plus"    />
        <result property="couponPlus"    column="coupon_plus"    />
        <result property="discountPlanType"    column="discount_plan_type"    />
        <result property="oilName"    column="oil_name"    />
        <result property="stationId"    column="station_id"    />
        <result property="stationName"    column="station_name"    />
        <result property="status"    column="status"    />
        <result property="createBy"    column="create_by"    />
        <result property="createTime"    column="create_time"  />
        <result property="updateBy"    column="update_by"    />
        <result property="updateTime"    column="update_time"    />
    </resultMap>

    <sql id="selectMarkertPlanVo">
       select p.id, p.grade, p.discount_term, p.discount_amt, p.gasoil_discount_amt, p.vip_discounty_plus, p.coupon_plus, p.discount_plan_type,
         p.oil_name, p.station_id,d.dept_name as station_name, p.status,p.create_by,p.create_time,p.update_by,p.update_time
          from markert_plan p join sys_dept d on p.station_id = d.dept_id
    </sql>

    <select id="selectMarkertPlanList" parameterType="MarkertPlan" resultMap="MarkertPlanResult">
        select  p.id,  p.grade, p.discount_term,  p.discount_amt,  p.gasoil_discount_amt, p.vip_discounty_plus, p.coupon_plus, p.discount_plan_type,
        p.oil_name, station_id, p.status,p.create_by,p.create_time,p.update_by,p.update_time,d.dept_name as station_name
        from markert_plan p join sys_dept d on  d.dept_id =p.station_id
        <where>
            <if test="grade != null  and grade != ''"> and p.grade = #{grade}</if>
            <if test="discountTerm != null  and discountTerm != ''"> and p.discount_term = #{discountTerm}</if>
            <if test="discountAmt != null  and discountAmt != ''"> and p.discount_amt = #{discountAmt}</if>
            <if test="gasoilDiscountAmt != null  and gasoilDiscountAmt != ''"> and p.gasoil_discount_amt = #{gasoilDiscountAmt}</if>
            <if test="vipDiscountyPlus != null  and vipDiscountyPlus != ''"> and p.vip_discounty_plus = #{vipDiscountyPlus}</if>
            <if test="couponPlus != null  and couponPlus != ''"> and p.coupon_plus = #{couponPlus}</if>
            <if test="discountPlanType != null  and discountPlanType != ''"> and p.discount_plan_type = #{discountPlanType}</if>
            <if test="oilName != null  and oilName != ''"> and p.oil_name like concat('%', #{oilName}, '%')</if>
            <if test="stationId != null "> and p.station_id = #{stationId}</if>
            <if test="status != null  and status != ''"> and p.status = #{status}</if>
            <if test="stationIdList != null ">
                and station_id in
                <foreach item="item" index="index" collection="stationIdList"
                         open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
        </where>
    </select>

    <select id="selectMarkertPlanById" parameterType="Long" resultMap="MarkertPlanResult">
          select  p.id,  p.grade, p.discount_term,  p.discount_amt,  p.gasoil_discount_amt, p.vip_discounty_plus, p.coupon_plus, p.discount_plan_type,
          p.oil_name, station_id, p.status,p.create_by,p.create_time,p.update_by,p.update_time,d.dept_name as station_name
          from markert_plan p join sys_dept d on  d.dept_id =p.station_id
        where id = #{id}
    </select>

    <insert id="insertMarkertPlan" parameterType="MarkertPlan" useGeneratedKeys="true" keyProperty="id">
        insert into markert_plan
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="grade != null">grade,</if>
            <if test="discountTerm != null">discount_term,</if>
            <if test="discountAmt != null">discount_amt,</if>
            <if test="gasoilDiscountAmt != null">gasoil_discount_amt,</if>
            <if test="vipDiscountyPlus != null">vip_discounty_plus,</if>
            <if test="couponPlus != null">coupon_plus,</if>
            <if test="discountPlanType != null">discount_plan_type,</if>
            <if test="oilName != null">oil_name,</if>
            <if test="stationId != null">station_id,</if>
            <if test="status != null">status,</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>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="grade != null">#{grade},</if>
            <if test="discountTerm != null">#{discountTerm},</if>
            <if test="discountAmt != null">#{discountAmt},</if>
            <if test="gasoilDiscountAmt != null">#{gasoilDiscountAmt},</if>
            <if test="vipDiscountyPlus != null">#{vipDiscountyPlus},</if>
            <if test="couponPlus != null">#{couponPlus},</if>
            <if test="discountPlanType != null">#{discountPlanType},</if>
            <if test="oilName != null">#{oilName},</if>
            <if test="stationId != null">#{stationId},</if>
            <if test="status != null">#{status},</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="updateMarkertPlan" parameterType="MarkertPlan">
        update markert_plan
        <trim prefix="SET" suffixOverrides=",">
            <if test="grade != null">grade = #{grade},</if>
            <if test="discountTerm != null">discount_term = #{discountTerm},</if>
            <if test="discountAmt != null">discount_amt = #{discountAmt},</if>
            <if test="gasoilDiscountAmt != null">gasoil_discount_amt = #{gasoilDiscountAmt},</if>
            <if test="vipDiscountyPlus != null">vip_discounty_plus = #{vipDiscountyPlus},</if>
            <if test="couponPlus != null">coupon_plus = #{couponPlus},</if>
            <if test="discountPlanType != null">discount_plan_type = #{discountPlanType},</if>
            <if test="oilName != null">oil_name = #{oilName},</if>
            <if test="stationId != null">station_id = #{stationId},</if>
            <if test="status != null">status = #{status},</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="deleteMarkertPlanById" parameterType="Long">
        delete from markert_plan where id = #{id}
    </delete>

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

</mapper>
