<?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.ssm.mapper.system.SysRoleMapper" >
  <resultMap id="BaseResultMap" type="com.ssm.model.system.SysRole" >
    <id column="roleID" property="roleId" jdbcType="INTEGER" />
    <result column="roleName" property="roleName" jdbcType="VARCHAR" />
    <result column="roleDesc" property="roleDesc" jdbcType="VARCHAR" />
    <result column="isDeleted" property="isDeleted" jdbcType="BIT" />
    <result column="createdUserID" property="createdUserId" jdbcType="INTEGER" />
    <result column="createdDatetime" property="createdDateTime" jdbcType="TIMESTAMP" />
    <result column="lastUpdateUserID" property="lastupDateUserId" jdbcType="INTEGER" />
    <result column="lastUpdateDatetime" property="lastUpdateDateTime" jdbcType="TIMESTAMP" />
    <result column="isEnabled" property="isEnabled" jdbcType="BIT" /> 
    <result column="lastDate" property="lastDate" jdbcType="VARCHAR" />
    <collection property="menuList" ofType="com.ssm.model.system.SysMenu">
			<id property="menuid" column="menuid" />
			<result property="menucnname" column="menucnname" />
			<result column="MenuEnName" property="menuenname" jdbcType="VARCHAR" />
		    <result column="MenuUrl" property="menuurl" jdbcType="VARCHAR" />
		    <result column="ActionName" property="actionname" jdbcType="VARCHAR" />
		    <result column="ParentID" property="parentid" jdbcType="INTEGER" />
		    <result column="CreatedUserID" property="createduserid" jdbcType="INTEGER" />
		    <result column="CreatedDatetime" property="createddatetime" jdbcType="TIMESTAMP" />
		    <result column="LastUpdateUserID" property="lastupdateuserid" jdbcType="INTEGER" />
		    <result column="LastUpdateDatetime" property="lastupdatedatetime" jdbcType="TIMESTAMP" />
		    <result column="Level" property="level" jdbcType="INTEGER" />
		    <result column="MenuType" property="menutype" jdbcType="VARCHAR" />
		    <result column="pmenucnname" property="pmenucnname" jdbcType="VARCHAR" />
		    <result column="createdusername" property="createdusername" jdbcType="VARCHAR" />
		    <result column="OrderBy" property="orderby" jdbcType="CLOB" />
		    <result column="Icon" property="icon" jdbcType="CLOB" />
		    <result column="MenuState" property="menustate" jdbcType="CLOB" />
	</collection> 
  </resultMap>
  <sql id="Example_Where_Clause" >
    <where >
      <foreach collection="oredCriteria" item="criteria" separator="or" >
        <if test="criteria.valid" >
          <trim prefix="(" suffix=")" prefixOverrides="and" >
            <foreach collection="criteria.criteria" item="criterion" >
              <choose >
                <when test="criterion.noValue" >
                  and ${criterion.condition}
                </when>
                <when test="criterion.singleValue" >
                  and ${criterion.condition} #{criterion.value}
                </when>
                <when test="criterion.betweenValue" >
                  and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}
                </when>
                <when test="criterion.listValue" >
                  and ${criterion.condition}
                  <foreach collection="criterion.value" item="listItem" open="(" close=")" separator="," >
                    #{listItem}
                  </foreach>
                </when>
              </choose>
            </foreach>
          </trim>
        </if>
      </foreach>
    </where>
  </sql>
  <sql id="Update_By_Example_Where_Clause" >
    <where >
      <foreach collection="example.oredCriteria" item="criteria" separator="or" >
        <if test="criteria.valid" >
          <trim prefix="(" suffix=")" prefixOverrides="and" >
            <foreach collection="criteria.criteria" item="criterion" >
              <choose >
                <when test="criterion.noValue" >
                  and ${criterion.condition}
                </when>
                <when test="criterion.singleValue" >
                  and ${criterion.condition} #{criterion.value}
                </when>
                <when test="criterion.betweenValue" >
                  and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}
                </when>
                <when test="criterion.listValue" >
                  and ${criterion.condition}
                  <foreach collection="criterion.value" item="listItem" open="(" close=")" separator="," >
                    #{listItem}
                  </foreach>
                </when>
              </choose>
            </foreach>
          </trim>
        </if>
      </foreach>
    </where>
  </sql>
  <sql id="Base_Column_List" >
    RoleID, RoleName, RoleDesc, IsDeleted, CreatedUserID, CreatedDatetime, LastUpdateUserID, 
     LastUpdateDatetime,IsEnabled
<!--    ,CONVERT(varchar(100),CreatedDate,25) as cd -->
  </sql>
<!--    <select id="selectByExample" resultMap="BaseResultMap" parameterType="com.ssm.model.system.SysRoleExample" >
    select
    <if test="distinct" >
      distinct
    </if>
    <include refid="Base_Column_List" />
    from SysRole
    <if test="_parameter != null" >
      <include refid="Example_Where_Clause" />
    </if>
    <if test="orderByClause != null" >
      order by ${orderByClause}
    </if>
  </select>-->
  <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
    select *
    <!--  <include refid="Base_Column_List" />-->
    from SysRole
    where RoleID = #{roleId,jdbcType=INTEGER}
  </select>
  <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer" >
    delete from SysRole
    where RoleID = #{roleId,jdbcType=INTEGER}
  </delete>
  <delete id="deleteByExample" parameterType="com.ssm.model.system.SysRoleExample" >
    delete from SysRole
    <if test="_parameter != null" >
      <include refid="Example_Where_Clause" />
    </if>
  </delete>
  <insert id="insert" parameterType="com.ssm.model.system.SysRole" >
    insert into SysRole (RoleName, RoleDesc, 
      IsDeleted, CreatedUserID, CreatedDatetime, 
      LastUpdateUserID, LastUpdateDatetime, 
      IsEnabled)
    values (#{roleName,jdbcType=VARCHAR}, #{roleDesc,jdbcType=VARCHAR}, 
      #{isDeleted,jdbcType=BIT}, #{createdUserId,jdbcType=INTEGER}, #{createdDateTime,jdbcType=TIMESTAMP}, 
      #{lastUpdateUserId,jdbcType=INTEGER}, #{lastUpdateDateTime,jdbcType=TIMESTAMP}, 
      #{isEnabled,jdbcType=BIT})
  </insert>
  <insert id="insertSelective" parameterType="com.ssm.model.system.SysRole" >
    insert into SysRole
    <trim prefix="(" suffix=")" suffixOverrides="," >
      <if test="roleId != null" >
        RoleID,
      </if>
      <if test="roleName != null" >
        RoleName,
      </if>
      <if test="roleDesc != null" >
        RoleDesc,
      </if>
      <if test="isDeleted != null" >
        IsDeleted,
      </if>
      <if test="createdUserId != null" >
        CreatedUserID,
      </if>
      <if test="createdDateTime != null" >
        CreatedDatetime,
      </if>
      <if test="lastUpdateUserId != null" >
        LastUpdateUserID,
      </if>
      <if test="lastUpdateDateTime != null" >
        LastUpdateDatetime,
      </if>
      <if test="isEnabled != null" >
        IsEnabled,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides="," >
      <if test="roleId != null" >
        #{roleId,jdbcType=INTEGER},
      </if>
      <if test="roleName != null" >
        #{roleName,jdbcType=VARCHAR},
      </if>
      <if test="roleDesc != null" >
        #{roleDesc,jdbcType=VARCHAR},
      </if>
      <if test="isDeleted != null" >
        #{isDeleted,jdbcType=BIT},
      </if>
      <if test="createdUserId != null" >
        #{createdUserId,jdbcType=INTEGER},
      </if>
      <if test="createdDateTime != null" >
        #{createdDateTime,jdbcType=TIMESTAMP},
      </if>
      <if test="lastUpdateUserId != null" >
        #{lastupDateUserId,jdbcType=INTEGER},
      </if>
      <if test="lastUpdateDateTime != null" >
        #{lastUpdateDateTime,jdbcType=TIMESTAMP},
      </if>
      <if test="isEnabled != null" >
        #{isEnabled,jdbcType=BIT},
      </if>
    </trim>
  </insert>
  <select id="countByExample" parameterType="com.ssm.model.system.SysRoleExample" resultType="java.lang.Integer" >
    select count(*) from SysRole
    <if test="_parameter != null" >
      <include refid="Example_Where_Clause" />
    </if>
  </select>
  <update id="updateByExampleSelective" parameterType="map" >
    update SysRole
    <set >
      <if test="record.roleId != null" >
        RoleID = #{record.roleId,jdbcType=INTEGER},
      </if>
      <if test="record.roleName != null" >
        RoleName = #{record.roleName,jdbcType=VARCHAR},
      </if>
      <if test="record.roleDesc != null" >
        RoleDesc = #{record.roleDesc,jdbcType=VARCHAR},
      </if>
      <if test="record.isDeleted != null" >
        IsDeleted = #{record.isDeleted,jdbcType=BIT},
      </if>
      <if test="record.createdUserId != null" >
        CreatedUserID = #{record.createdUserId,jdbcType=INTEGER},
      </if>
      <if test="record.createdDateTime != null" >
        CreatedDatetime = #{record.createdDateTime,jdbcType=TIMESTAMP},
      </if>
      <if test="record.lastupDateUserId != null" >
        LastUpdateUserID = #{record.lastupDateUserId,jdbcType=INTEGER},
      </if>
      <if test="record.lastUpdateDateTime != null" >
        LastUpdateDatetime = #{record.lastUpdateDateTime,jdbcType=TIMESTAMP},
      </if>
      <if test="record.isEnabled != null" >
        IsEnabled = #{record.isEnabled,jdbcType=BIT},
      </if>
    </set>
    <if test="_parameter != null" >
      <include refid="Update_By_Example_Where_Clause" />
    </if>
  </update>
  <update id="updateByExample" parameterType="map" >
    update SysRole
    set RoleID = #{record.roleId,jdbcType=INTEGER},
      RoleName = #{record.roleName,jdbcType=VARCHAR},
      RoleDesc = #{record.roleDesc,jdbcType=VARCHAR},
      IsDeleted = #{record.isDeleted,jdbcType=BIT},
      CreatedUserID = #{record.createdUserId,jdbcType=INTEGER},
      CreatedDatetime = #{record.createdDateTime,jdbcType=TIMESTAMP},
      LastUpdateUserID = #{record.lastupDateUserId,jdbcType=INTEGER},
      LastUpdateDatetime = #{record.lastUpdateDateTime,jdbcType=TIMESTAMP},
      IsEnabled = #{record.isEnabled,jdbcType=BIT}
    <if test="_parameter != null" >
      <include refid="Update_By_Example_Where_Clause" />
    </if>
  </update>
  <update id="updateByPrimaryKeySelective" parameterType="com.ssm.model.system.SysRole" >
    update SysRole
    <set >
      <if test="roleName != null" >
        RoleName = #{roleName,jdbcType=VARCHAR},
      </if>
      <if test="roleDesc != null" >
        RoleDesc = #{roleDesc,jdbcType=VARCHAR},
      </if>
      <if test="isDeleted != null" >
        IsDeleted = #{isDeleted,jdbcType=BIT},
      </if>
      <if test="createdUserId != null" >
        CreatedUserID = #{createdUserId,jdbcType=INTEGER},
      </if>
      <if test="createdDateTime != null" >
        CreatedDatetime = #{createdDateTime,jdbcType=TIMESTAMP},
      </if>
      <if test="lastupDateUserId != null" >
        LastUpdateUserID = #{lastupDateUserId,jdbcType=INTEGER},
      </if>
      <if test="lastUpdateDateTime != null" >
        LastUpdateDatetime = #{lastUpdateDateTime,jdbcType=TIMESTAMP},
      </if>
      <if test="isEnabled != null" >
        IsEnabled = #{isEnabled,jdbcType=BIT},
      </if>
    </set>
    where RoleID = #{roleId,jdbcType=INTEGER}
  </update>
  <update id="updateByPrimaryKey" parameterType="com.ssm.model.system.SysRole" >
    update SysRole
    set RoleName = #{roleName,jdbcType=VARCHAR},
      RoleDesc = #{roleDesc,jdbcType=VARCHAR},
      IsDeleted = #{isDeleted,jdbcType=BIT},
      CreatedUserID = #{createdUserId,jdbcType=INTEGER},
      CreatedDatetime = now(),
      LastUpdateUserID = #{lastUpdateUserId,jdbcType=INTEGER},
      LastUpdateDatetime = now(),
      IsEnabled = #{isEnabled,jdbcType=BIT}
    where RoleID = #{roleId,jdbcType=INTEGER}
  </update>
  
  <select id="findAll" resultMap="BaseResultMap" parameterType="string">
     select 
            <include refid="Base_Column_List" />
          
     from 
     		SysRole        
     where 1 = 1 
     AND isDeleted = 0
     	<if test="_parameter != null and _parameter != ''">      
             AND roleName LIKE '%${_parameter}%'
     	</if>
     order by roleId
  </select>
  
  <select id="selectMenusByRoleId" resultType="java.util.List" parameterType="java.lang.Integer">
   	    SELECT
			su.*
		FROM
			SysRoleMenu sm
		LEFT JOIN SysMenu su ON sm.MenuID = su.MenuID
		WHERE
			sm.RoleID = #{roleId}
		AND isDelete = 0	
     	ORDER BY su.OrderBy
			
  </select>
	
  <select id="selectRoleAndMenuList" resultMap="BaseResultMap" parameterType="java.lang.String">
  		SELECT
			sr.RoleID,
			sr.RoleName,
			sm.MenuID,
			sm.MenuCnName,
			sm.MenuEnName,
		    sm.MenuUrl,
		   	sm.ActionName,
		    sm.ParentID,
		    sm.Level,
		    sm.OrderBy,
		    sm.Icon,
		    sm.MenuType
		FROM
			SysRole sr
		INNER JOIN SysRoleMenu srm ON sr.RoleID = srm.RoleID
		INNER JOIN SysMenu sm ON srm.MenuID = sm.MenuID and sm.isDelete = 0 and sm.isenable = 0
		where 1=1 AND sr.IsDeleted=0 AND sr.IsEnabled = 0
  </select>	  
  <select id="findUserRoleByUserId" parameterType="int" resultMap="BaseResultMap" >
  		SELECT sr.* from SysRole sr, SysUserRole sur
		WHERE sr.RoleID=sur.RoleID AND sur.UserID=#{userid} AND sr.IsDeleted=0 AND sr.IsEnabled = 0
  </select>
<!--   <insert  id="addRoleRelevancePer"  parameterType="java.lang.Integer"> -->
<!--    	  INSERT INTO SYSROLEMENU(ROLEID,MENUID) VALUES(#{roleId,jdbcType=INTEGER},#{menuId,jdbcType=INTEGER}) -->
<!--   </insert > -->
  
  <insert id = "addRoleRelevancePer"  parameterType="java.lang.Integer"> 
  INSERT INTO SysRoleMenu (
	ROLEID,
	MENUID
	)
	VALUES
		(
			#{ roleId }, 
			#{ menuId }
		)
	</insert>
	
	
   <select id = "selectRoleRelevancePer"  resultType="java.lang.Integer" parameterType="java.lang.Integer"> 
  		SELECT count(*) FROM SYSROLEMENU WHERE ROLEID = #{roleId} AND MENUID = #{menuId}
   </select>
	
	<select id = "selectUserByRoleId"  resultType="com.ssm.model.system.SysUser" parameterType="java.lang.Integer"> 
	  	SELECT
			su.*
		FROM
			SysUser su
		LEFT JOIN SysUserRole sr ON su.UserID = sr.UserID
		WHERE
			sr.RoleID = #{roleId}
			<if test="username != null and username != ''">
					 AND username LIKE '%${username}%'
			</if>
			<if test="employeeno != null and employeeno != ''">
					 AND employeeno LIKE '%${employeeno}%'
			</if>
			
		Order by su.userId
	</select>
	
	<delete id="deleteUserByRole" parameterType="java.lang.Integer">
	       delete from SysUserRole where userid = #{userId} and roleid = #{roleId}
	</delete>
	
	<delete id="deletePerPageByRoleIdAndMenuId" parameterType="java.lang.Integer">
	       delete from SysRoleMenu where menuId = #{menuId} and roleId = #{roleId}
	</delete>
	
	<select id="selectByRoleName"  resultMap="BaseResultMap">
		SELECT * from SysRole s where s.RoleName=#{roleName}
	</select>
	
</mapper>