<?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.jeeplus.modules.sys.dao.RoleDao">

	<resultMap id="roleResult" type="Role">
		<id property="id" column="id" />
		<result property="name" column="name" />
		<result property="number" column="number" />
		<result property="enname" column="enname" />
		<result property="branchCompany.id" column="branchCompany.id" />
		<result property="roleType" column="roleType" />
		<result property="dataScope" column="dataScope" />
		<result property="remarks" column="remarks" />
		<result property="useable" column="useable" />
		<result property="office.id" column="office.id" />
		<result property="office.name" column="office.name" />
		<result property="office.topCompany" column="office.topCompany" />
		<result property="company.id" column="company.id" />
		<result property="company.name" column="company.name" />
		<result property="company.logo" column="company.logo" />
		<result property="company.master" column="company.master" />
		<result property="company.useable" column="company.useable" />
		<result property="company.companyQrcode" column="company.companyQrcode" />
		<result property="sysData" column="sysData" /><!--
		<collection property="userList" ofType="User">
			<id property="id" column="userList.id" />
			<id property="loginName" column="userList.loginName" />
			<id property="name" column="userList.name" />
			<id property="email" column="userList.email" />
			<id property="phone" column="userList.phone" />
			<id property="mobile" column="userList.mobile" />
			<id property="office.name" column="userList.office.name" />
			<id property="company.name" column="userList.company.name" />
		</collection> -->
		<collection property="menuList" ofType="Menu">
			<id property="id" column="menuList.id" />
		</collection>
		<collection property="roleActivityList" ofType="SysRoleActivity">
			<id property="id" column="roleActivityList.id" />
			<id property="name" column="roleActivityList.name" />
			<id property="type" column="roleActivityList.type" />
			<id property="remarks" column="roleActivityList.remarks" />
			<id property="enname" column="roleActivityList.enname" />
		</collection>
	</resultMap>
	<resultMap id="roleResults" type="Role">
		<id property="id" column="id" />
		<result property="name" column="name" />
		<result property="office.id" column="office.id" />
		<result property="office.name" column="office.name" />
		<result property="office.parentIds" column="office.parentIds" />
	</resultMap>

	<sql id="roleColumns">
		a.id,
		a.office_id AS "office.id",
		a.company_id AS "company.id",
		com.name AS "company.name",
		a.branch_company AS "branchCompany.id",
		a.name,
		a.number,
		a.enname,
		a.role_type AS roleType,
		a.data_scope AS dataScope,
		a.remarks,
		a.create_by AS "createBy.id",
		a.create_date,
		a.update_by AS "updateBy.id",
		a.update_date,
		a.del_flag,
		o.name AS "office.name",
		o.code,
		o.top_company AS "office.topCompany",
		com.logo AS "company.logo",
		com.company_qrcode AS "company.companyQrcode",
		com.master AS "company.master",
		com.useable AS "company.useable",
		a.useable AS useable,
		a.is_sys AS sysData
	</sql>
	<sql id="roleJoins">
		LEFT JOIN sys_office com ON com.id = a.company_id
	</sql>
	<select id="get" resultMap="roleResult">
		SELECT
		<include refid="roleColumns"/><!--
			ur.user_id AS "userList.id",
			u.login_name AS "userList.loginName",
			u.name AS "userList.name",
			u.email AS "userList.email",
			u.phone AS "userList.phone",
			u.mobile AS "userList.mobile",
			uc.name AS "userList.company.name",
			uo.name AS "userList.office.name",
			rm.menu_id AS "menuList.id", -->
		FROM sys_role a
		<include refid="roleJoins"/>
		LEFT JOIN sys_office o ON o.id = a.office_id <!--
		LEFT JOIN sys_user_role ur ON ur.role_id = a.id
		LEFT JOIN sys_user u ON u.id = ur.user_id
		LEFT JOIN sys_office uc ON uc.id = u.company_id
		LEFT JOIN sys_office uo ON uo.id = u.office_id
		LEFT JOIN sys_role_menu rm ON rm.role_id = a.id-->
		WHERE a.id = #{id} and a.del_flag='0'
	</select>
	<select id="getActivityByRole" resultType="Role">
		SELECT
		<include refid="roleColumns"/>
		FROM sys_role a
		<include refid="roleJoins"/>
		LEFT JOIN sys_office o ON o.id = a.office_id
		LEFT JOIN sys_role_activity_relation ar ON ar.role_id = a.id
		LEFT JOIN sys_role_activity ra ON ra.id = ar.role_activity_id
		WHERE a.del_flag='0' AND a.company_id = #{company.id} AND ra.id = #{activityId}
	</select>
	<select id="getByRoleActivity" resultMap="roleResult">
		SELECT
		<include refid="roleColumns"/>,
		ra.id AS "roleActivityList.id",
		ra.name AS "roleActivityList.name",
		ra.type AS "roleActivityList.type",
		ra.remarks AS "roleActivityList.remarks",
		ra.enname AS "roleActivityList.enname"
		FROM sys_role a
		<include refid="roleJoins"/>
		LEFT JOIN sys_office o ON o.id = a.office_id
		LEFT JOIN sys_role_activity_relation ar ON ar.role_id = a.id
		LEFT JOIN sys_role_activity ra ON ra.id = ar.role_activity_id
		WHERE a.id = #{id} and a.del_flag='0'
	</select>

	<select id="getByRoleActivityEnname" resultMap="roleResult">
		SELECT
		a.id
		FROM sys_role a
		JOIN sys_role_activity_relation ar ON ar.role_id = a.id
		JOIN sys_role_activity ra ON ra.id = ar.role_activity_id
		WHERE ra.enname = #{enname} and a.del_flag='0'
		<if test="office != null and office.id != null and office.id != ''">
			AND a.office_id = #{office.id}
		</if>
		<if test="branchCompany != null and branchCompany.id != null and branchCompany.id != ''">
			AND a.branch_company = #{branchCompany.id}
		</if>
		<if test="branchCompany != null and branchCompany.name != null and branchCompany.name != ''">
			AND a.branch_company IS NULL
		</if>
	</select>

	<select id="getMenuList" resultMap="roleResult">
		SELECT
		<include refid="roleColumns"/>,
		ur.user_id AS "userList.id",
		u.login_name AS "userList.loginName",
		u.name AS "userList.name",
		u.email AS "userList.email",
		u.phone AS "userList.phone",
		u.mobile AS "userList.mobile",
		uc.name AS "userList.company.name",
		uo.name AS "userList.office.name",
		rm.menu_id AS "menuList.id"
		FROM sys_role a
		<include refid="roleJoins"/>
		LEFT JOIN sys_office o ON o.id = a.office_id
		LEFT JOIN sys_user_role ur ON ur.role_id = a.id
		LEFT JOIN sys_user u ON u.id = ur.user_id
		LEFT JOIN sys_office uc ON uc.id = u.company_id
		LEFT JOIN sys_office uo ON uo.id = u.office_id
		LEFT JOIN sys_role_menu rm ON rm.role_id = a.id
		WHERE a.id = #{id} and a.del_flag='0'
	</select>

	<select id="getRolesByMenu" resultType="Role">
		SELECT
            a.id,
		a.office_id AS "office.id",
		a.company_id AS "company.id",
		a.branch_company AS "branchCompany.id",
		a.name,
		a.number,
		a.enname,
		a.role_type AS roleType,
		a.data_scope AS dataScope,
		a.remarks,
		a.create_by AS "createBy.id",
		a.create_date,
		a.update_by AS "updateBy.id",
		a.update_date,
		a.del_flag,
		a.useable AS useable,
		a.is_sys AS sysData,
		com.parent_ids AS "company.parentIds",
		o.parent_ids AS "office.parentIds",
		br.parent_ids AS "branchCompany.parentIds"
        FROM sys_role a
        LEFT JOIN sys_user_role ur ON ur.role_id = a.id
        LEFT JOIN sys_role_menu rm ON rm.role_id = a.id
        LEFT JOIN sys_office com ON com.id = a.company_id
        LEFT JOIN sys_office o ON o.id = a.office_id
        LEFT JOIN sys_office br ON br.id = a.branch_company
        WHERE ur.user_id = #{userId} AND rm.menu_id = #{menuId} AND a.company_id = #{companyId} AND a.del_flag='0';
	</select>
	<select id="getByName" resultType="Role">
		SELECT
		<include refid="roleColumns"/>
		FROM sys_role a
		<include refid="roleJoins"/>
		JOIN sys_office o ON o.id = a.office_id
		WHERE a.name = #{name} AND a.del_flag = #{DEL_FLAG_NORMAL} AND a.company_id = #{company.id}
	</select>
	<select id="getByGlyCount" resultType="java.lang.Integer">
		SELECT COUNT(*)
		FROM sys_role a
		LEFT JOIN sys_user_role ur ON a.id = ur.role_id
		WHERE a.del_flag = #{DEL_FLAG_NORMAL} AND a.enname in ('gly','fgly') AND a.company_id = a.company_id
		AND a.company_id = #{company.id}
		AND ur.user_id = #{user.id}
	</select>
	<select id="getMaxNumberByOffice" resultType="java.lang.Integer">
		SELECT MAX(number)
		FROM sys_role
		WHERE del_flag = #{DEL_FLAG_NORMAL} AND office_id = #{office.id}
	</select>

	<select id="getByEnname" resultType="Role">
		SELECT
		<include refid="roleColumns"/>
		FROM sys_role a
		<include refid="roleJoins"/>
		JOIN sys_office o ON o.id = a.office_id
		WHERE a.del_flag = #{DEL_FLAG_NORMAL} AND a.company_id = #{company.id}
        <if test="office!=null and office.id!=null and office.id!=''">
            AND a.office_id = #{office.id}
        </if>
        <if test="enname!=null and enname!=''">
            AND a.enname = #{enname}
        </if>
	</select>

	<select id="findList" resultMap="roleResult">
		SELECT <!-- DISTINCT -->
		<include refid="roleColumns"/>
		FROM sys_role a
		<include refid="roleJoins"/>
		LEFT JOIN sys_office o ON o.id = a.office_id
		LEFT JOIN sys_user_role ur ON ur.role_id = a.id
		LEFT JOIN sys_user u ON u.id = ur.user_id
		WHERE a.del_flag = #{DEL_FLAG_NORMAL} AND a.useable=#{useable} and o.useable = '1'
		<if test="user != null and user.id != null and user.id != ''">
			AND u.id = #{user.id}
		</if>
		<if test="company != null and company.id != null and company.id != ''">
			AND a.company_id = #{company.id}
		</if>
		<if test="office != null and office.id != null and office.id != ''">
			AND a.office_id = #{office.id}
		</if>
		<if test="user != null and user.loginName != null and user.loginName != ''">
			AND u.login_name = #{user.loginName}
		</if>
		<if test="name != null and name != ''">
			AND a.name like
			<if test="dbName == 'oracle'">'%'||#{name}||'%'</if>
			<if test="dbName == 'mysql'">CONCAT('%', #{name}, '%')</if>
		</if>
		<if test="enname != null and enname != ''">
			AND a.enname like
			<if test="dbName == 'oracle'">'%'||#{enname}||'%'</if>
			<if test="dbName == 'mysql'">CONCAT('%', #{enname}, '%')</if>
		</if>
		<!-- 数据范围过滤 -->
		${sqlMap.dsf}
		ORDER BY a.number
	</select>

	<select id="getRoleListByCompanys" resultMap="roleResults">
		SELECT
		DISTINCT
		a.id AS "id",
		a.name AS "name",
		a.number AS "number",
		o.id AS "office.id",
		o.name AS "office.name",
		o.top_company AS "office.topCompany",
		o.parent_ids AS "office.parentIds"
		FROM sys_role a
		LEFT JOIN sys_office o ON o.id = a.office_id
		LEFT JOIN sys_user_role ur ON ur.role_id = a.id
		LEFT JOIN sys_user u ON u.id = ur.user_id
		WHERE a.del_flag = #{DEL_FLAG_NORMAL} AND a.useable=#{useable} and o.USEABLE = '1'
		<if test="user != null and user.id != null and user.id != ''">
			AND u.id = #{user.id}
		</if>
		<if test="company != null and company.id != null and company.id != ''">
			AND a.company_id = #{company.id}
		</if>
		<if test="office != null and office.id != null and office.id != ''">
			AND a.office_id = #{office.id}
		</if>
		<if test="branchCompany != null and branchCompany.id != null and branchCompany.id != ''">
			AND (find_in_set(#{branchCompany.id},o.parent_ids) OR a.branch_company = #{branchCompany.id})
		</if>
		<if test="user != null and user.loginName != null and user.loginName != ''">
			AND u.login_name = #{user.loginName}
		</if>
		<if test="name != null and name != ''">
			AND a.name like
			<if test="dbName == 'oracle'">'%'||#{name}||'%'</if>
			<if test="dbName == 'mysql'">CONCAT('%', #{name}, '%')</if>
		</if>
		<if test="enname != null and enname != ''">
			AND a.enname like
			<if test="dbName == 'oracle'">'%'||#{enname}||'%'</if>
			<if test="dbName == 'mysql'">CONCAT('%', #{enname}, '%')</if>
		</if>
		<!-- 数据范围过滤 -->
		${sqlMap.dsf}
		ORDER BY a.number ASC  
	</select>

	<select id="findListRole" resultType="Role">
		SELECT <!-- DISTINCT -->
		<include refid="roleColumns"/>
		FROM sys_role a
		<include refid="roleJoins"/>
		LEFT JOIN sys_office o ON o.id = a.office_id
		<where>
			a.del_flag = #{DEL_FLAG_NORMAL}
			AND a.useable=#{useable}
			and o.USEABLE = '1'
			<if test="company != null and company.id != null and company.id != ''">
				AND a.company_id = #{company.id}
			</if>
			<if test="office != null and office.id != null and office.id != ''">
				AND a.office_id = #{office.id}
			</if>
			<if test="name != null and name != ''">
				AND a.name like
				<if test="dbName == 'oracle'">'%'||#{name}||'%'</if>
				<if test="dbName == 'mysql'">CONCAT('%', #{name}, '%')</if>
			</if>
			<if test="enname != null and enname != ''">
				AND a.enname like
				<if test="dbName == 'oracle'">'%'||#{enname}||'%'</if>
				<if test="dbName == 'mysql'">CONCAT('%', #{enname}, '%')</if>
			</if>
			<!-- 数据范围过滤 -->
			${sqlMap.dsf}
		</where>
		<choose>
			<when test="page !=null and page.orderBy != null and page.orderBy != ''">
				ORDER BY ${page.orderBy}
			</when>
			<otherwise>
				ORDER BY a.number
			</otherwise>
		</choose>
	</select>

	<select id="findRoleList" resultMap="roleResult">
		SELECT <!-- DISTINCT -->
		<include refid="roleColumns"/>
		FROM sys_role a
		<include refid="roleJoins"/>
		LEFT JOIN sys_office o ON o.id = a.office_id
		LEFT JOIN sys_user_role ur ON ur.role_id = a.id
		LEFT JOIN sys_user u ON u.id = ur.user_id
		WHERE a.del_flag = #{DEL_FLAG_NORMAL} AND a.useable=#{useable} and o.USEABLE = '1'
		<if test="user != null and user.id != null and user.id != ''">
			AND u.id = #{user.id}
		</if>
		<if test=" userId != null and userId != ''">
			AND u.id = #{userId}
		</if>
		<if test="user != null and user.comId != null and user.comId != ''">
			AND a.company_id= #{user.comId}
		</if>
		<if test="company != null and company.id != null and company.id != ''">
			AND a.company_id = #{company.id}
		</if>
		<if test="branchCompany != null and branchCompany.id != null and branchCompany.id != ''">
			AND a.branch_company = #{branchCompany.id}
		</if>
		<if test="user != null and user.loginName != null and user.loginName != ''">
			AND u.login_name = #{user.loginName}
		</if>
		<if test="name != null and name != ''">
			AND a.name like
			<if test="dbName == 'oracle'">'%'||#{name}||'%'</if>
			<if test="dbName == 'mysql'">CONCAT('%', #{name}, '%')</if>
		</if>
		<if test="enname != null and enname != ''">
			AND a.enname like
			<if test="dbName == 'oracle'">'%'||#{enname}||'%'</if>
			<if test="dbName == 'mysql'">CONCAT('%', #{enname}, '%')</if>
		</if>
		<!-- 数据范围过滤 -->
		${sqlMap.dsf}
		ORDER BY a.number
	</select>
	<select id="findAllList" resultType="Role">
		SELECT
		<include refid="roleColumns"/>
		FROM sys_role a
		<include refid="roleJoins"/>
		LEFT JOIN sys_office o ON o.id = a.office_id
		WHERE a.del_flag = #{DEL_FLAG_NORMAL}
		ORDER BY a.number
	</select>

	<select id="getRoleByCompanyId" resultMap="roleResult">
		SELECT <include refid="roleColumns"/>
		FROM sys_role a  <include refid="roleJoins"/>
		LEFT JOIN sys_office o ON o.id = a.office_id
		WHERE
		a.del_flag = '0' and o.USEABLE = '1'
		<if test="roleId !=null and roleId !=''">
			AND a.id = #{roleId}
		</if>
		<if test="companyId != null and companyId !=''">
			AND a.company_id = #{companyId}
		</if>
	</select>

	<insert id="insert">
		INSERT INTO sys_role(
		id,
		office_id,
		company_id,
		name,
		enname,
		role_type,
		data_scope,
		create_by,
		create_date,
		update_by,
		update_date,
		remarks,
		del_flag,
		is_sys,
		branch_company,
		number,
		useable
		) VALUES (
		#{id},
		#{office.id},
		#{company.id},
		#{name},
		#{enname},
		#{roleType},
		#{dataScope},
		#{createBy.id},
		#{createDate},
		#{updateBy.id},
		#{updateDate},
		#{remarks},
		#{delFlag},
		#{sysData},
		#{branchCompany.id},
		#{number},
		#{useable}
		)
	</insert>

	<update id="update">
		UPDATE sys_role SET
		office_id = #{office.id},
		company_id = #{company.id},
		name = #{name},
		role_type = #{roleType},
		data_scope = #{dataScope},
		update_by = #{updateBy.id},
		update_date = #{updateDate},
		remarks = #{remarks},
		is_sys = #{sysData},
		useable = #{useable},
		number = #{number},
		branch_company = #{branchCompany.id}
		WHERE id = #{id}
	</update>

	<delete id="deleteRoleMenu">
		DELETE FROM sys_role_menu WHERE role_id = #{id}
	</delete>

	<insert id="insertRoleMenu">
		INSERT INTO sys_role_menu(role_id, menu_id)
		<foreach collection="menuList" item="menu" separator=" union all ">
			SELECT #{id}, #{menu.id} FROM dual
		</foreach>
	</insert>

	<!--<delete id="deleteRoleOffice">
		DELETE FROM sys_role_office WHERE role_id = #{id}
	</delete>

	<insert id="insertRoleOffice">
		INSERT INTO sys_role_office(role_id, office_id)
		<foreach collection="officeList" item="office" separator=" union all ">
			SELECT #{id}, #{office.id} FROM dual
		</foreach>
	</insert>-->
	<delete id="deleteRoleActivity">
		DELETE FROM sys_role_activity_relation WHERE role_id = #{id}
	</delete>
	<delete id="deleteRoleActivityOne">
		DELETE FROM sys_role_activity_relation WHERE role_id = #{id} AND role_activity_id = #{roleActivityId}
	</delete>

	<insert id="insertRoleActivity">
		INSERT INTO sys_role_activity_relation(role_id, role_activity_id)
		<foreach collection="roleActivityList" item="roleActivity" separator=" union all ">
			SELECT #{id}, #{roleActivity.id} FROM dual
		</foreach>
	</insert>

	<delete id="deleteUserRole">
		DELETE FROM sys_user_role WHERE role_id = #{id}
	</delete>

	<update id="delete">
		DELETE FROM sys_role
		WHERE id = #{id}
	</update>

	<update id="deleteByLogic">
		UPDATE sys_role SET
		del_flag = #{DEL_FLAG_DELETE}
		WHERE id = #{id}
	</update>
	<select id="checkRole" resultType="Role">
		select * from sys_role where id in (
		select role_id from  sys_user_role  where user_id = #{0} and role_id in
		(select id from sys_role where company_id =#{1})
		) ORDER BY data_scope ASC
	</select>
	<select id="getRoleUserId" resultType="java.lang.String" >
		  select user_id from sys_user_role where role_id=(select id from  sys_role where company_id=#{0} and del_flag=0 and RIGHT(enname,4)='bmzg')
		   and  user_id in (select user_id from sys_user_office where company_id=#{0}) and user_id != #{1}
	</select>
	<select id="queryRoleEnameByOfficeId" resultType="Role">
		SELECT * FROM sys_role where company_id = #{officeId} and del_flag = 0 limit 1
	</select>
	<select id="findUserRoleCount" resultType="java.lang.Integer">
		SELECT count(1) FROM sys_user_role where role_id = #{id}
	</select>

	<select id="queryMasterByEnameAndOfficeId" resultType="java.lang.String" parameterType="java.util.HashMap">
		select * from (select a.id from sys_user a ,sys_role b,sys_user_role c
		where a.id = c.user_id and b.id = c.role_id
		and b.enname like concat('%',#{ename},'%') and b.company_id = #{companyId}) dt
		,sys_user_office su where dt.id = su.user_id and su.office_id = #{officeId} and su.del_flag = 0 and su.status = 3
	</select>
<!--部门调整用-->
	<select id="getRoleByOfficeId" resultType="Role">
		SELECT * FROM sys_role r where r.office_id = #{office.id} and r.name = #{name} and role_type = #{roleType}
	</select>

    <update id="deleteByOffice">
        UPDATE  sys_role a LEFT JOIN sys_office o ON o.id=a.office_id
        SET a.del_flag = '1' WHERE o.id = #{id} or find_in_set(#{id},o.parent_ids)
    </update>
	<select id="getManagerRoleList" resultType="Role">
		SELECT
		r.id,
		r.enname,
		r.office_id AS "office.id",
		r.company_id AS "company.id",
		o.name AS "office.name"
		FROM sys_role r LEFT JOIN sys_office o ON o.id = r.office_id AND o.del_flag = '0'
		INNER JOIN sys_user_role ur ON ur.role_id = r.id AND ur.user_id = #{userId}
		where (r.enname = 'gly' or r.enname = 'fgly') AND (find_in_set(#{companyId},o.parent_ids) OR o.id=#{companyId})
			and r.del_flag = '0'
	</select>

	<select id="getByOfficeId" resultType="Role" parameterType="String">
		select id ,
-- 		       office_id as office.id,
		       name
		from sys_role
		<where>
			office_id = #{officeId} and useable = '1' and del_flag = '0'
		</where>

	</select>

	<select id="getRoleIdByUserId" resultType="String">
		select role_id
		from sys_user_role
		where user_id = #{userId,jdbcType=VARCHAR}
	</select>

	<select id="getRoleByUserId" parameterType="String" resultType="Role">
		select r.*
		from sys_role r left join sys_user_role ur
		on r.id = ur.role_id
		where ur.user_id = #{userId}
	</select>

	<update id="updateRoleByUserId">
		update sys_user_role
		set role_id = #{roleId}
		where user_id = #{userId}
	</update>
</mapper>