<?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="cn.edu.whu.boot.xml.mapper.TaskMapper">
  <resultMap id="BaseResultMap" type="cn.edu.whu.boot.xml.domain.Task">
    <id column="id" jdbcType="INTEGER" property="id" />
    <result column="task_name" jdbcType="VARCHAR" property="taskName" />
    <result column="operator" jdbcType="VARCHAR" property="operator" />
    <result column="create_time" jdbcType="TIMESTAMP" property="createTime" />
    <result column="modify_time" jdbcType="TIMESTAMP" property="modifyTime" />
    <result column="xml_id" jdbcType="INTEGER" property="xmlId" />
    <result column="has_create_tables" jdbcType="SMALLINT" property="hasCreateTables" />
    <result column="config" jdbcType="VARCHAR" property="config" />
    <result column="database_name" jdbcType="VARCHAR" property="databaseName" />
    <result column="introduction" jdbcType="VARCHAR" property="introduction" />
    <result column="upload_deadline" jdbcType="VARCHAR" property="uploadDeadline" />
    <result column="create_tables_md5code" jdbcType="VARCHAR" property="createTablesMd5code" />
    <result column="is_allocation_optional" jdbcType="SMALLINT" property="isAllocationOptional" />
    <result column="is_reviewer_restricted" jdbcType="SMALLINT" property="isReviewerRestricted"/>
  </resultMap>
  <sql id="Example_Where_Clause">
    <where>
      <foreach collection="oredCriteria" item="criteria" separator="or">
        <if test="criteria.valid">
          <trim prefix="(" prefixOverrides="and" suffix=")">
            <foreach collection="criteria.criteria" item="criterion">
              <choose>
                <when test="criterion.noValue">
                  and ${criterion.condition}
                </when>
                <when test="criterion.singleValue">
                  and ${criterion.condition} #{criterion.value}
                </when>
                <when test="criterion.betweenValue">
                  and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}
                </when>
                <when test="criterion.listValue">
                  and ${criterion.condition}
                  <foreach close=")" collection="criterion.value" item="listItem" open="(" separator=",">
                    #{listItem}
                  </foreach>
                </when>
              </choose>
            </foreach>
          </trim>
        </if>
      </foreach>
    </where>
  </sql>
  <sql id="Update_By_Example_Where_Clause">
    <where>
      <foreach collection="example.oredCriteria" item="criteria" separator="or">
        <if test="criteria.valid">
          <trim prefix="(" prefixOverrides="and" suffix=")">
            <foreach collection="criteria.criteria" item="criterion">
              <choose>
                <when test="criterion.noValue">
                  and ${criterion.condition}
                </when>
                <when test="criterion.singleValue">
                  and ${criterion.condition} #{criterion.value}
                </when>
                <when test="criterion.betweenValue">
                  and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}
                </when>
                <when test="criterion.listValue">
                  and ${criterion.condition}
                  <foreach close=")" collection="criterion.value" item="listItem" open="(" separator=",">
                    #{listItem}
                  </foreach>
                </when>
              </choose>
            </foreach>
          </trim>
        </if>
      </foreach>
    </where>
  </sql>
  <sql id="Base_Column_List">
    id, task_name, operator, create_time, modify_time, xml_id, has_create_tables, config,
    database_name, introduction, upload_deadline, create_tables_md5code, is_allocation_optional, is_reviewer_restricted
  </sql>
  <select id="selectByExample" parameterType="cn.edu.whu.boot.xml.domain.TaskExample" resultMap="BaseResultMap">
    select
    <if test="distinct">
      distinct
    </if>
    <include refid="Base_Column_List" />
    from task
    <if test="_parameter != null">
      <include refid="Example_Where_Clause" />
    </if>
    <if test="orderByClause != null">
      order by ${orderByClause}
    </if>
  </select>
  <select id="selectByPrimaryKey" parameterType="java.lang.Integer" resultMap="BaseResultMap">
    select
    <include refid="Base_Column_List" />
    from task
    where id = #{id,jdbcType=INTEGER}
  </select>
  <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer">
    delete from task
    where id = #{id,jdbcType=INTEGER}
  </delete>
  <delete id="deleteByExample" parameterType="cn.edu.whu.boot.xml.domain.TaskExample">
    delete from task
    <if test="_parameter != null">
      <include refid="Example_Where_Clause" />
    </if>
  </delete>
  <insert id="insert" parameterType="cn.edu.whu.boot.xml.domain.Task">
    <selectKey keyProperty="id" order="AFTER" resultType="java.lang.Integer">
      SELECT currval('task_id_seq'::regclass)
    </selectKey>
    insert into task (task_name, operator,
    create_time, modify_time, xml_id,
    has_create_tables, config, database_name,
    introduction, upload_deadline, create_tables_md5code,
    is_allocation_optional, is_reviewer_restricted)
    values (#{taskName,jdbcType=VARCHAR}, #{operator,jdbcType=VARCHAR},
    #{createTime,jdbcType=TIMESTAMP}, #{modifyTime,jdbcType=TIMESTAMP}, #{xmlId,jdbcType=INTEGER},
    #{hasCreateTables,jdbcType=SMALLINT}, #{config,jdbcType=VARCHAR}, #{databaseName,jdbcType=VARCHAR},
    #{introduction,jdbcType=VARCHAR}, #{uploadDeadline,jdbcType=VARCHAR}, #{createTablesMd5code,jdbcType=VARCHAR},
    #{isAllocationOptional,jdbcType=SMALLINT}, #{isReviewerRestricted,jdbcType=SMALLINT})
  </insert>
  <insert id="insertSelective" parameterType="cn.edu.whu.boot.xml.domain.Task">
    <selectKey keyProperty="id" order="AFTER" resultType="java.lang.Integer">
      SELECT currval('task_id_seq'::regclass)
    </selectKey>
    insert into task
    <trim prefix="(" suffix=")" suffixOverrides=",">
      <if test="taskName != null">
        task_name,
      </if>
      <if test="operator != null">
        operator,
      </if>
      <if test="createTime != null">
        create_time,
      </if>
      <if test="modifyTime != null">
        modify_time,
      </if>
      <if test="xmlId != null">
        xml_id,
      </if>
      <if test="hasCreateTables != null">
        has_create_tables,
      </if>
      <if test="config != null">
        config,
      </if>
      <if test="databaseName != null">
        database_name,
      </if>
      <if test="introduction != null">
        introduction,
      </if>
      <if test="uploadDeadline != null">
        upload_deadline,
      </if>
      <if test="createTablesMd5code != null">
        create_tables_md5code,
      </if>
      <if test="isAllocationOptional != null">
        is_allocation_optional,
      </if>
      <if test="isReviewerRestricted != null">
        is_reviewer_restricted,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides=",">
      <if test="id != null">
        #{id,jdbcType=INTEGER},
      </if>
      <if test="taskName != null">
        #{taskName,jdbcType=VARCHAR},
      </if>
      <if test="operator != null">
        #{operator,jdbcType=VARCHAR},
      </if>
      <if test="createTime != null">
        #{createTime,jdbcType=TIMESTAMP},
      </if>
      <if test="modifyTime != null">
        #{modifyTime,jdbcType=TIMESTAMP},
      </if>
      <if test="xmlId != null">
        #{xmlId,jdbcType=INTEGER},
      </if>
      <if test="hasCreateTables != null">
        #{hasCreateTables,jdbcType=SMALLINT},
      </if>
      <if test="config != null">
        #{config,jdbcType=VARCHAR},
      </if>
      <if test="databaseName != null">
        #{databaseName,jdbcType=VARCHAR},
      </if>
      <if test="introduction != null">
        #{introduction,jdbcType=VARCHAR},
      </if>
      <if test="uploadDeadline != null">
        #{uploadDeadline,jdbcType=VARCHAR},
      </if>
      <if test="createTablesMd5code != null">
        #{createTablesMd5code,jdbcType=VARCHAR},
      </if>
      <if test="isAllocationOptional != null">
        #{isAllocationOptional,jdbcType=SMALLINT},
      </if>
    </trim>
  </insert>
  <select id="countByExample" parameterType="cn.edu.whu.boot.xml.domain.TaskExample" resultType="java.lang.Long">
    select count(*) from task
    <if test="_parameter != null">
      <include refid="Example_Where_Clause" />
    </if>
  </select>
  <update id="updateByExampleSelective" parameterType="map">
    update task
    <set>
      <if test="record.id != null">
        id = #{record.id,jdbcType=INTEGER},
      </if>
      <if test="record.taskName != null">
        task_name = #{record.taskName,jdbcType=VARCHAR},
      </if>
      <if test="record.operator != null">
        operator = #{record.operator,jdbcType=VARCHAR},
      </if>
      <if test="record.createTime != null">
        create_time = #{record.createTime,jdbcType=TIMESTAMP},
      </if>
      <if test="record.modifyTime != null">
        modify_time = #{record.modifyTime,jdbcType=TIMESTAMP},
      </if>
      <if test="record.xmlId != null">
        xml_id = #{record.xmlId,jdbcType=INTEGER},
      </if>
      <if test="record.hasCreateTables != null">
        has_create_tables = #{record.hasCreateTables,jdbcType=SMALLINT},
      </if>
      <if test="record.config != null">
        config = #{record.config,jdbcType=VARCHAR},
      </if>
      <if test="record.databaseName != null">
        database_name = #{record.databaseName,jdbcType=VARCHAR},
      </if>
      <if test="record.introduction != null">
        introduction = #{record.introduction,jdbcType=VARCHAR},
      </if>
      <if test="record.uploadDeadline != null">
        upload_deadline = #{record.uploadDeadline,jdbcType=VARCHAR},
      </if>
      <if test="record.createTablesMd5code != null">
        create_tables_md5code = #{record.createTablesMd5code,jdbcType=VARCHAR},
      </if>
      <if test="record.isAllocationOptional != null">
        is_allocation_optional = #{record.isAllocationOptional,jdbcType=SMALLINT},
      </if>
      <if test="record.isReviewerRestricted != null">
        is_reviewer_restricted = #{isReviewerRestricted, jdbcType=SMALLINT},
      </if>
    </set>
    <if test="_parameter != null">
      <include refid="Update_By_Example_Where_Clause" />
    </if>
  </update>
  <update id="updateByExample" parameterType="map">
    update task
    set id = #{record.id,jdbcType=INTEGER},
    task_name = #{record.taskName,jdbcType=VARCHAR},
    operator = #{record.operator,jdbcType=VARCHAR},
    create_time = #{record.createTime,jdbcType=TIMESTAMP},
    modify_time = #{record.modifyTime,jdbcType=TIMESTAMP},
    xml_id = #{record.xmlId,jdbcType=INTEGER},
    has_create_tables = #{record.hasCreateTables,jdbcType=SMALLINT},
    config = #{record.config,jdbcType=VARCHAR},
    database_name = #{record.databaseName,jdbcType=VARCHAR},
    introduction = #{record.introduction,jdbcType=VARCHAR},
    upload_deadline = #{record.uploadDeadline,jdbcType=VARCHAR},
    create_tables_md5code = #{record.createTablesMd5code,jdbcType=VARCHAR},
    is_allocation_optional = #{record.isAllocationOptional,jdbcType=SMALLINT},
    is_reviewer_restricted = #{isReviewerRestricted, jdbcType=SMALLINT}
    <if test="_parameter != null">
      <include refid="Update_By_Example_Where_Clause" />
    </if>
  </update>
  <update id="updateByPrimaryKeySelective" parameterType="cn.edu.whu.boot.xml.domain.Task">
    update task
    <set>
      <if test="taskName != null">
        task_name = #{taskName,jdbcType=VARCHAR},
      </if>
      <if test="operator != null">
        operator = #{operator,jdbcType=VARCHAR},
      </if>
      <if test="createTime != null">
        create_time = #{createTime,jdbcType=TIMESTAMP},
      </if>
      <if test="modifyTime != null">
        modify_time = #{modifyTime,jdbcType=TIMESTAMP},
      </if>
      <if test="xmlId != null">
        xml_id = #{xmlId,jdbcType=INTEGER},
      </if>
      <if test="hasCreateTables != null">
        has_create_tables = #{hasCreateTables,jdbcType=SMALLINT},
      </if>
      <if test="config != null">
        config = #{config,jdbcType=VARCHAR},
      </if>
      <if test="databaseName != null">
        database_name = #{databaseName,jdbcType=VARCHAR},
      </if>
      <if test="introduction != null">
        introduction = #{introduction,jdbcType=VARCHAR},
      </if>
      <if test="uploadDeadline != null">
        upload_deadline = #{uploadDeadline,jdbcType=VARCHAR},
      </if>
      <if test="createTablesMd5code != null">
        create_tables_md5code = #{createTablesMd5code,jdbcType=VARCHAR},
      </if>
      <if test="isAllocationOptional != null">
        is_allocation_optional = #{isAllocationOptional,jdbcType=SMALLINT},
      </if>
      <if test="isReviewerRestricted != null">
        is_reviewer_restricted = #{isReviewerRestricted, jdbcType=SMALLINT},
      </if>
    </set>
    where id = #{id,jdbcType=INTEGER}
  </update>
  <update id="updateByPrimaryKey" parameterType="cn.edu.whu.boot.xml.domain.Task">
    update task
    set task_name = #{taskName,jdbcType=VARCHAR},
      operator = #{operator,jdbcType=VARCHAR},
      create_time = #{createTime,jdbcType=TIMESTAMP},
      modify_time = #{modifyTime,jdbcType=TIMESTAMP},
      xml_id = #{xmlId,jdbcType=INTEGER},
      has_create_tables = #{hasCreateTables,jdbcType=SMALLINT},
      config = #{config,jdbcType=VARCHAR},
      database_name = #{databaseName,jdbcType=VARCHAR},
      introduction = #{introduction,jdbcType=VARCHAR},
      upload_deadline = #{uploadDeadline,jdbcType=VARCHAR},
      create_tables_md5code = #{createTablesMd5code,jdbcType=VARCHAR},
      is_allocation_optional = #{isAllocationOptional,jdbcType=SMALLINT},
      is_reviewer_restricted = #{isReviewerRestricted, jdbcType=SMALLINT}
    where id = #{id,jdbcType=INTEGER}
  </update>

  <select id="selectUsersByTaskId" resultType="cn.edu.whu.boot.user.entities.User">
        SELECT u.id, u.username, u.real_name, u.phone, u.email, t.work_role AS role
        FROM user_staff u INNER JOIN task_user_role t
            ON u.id = t.user_id AND t.task_id = #{taskId}
  </select>

  <select id="selectUsersByTableNameAndIdFieldName" resultType="cn.edu.whu.boot.user.entities.User">
        SELECT id, username, real_name, phone, email, 0 AS role
        FROM user_staff WHERE id IN (SELECT DISTINCT ${IdFieldName} FROM ${tableName})
  </select>
</mapper>