<?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.sckw.system.dao.KwsMenuDao">
  <resultMap id="BaseResultMap" type="com.sckw.system.model.KwsMenu">
    <id column="id" jdbcType="BIGINT" property="id" />
    <result column="client_type" jdbcType="INTEGER" property="clientType" />
    <result column="parent_id" jdbcType="BIGINT" property="parentId" />
    <result column="name" jdbcType="VARCHAR" property="name" />
    <result column="url" jdbcType="VARCHAR" property="url" />
    <result column="perms" jdbcType="VARCHAR" property="perms" />
    <result column="type" jdbcType="INTEGER" property="type" />
    <result column="icon" jdbcType="VARCHAR" property="icon" />
    <result column="sort" jdbcType="INTEGER" property="sort" />
    <result column="level" jdbcType="INTEGER" property="level" />
    <result column="custom" jdbcType="INTEGER" property="custom" />
    <result column="is_main" jdbcType="INTEGER" property="isMain" />
    <result column="is_display" jdbcType="INTEGER" property="isDisplay" />
    <result column="using_roles" jdbcType="VARCHAR" property="usingRoles" />
    <result column="remark" jdbcType="VARCHAR" property="remark" />
    <result column="status" jdbcType="INTEGER" property="status" />
    <result column="create_by" jdbcType="BIGINT" property="createBy" />
    <result column="create_time" jdbcType="TIMESTAMP" property="createTime" />
    <result column="update_by" jdbcType="BIGINT" property="updateBy" />
    <result column="update_time" jdbcType="TIMESTAMP" property="updateTime" />
    <result column="del_flag" jdbcType="INTEGER" property="delFlag" />
  </resultMap>

  <sql id="Base_Column_List">
        sm.id,
        sm.client_type,
        sm.parent_id,
        sm.name,
        sm.url,
        sm.perms,
        sm.type,
        sm.icon,
        sm.sort,
        sm.level,
        sm.custom,
        sm.links,
        sm.is_main,
        sm.using_roles,
        sm.remark,
        sm.status,
        sm.create_by,
        sm.create_time,
        sm.update_by,
        sm.update_time,
        sm.del_flag
  </sql>

  <select id="selectByKey" parameterType="java.lang.Long" resultMap="BaseResultMap">
    select
    <include refid="Base_Column_List" />
    from kws_menu sm
    where id = #{id,jdbcType=BIGINT}
  </select>

  <insert id="insert" parameterType="com.sckw.system.model.KwsMenu">
    insert into kws_menu
    <trim prefix="(" suffix=")" suffixOverrides=",">
      <if test="id != null">
        id,
      </if>
      <if test="clientType != null">
        client_type,
      </if>
      <if test="parentId != null">
        parent_id,
      </if>
      <if test="name != null">
        name,
      </if>
      <if test="url != null">
        url,
      </if>
      <if test="links != null">
        links,
      </if>
      <if test="perms != null">
        perms,
      </if>
      <if test="type != null">
        type,
      </if>
      <if test="icon != null">
        icon,
      </if>
      <if test="sort != null">
        sort,
      </if>
      <if test="level != null">
        level,
      </if>
      <if test="custom != null">
        custom,
      </if>
      <if test="isMain != null">
        is_main,
      </if>
      <if test="isDisplay != null">
        is_display,
      </if>
      <if test="usingRoles != null">
        using_roles,
      </if>
      <if test="remark != null">
        remark,
      </if>
      <if test="status != null">
        status,
      </if>
      <if test="createBy != null">
        create_by,
      </if>
      <if test="createTime != null">
        create_time,
      </if>
      <if test="updateBy != null">
        update_by,
      </if>
      <if test="updateTime != null">
        update_time,
      </if>
      <if test="delFlag != null">
        del_flag,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides=",">
      <if test="id != null">
        #{id,jdbcType=BIGINT},
      </if>
      <if test="clientType != null">
        #{clientType,jdbcType=INTEGER},
      </if>
      <if test="parentId != null">
        #{parentId,jdbcType=BIGINT},
      </if>
      <if test="name != null">
        #{name,jdbcType=VARCHAR},
      </if>
      <if test="url != null">
        #{url,jdbcType=VARCHAR},
      </if>
      <if test="links != null">
        #{links,jdbcType=VARCHAR},
      </if>
      <if test="perms != null">
        #{perms,jdbcType=VARCHAR},
      </if>
      <if test="type != null">
        #{type,jdbcType=INTEGER},
      </if>
      <if test="icon != null">
        #{icon,jdbcType=VARCHAR},
      </if>
      <if test="sort != null">
        #{sort,jdbcType=INTEGER},
      </if>
      <if test="level != null">
        #{level,jdbcType=INTEGER},
      </if>
      <if test="custom != null">
        #{custom,jdbcType=INTEGER},
      </if>
      <if test="isDisplay != null">
        #{isDisplay,jdbcType=INTEGER},
      </if>
      <if test="isMain != null">
        #{isMain,jdbcType=INTEGER},
      </if>
      <if test="usingRoles != null">
        #{usingRoles,jdbcType=VARCHAR},
      </if>
      <if test="remark != null">
        #{remark,jdbcType=VARCHAR},
      </if>
      <if test="status != null">
        #{status,jdbcType=INTEGER},
      </if>
      <if test="createBy != null">
        #{createBy,jdbcType=BIGINT},
      </if>
      <if test="createTime != null">
        #{createTime,jdbcType=TIMESTAMP},
      </if>
      <if test="updateBy != null">
        #{updateBy,jdbcType=BIGINT},
      </if>
      <if test="updateTime != null">
        #{updateTime,jdbcType=TIMESTAMP},
      </if>
      <if test="delFlag != null">
        #{delFlag,jdbcType=INTEGER},
      </if>
    </trim>
  </insert>

  <update id="update" parameterType="com.sckw.system.model.KwsMenu">
    update kws_menu
    <set>
      <if test="clientType != null">
        client_type = #{clientType,jdbcType=INTEGER},
      </if>
      <if test="parentId != null">
        parent_id = #{parentId,jdbcType=BIGINT},
      </if>
      <if test="name != null">
        name = #{name,jdbcType=VARCHAR},
      </if>
      <if test="url != null">
        url = #{url,jdbcType=VARCHAR},
      </if>
      <if test="perms != null">
        perms = #{perms,jdbcType=VARCHAR},
      </if>
      <if test="type != null">
        type = #{type,jdbcType=INTEGER},
      </if>
      <if test="icon != null">
        icon = #{icon,jdbcType=VARCHAR},
      </if>
      <if test="sort != null">
        sort = #{sort,jdbcType=INTEGER},
      </if>
      <if test="level != null">
        level = #{level,jdbcType=INTEGER},
      </if>
      <if test="custom != null">
        custom = #{custom,jdbcType=INTEGER},
      </if>
      <if test="isDisplay != null">
        is_display = #{isDisplay,jdbcType=INTEGER},
      </if>
      <if test="isMain != null">
        is_main = #{isMain,jdbcType=INTEGER},
      </if>
      <if test="usingRoles != null">
        using_roles = #{usingRoles,jdbcType=VARCHAR},
      </if>
      <if test="remark != null">
        remark = #{remark,jdbcType=VARCHAR},
      </if>
      <if test="status != null">
        status = #{status,jdbcType=INTEGER},
      </if>
      <if test="createBy != null">
        create_by = #{createBy,jdbcType=BIGINT},
      </if>
      <if test="createTime != null">
        create_time = #{createTime,jdbcType=TIMESTAMP},
      </if>
      <if test="updateBy != null">
        update_by = #{updateBy,jdbcType=BIGINT},
      </if>
      <if test="updateTime != null">
        update_time = #{updateTime,jdbcType=TIMESTAMP},
      </if>
      <if test="delFlag != null">
        del_flag = #{delFlag,jdbcType=INTEGER},
      </if>
    </set>
    where id = #{id,jdbcType=BIGINT}
  </update>

  <select id="findList" resultType="com.sckw.system.model.vo.res.KwsMenuResVo"
          parameterType="com.sckw.system.model.pojo.FindMenuTreePojo" >
    select distinct
    <include refid="Base_Column_List" />
    from kws_menu sm
    left join kws_menu_rights smr on sm.id = smr.menu_id
    where sm.del_flag = 0
    <if test="clientType != null">
      and sm.client_type = #{clientType}
    </if>
    <if test="name != null and name != ''">
      and sm.name = #{name, jdbcType=VARCHAR}
    </if>
      <if test="entTypeList != null and entTypeList.size() > 0">
        and
        <foreach collection="entTypeList" item="item" open="(" close=")" separator=" or ">
          using_roles like concat('%', #{item}, '%')
        </foreach>
      </if>
    <if test="entId != null">
      and smr.ent_id = #{entId}
      and smr.role_id is null
    </if>
    <if test="roleIds != null and roleIds.size() > 0">
      and smr.role_id in
      <foreach collection="roleIds" separator="," open="(" close=")" item="item">
        #{item}
      </foreach>
    </if>
    <if test="type != null">
      and sm.type = #{type}
    </if>
    ORDER BY sm.level, sm.sort
  </select>

  <select id="selectByKeys" resultType="com.sckw.system.model.KwsMenu">
    select
    <include refid="Base_Column_List" />
    from kws_menu sm
    where del_flag = 0
      and id in
    <foreach collection="list" item="item" open="(" close=")" separator=",">
      #{item}
    </foreach>
  </select>

  <select id="selectAll" resultType="com.sckw.system.model.KwsMenu">
    select * from kws_menu where del_flag = 0
  </select>

  <select id="select" resultType="com.sckw.system.model.KwsMenu">
    select *
      from kws_menu
     where del_flag = 0
    <if test="id != null">
      and id = #{id,jdbcType=BIGINT}
    </if>
    <if test="clientType != null">
      and client_type = #{clientType,jdbcType=INTEGER}
    </if>
    <if test="parentId != null">
      and parent_id = #{parentId,jdbcType=BIGINT}
    </if>
    <if test="name != null">
      and name = #{name,jdbcType=VARCHAR}
    </if>
    <if test="url != null">
      and url = #{url,jdbcType=VARCHAR}
    </if>
    <if test="links != null">
      and links = #{links,jdbcType=VARCHAR}
    </if>
    <if test="perms != null">
      and perms = #{perms,jdbcType=VARCHAR}
    </if>
    <if test="type != null">
      and type = #{type,jdbcType=INTEGER}
    </if>
    <if test="icon != null">
      and icon = #{icon,jdbcType=VARCHAR}
    </if>
    <if test="sort != null">
      and sort = #{sort,jdbcType=INTEGER}
    </if>
    <if test="level != null">
      and level = #{level,jdbcType=INTEGER}
    </if>
    <if test="custom != null">
      and custom = #{custom,jdbcType=INTEGER}
    </if>
    <if test="isDisplay != null">
      and is_display = #{isDisplay,jdbcType=INTEGER}
    </if>
    <if test="isMain != null">
      and is_main = #{isMain,jdbcType=INTEGER}
    </if>
    <if test="usingRoles != null">
      and using_roles = #{usingRoles,jdbcType=VARCHAR}
    </if>
    <if test="remark != null">
     and remark = #{remark,jdbcType=VARCHAR}
    </if>
    order by sort
  </select>

  <select id="queryChildMenu" resultType="com.sckw.system.model.KwsMenu">
    select *
    from kws_menu
    where del_flag = 0
    <if test="parentId != null">
      and parent_id = #{parentId,jdbcType=BIGINT}
    </if>
    <if test="type != null">
      and type = #{type,jdbcType=INTEGER}
    </if>
    <if test="perms != null">
      and perms like concat('%', #{perms,jdbcType=VARCHAR}, '%')
    </if>
    <if test="name != null">
      and name like concat('%', #{name,jdbcType=VARCHAR}, '%')
    </if>
  </select>
</mapper>