<?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.jeeplus.modules.test.treetable.mapper.CarKindMapper">

    <resultMap id="carKindResult" type="CarKind">
   		<result property="id" column="id" />
		<result property="name" column="name" />
		<result property="sort" column="sort" />
		<result property="parentIds" column="parentIds" />
    </resultMap>
    
	<sql id="carKindColumns">
		a.id AS "id",
		a.name AS "name",
		a.parent_id AS "parent.id",
		a.sort AS "sort",
		a.remarks AS "remarks",
		a.create_by AS "createBy.id",
		a.create_date AS "createDate",
		a.update_by AS "updateBy.id",
		a.update_date AS "updateDate",
		a.del_flag AS "delFlag",
		a.parent_ids AS "parentIds"
	</sql>
	

	

	<sql id="carKindJoins">
		
	</sql>
	

    
	<select id="get" resultType="CarKind">
		SELECT 
			<include refid="carKindColumns"/>
		FROM test_car_kind a
		<include refid="carKindJoins"/>
		WHERE a.id = #{id}
	</select>
	
	<select id="findList" resultType="CarKind">
		SELECT 
			<include refid="carKindColumns"/>
		FROM test_car_kind a
		<include refid="carKindJoins"/>
		<where>
			a.del_flag = #{DEL_FLAG_NORMAL}
			${dataScope}
			<if test="name != null and name != ''">
				AND a.name LIKE 
				    <if test="_databaseId == 'postgre'">'%'||#{name}||'%'</if>
					<if test="_databaseId == 'oracle'">'%'||#{name}||'%'</if>
					<if test="_databaseId == 'mssql'">'%'+#{name}+'%'</if>
					<if test="_databaseId == 'mysql'">concat('%',#{name},'%')</if>
			</if>
			<if test="parent != null and parent.id != null and parent.id != ''">
				AND a.parent_id = #{parent.id}
			</if>
			<if test="parentIds != null and parentIds != ''">
				AND a.parent_ids LIKE 
				    <if test="_databaseId == 'postgre'">'%'||#{parentIds}||'%'</if>
					<if test="_databaseId == 'oracle'">'%'||#{parentIds}||'%'</if>
					<if test="_databaseId == 'mssql'">'%'+#{parentIds}+'%'</if>
					<if test="_databaseId == 'mysql'">concat('%',#{parentIds},'%')</if>
			</if>
		</where>
		ORDER BY a.sort ASC
	</select>
	
	<select id="findAllList" resultType="CarKind">
		SELECT 
			<include refid="carKindColumns"/>
		FROM test_car_kind a
		<include refid="carKindJoins"/>
		<where>
			a.del_flag = #{DEL_FLAG_NORMAL}
			${dataScope}
		</where>
		ORDER BY a.sort ASC
	</select>
	
	<select id="getChildren" parameterType="String" resultMap="carKindResult">  
        select * from test_car_kind where parent_id = #{id} ORDER BY sort 
    </select> 

	<select id="findByParentIdsLike" resultType="CarKind">
		SELECT
			a.id,
			a.parent_id AS "parent.id",
			a.parent_ids
		FROM test_car_kind a
		<include refid="carKindJoins"/>
		<where>
			a.del_flag = #{DEL_FLAG_NORMAL}
			AND a.parent_ids LIKE #{parentIds}
		</where>
		ORDER BY a.sort ASC
	</select>
	
	<insert id="insert">
		INSERT INTO test_car_kind(
			   id,
			   name,
			   parent_id,
			   sort,
			   remarks,
			   create_by,
			   create_date,
			   update_by,
			   update_date,
			   del_flag,
			   parent_ids
		) VALUES (
			#{id},
			#{name},
			#{parent.id},
			#{sort},
			#{remarks},
			#{createBy.id},
			#{createDate},
			#{updateBy.id},
			#{updateDate},
			#{delFlag},
			#{parentIds}
		)
	</insert>
	
	<update id="update">
		UPDATE test_car_kind SET 	
			name = #{name},
			parent_id = #{parent.id},
			sort = #{sort},
			remarks = #{remarks},
			update_by = #{updateBy.id},
			update_date = #{updateDate},
			parent_ids = #{parentIds}
		WHERE id = #{id}
	</update>
	
	<update id="updateParentIds">
		UPDATE test_car_kind SET 
			parent_id = #{parent.id}, 
			parent_ids = #{parentIds}
		WHERE id = #{id}
	</update>
	
	<!--物理删除-->
	<update id="delete">
		DELETE FROM test_car_kind
		WHERE id = #{id} OR parent_ids LIKE 
		<if test="_databaseId == 'postgre'">'%,'||#{id}||',%'</if>
		<if test="_databaseId == 'oracle'">'%,'||#{id}||',%'</if>
		<if test="_databaseId == 'mssql'">'%,'+#{id}+',%'</if>
        <if test="_databaseId == 'mysql'">CONCAT('%,', #{id}, ',%')</if>
	</update>
	
	<!--逻辑删除-->
	<update id="deleteByLogic">
		UPDATE test_car_kind SET 
			del_flag = #{DEL_FLAG_DELETE}
		WHERE id = #{id} OR parent_ids LIKE 
		<if test="_databaseId == 'postgre'">'%,'||#{id}||',%'</if>
		<if test="_databaseId == 'oracle'">'%,'||#{id}||',%'</if>
		<if test="_databaseId == 'mssql'">'%,'+#{id}+',%'</if>
        <if test="_databaseId == 'mysql'">CONCAT('%,', #{id}, ',%')</if>
	</update>
	
</mapper>