<?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.zyd.blog.persistence.mapper.BizTypeMapper">
	<resultMap id="rm" type="com.zyd.blog.persistence.beans.BizType">
		<result property="id" jdbcType="BIGINT" column="id"/>
		<result property="pid" jdbcType="BIGINT" column="pid"/>
		<result property="name" jdbcType="VARCHAR" column="name"/>
		<result property="description" jdbcType="VARCHAR" column="description"/>
		<result property="sort" jdbcType="INTEGER" column="sort"/>
		<result property="available" jdbcType="BIT" column="available"/>
		<result property="icon" jdbcType="VARCHAR" column="icon"/>
		<result property="createTime" jdbcType="TIMESTAMP" column="create_time"/>
		<result property="updateTime" jdbcType="TIMESTAMP" column="update_time"/>
		<association property="parent" javaType="com.zyd.blog.persistence.beans.BizType">
			<result property="id" jdbcType="BIGINT" column="parent_id"/>
			<result property="pid" jdbcType="BIGINT" column="parent_pid"/>
			<result property="name" jdbcType="VARCHAR" column="parent_name"/>
			<result property="description" jdbcType="VARCHAR" column="parent_description"/>
			<result property="available" jdbcType="BIT" column="parent_available"/>
			<result property="icon" jdbcType="VARCHAR" column="parent_icon"/>
		</association>
		<collection property="nodes" column="node_id" javaType="ArrayList" ofType="com.zyd.blog.persistence.beans.BizType">
			<result property="id" jdbcType="BIGINT" column="node_id"/>
			<result property="pid" jdbcType="BIGINT" column="node_pid"/>
			<result property="name" jdbcType="VARCHAR" column="node_name"/>
			<result property="description" jdbcType="VARCHAR" column="node_description"/>
			<result property="available" jdbcType="BIT" column="node_available"/>
			<result property="icon" jdbcType="VARCHAR" column="node_icon"/>
		</collection>
	</resultMap>

	<select id="findPageBreakByCondition" parameterType="com.zyd.blog.business.vo.TypeConditionVO" resultMap="rm">
		SELECT
			t.*,
			parent.id parent_id,
			parent.`pid` parent_pid,
			parent.`name` parent_name,
			parent.description parent_description,
			parent.available parent_available,
			parent.icon parent_icon
		FROM
			biz_type t
		LEFT JOIN biz_type parent ON t.pid = parent.id
		WHERE
			1 = 1
		<if test="pid != null and pid != ''">
			AND t.pid = #{pid}
		</if>
		<if test="keywords != null and keywords != '' ">
			AND (
				t.name LIKE CONCAT('%',#{keywords , jdbcType=VARCHAR},'%') OR
				t.description LIKE CONCAT('%',#{keywords , jdbcType=VARCHAR},'%') OR
				t.icon LIKE CONCAT('%',#{keywords , jdbcType=VARCHAR},'%')
			)
		</if>
		ORDER BY
			parent.sort ASC, t.sort ASC
	</select>

	<select id="listParent" resultMap="rm">
		SELECT
		t.*
		FROM
		biz_type t
		WHERE
		t.pid = '' OR t.pid IS NULL
		ORDER BY
		t.sort ASC
	</select>

	<select id="listTypeForMenu" resultMap="rm">
		SELECT
			t.*,
			parent.id parent_id,
			parent.`pid` parent_pid,
			parent.`name` parent_name,
			parent.description parent_description,
			parent.available parent_available,
			parent.icon parent_icon,
			node.`id` node_id,
			node.`pid` node_pid,
			node.`name` node_name,
			node.description node_description,
			node.available node_available,
			node.icon node_icon
		FROM
			biz_type t
		LEFT JOIN biz_type parent ON t.pid = parent.id
		LEFT JOIN biz_type node ON node.pid = t.id
		WHERE
		      t.available = 1
			AND (t.pid IS NULL OR t.pid = '')
		ORDER BY
			t.sort ASC
	</select>
</mapper>

