<?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.fjs.scenic.mapper.good.PriceRuleMapper" >
  <resultMap id="BaseResultMap" type="com.fjs.scenic.entity.good.PriceRule" >
    <constructor >
      <idArg column="id" jdbcType="INTEGER" javaType="java.lang.Integer" />
      <arg column="goods_id" jdbcType="INTEGER" javaType="java.lang.Integer" />
      <arg column="date" jdbcType="TIMESTAMP" javaType="java.util.Date" />
      <arg column="sell_price" jdbcType="INTEGER" javaType="java.lang.Integer" />
      <arg column="settlement_price" jdbcType="INTEGER" javaType="java.lang.Integer" />
      <arg column="suggest_price" jdbcType="INTEGER" javaType="java.lang.Integer" />
      <arg column="repertory" jdbcType="INTEGER" javaType="java.lang.Integer" />
    </constructor>
  </resultMap>
  <sql id="Base_Column_List" >
    id, goods_id, date, sell_price, settlement_price, suggest_price, repertory
  </sql>
  <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
    select 
    <include refid="Base_Column_List" />
    from price_rule
    where id = #{id,jdbcType=INTEGER}
  </select>
  
  <select id="selectPriceRulesByGoodsIdAndNowDate" resultMap="BaseResultMap">
  SELECT <include refid="Base_Column_List" /> FROM price_rule WHERE TO_DAYS(date)=TO_DAYS(NOW()) AND goods_id=#{goodsId,jdbcType=INTEGER}
  </select>
  
  <select id="selectPriceRulesByGoodsId" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
    select 
    id, goods_id, date, sell_price, settlement_price, suggest_price, repertory
    from price_rule
    where goods_id = #{goodsId,jdbcType=INTEGER}
  </select>
  
  <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer" >
    delete from price_rule
    where id = #{id,jdbcType=INTEGER}
  </delete>
  
  <delete id="deleteByGoodsId" parameterType="java.lang.Integer" >
    delete from price_rule
    where goods_id = #{goodsId,jdbcType=INTEGER}
  </delete>
  
  <insert id="insert" parameterType="com.fjs.scenic.entity.good.PriceRule" >
    insert into price_rule (id, goods_id, date, 
      sell_price, settlement_price, suggest_price, 
      repertory)
    values (#{id,jdbcType=INTEGER}, #{goodsId,jdbcType=INTEGER}, #{date,jdbcType=TIMESTAMP}, 
      #{sellPrice,jdbcType=INTEGER}, #{settlementPrice,jdbcType=INTEGER}, #{suggestPrice,jdbcType=INTEGER}, 
      #{repertory,jdbcType=INTEGER})
  </insert>
  <insert id="insertSelective" parameterType="com.fjs.scenic.entity.good.PriceRule" useGeneratedKeys="true" keyProperty="id">
    insert into price_rule
    <trim prefix="(" suffix=")" suffixOverrides="," >
      <if test="id != null" >
        id,
      </if>
      <if test="goodsId != null" >
        goods_id,
      </if>
      <if test="date != null" >
        date,
      </if>
      <if test="sellPrice != null" >
        sell_price,
      </if>
      <if test="settlementPrice != null" >
        settlement_price,
      </if>
      <if test="suggestPrice != null" >
        suggest_price,
      </if>
      <if test="repertory != null" >
        repertory,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides="," >
      <if test="id != null" >
        #{id,jdbcType=INTEGER},
      </if>
      <if test="goodsId != null" >
        #{goodsId,jdbcType=INTEGER},
      </if>
      <if test="date != null" >
        #{date,jdbcType=TIMESTAMP},
      </if>
      <if test="sellPrice != null" >
        #{sellPrice,jdbcType=INTEGER},
      </if>
      <if test="settlementPrice != null" >
        #{settlementPrice,jdbcType=INTEGER},
      </if>
      <if test="suggestPrice != null" >
        #{suggestPrice,jdbcType=INTEGER},
      </if>
      <if test="repertory != null" >
        #{repertory,jdbcType=INTEGER},
      </if>
    </trim>
  </insert>
  <update id="updateByPrimaryKeySelective" parameterType="com.fjs.scenic.entity.good.PriceRule" >
    update price_rule
    <set >
      <if test="goodsId != null" >
        goods_id = #{goodsId,jdbcType=INTEGER},
      </if>
      <if test="date != null" >
        date = #{date,jdbcType=TIMESTAMP},
      </if>
      <if test="sellPrice != null" >
        sell_price = #{sellPrice,jdbcType=INTEGER},
      </if>
      <if test="settlementPrice != null" >
        settlement_price = #{settlementPrice,jdbcType=INTEGER},
      </if>
      <if test="suggestPrice != null" >
        suggest_price = #{suggestPrice,jdbcType=INTEGER},
      </if>
      <if test="repertory != null" >
        repertory = #{repertory,jdbcType=INTEGER},
      </if>
    </set>
    where id = #{id,jdbcType=INTEGER}
  </update>
  <update id="updateByPrimaryKey" parameterType="com.fjs.scenic.entity.good.PriceRule" >
    update price_rule
    set goods_id = #{goodsId,jdbcType=INTEGER},
      date = #{date,jdbcType=TIMESTAMP},
      sell_price = #{sellPrice,jdbcType=INTEGER},
      settlement_price = #{settlementPrice,jdbcType=INTEGER},
      suggest_price = #{suggestPrice,jdbcType=INTEGER},
      repertory = #{repertory,jdbcType=INTEGER}
    where id = #{id,jdbcType=INTEGER}
  </update>


  <!--暂停或恢复售票-->
  <update id="saleOrRecoverSaleTicket" parameterType="com.fjs.scenic.entity.good.PriceRule" >
    update price_rule
    set repertory = #{saleTicketType,jdbcType=INTEGER}
    where goods_id in (SELECT resourceId FROM goods WHERE sort_type=1) and <![CDATA[
        DATE_FORMAT(date , '%Y-%m-%d')>=  DATE_FORMAT(#{startDate}, '%Y-%m-%d')
        and DATE_FORMAT(date , '%Y-%m-%d')<=  DATE_FORMAT(#{endDate}, '%Y-%m-%d')
       ]]>
  </update>
</mapper>