<?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"/>
        <result property="monitor_user_id" column="monitor_user_id"/>
        <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"/>
            <result property="monitor_user_id" column="monitor_user_id_u"/>
        </collection>
    </resultMap>

    <resultMap type="com.xintong.visualinspection.bean.UnregularUserClass" id="unregularUserClass">
        <id property="id" column="id"/>
        <result property="start_time" column="start_time"/>
        <result property="end_time" column="end_time"/>
        <result property="user_name" column="user_name"/>
        <result property="user_id" column="user_id"/>
        <result property="work_date" column="work_date"/>
        <result property="dept_id" column="dept_id"/>
        <result property="class_type" column="class_type"/>
        <result property="team_id" column="team_id"/>
        <result property="lane" column="lane"/>
        <result property="update_time" column="update_time"/>
        <result property="monitor_user_id" column="monitor_user_id"/>
        <result property="class_id" column="class_id"/>
    </resultMap>


    <insert id="insertremove" parameterType="com.xintong.visualinspection.bean.UserClassRemove" useGeneratedKeys="true"
            keyProperty="id">
        INSERT INTO
        sys_user_class_remove
        (data,type)
        VALUES
        (#{data},#{type})
    </insert>


    <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,monitor_user_id)
        VALUES
        (#{work_date},#{dept_id},#{user_id},#{class_type},#{team_id},#{start_time},#{end_time},#{lane},#{class_flag},#{monitor_user_id})
    </insert>

    <update  id="update" parameterType="com.xintong.visualinspection.bean.UserClass" >
        UPDATE sys_user_class SET update_time = now()
        <if test="work_date != null">,work_date = #{work_date}</if>
        <if test="dept_id != null">,dept_id = #{dept_id}</if>
        <if test="user_id != null">,user_id = #{user_id}</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>
        <if test="lane != null">,lane = #{lane}</if>
        <if test="class_flag != null">,class_flag = #{class_flag}</if>
        <if test="monitor_user_id != null">,monitor_user_id = #{monitor_user_id}</if>
         WHERE
        id = #{id}
    </update>




    <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,monitor_user_id)
        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},#{item.monitor_user_id})
        </foreach>
    </insert>

    <insert id="insert_detail2" parameterType="com.xintong.visualinspection.bean.UnregularUserClass">
        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,monitor_user_id)
        VALUES
         (#{work_date},#{dept_id},#{user_id},#{class_type},#{team_id},#{start_time},#{end_time},#{lane},#{class_id},#{user_name},#{monitor_user_id})
    </insert>

    <update  id="update_detail" parameterType="com.xintong.visualinspection.bean.UnregularUserClass" >
        UPDATE sys_user_class_unregular SET update_time = now()
        <if test="work_date != null">,work_date = #{work_date}</if>
        <if test="dept_id != null">,dept_id = #{dept_id}</if>
        <if test="user_id != null">,user_id = #{user_id}</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>
        <if test="lane != null">,lane = #{lane}</if>
        <if test="class_id != null">,class_id = #{class_id}</if>
        <if test="user_name != null">,user_name = #{user_name}</if>
        <if test="monitor_user_id != null">,monitor_user_id = #{monitor_user_id}</if>
        WHERE
        id = #{id}
    </update>

    <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,t.monitor_user_id AS monitor_user_id_u 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 t.start_time asc,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="deleteById" parameterType="java.util.List">
        DELETE FROM
        sys_user_class
        WHERE
        id IN
        <foreach item="item" index="index" collection="list" open="(" separator="," close=")">
                #{item}
        </foreach>
    </delete>


    <select id="getUsersClassByDeptAndWorkDay" parameterType="com.xintong.visualinspection.bean.UserClass"
            resultMap="userClassMap">
        SELECT * FROM
        sys_user_class
        WHERE
        work_date =#{work_date} and dept_id=#{dept_id}
    </select>

    <select id="getUnregularUsersClassByDeptAndWorkDay" parameterType="com.xintong.visualinspection.bean.UserClass"
            resultMap="unregularUserClass">
        SELECT * FROM
        sys_user_class_unregular
        WHERE
        work_date =#{work_date} and dept_id=#{dept_id}
    </select>




    <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>

    <delete id="deleteDetailById" parameterType="java.util.List">
        DELETE FROM
        sys_user_class_unregular
        WHERE
        id IN
        <foreach item="item" index="index" collection="list" open="(" separator="," close=")">
            #{item}
        </foreach>
    </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>