<?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.pcloud.book.timecontrol.mapper.TaskPrizeMapper" >
  <resultMap id="BaseResultMap" type="com.pcloud.book.timecontrol.entity.TimeControlTaskPrize" >
    <id column="id" property="id" jdbcType="INTEGER" />
    <result column="task_id" property="taskId" jdbcType="INTEGER" />
    <result column="task_item_id" property="taskItemId" jdbcType="INTEGER" />
    <result column="type" property="type" jdbcType="INTEGER" />
    <result column="total_prize_num" property="totalPrizeNum" jdbcType="INTEGER" />
    <result column="has_coupon" property="hasCoupon" jdbcType="INTEGER" />
    <result column="has_app" property="hasApp" jdbcType="INTEGER" />
    <result column="has_certificate" property="hasCertificate" jdbcType="INTEGER" />
    <result column="create_time" property="createTime" jdbcType="TIMESTAMP" />
    <result column="update_time" property="updateTime" jdbcType="TIMESTAMP" />
  </resultMap>

  <sql id="Base_Column_List" >
    id, task_id, task_item_id, type, total_prize_num, has_coupon, has_app, has_certificate, create_time, update_time
  </sql>

  <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
    select
    <include refid="Base_Column_List" />
    from time_control_task_prize
    where id = #{id,jdbcType=INTEGER}
  </select>

  <select id="getByTaskId" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
    select
    <include refid="Base_Column_List" />
    from time_control_task_prize
    where task_id = #{taskId,jdbcType=INTEGER} AND type = #{type,jdbcType=INTEGER}
  </select>

  <select id="listByTaskId" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
    select
    <include refid="Base_Column_List" />
    from time_control_task_prize
    where task_id = #{taskId,jdbcType=INTEGER} AND type = #{type,jdbcType=INTEGER}
  </select>

  <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer" >
    delete from time_control_task_prize
    where id = #{id,jdbcType=INTEGER}
  </delete>

  <delete id="deleteByTaskId">
    delete from time_control_task_prize
    where task_id = #{taskId,jdbcType=INTEGER} AND type = #{type,jdbcType=INTEGER}
  </delete>

  <delete id="deleteByTaskItemId" parameterType="int">
    delete from time_control_task_prize
    where task_item_id = #{taskItemId}
  </delete>

  <insert id="insert" parameterType="com.pcloud.book.timecontrol.entity.TimeControlTaskPrize" useGeneratedKeys="true" keyProperty="id" >
    insert into time_control_task_prize (task_id, task_item_id,
      type, total_prize_num, has_coupon,
      has_app, has_certificate, create_time,update_time)
    values (#{taskId,jdbcType=INTEGER}, #{taskItemId,jdbcType=INTEGER},
      #{type,jdbcType=INTEGER}, #{totalPrizeNum,jdbcType=INTEGER}, #{hasCoupon,jdbcType=INTEGER},
      #{hasApp,jdbcType=INTEGER}, #{hasCertificate,jdbcType=INTEGER}, NOW(),NOW())
  </insert>

  <insert id="insertSelective" parameterType="com.pcloud.book.timecontrol.entity.TimeControlTaskPrize" >
    insert into time_control_task_prize
    <trim prefix="(" suffix=")" suffixOverrides="," >
      <if test="id != null" >
        id,
      </if>
      <if test="taskId != null" >
        task_id,
      </if>
      <if test="taskItemId != null" >
        task_item_id,
      </if>
      <if test="type != null" >
        type,
      </if>
      <if test="totalPrizeNum != null" >
        total_prize_num,
      </if>
      <if test="hasCoupon != null" >
        has_coupon,
      </if>
      <if test="hasApp != null" >
        has_app,
      </if>
      <if test="hasCertificate != null" >
        has_certificate,
      </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,jdbcType=INTEGER},
      </if>
      <if test="taskId != null" >
        #{taskId,jdbcType=INTEGER},
      </if>
      <if test="taskItemId != null" >
        #{taskItemId,jdbcType=INTEGER},
      </if>
      <if test="type != null" >
        #{type,jdbcType=INTEGER},
      </if>
      <if test="totalPrizeNum != null" >
        #{totalPrizeNum,jdbcType=INTEGER},
      </if>
      <if test="hasCoupon != null" >
        #{hasCoupon,jdbcType=INTEGER},
      </if>
      <if test="hasApp != null" >
        #{hasApp,jdbcType=INTEGER},
      </if>
      <if test="hasCertificate != null" >
        #{hasCertificate,jdbcType=INTEGER},
      </if>
      <if test="createTime != null" >
        #{createTime,jdbcType=TIMESTAMP},
      </if>
      <if test="updateTime != null" >
        #{updateTime,jdbcType=TIMESTAMP},
      </if>
    </trim>
  </insert>

  <update id="updateByPrimaryKeySelective" parameterType="com.pcloud.book.timecontrol.entity.TimeControlTaskPrize" >
    update time_control_task_prize
    <set >
      <if test="taskId != null" >
        task_id = #{taskId,jdbcType=INTEGER},
      </if>
      <if test="taskItemId != null" >
        task_item_id = #{taskItemId,jdbcType=INTEGER},
      </if>
      <if test="type != null" >
        type = #{type,jdbcType=INTEGER},
      </if>
      <if test="totalPrizeNum != null" >
        total_prize_num = #{totalPrizeNum,jdbcType=INTEGER},
      </if>
      <if test="hasCoupon != null" >
        has_coupon = #{hasCoupon,jdbcType=INTEGER},
      </if>
      <if test="hasApp != null" >
        has_app = #{hasApp,jdbcType=INTEGER},
      </if>
      <if test="hasCertificate != null" >
        has_certificate = #{hasCertificate,jdbcType=INTEGER},
      </if>
      <if test="createTime != null" >
        create_time = #{createTime,jdbcType=TIMESTAMP},
      </if>
      <if test="updateTime != null" >
        update_time = #{updateTime,jdbcType=TIMESTAMP},
      </if>
    </set>
    where id = #{id,jdbcType=INTEGER}
  </update>

  <update id="updateByPrimaryKey" parameterType="com.pcloud.book.timecontrol.entity.TimeControlTaskPrize" >
    update time_control_task_prize
    set task_id = #{taskId,jdbcType=INTEGER},
      task_item_id = #{taskItemId,jdbcType=INTEGER},
      type = #{type,jdbcType=INTEGER},
      total_prize_num = #{totalPrizeNum,jdbcType=INTEGER},
      has_coupon = #{hasCoupon,jdbcType=INTEGER},
      has_app = #{hasApp,jdbcType=INTEGER},
      has_certificate = #{hasCertificate,jdbcType=INTEGER},
      create_time = #{createTime,jdbcType=TIMESTAMP},
      update_time = #{updateTime,jdbcType=TIMESTAMP}
    where id = #{id,jdbcType=INTEGER}
  </update>
  <update id="update"></update>

  <select id="selectByTaskItemId" parameterType="int" resultMap="BaseResultMap">
    select <include refid="Base_Column_List"/>
    from time_control_task_prize where task_item_id = #{taskItemId}
  </select>

  <select id="getConfigItemId" resultType="Integer">
    select task_item_id
    from time_control_task_prize
    where 1=1
    <if test="taskItemIds!=null and taskItemIds.size()>0">
      and task_item_id  in
      <foreach collection="taskItemIds" open="(" close=")" separator="," item="item">
        #{item}
      </foreach>
    </if>
  </select>

  <select id="getAllCloseItemId" resultType="Integer">
    select DISTINCT task_item_id
    from time_control_task_prize
    where 1=1
    <if test="taskItemIds!=null and taskItemIds.size()>0">
      and task_item_id in
      <foreach collection="taskItemIds" open="(" close=")" separator="," item="item">
        #{item}
      </foreach>
    </if>
    and has_coupon=0 and has_app=0 and has_certificate=0
  </select>

  <select id="getByTaskItemId" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
    select
    <include refid="Base_Column_List" />
    from time_control_task_prize
    where task_item_id = #{taskItemId} AND type = 1
  </select>

  <select id="getByType" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
    select
    <include refid="Base_Column_List" />
    from time_control_task_prize
    where type = #{type}
    <if test="type == 0">
      and task_id = #{id}
    </if>
    <if test="type == 1">
      and task_item_id = #{id}
    </if>
  </select>

</mapper>
