<?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.xintong.visualinspection.dao.master.TeamClassDao" >
    <resultMap id="BaseResultMap" type="com.xintong.visualinspection.bean.TeamClass" >
        <id column="id" property="id" jdbcType="INTEGER" />
        <result column="work_date" property="work_date" jdbcType="DATE" />
        <result column="dept_id" property="dept_id" jdbcType="INTEGER" />
        <result column="team_name" property="team_name" jdbcType="VARCHAR" />
        <result column="user_ids" property="user_ids" jdbcType="VARCHAR" />
        <result column="class_type" property="class_type" jdbcType="VARCHAR" />
        <result column="team_id" property="team_id" jdbcType="INTEGER" />
        <result column="start_time" property="start_time" jdbcType="DATE" />
        <result column="end_time" property="end_time" jdbcType="DATE" />
    </resultMap>

    <select id="getAll" resultMap="BaseResultMap"  >
        SELECT *
        FROM sys_team_class
    </select>

    <select id="getOne" parameterType="java.lang.Long" resultMap="BaseResultMap" >
        SELECT *
        FROM sys_team_class
        WHERE id = #{id}
    </select>
    
    <select id="getClassListForCharge" parameterType="com.xintong.visualinspection.bean.TeamClass" resultMap="BaseResultMap" >
        SELECT * FROM ( SELECT t.id,t.work_date,t.dept_id,t.class_type,t.team_id,t.start_time,t.end_time,st.user_ids ,st.name as team_name,st.type
        FROM sys_team_class t left join sys_team st ON t.team_id = st.id
        WHERE 1=1
        <if test="dept_id != null">AND t.dept_id = #{dept_id}</if>
        <if test="start_time != null">AND t.work_date &gt;= #{start_time}</if>
        <if test="end_time != null">AND t.work_date &lt;= #{end_time}</if>
        ORDER BY t.work_date ASC ,t.class_type ASC) tc WHERE tc.type =#{type}
    </select>
    
    <select id="getClassList" parameterType="com.xintong.visualinspection.bean.TeamClass" resultMap="BaseResultMap" >
        SELECT * FROM ( SELECT t.id,t.work_date,t.dept_id,t.class_type,t.team_id,t.start_time,t.end_time,t.user_ids ,st.name as team_name,st.type
        FROM sys_team_class t left join sys_team st ON t.team_id = st.id
        WHERE 1=1
        <if test="dept_id != null">AND t.dept_id = #{dept_id}</if>
        <if test="start_time != null">AND t.work_date &gt;= #{start_time}</if>
        <if test="end_time != null">AND t.work_date &lt;= #{end_time}</if>
        ORDER BY t.class_type asc,t.work_date ASC) tc WHERE tc.type =#{type}
    </select>

    <insert id="insert" parameterType="com.xintong.visualinspection.bean.TeamClass" >
        INSERT INTO
        sys_team_class
        (work_date,dept_id,user_ids,class_type,team_id,start_time,end_time)
        VALUES
        (#{work_date}, #{dept_id},#{user_ids},#{class_type},#{team_id},#{start_time},#{end_time})
    </insert>

    <update id="update" parameterType="com.xintong.visualinspection.bean.TeamClass" >
        UPDATE
        sys_team_class
        SET id=id 
        <if test="work_date != null">,work_date = #{work_date}</if>
        <if test="dept_id != null">,dept_id = #{dept_id}</if>
        <if test="user_ids != null">,user_ids = #{user_ids}</if>
        <if test="class_type != null">,class_type = #{class_type}</if>
        <if test="team_id != null">,team_id = #{team_id},</if>
        <if test="start_time != null">,start_time = #{start_time}</if>
        <if test="end_time != null">,end_time = #{end_time}</if>
        WHERE
        id = #{id}
    </update>
    
     <update id="updateUserIds" parameterType="com.xintong.visualinspection.bean.TeamClass" >
        UPDATE
        sys_team_class
        SET id=id 
        <if test="dept_id != null">,dept_id = #{dept_id}</if>
        <if test="user_ids != null">,user_ids = #{user_ids}</if>
        <if test="class_type != null">,class_type = #{class_type}</if>
        <if test="start_time != null">,start_time = #{start_time}</if>
        <if test="end_time != null">,end_time = #{end_time}</if>
        WHERE
        team_id = #{team_id} and work_date = #{work_date}
    </update>

    <delete id="delete" parameterType="java.lang.Long" >
        DELETE FROM
        sys_team_class
        WHERE
        id =#{id}
    </delete>
    
    <delete id="deleteByDeptWorkdate" parameterType="com.xintong.visualinspection.bean.TeamClass" >
        DELETE FROM
        sys_team_class
        WHERE
        work_date =#{work_date} and dept_id=#{dept_id}
    </delete>
    
    <select id="getClassCode" parameterType="com.xintong.visualinspection.bean.ClassCode" resultType="com.xintong.visualinspection.bean.ClassCode">
    	SELECT * FROM sys_class_type where 1=1 
    	<if test="position_id!=null">
    		AND position_id = #{position_id}
    	</if>
    </select>
    
</mapper>