<?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.community.huiju.dao.ToUserMapper" >
  <resultMap id="BaseResultMap" type="com.community.huiju.model.ToUser" >
    <id column="id" property="id" jdbcType="INTEGER" />
    <result column="user_name" property="userName" jdbcType="VARCHAR" />
    <result column="login_name" property="loginName" jdbcType="VARCHAR" />
    <result column="login_password" property="loginPassword" jdbcType="VARCHAR" />
    <result column="email" property="email" jdbcType="VARCHAR" />
    <result column="head_portrait" property="headPortrait" jdbcType="VARCHAR" />
    <result column="remark" property="remark" jdbcType="VARCHAR" />
    <result column="status" property="status" jdbcType="CHAR" />
    <result column="create_user" property="createUser" jdbcType="INTEGER" />
    <result column="create_date" property="createDate" jdbcType="TIMESTAMP" />
    <result column="update_user" property="updateUser" jdbcType="INTEGER" />
    <result column="update_date" property="updateDate" jdbcType="TIMESTAMP" />
    <result column="role_name" property="roleName" jdbcType="VARCHAR" />
    <result column="role_id" property="roleId" jdbcType="VARCHAR" />
  </resultMap>
  <sql id="Base_Column_List" >
    id, user_name, login_name, login_password, email, head_portrait, remark, status, 
    create_user, create_date, update_user, update_date
  </sql>
  <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
    select 
    <include refid="Base_Column_List" />
    from to_user
    where id = #{id,jdbcType=INTEGER}
  </select>
  <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer" >
    delete from to_user
    where id = #{id,jdbcType=INTEGER}
  </delete>
  <insert id="insert" parameterType="com.community.huiju.model.ToUser" useGeneratedKeys="true" keyProperty="id" keyColumn="id">
    insert into to_user (id, user_name, login_name, 
      login_password, email, head_portrait, 
      remark, status, create_user, 
      create_date, update_user, update_date
      )
    values (#{id,jdbcType=INTEGER}, #{userName,jdbcType=VARCHAR}, #{loginName,jdbcType=VARCHAR}, 
      #{loginPassword,jdbcType=VARCHAR}, #{email,jdbcType=VARCHAR}, #{headPortrait,jdbcType=VARCHAR}, 
      #{remark,jdbcType=VARCHAR}, #{status,jdbcType=CHAR}, #{createUser,jdbcType=INTEGER}, 
      #{createDate,jdbcType=TIMESTAMP}, #{updateUser,jdbcType=INTEGER}, #{updateDate,jdbcType=TIMESTAMP}
      )
  </insert>
  <insert id="insertSelective" parameterType="com.community.huiju.model.ToUser" >
    insert into to_user
    <trim prefix="(" suffix=")" suffixOverrides="," >
      <if test="id != null" >
        id,
      </if>
      <if test="userName != null" >
        user_name,
      </if>
      <if test="loginName != null" >
        login_name,
      </if>
      <if test="loginPassword != null" >
        login_password,
      </if>
      <if test="email != null" >
        email,
      </if>
      <if test="headPortrait != null" >
        head_portrait,
      </if>
      <if test="remark != null" >
        remark,
      </if>
      <if test="status != null" >
        status,
      </if>
      <if test="createUser != null" >
        create_user,
      </if>
      <if test="createDate != null" >
        create_date,
      </if>
      <if test="updateUser != null" >
        update_user,
      </if>
      <if test="updateDate != null" >
        update_date,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides="," >
      <if test="id != null" >
        #{id,jdbcType=INTEGER},
      </if>
      <if test="userName != null" >
        #{userName,jdbcType=VARCHAR},
      </if>
      <if test="loginName != null" >
        #{loginName,jdbcType=VARCHAR},
      </if>
      <if test="loginPassword != null" >
        #{loginPassword,jdbcType=VARCHAR},
      </if>
      <if test="email != null" >
        #{email,jdbcType=VARCHAR},
      </if>
      <if test="headPortrait != null" >
        #{headPortrait,jdbcType=VARCHAR},
      </if>
      <if test="remark != null" >
        #{remark,jdbcType=VARCHAR},
      </if>
      <if test="status != null" >
        #{status,jdbcType=CHAR},
      </if>
      <if test="createUser != null" >
        #{createUser,jdbcType=INTEGER},
      </if>
      <if test="createDate != null" >
        #{createDate,jdbcType=TIMESTAMP},
      </if>
      <if test="updateUser != null" >
        #{updateUser,jdbcType=INTEGER},
      </if>
      <if test="updateDate != null" >
        #{updateDate,jdbcType=TIMESTAMP},
      </if>
    </trim>
  </insert>
  <update id="updateByPrimaryKeySelective" parameterType="com.community.huiju.model.ToUser" >
    update to_user
    <set >
      <if test="id != null" >
       id = #{id,jdbcType=INTEGER},
      </if>
      <if test="userName != null" >
        user_name = #{userName,jdbcType=VARCHAR},
      </if>
      <if test="loginName != null" >
        login_name = #{loginName,jdbcType=VARCHAR},
      </if>
      <if test="loginPassword != null" >
        login_password = #{loginPassword,jdbcType=VARCHAR},
      </if>
      <if test="email != null" >
        email = #{email,jdbcType=VARCHAR},
      </if>
      <if test="headPortrait != null" >
        head_portrait = #{headPortrait,jdbcType=VARCHAR},
      </if>
      <if test="remark != null" >
        remark = #{remark,jdbcType=VARCHAR},
      </if>
      <if test="status != null" >
        status = #{status,jdbcType=CHAR},
      </if>
      <if test="createUser != null" >
        create_user = #{createUser,jdbcType=INTEGER},
      </if>
      <if test="createDate != null" >
        create_date = #{createDate,jdbcType=TIMESTAMP},
      </if>
      <if test="updateUser != null" >
        update_user = #{updateUser,jdbcType=INTEGER},
      </if>
      <if test="updateDate != null" >
        update_date = #{updateDate,jdbcType=TIMESTAMP},
      </if>
    </set>
    where id = #{id,jdbcType=INTEGER}
  </update>
  <update id="updateByPrimaryKey" parameterType="com.community.huiju.model.ToUser" >
    update to_user
    set user_name = #{userName,jdbcType=VARCHAR},
      login_name = #{loginName,jdbcType=VARCHAR},
      login_password = #{loginPassword,jdbcType=VARCHAR},
      email = #{email,jdbcType=VARCHAR},
      head_portrait = #{headPortrait,jdbcType=VARCHAR},
      remark = #{remark,jdbcType=VARCHAR},
      status = #{status,jdbcType=CHAR},
      create_user = #{createUser,jdbcType=INTEGER},
      create_date = #{createDate,jdbcType=TIMESTAMP},
      update_user = #{updateUser,jdbcType=INTEGER},
      update_date = #{updateDate,jdbcType=TIMESTAMP}
    where id = #{id,jdbcType=INTEGER}
  </update>

  <select id="selectByLoginName" parameterType="string" resultMap="BaseResultMap" >
    select
    <include refid="Base_Column_List"/>
    from to_user where login_name = #{loginName,jdbcType=VARCHAR}
  </select>

  <select id="userList" parameterType="com.community.huiju.model.ToUser" resultMap="BaseResultMap" >
    SELECT
    tu.create_user,
    tu.create_date,
    tu.update_date,
    tu.update_user,
    tu.user_name,
    tu.remark,
    tu.login_name,
    tu.status,
    tu.id
    FROM
    to_user tu
    <where>
    <if test="loginName != null and loginName !=''" >
      and tu.login_name like concat('%',#{loginName,jdbcType=VARCHAR},'%')
    </if>
    <if test="userName != null and userName !=''" >
      and tu.user_name like concat('%',#{userName,jdbcType=VARCHAR},'%')
    </if>
    </where>
    order by  tu.create_date desc
  </select>

  <select id="selectByIdUserName" parameterType="int" resultType="string">
    SELECT
    user_name
    FROM
    to_user
    WHERE id=#{id,jdbcType=INTEGER}
  </select>

  <select id="selectUser"  resultMap="BaseResultMap" >
    SELECT
    sr.role_name,
    sr.description,
    sr.create_user,
    sr.create_date,
    sr.update_date,
    sr.update_user,
    tu.user_name,
    tu.remark,
    tu.login_name,
    tu.status,
    tu.id,
    to_sys_user_role.role_id
    FROM
    to_user tu
    LEFT JOIN to_sys_user_role ON tu.id = to_sys_user_role.user_id
    LEFT JOIN to_sys_role sr ON to_sys_user_role.role_id = sr.id
    WHERE tu.id=#{id,jdbcType=INTEGER}
  </select>
  <select id="selectRole"  resultMap="BaseResultMap" parameterType="int">
      SELECT
          sr.role_name,
          tu.remark,
          tu.login_name,
          tu.id,
          to_sys_user_role.role_id
      FROM
          to_user tu
      LEFT JOIN to_sys_user_role ON tu.id = to_sys_user_role.user_id
      LEFT JOIN to_sys_role sr ON to_sys_user_role.role_id = sr.id
      WHERE tu.id=#{id,jdbcType=INTEGER}
  </select>

  <select id="getPermission" resultType="java.util.Map">
        SELECT
            r.id,
            r.menu_name AS menuName,
            r.menu_url AS menuUrl,
            r.parent_id
        FROM
            to_sys_user_role t
            LEFT JOIN to_sys_role_menu m ON t.role_id = m.role_id
            LEFT JOIN to_sys_menu r ON m.menu_id = r.id
            AND r.STATUS = 0
        WHERE
            t.user_id = #{userId}
        GROUP BY
            r.id
    </select>

  <select id="selectByPermissionList" resultType="java.util.Map">
    SELECT id,menu_name as menuName,menu_url as menuUrl,parent_id FROM to_sys_menu WHERE id in
    <foreach item="item" index="index" collection="array" open="(" separator="," close=")">
      #{item}
    </foreach>
  </select>
</mapper>