ROLEMapper.xml 11.0 KB
<?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.tianbo.warehouse.dao.ROLEMapper">
  <resultMap id="BaseResultMap" type="com.tianbo.warehouse.model.ROLE">
    <id column="role_id" jdbcType="INTEGER" property="roleId" />
    <result column="role_name" jdbcType="VARCHAR" property="roleName" />
    <result column="role_sign" jdbcType="VARCHAR" property="roleSign" />
    <result column="description" jdbcType="VARCHAR" property="description" />
    <result column="type" jdbcType="VARCHAR" property="type" />
    <result column="parentId" jdbcType="INTEGER" property="parentid" />
    <result column="rsort" jdbcType="INTEGER" property="rsort" />
    <result column="customs_reg_code" jdbcType="VARCHAR" property="customsRegCode" />
    <result column="business_license" jdbcType="VARCHAR" property="businessLicense" />
    <result column="departmentId" jdbcType="VARCHAR" property="departmentid" />
    <result column="mq_code" jdbcType="VARCHAR" property="mqcode" />
  </resultMap>
  <resultMap id="TreeWithPermResultMap" type="com.tianbo.warehouse.model.ROLE" extends="BaseResultMap">
    <collection javaType="java.util.ArrayList" ofType="com.tianbo.warehouse.model.PERMISSION" property="permissions">
      <result column="permission_id" property="permissionId" jdbcType="INTEGER" />
      <result column="name" property="name" jdbcType="VARCHAR" />
      <result column="permission_order" property="permissionOrder" jdbcType="VARCHAR" />
      <result column="permission_description" property="description" jdbcType="VARCHAR" />
      <result column="ismenu" property="ismenu" jdbcType="BOOLEAN" />
      <result column="hidden" property="hidden" jdbcType="BOOLEAN" />
      <result column="parent_id" property="parentId" jdbcType="INTEGER" />
      <result column="path" property="path" jdbcType="VARCHAR" />
      <result column="url" property="url" jdbcType="VARCHAR" />
      <result column="method" property="method" jdbcType="VARCHAR" />
      <result column="iconCls" property="iconCls" jdbcType="VARCHAR" />
      <result column="component" property="component" jdbcType="VARCHAR" />
    </collection>
    <collection column="role_id" property="children" select="selectByParentId" />
  </resultMap>
  <resultMap id="TreeWithResultMap" type="com.tianbo.warehouse.model.ROLE" extends="BaseResultMap">
    <collection column="role_id" property="children" select="selectByParentId" />
  </resultMap>
  <sql id="Base_Column_List">
    role_id, role_name, role_sign, description, `type`, parentId, rsort, customs_reg_code,
    business_license, departmentId, mq_code
  </sql>
  <select id="selectByPrimaryKey" parameterType="java.lang.Integer" resultMap="BaseResultMap">
    select
    <include refid="Base_Column_List" />
    from role
    where role_id = #{roleId,jdbcType=INTEGER}
  </select>
  <select id="selectByParentId" parameterType="java.lang.Integer" resultMap="TreeWithPermResultMap">
    select
    <include refid="Base_Column_List" />
    from role
    where parentId = #{roleId,jdbcType=INTEGER}
  </select>
  <select id="selectTopByChildID" parameterType="java.lang.Integer" resultMap="BaseResultMap">
    SELECT
	*
FROM
	(
	SELECT
		t3.*
	FROM
		(
		SELECT
			t1.*,
		IF
			(
				FIND_IN_SET( role_id, @ids ) > 0,
				@ids := CONCAT( parentId, ',', @ids ),
				'0'
			) AS isparent
		FROM
			(
			SELECT
				t.*
			FROM
				role AS t
			ORDER BY
				t.role_id DESC
			) t1,
			( SELECT @ids := #{roleId,jdbcType=INTEGER} ) t2
		) t3
	WHERE
		t3.isparent != '0'
	) t4
WHERE
	t4.parentId = 0
	order by role_id
  </select>
  <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer">
    delete from role
    where role_id = #{roleId,jdbcType=INTEGER}
  </delete>
  <insert id="insert" parameterType="com.tianbo.warehouse.model.ROLE">
    insert into role (role_id, role_name, role_sign,
      description, type, parentId,
      rsort, customs_reg_code, business_license,
      departmentId, mq_code)
    values (#{roleId,jdbcType=INTEGER}, #{roleName,jdbcType=VARCHAR}, #{roleSign,jdbcType=VARCHAR},
      #{description,jdbcType=VARCHAR}, #{type,jdbcType=VARCHAR}, #{parentid,jdbcType=INTEGER},
      #{rsort,jdbcType=INTEGER}, #{customsRegCode,jdbcType=VARCHAR}, #{businessLicense,jdbcType=VARCHAR},
      #{departmentid,jdbcType=VARCHAR},#{mqcode,jdbcType=VARCHAR})
  </insert>
  <insert id="insertSelective" parameterType="com.tianbo.warehouse.model.ROLE">
    insert into role
    <trim prefix="(" suffix=")" suffixOverrides=",">
      <if test="roleId != null">
        role_id,
      </if>
      <if test="roleName != null">
        role_name,
      </if>
      <if test="roleSign != null">
        role_sign,
      </if>
      <if test="description != null">
        description,
      </if>
      <if test="type != null">
        type,
      </if>
      <if test="parentid != null">
        parentId,
      </if>
      <if test="rsort != null">
        rsort,
      </if>
      <if test="customsRegCode != null">
        customs_reg_code,
      </if>
      <if test="businessLicense != null">
        business_license,
      </if>
      <if test="departmentid != null">
        departmentId,
      </if>
      <if test="mqcode != null">
        mq_code,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides=",">
      <if test="roleId != null">
        #{roleId,jdbcType=INTEGER},
      </if>
      <if test="roleName != null">
        #{roleName,jdbcType=VARCHAR},
      </if>
      <if test="roleSign != null">
        #{roleSign,jdbcType=VARCHAR},
      </if>
      <if test="description != null">
        #{description,jdbcType=VARCHAR},
      </if>
      <if test="type != null">
        #{type,jdbcType=VARCHAR},
      </if>
      <if test="parentid != null">
        #{parentid,jdbcType=INTEGER},
      </if>
      <if test="rsort != null">
        #{rsort,jdbcType=INTEGER},
      </if>
      <if test="customsRegCode != null">
        #{customsRegCode,jdbcType=VARCHAR},
      </if>
      <if test="businessLicense != null">
        #{businessLicense,jdbcType=VARCHAR},
      </if>
      <if test="departmentid != null">
        #{departmentid,jdbcType=VARCHAR},
      </if>
      <if test="mqcode != null">
        #{mqcode,jdbcType=VARCHAR},
      </if>
    </trim>
  </insert>
  <update id="updateByPrimaryKeySelective" parameterType="com.tianbo.warehouse.model.ROLE">
    update role
    <set>
      <if test="roleName != null">
        role_name = #{roleName,jdbcType=VARCHAR},
      </if>
      <if test="roleSign != null">
        role_sign = #{roleSign,jdbcType=VARCHAR},
      </if>
      <if test="description != null">
        description = #{description,jdbcType=VARCHAR},
      </if>
      <if test="type != null">
        type = #{type,jdbcType=VARCHAR},
      </if>
      <if test="parentid != null">
        parentId = #{parentid,jdbcType=INTEGER},
      </if>
      <if test="rsort != null">
        rsort = #{rsort,jdbcType=INTEGER},
      </if>
      <if test="customsRegCode != null">
        customs_reg_code = #{customsRegCode,jdbcType=VARCHAR},
      </if>
      <if test="businessLicense != null">
        business_license = #{businessLicense,jdbcType=VARCHAR},
      </if>
      <if test="departmentid != null">
        departmentId = #{departmentid,jdbcType=VARCHAR},
      </if>
      <if test="mqcode != null">
        mq_code = #{mqcode,jdbcType=VARCHAR},
      </if>
    </set>
    where role_id = #{roleId,jdbcType=INTEGER}
  </update>
  <update id="updateByPrimaryKey" parameterType="com.tianbo.warehouse.model.ROLE">
    update role
    set role_name = #{roleName,jdbcType=VARCHAR},
      role_sign = #{roleSign,jdbcType=VARCHAR},
      description = #{description,jdbcType=VARCHAR},
      type = #{type,jdbcType=VARCHAR},
      parentId = #{parentid,jdbcType=INTEGER},
      rsort = #{rsort,jdbcType=INTEGER},
      customs_reg_code = #{customsRegCode,jdbcType=VARCHAR},
      business_license = #{businessLicense,jdbcType=VARCHAR},
      departmentId = #{departmentid,jdbcType=VARCHAR},
      mq_code = #{mqcode,jdbcType=VARCHAR},
    where role_id = #{roleId,jdbcType=INTEGER}
  </update>

  <select id="findAll" parameterType="java.lang.String" resultMap="TreeWithPermResultMap">
    SELECT
    <include refid="Base_Column_List" />
    FROM role
    <where>
      parentId = 0
      <if test="roleName != ''  and roleName !=null">
        and role_name = #{roleName, jdbcType=VARCHAR}
      </if>
      <if test="type != ''  and type !=null">
        and type like '%' #{type} '%'
      </if>
    </where>
    order by rsort
  </select>

  <select id="findAllWithChildAndTree" resultMap="TreeWithPermResultMap">
      select r.role_id,
             r.role_name,
             r.role_sign,
             r.description,
             r.`type`,
             r.parentId,
             r.rsort,
             r.customs_reg_code,
             r.business_license,
             r.departmentId,
             r.mq_code,
             p.permission_id,
             p.name,
             p.permission_order,
             p.description as permission_description,
             p.ismenu,
             p.hidden,
             p.parent_id,
             p.path,
             p.url,
             p.method,
             p.iconCls,
             p.component
      from (
               SELECT role_id,
                      role_name,
                      role_sign,
                      description,
                      `type`,
                      parentId,
                      rsort,
                      customs_reg_code,
                      business_license,
                      departmentId,
                      mq_code
               FROM role
                <where>
                  parentId = 0
                  <if test="roleName != ''  and roleName !=null">
                    and role_name = #{roleName, jdbcType=VARCHAR}
                  </if>
                  <if test="type != ''  and type !=null">
                    and type like '%' #{type} '%'
                  </if>
                </where>
                order by rsort
                limit 5
           ) r
               left join role_permission rp
                         on r.role_id = rp.role_id
               left join permission p
                         on rp.permission_id = p.permission_id
      where p.permission_id is not null
      order by r.rsort, r.role_id, p.ismenu, p.name, p.permission_order

  </select>

  <select id="findRolesByUserId" parameterType="java.lang.Integer" resultMap="BaseResultMap">
    SELECT
    R.*
    FROM
    users U
    LEFT JOIN user_role UR ON U.user_id = UR.user_id
    LEFT JOIN role R ON  R.role_id= UR.role_id
    where U.user_id = #{userId,jdbcType=INTEGER}
  </select>

  <select id="findAllWithOutTree" parameterType="java.lang.String" resultMap="BaseResultMap">
    SELECT
    <include refid="Base_Column_List" />
    FROM role
    <where>
      <if test="roleName != ''  and roleName !=null">
        and role_name = #{roleName, jdbcType=VARCHAR}
      </if>
      <if test="type != ''  and type !=null">
        and type like '%' #{type} '%'
      </if>
    </where>
    order by parentId,rsort
  </select>
</mapper>