<?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.User" >
        <id column="id" property="id" jdbcType="INTEGER" />
        <result column="username" property="username" jdbcType="VARCHAR" />
        <result column="age" property="age" jdbcType="INTEGER" />
        <result column="password" property="password" jdbcType="VARCHAR" />
    </resultMap>
    <resultMap id="userMap" type="com.xintong.visualinspection.bean.User">
        <id property="id" column="ID"/>
        <result property="username" column="username"/>
        <result property="password" column="PASSWORD"/>
        <collection property="roles" ofType="com.xintong.visualinspection.bean.Role">
            <result column="name" property="name"/>
        </collection>
    </resultMap>

    <select id="getAll" resultMap="BaseResultMap"  >
        SELECT
        id,username,age,password
        FROM sys_user
    </select>

    <select id="getOne" parameterType="java.lang.Long" resultMap="BaseResultMap" >
        SELECT
        id,username,age,password
        FROM sys_user
        WHERE id = #{id}
    </select>

    <insert id="insert" parameterType="com.xintong.visualinspection.bean.User" >
        INSERT INTO
        sys_user
        (username,age,password,TRUENAME,ORGANID,BIRTH,AGE,MOBILE,POSITIONID,SEX,WORKNO,IDNO)
        VALUES
        (#{username}, #{age},#{password},#{truename},#{deptId},#{birth},#{age},#{mobile},#{positionId},#{sex},#{workno},#{idno})
    </insert>

    <update id="update" parameterType="com.xintong.visualinspection.bean.User" >
        UPDATE
        sys_user
        SET
        <if test="username != null">userName = #{username},</if>
        <if test="age != null">age = #{age},</if>
        <if test="password != null">password = #{password},</if>
        id = #{id}
        WHERE
        id = #{id}
    </update>

    <delete id="delete" parameterType="java.lang.Long" >
        DELETE FROM
        sys_user
        WHERE
        id =#{id}
    </delete>


    <select id="findByUserName" parameterType="String" resultMap="userMap">
        select u.*
        ,r.name
        from sys_user u
        LEFT JOIN sys_role_user sru on u.id= sru.Sys_User_id
        LEFT JOIN sys_role r on sru.sys_role_id=r.id
        where username= #{username}
    </select>
</mapper>