<?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.ho.user.mapper.SysPermissionMapper">

    <sql id="Base_Column_List">
        id, code, `name`, perms, url,url_app, icon, icon_app,`method`, pid, order_num, `type`,`show`, color, `status`, create_time,
    update_time, deleted, `scope`,is_config ,component, menu_name
    </sql>

    <sql id="Base_Column_List1">
        id, code, name_en name, perms, url,url_app, icon, icon_app,`method`, pid, order_num, `type`,`show`, color, `status`, create_time,
    update_time, deleted, `scope`,is_config ,component, menu_name
    </sql>

    <sql id="Base_Column_List2">
        id, code, name_fra name, perms, url,url_app, icon, icon_app,`method`, pid, order_num, `type`,`show`, color, `status`, create_time,
    update_time, deleted, `scope`,is_config ,component, menu_name
    </sql>

    <sql id="Base_Column_List3">
        id, code, name_spa name, perms, url,url_app, icon, icon_app,`method`, pid, order_num, `type`,`show`, color, `status`, create_time,
    update_time, deleted, `scope`,is_config ,component, menu_name
    </sql>
    <sql id="Base_Column_List4">
        id, code, name_de name, perms, url,url_app, icon, icon_app,`method`, pid, order_num, `type`,`show`, color, `status`, create_time,
    update_time, deleted, `scope`,is_config ,component, menu_name
    </sql>
    <sql id="Base_Column_List5">
        id, code, name_pl name, perms, url,url_app, icon, icon_app,`method`, pid, order_num, `type`,`show`, color, `status`, create_time,
    update_time, deleted, `scope`,is_config ,component, menu_name
    </sql>
    <sql id="Base_Column_List6">
        id, code, name_ara name, perms, url,url_app, icon, icon_app,`method`, pid, order_num, `type`,`show`, color, `status`, create_time,
    update_time, deleted, `scope`,is_config ,component, menu_name
    </sql>

    <select id="selectByPrimaryKey" parameterType="java.lang.Integer"
            resultType="com.ho.common.tools.entity.SysPermission">
        select
        <include refid="Base_Column_List"/>
        from sys_permission
        where id = #{id}
    </select>

    <select id="selectOneByCondition" parameterType="java.lang.Integer"
            resultType="com.ho.common.tools.entity.SysPermission">
        select
        <include refid="Base_Column_List"/>
        from sys_permission
        <where>
            <if test="name!=null and name !=''">
                and name = #{name}
            </if>
            <if test="id!=null">
                and id = #{id}
            </if>
            <if test="pid!=null">
                and pid = #{pid}
            </if>
            and deleted = 1
        </where>

    </select>

    <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer">
        delete
        from sys_permission
        where id = #{id}
    </delete>

    <insert id="insert" parameterType="com.ho.common.tools.entity.SysPermission">
        insert into sys_permission (code, `name`,
                                    perms, url, icon, `method`,
                                    pid, order_num, `type`,
                                    `status`, create_time, update_time,
                                    deleted, `scope`)
        values (#{code,jdbcType=VARCHAR}, #{name,jdbcType=VARCHAR},
                #{perms,jdbcType=VARCHAR}, #{url,jdbcType=VARCHAR}, #{icon,jdbcType=VARCHAR},
                #{method,jdbcType=VARCHAR},
                #{pid,jdbcType=INTEGER}, #{orderNum,jdbcType=INTEGER}, #{type,jdbcType=TINYINT},
                #{status,jdbcType=TINYINT}, #{createTime,jdbcType=TIMESTAMP}, #{updateTime,jdbcType=TIMESTAMP},
                #{deleted,jdbcType=TINYINT}, #{scope})
    </insert>

    <insert id="insertSelective"  keyProperty="id" parameterType="com.ho.common.tools.entity.SysPermission"
            useGeneratedKeys="true">
        insert into sys_permission
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="record.id != null">
                id,
            </if>
            <if test="record.code != null">
                code,
            </if>
            <if test="record.name != null">
                `name`,
            </if>
            <if test="record.perms != null">
                perms,
            </if>
            <if test="record.url != null">
                url,
            </if>
            <if test="record.urlApp != null">
                url_app,
            </if>
            <if test="record.icon != null">
                icon,
            </if>
            <if test="record.iconApp != null">
                icon_app,
            </if>
            <if test="record.method != null">
                `method`,
            </if>
            <if test="record.pid != null">
                pid,
            </if>
            <if test="record.orderNum != null">
                order_num,
            </if>
            <if test="record.type != null">
                `type`,
            </if>
            <if test="record.show != null">
                `show`,
            </if>
            <if test="record.status != null">
                `status`,
            </if>
            <if test="record.createTime != null">
                create_time,
            </if>
            <if test="record.updateTime != null">
                update_time,
            </if>
            <if test="record.deleted != null">
                deleted,
            </if>
            <if test="record.isConfig != null">
                is_config,
            </if>
            <if test="record.component != null">
                component,
            </if>
            <if test="record.menuName != null">
                menu_name,
            </if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="record.id != null">
                #{record.id},
            </if>
            <if test="record.code != null">
                #{record.code,jdbcType=VARCHAR},
            </if>
            <if test="record.name != null">
                #{record.name,jdbcType=VARCHAR},
            </if>
            <if test="record.perms != null">
                #{record.perms,jdbcType=VARCHAR},
            </if>
            <if test="record.url != null">
                #{record.url,jdbcType=VARCHAR},
            </if>
            <if test="record.urlApp != null">
                #{record.urlApp},
            </if>
            <if test="record.icon != null">
                #{record.icon,jdbcType=VARCHAR},
            </if>
            <if test="record.iconApp != null">
                #{record.iconApp},
            </if>
            <if test="record.method != null">
                #{record.method,jdbcType=VARCHAR},
            </if>
            <if test="record.pid != null">
                #{record.pid},
            </if>
            <if test="record.orderNum != null">
                #{record.orderNum,jdbcType=INTEGER},
            </if>
            <if test="record.type != null">
                #{record.type,jdbcType=TINYINT},
            </if>
            <if test="record.show != null">
                #{record.show},
            </if>
            <if test="record.status != null">
                #{record.status,jdbcType=TINYINT},
            </if>
            <if test="record.createTime != null">
                #{record.createTime,jdbcType=TIMESTAMP},
            </if>
            <if test="record.updateTime != null">
                #{record.updateTime,jdbcType=TIMESTAMP},
            </if>
            <if test="record.deleted != null">
                #{record.deleted,jdbcType=TINYINT},
            </if>
            <if test="record.isConfig != null">
                #{record.isConfig},
            </if>
            <if test="record.component != null">
                #{record.component},
            </if>
            <if test="record.menuName != null">
                #{record.menuName},
            </if>
        </trim>
    </insert>

    <update id="updateByPrimaryKeySelective" parameterType="com.ho.common.tools.entity.SysPermission">
        update sys_permission
        <set>
            <if test="code != null">
                code = #{code,jdbcType=VARCHAR},
            </if>
            <if test="name != null">
                `name` = #{name,jdbcType=VARCHAR},
            </if>
            <if test="nameEn != null">
                `name_en` = #{nameEn},
            </if>
            <if test="perms != null">
                perms = #{perms,jdbcType=VARCHAR},
            </if>
            <if test="url != null">
                url = #{url,jdbcType=VARCHAR},
            </if>
            <if test="urlApp != null">
                url_app = #{urlApp},
            </if>
            <if test="icon != null">
                icon = #{icon,jdbcType=VARCHAR},
            </if>
            <if test="iconApp != null">
                icon_app = #{iconApp},
            </if>
            <if test="method != null">
                `method` = #{method,jdbcType=VARCHAR},
            </if>
            <if test="pid != null">
                pid = #{pid},
            </if>
            <if test="orderNum != null">
                order_num = #{orderNum,jdbcType=INTEGER},
            </if>
            <if test="orderNum == null">
                order_num = null,
            </if>
            <if test="type != null">
                `type` = #{type,jdbcType=TINYINT},
            </if>
            <if test="show != null">
                `show` = #{show},
            </if>
            <if test="status != null">
                `status` = #{status,jdbcType=TINYINT},
            </if>
            <if test="createTime != null">
                create_time = #{createTime,jdbcType=TIMESTAMP},
            </if>
            <if test="updateTime != null">
                update_time = #{updateTime,jdbcType=TIMESTAMP},
            </if>
            <if test="updateTime == null">
                update_time = now(),
            </if>
            <if test="deleted != null">
                deleted = #{deleted,jdbcType=TINYINT},
            </if>
            <if test="isConfig != null">
                is_config = #{isConfig},
            </if>
            <if test="component != null">
                component = #{component},
            </if>
            <if test="menuName != null">
                menu_name = #{menuName},
            </if>
        </set>
        where id = #{id}
    </update>

    <update id="updateByPrimaryKey" parameterType="com.ho.common.tools.entity.SysPermission">
        update sys_permission
        set code        = #{code,jdbcType=VARCHAR},
            `name`      = #{name,jdbcType=VARCHAR},
            perms       = #{perms,jdbcType=VARCHAR},
            url         = #{url,jdbcType=VARCHAR},
            icon        = #{icon,jdbcType=VARCHAR},
            `method`    = #{method,jdbcType=VARCHAR},
            pid         = #{pid,jdbcType=INTEGER},
            order_num   = #{orderNum,jdbcType=INTEGER},
            `type`      = #{type,jdbcType=TINYINT},
            `status`    = #{status,jdbcType=TINYINT},
            create_time = #{createTime,jdbcType=TIMESTAMP},
            update_time = #{updateTime,jdbcType=TIMESTAMP},
            deleted     = #{deleted,jdbcType=TINYINT}
        where id = #{id,jdbcType=INTEGER}
    </update>

    <select id="selectInfoByIds" resultType="com.ho.common.tools.entity.SysPermission">
       select
           <choose>
               <when test="query.lang != null and query.lang=='en_US' ">
                   <include refid="Base_Column_List1"></include>
               </when>
               <when test="query.lang != null and query.lang=='fr_FR' ">
                   <include refid="Base_Column_List2"></include>
               </when>
               <when test="query.lang != null and query.lang=='es_ES' ">
                   <include refid="Base_Column_List3"></include>
               </when>
               <when test="query.lang != null and query.lang=='de_DE' ">
                   <include refid="Base_Column_List4"></include>
               </when>
               <when test="query.lang != null and query.lang=='pl_PL' ">
                   <include refid="Base_Column_List5"></include>
               </when>
               <when test="query.lang != null and query.lang=='ar_EG' ">
                   <include refid="Base_Column_List6"></include>
               </when>
                <otherwise>
                    <include refid="Base_Column_List"></include>
                </otherwise>
           </choose>
       from sys_permission
       <where>
           and deleted = 1
           <if test="ids != null and ids.size != 0">
               and id in
               <foreach item="item" collection="ids" open="(" separator="," close=")">
                   #{item}
               </foreach>
           </if>
           <if test="query.status != null">
               and `status` = #{query.status}
           </if>
           <if test="query.title != null">
               and `name` LIKE CONCAT('%',#{query.title},'%')
           </if>
           and status = 1
           order by order_num
       </where>
    </select>

    <select id="selectTopMenu" resultType="com.ho.common.tools.entity.SysPermission">
        select
        <include refid="Base_Column_List"/>
        from sys_permission
        where pid = 0 and deleted=1
        order by order_num
    </select>

    <select id="selectAll" resultType="com.ho.common.tools.entity.SysPermission">
        select
        <include refid="Base_Column_List"/>
        from sys_permission
        <where>
            and deleted=1
            <if test="enableStatus != null ">
                and status = 1 and type != 3
            </if>
            <if test="enableType != null ">
                and status = 1
            </if>
            <if test="query.status != null">
                and `status` = #{query.status}
            </if>
            <if test="query.title != null">
                and `name` LIKE CONCAT('%',#{query.title},'%')
            </if>
        </where>
    </select>

    <select id="selectChild" resultType="com.ho.common.tools.entity.SysPermission">
        select
        <include refid="Base_Column_List"/>
        from sys_permission
        where deleted=1 and
        pid = #{pid}
        order by order_num
    </select>

    <select id="selectChilds" resultType="com.ho.common.tools.entity.SysPermission">
        select
        <include refid="Base_Column_List"/>
        from sys_permission
        <where>
            <if test="pids != null and pids.size != 0">
                and pid in
                <foreach item="item" collection="pids" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
            and deleted = 1
            and status = 1
            order by order_num
        </where>
    </select>


    <select id="selectByPerms" resultType="com.ho.common.tools.entity.SysPermission">
        select
        <include refid="Base_Column_List"/>
        from sys_permission
        where deleted = 1 and
        perms = #{perms}
    </select>

    <select id="selectOtherByIdAndPerms" resultType="com.ho.common.tools.entity.SysPermission">
        select
        <include refid="Base_Column_List"/>
        from sys_permission
        where deleted = 1
        and id != #{id}
        and perms = #{perms}
    </select>

    <select id="selectByName" resultType="com.ho.common.tools.entity.SysPermission">
        select
        <include refid="Base_Column_List"/>
        from sys_permission
        <where>
            <if test="name != null">
                and `name` = #{name,jdbcType=VARCHAR}
            </if>
            <if test="pid != null">
                and pid = #{pid}
            </if>
            <if test="id != null">
                and id != #{id}
            </if>
            and deleted = 1
        </where>
    </select>

    <select id="selectByPid" resultType="com.ho.common.tools.entity.SysPermission">
        select
        <include refid="Base_Column_List"/>
        from sys_permission
        where deleted = 1 and
        id = #{pid}
    </select>

    <select id="selectByScope" resultType="com.ho.common.tools.entity.SysPermission">
        select
        <include refid="Base_Column_List"/>
        from sys_permission
        where deleted = 1 and
        scope = 1
    </select>

    <select id="selectByIds" resultType="com.ho.common.tools.entity.SysPermission">
        select
        <choose>
            <when test="lang != null and lang=='en_US' ">
                <include refid="Base_Column_List1"></include>
            </when>
            <when test="lang != null and lang=='fr_FR' ">
                <include refid="Base_Column_List2"/>
            </when>
            <when test="lang != null and lang=='es_ES' ">
                <include refid="Base_Column_List3"/>
            </when>
            <when test="lang != null and lang=='de_DE' ">
                <include refid="Base_Column_List4"/>
            </when>
            <when test="lang != null and lang=='pl_PL' ">
                <include refid="Base_Column_List5"/>
            </when>
            <when test="lang != null and lang=='ar_EG' ">
                <include refid="Base_Column_List6"/>
            </when>
            <otherwise>
                <include refid="Base_Column_List"></include>
            </otherwise>
        </choose>
        from sys_permission
        <where>
            <if test="ids != null and ids.size != 0">
                and id in
                <foreach item="item" collection="ids" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
            order by order_num
        </where>
    </select>


</mapper>