<?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.ym.mec.task.dal.dao.TaskDao" >
  <resultMap id="TaskResultMap" type="com.ym.mec.task.dal.model.Task" >
    <id column="id_" property="id" jdbcType="INTEGER" />
    <result column="name_" property="name" jdbcType="VARCHAR" />
    <result column="group_" property="group" jdbcType="VARCHAR" />
    <result column="job_class_" property="jobClass" jdbcType="VARCHAR" />
    <result column="time_exp_" property="timeExp" jdbcType="VARCHAR" />
    <result column="status_" property="status" jdbcType="TINYINT" />
    <result column="description_" property="description" jdbcType="VARCHAR" />
    <result column="create_on_" property="createOn" jdbcType="TIMESTAMP" />
    <result column="modify_on_" property="modifyOn" jdbcType="TIMESTAMP" />
  </resultMap>
  
  <sql id="Base_Column_List" >
    id_, name_, group_, job_class_, time_exp_, status_, description_, create_on_, modify_on_
  </sql>
  
  <sql id="queryCondition">
  	<where>
	  	<if test="search != null">
	  		and (name_ like '%' #{search} '%' or description_ like '%' #{search} '%')
	  	</if>
	  	<if test="status != null">
	  		and status_ = #{status}
	  	</if>
  	</where>
  </sql>
  
  <select id="get" resultMap="TaskResultMap" parameterType="java.lang.Integer" >
    select 
    <include refid="Base_Column_List" />
    from sys_task
    where id_ = #{id,jdbcType=INTEGER}
  </select>
  
  <delete id="delete" parameterType="java.lang.Integer" >
    delete from sys_task
    where id_ = #{id,jdbcType=INTEGER}
  </delete>
  
  <insert id="insert" parameterType="com.ym.mec.task.dal.model.Task" >
    insert into sys_task (id_, name_, group_, 
      job_class_, time_exp_, status_, 
      description_, create_on_, modify_on_
      )
    values (#{id,jdbcType=INTEGER}, #{name,jdbcType=VARCHAR}, #{group,jdbcType=VARCHAR}, 
      #{jobClass,jdbcType=VARCHAR}, #{timeExp,jdbcType=VARCHAR}, #{status,jdbcType=TINYINT}, 
      #{description,jdbcType=VARCHAR}, #{createOn,jdbcType=TIMESTAMP}, #{modifyOn,jdbcType=TIMESTAMP}
      )
  </insert>
  
  <update id="update" parameterType="com.ym.mec.task.dal.model.Task" >
    update sys_task
    set name_ = #{name,jdbcType=VARCHAR},
      group_ = #{group,jdbcType=VARCHAR},
      job_class_ = #{jobClass,jdbcType=VARCHAR},
      time_exp_ = #{timeExp,jdbcType=VARCHAR},
      status_ = #{status,jdbcType=TINYINT},
      description_ = #{description,jdbcType=VARCHAR},
      create_on_ = #{createOn,jdbcType=TIMESTAMP},
      modify_on_ = #{modifyOn,jdbcType=TIMESTAMP}
    where id_ = #{id,jdbcType=INTEGER}
  </update>
  
  <select id="queryAvailableTasks" resultMap="TaskResultMap">
    select  * from sys_task
    where status_ != 0 and status_ != 2
  </select>
  
  <select id="queryTaskByNameAndGroup" resultMap="TaskResultMap" parameterType="java.util.Map" >
    select 
    <include refid="Base_Column_List" />
    from sys_task
    where name_ = #{name} and group_ = #{group}
  </select>
  
  <update id="updateStatusById" parameterType="java.util.Map">
  	update sys_task set status_ = #{curStatus},modify_on_ = now() where id_ = #{id} and status_ = #{oldStatus} 
  </update>
  
  <select id="queryCount" parameterType="map" resultType="int">
  	select count(*) from  sys_task
   <include refid="queryCondition"/>
  </select>

  <select id="queryPage" parameterType="map" resultMap="TaskResultMap">
 	 select * from sys_task
   <include refid="queryCondition"/>
   order by create_on_ desc
   <include refid="global.limit"/>
  </select>

	<select id="getLocked" resultMap="TaskResultMap" parameterType="java.lang.Integer">
		select
		<include refid="Base_Column_List" />
		from sys_task
		where id_ = #{id,jdbcType=INTEGER} for update
	</select>
</mapper>