<?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.iamberry.wechat.service.sys.mapper.SysMapper">

	<!-- 根据用户名称获取用户的盐值 -->
	<select id="getSaltByLoginName" parameterType="String" resultType="String">
		SELECT admin_salt FROM tb_sys_admin WHERE admin_account = #{loginName}
	</select>

	<!-- 获取管理员信息 -->
	<select id="get" parameterType="Admin" resultType="Admin">
		SELECT * FROM tb_sys_admin
		WHERE
		<choose>
			<when test="adminAccount != null">
				admin_account = #{adminAccount}
			</when>
			<otherwise>
				admin_id = #{adminId}
			</otherwise>
		</choose>
	</select>

	<!-- 修改管理员信息 -->
	<update id="updateAdmin" parameterType="Admin">
		UPDATE tb_sys_admin
		<set>
			<if test="adminName != null">
				admin_name = #{adminName},
			</if>
			<if test="adminSex != null">
				admin_sex = #{adminSex},
			</if>
			<if test="adminTel != null">
				admin_tel = #{adminTel},
			</if>
			<if test="adminPassword != null">
				admin_password = #{adminPassword},
			</if>
			<if test="adminSalt != null">
				admin_salt = #{adminSalt},
			</if>
			<if test="adminStatus != null">
				admin_status = #{adminStatus},
			</if>
			<if test="adminMail != null">
				admin_mail = #{adminMail},
			</if>
			<if test="ruleId != null">
				rule_id = #{ruleId},
			</if>
			admin_update_time = NOW()
		</set>
		WHERE admin_id = #{adminId}
	</update>

	<!-- 查询信息 -->
	<select id="listAdmin" parameterType="Admin" resultType="Admin">
		SELECT
			a.*, r.rule_name "rule.ruleName"
		FROM tb_sys_admin a
		left join tb_sys_rule r on a.rule_id = r.rule_id
		<where>
			<if test="adminName != null and adminName != ''">
				AND admin_name LIKE CONCAT('%', #{adminName},'%')
			</if>
			<if test="adminSex != null and adminSex != ''">
				AND admin_sex = #{adminSex}
			</if>
			<if test="adminTel != null and adminTel != ''">
				AND admin_tel LIKE CONCAT('%', #{adminTel},'%')
			</if>
			<if test="adminMail != null and adminMail != ''">
				AND admin_mail LIKE CONCAT('%', #{adminMail},'%')
			</if>
			<if test="adminAccount != null and adminAccount != ''">
				AND admin_account = #{adminAccount}
			</if>
			<if test="adminId != null">
				AND admin_id = #{adminId}
			</if>
		</where>
	</select>

	<!-- 添加管理员信息 -->
	<insert id="addAdmin" parameterType="Admin" useGeneratedKeys="true" keyProperty="adminId">
		INSERT INTO tb_sys_admin(admin_name, rule_id, supplier_id, admin_sex, admin_tel, admin_account, admin_password, admin_salt, admin_status, admin_mail, admin_create_time, admin_update_time)
		VALUES (#{adminName}, #{ruleId}, #{supplierId}, #{adminSex}, #{adminTel}, #{adminAccount}, #{adminPassword}, #{adminSalt}, #{adminStatus}, #{adminMail}, NOW(), NOW())
	</insert>

	<!-- 查询菜单信息 -->
	<select id="listMenu" resultMap="MenuMap">
		select * from tb_sys_menu m
		left join tb_sys_sub_menu s on m.menu_id = s.sub_menu_id and s.sub_status = 1
		where m.menu_status = 1
	</select>

	<resultMap type="NewMenu" id="MenuMap">
		<id column="menu_id" property="menuId"/>
		<result column="menu_name" property="menuName"/>
		<result column="menu_icon" property="menuIcon"/>
		<result column="menu_status" property="menuStatus"/>
		<result column="menu_create_time" property="menuCreateTime"/>
		<collection property="subMenus" ofType="SubMenu">
			<id column="sub_id" property="subId"/>
			<result column="sub_menu_id" property="subMenuId"/>
			<result column="sub_name" property="subName"/>
			<result column="sub_icon" property="subIcon"/>
			<result column="sub_status" property="subStatus"/>
			<result column="sub_url" property="subUrl"/>
			<result column="sub_create_time" property="subCreateTime"/>
		</collection>
	</resultMap>

	<!-- 根据角色查询菜单信息 -->
	<select id="listMenuByRule" parameterType="Integer" resultMap="MenuMap">
		select m.*, s.* from tb_sys_admin_pocess p
		left join tb_sys_menu m on m.menu_id = p.menu_id and m.menu_status = 1
		left join tb_sys_sub_menu s on s.sub_id = p.sub_id and s.sub_status = 1
		where p.rule_id = #{ruleId}
	</select>
	
	<!-- 根据角色查询权限 -->
	<select id="listSymbolByRule" parameterType="Integer" resultType="String">
		select pocess_symbol from tb_sys_admin_pocess where rule_id = #{ruleId}
	</select>

	<!-- 查询所有的角色 -->
	<select id="listRule" parameterType="Integer" resultType="SysRule">
		select * from tb_sys_rule where supplier_id = #{supplierId} and rule_state = 1
	</select>

    <!-- 获取单个角色信息 -->
    <select id="getRule" parameterType="Integer" resultType="SysRule">
        select * from tb_sys_rule where rule_id = #{ruleId}
    </select>

    <update id="editRule" parameterType="SysRule" >
        update tb_sys_rule
        <set >
            <if test="ruleName != null" >
                rule_name = #{ruleName,jdbcType=VARCHAR},
            </if>
            <if test="ruleState != null" >
                rule_state = #{ruleState,jdbcType=TINYINT},
            </if>
            <if test="ruleOrderPermission != null" >
				rule_order_permission = #{ruleOrderPermission,jdbcType=TINYINT},
            </if>
            rule_update_time = NOW()
        </set>
        where rule_id = #{ruleId,jdbcType=INTEGER}
    </update>

	<!-- 添加角色 -->
	<insert id="addRule" parameterType="SysRule" useGeneratedKeys="true" keyProperty="ruleId">
		insert into tb_sys_rule (rule_name, rule_state, rule_create_time, rule_update_time, supplier_id, rule_order_permission)
		values (#{ruleName,jdbcType=VARCHAR}, #{ruleState,jdbcType=TINYINT}, NOW(), NOW(), #{supplierId}, #{ruleOrderPermission})
	</insert>
	
	<!-- 根据二级菜单查询菜单权限 -->
	<select id="listPermisssionBySubId" parameterType="Integer" resultType="MenuPermisssion">
		select
			   p.sub_id "permSubId", p.menu_id "permMenuId", p.perm_name,
			   p.perm_create_time, p.perm_id, p.perm_status, p.perm_symbol
		from tb_sys_menu_permisssion p where sub_id = #{subId}
	</select>

	<!-- 批量增加权限 -->
	<insert id="addPocess" parameterType="AdminPocess" useGeneratedKeys="true" keyProperty="pocessId">
		INSERT INTO tb_sys_admin_pocess(menu_id, sub_id, pocess_symbol, perm_id, rule_id, pocess_create_time)
		VALUES (#{menuId}, #{subId}, #{pocessSymbol}, #{permId}, #{ruleId}, NOW())
	</insert>

    <!-- 删除角色对应的权限 -->
    <delete id="delPocess" parameterType="Integer">
        delete from tb_sys_admin_pocess where rule_id = #{ruleId}
    </delete>
</mapper>
