<?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.lyms.platform.permission.dao.slave.PermissionsMapper">

    <resultMap id="PermissionsResultMap" type="com.lyms.platform.permission.model.Permissions">
        <id column="id" property="id" jdbcType="INTEGER"/>
        <result column="type" property="type" jdbcType="INTEGER"/>
        <result column="module_name" property="moduleName" jdbcType="VARCHAR"/>
        <result column="module_code" property="moduleCode" jdbcType="VARCHAR"/>
        <result column="function_name" property="functionName" jdbcType="VARCHAR"/>
        <result column="function_code" property="functionCode" jdbcType="VARCHAR"/>
        <result column="permission_name" property="permissionName" jdbcType="VARCHAR"/>
        <result column="icon_data" property="iconData" jdbcType="VARCHAR"/>
        <result column="action" property="action" jdbcType="VARCHAR"/>
        <result column="ext" property="ext" jdbcType="VARCHAR"/>
        <result column="url" property="url" jdbcType="VARCHAR"/>
        <result column="publish_id" property="publishId" jdbcType="INTEGER"/>
        <result column="publish_name" property="publishName" jdbcType="VARCHAR"/>
        <result column="yn" property="yn" jdbcType="INTEGER"/>
        <result column="modified" property="modified" jdbcType="TIMESTAMP"/>
        <result column="created" property="created" jdbcType="TIMESTAMP"/>
        <result column="ismenu" property="ismenu" jdbcType="INTEGER"/>
        <result column="weight" property="weight" jdbcType="INTEGER"/>
    </resultMap>


    <insert id="addPermissions" parameterType="com.lyms.platform.permission.model.Permissions">
        <selectKey order="AFTER" keyProperty="id" resultType="java.lang.Integer">SELECT LAST_INSERT_ID()</selectKey>
        insert into permissions
        (type,module_name,module_code,function_name,function_code,permission_name,action,ext,url,publish_id,publish_name,yn,modified,created,ismenu,weight
        <if test="id != null and id >= 0">
            ,id
        </if>
        ) values
        (#{type},#{moduleName},#{moduleCode},#{functionName},#{functionCode},#{permissionName},#{action},#{ext},#{url},#{publishId},#{publishName},#{yn},#{modified},#{created},#{ismenu},#{weight}
        <if test="id != null and id >= 0">
            ,#{id}
        </if>
        )
    </insert>


    <update id="updatePermissions" parameterType="com.lyms.platform.permission.model.Permissions">
        update permissions
        <set>
            <if test="type != null and type >= 0">
                type = #{type,jdbcType=INTEGER},
            </if>
            <if test="moduleName != null and moduleName != ''">
                module_name = #{moduleName,jdbcType=VARCHAR},
            </if>
            <if test="moduleCode != null and moduleCode != ''">
                module_code = #{moduleCode,jdbcType=VARCHAR},
            </if>
            <if test="functionName != null and functionName != ''">
                function_name = #{functionName,jdbcType=VARCHAR},
            </if>
            <if test="functionCode != null and functionCode != ''">
                function_code = #{functionCode,jdbcType=VARCHAR},
            </if>
            <if test="permissionName != null and permissionName != ''">
                permission_name = #{permissionName,jdbcType=VARCHAR},
            </if>
            <if test="action != null and action != ''">
                action = #{action,jdbcType=VARCHAR},
            </if>
            <if test="ext != null and ext != ''">
                ext = #{ext,jdbcType=VARCHAR},
            </if>
            <if test="url != null and url != ''">
                url = #{url,jdbcType=VARCHAR},
            </if>
            <if test="publishId != null and publishId >= 0">
                publish_id = #{publishId,jdbcType=INTEGER},
            </if>
            <if test="publishName != null and publishName != ''">
                publish_name = #{publishName,jdbcType=VARCHAR},
            </if>
            <if test="yn != null and yn >= 0">
                yn = #{yn,jdbcType=INTEGER},
            </if>
            <if test="modified != null">
                modified = #{modified,jdbcType=TIMESTAMP},
            </if>
            <if test="created != null">
                created = #{created,jdbcType=TIMESTAMP},
            </if>
            <if test="ismenu != null and ismenu >= 0">
                ismenu = #{ismenu,jdbcType=INTEGER},
            </if>
            <if test="weight != null and weight >= 0">
                weight = #{weight,jdbcType=INTEGER},
            </if>
        </set>
        where id = #{id,jdbcType=INTEGER}
    </update>


    <delete id="deletePermissions" parameterType="java.lang.Integer">
delete from permissions where id = #{id,jdbcType=INTEGER}
</delete>


    <select id="getPermissions" resultMap="PermissionsResultMap" parameterType="java.lang.Integer">
select id,type,module_name,module_code,function_name,function_code,permission_name,action,ext,url,publish_id,publish_name,yn,modified,created,ismenu,weight,icon_data
 from permissions where id = #{id,jdbcType=INTEGER}
</select>


    <sql id="orderAndLimit">

        order by weight
        <if test="need != null">
            limit #{offset, jdbcType=INTEGER} , #{limit, jdbcType=INTEGER}
        </if>

    </sql>


    <sql id="PermissionsCondition">
        <where>
            1 = 1
            <if test="id != null and id >= 0">
                and id = #{id,jdbcType=INTEGER}
            </if>
            <if test="type != null and type >= 0">
                and type = #{type,jdbcType=INTEGER}
            </if>
            <if test="moduleName != null and moduleName != ''">
                and module_name = #{moduleName,jdbcType=VARCHAR}
            </if>
            <if test="moduleCode != null and moduleCode != ''">
                and module_code = #{moduleCode,jdbcType=VARCHAR}
            </if>
            <if test="functionName != null and functionName != ''">
                and function_name = #{functionName,jdbcType=VARCHAR}
            </if>
            <if test="functionCode != null and functionCode != ''">
                and function_code = #{functionCode,jdbcType=VARCHAR}
            </if>
            <if test="permissionName != null and permissionName != ''">
                and permission_name = #{permissionName,jdbcType=VARCHAR}
            </if>
            <if test="action != null and action != ''">
                and action = #{action,jdbcType=VARCHAR}
            </if>
            <if test="ext != null and ext != ''">
                and ext = #{ext,jdbcType=VARCHAR}
            </if>
            <if test="url != null and url != ''">
                and url = #{url,jdbcType=VARCHAR}
            </if>
            <if test="publishId != null and publishId >= 0">
                and publish_id = #{publishId,jdbcType=INTEGER}
            </if>
            <if test="publishName != null and publishName != ''">
                and publish_name = #{publishName,jdbcType=VARCHAR}
            </if>
            <if test="yn != null and yn >= 0">
                and yn = #{yn,jdbcType=INTEGER}
            </if>
            <if test="modified != null">
                and modified = #{modified,jdbcType=TIMESTAMP}
            </if>
            <if test="created != null">
                and created = #{created,jdbcType=TIMESTAMP}
            </if>
            <if test="ismenu != null and ismenu >= 0">
                and ismenu = #{ismenu,jdbcType=INTEGER}
            </if>
            <if test="weight != null and weight >= 0">
                and weight = #{weight,jdbcType=INTEGER},
            </if>
        </where>
    </sql>


    <select id="queryPermissions" resultMap="PermissionsResultMap"
            parameterType="com.lyms.platform.permission.model.PermissionsQuery">
        select
        id,type,module_name,module_code,function_name,function_code,permission_name,action,ext,url,publish_id,publish_name,yn,modified,created,ismenu,weight,icon_data
        from permissions
        <include refid="PermissionsCondition"/>
        <include refid="orderAndLimit"/>
    </select>


    <select id="queryPermissionsCount" resultType="int"
            parameterType="com.lyms.platform.permission.model.PermissionsQuery">
        select count(1) from permissions
        <include refid="PermissionsCondition"/>
    </select>

    <select id="queryPermissionByRoleIds" parameterType="java.util.List" resultMap="PermissionsResultMap">
        select b.* from role_permission_maps a, permissions b where a.yn=1 and b.yn=1 and a.role_id
        in
        <foreach collection="list" index="index" item="item" open="(" separator="," close=")">
            #{item}
        </foreach>
        and a.permission_id=b.id ORDER BY b.weight
    </select>
    <select id="queryPermissionByRoleIds1" parameterType="int" resultMap="PermissionsResultMap">
      select b.* from role_permission_maps a, permissions b where  a.role_id=
      #{weight,jdbcType=INTEGER}
      and  b.yn=1 and a.yn=1 and a.permission_id=b.id;
   </select>
</mapper>