<?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.UserClassDao" >
   
   <resultMap type="com.xintong.visualinspection.bean.UserClass" id="userClassMap">
   		<id property="id" column="id" />
   		<result property="work_date" column="work_date" />
   		<result property="dept_id" column="dept_id" />
   		<result property="user_id" column="user_id" />
   		<result property="class_type" column="class_type" />
   		<result property="team_id" column="team_id" />
   		<result property="start_time" column="start_time"/>
   		<result property="end_time" column="end_time"/>
   		<result property="lane" column="lane"/>
   		<result property="class_flag" column="class_flag"/>
   		<collection property="list" ofType="com.xintong.visualinspection.bean.UnregularUserClass">
   			<id property="id" column="ids" />
   			<result property="start_time" column="start_time_u"/>
   			<result property="end_time" column="end_time_u"/>
   			<result property="user_name" column="user_name"/>
   			<result property="user_id" column="user_id_u"/>
   		</collection>
   </resultMap>
   
   <insert id="insert" parameterType="com.xintong.visualinspection.bean.UserClass" useGeneratedKeys="true" keyProperty="id">
   		INSERT INTO
        sys_user_class
        (work_date,dept_id,user_id,class_type,team_id,start_time,end_time,lane,class_flag)
        VALUES
        (#{work_date},#{dept_id},#{user_id},#{class_type},#{team_id},#{start_time},#{end_time},#{lane},#{class_flag})
   </insert>
   
   
   <insert id="insert_detail" parameterType="java.util.List" >
   		INSERT INTO
        sys_user_class_unregular
        (work_date,dept_id,user_id,class_type,team_id,start_time,end_time,lane,class_id,user_name)
        VALUES
        <foreach collection="list" item="item" index="index" separator="," > 
			(#{item.work_date},#{item.dept_id},#{item.user_id},#{item.class_type},#{item.team_id},#{item.start_time},#{item.end_time},#{item.lane},#{item.class_id},#{item.user_name})
		</foreach> 
   </insert>
   
   <select id="selectList" parameterType="com.xintong.visualinspection.bean.UserClass" resultMap="userClassMap" >
   		SELECT su.*,t.start_time AS start_time_u,t.end_time AS end_time_u,t.user_name , t.user_id AS user_id_u , t.id AS ids FROM sys_user_class su LEFT JOIN sys_user_class_unregular t ON su.id = t.class_id 
   		 WHERE su.work_date = #{work_date} AND su.dept_id=#{dept_id} order by class_type asc , lane asc
   </select>
   
   <delete id="delete" parameterType="com.xintong.visualinspection.bean.UserClass">
   		DELETE FROM
        sys_user_class
        WHERE
        work_date =#{work_date} and dept_id=#{dept_id}
   </delete>
   
   <delete id="deleteDetail" parameterType="com.xintong.visualinspection.bean.UserClass">
   		DELETE FROM 
   		sys_user_class_unregular
   		WHERE   work_date =#{work_date} and dept_id=#{dept_id}
   </delete>
   
   
   <select id="getUsersByTeamId" parameterType="java.lang.Integer" resultType="String"  >
        SELECT user_ids
        FROM sys_team
        WHERE id = 
		        (	SELECT team_id
		        	FROM sys_user_class
		        	where id = #{user_class_id}
	        	)
    </select>
</mapper>