<?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.boman.gen.mapper.GenTableRelationMapper">

	<resultMap type="GenTableRelation" id="GenTableRelationResult">
		<result property="id"     column="id"      />
		<result property="description"     column="description"      />
		<result property="relationParentId"     column="relation_parent_id"      />
		<result property="relationChildId"     column="relation_child_id"      />
		<result property="relationType"     column="relation_type"      />
		<result property="embedEdit"     column="embed_edit"      />
		<result property="displayConditions"     column="display_conditions"      />
		<result property="sort"     column="sort"      />
		<result property="createBy"     column="create_by"      />
		<result property="createTime"     column="create_time"      />
		<result property="updateBy"     column="update_by"      />
		<result property="updateTime"     column="update_time"      />
		<result property="isDel"     column="is_del"      />
		<result property="tableName"     column="table_name"      />
		<result property="columnName"     column="column_name"      />
	</resultMap>

	<sql id="selectGenTableRelationVo">
        select id, description, relation_parent_id, relation_child_id, relation_type, sort, embed_edit, display_conditions, create_by, create_time, update_by, update_time, is_del
		from gen_table_relation
    </sql>

	<select id="selectGenTableRelationList" parameterType="GenTableRelation" resultMap="GenTableRelationResult">
		select r.id, r.description, r.relation_parent_id, r.relation_child_id, r.relation_type, r.sort, r.embed_edit, r.display_conditions, r.create_by, r.create_time, r.update_by, r.update_time, r.is_del,
		t.table_name, c.column_name
		from gen_table_relation r
		left join gen_table t on t.id = r.relation_parent_id
		left join gen_table_column c on c.id = r.relation_child_id
		<where>
			<if test="description != null and description != ''">
				AND description like concat('%', #{description}, '%')
			</if>
			<if test="relationParentId != null">
				AND relation_parent_id = #{relationParentId}
			</if>
			<if test="relationChildId != null ">
				AND relation_child_id = #{relationChildId}
			</if>
			<if test="relationType != null ">
				AND relation_type = #{relationType}
			</if>
			<if test="params.beginTime != null and params.beginTime != ''"><!-- 开始时间检索 -->
				and date_format(create_time,'%y%m%d') &gt;= date_format(#{params.beginTime},'%y%m%d')
			</if>
			<if test="params.endTime != null and params.endTime != ''"><!-- 结束时间检索 -->
				and date_format(create_time,'%y%m%d') &lt;= date_format(#{params.endTime},'%y%m%d')
			</if>
		</where>
	</select>


	<select id="selectGenTableRelationById" parameterType="GenTableRelation" resultMap="GenTableRelationResult">
		<include refid="selectGenTableRelationVo"/>
		where id = #{id}
	</select>

	<insert id="insertGenTableRelation" parameterType="GenTableRelation">
		insert into gen_table_relation (
		<if test="description != null and description != '' ">description,</if>
		<if test="relationParentId != null">relation_parent_id,</if>
		<if test="relationChildId != null">relation_child_id,</if>
		<if test="relationType != null">relation_type,</if>
		<if test="embedEdit != null">embed_edit,</if>
		<if test="displayConditions != null and displayConditions != ''">display_conditions,</if>
		<if test="sort != null">sort,</if>
		<if test="createBy != null and createBy != ''">create_by,</if>
		<if test="updateBy != null and updateBy != ''">update_by,</if>
		<if test="updateTime != null and updateTime != ''">update_time,</if>
		<if test="isDel != null and isDel != ''">is_del,</if>
		create_time
		)values(
		<if test="description != null and description != ''">#{description},</if>
		<if test="relationParentId != null">#{relationParentId},</if>
		<if test="relationChildId != null">#{relationChildId},</if>
		<if test="relationType != null">#{relationType},</if>
		<if test="embedEdit != null">#{embedEdit},</if>
		<if test="displayConditions != null and displayConditions != ''">#{displayConditions},</if>
		<if test="sort != null">#{sort},</if>
		<if test="createBy != null and createBy != ''">#{createBy},</if>
		<if test="updateBy != null and updateBy != ''">#{updateBy},</if>
		<if test="updateTime != null and updateTime != ''">#{updateTime},</if>
		<if test="isDel != null and isDel != ''">#{isDel},</if>
		sysdate()
		)
	</insert>

	<update id="updateGenTableRelation" parameterType="GenTableRelation">
		update gen_table_relation
		<set>
			<if test="description != null and description != ''">description = #{description},</if>
			<if test="relationParentId != null">relation_parent_id = #{relationParentId},</if>
			<if test="relationChildId != null">relation_child_id = #{relationChildId},</if>
			<if test="relationType != null">relation_type = #{relationType},</if>
			<if test="embedEdit != null">embed_edit = #{embedEdit},</if>
			<if test="displayConditions != null and displayConditions != ''">display_conditions = #{displayConditions},</if>
			<if test="sort != null">sort = #{sort},</if>
			<if test="createBy != null and createBy != ''">create_by = #{createBy},</if>
			<if test="createTime != null ">create_time = #{createTime},</if>
			<if test="updateBy != null and updateBy != null">update_by = #{updateBy},</if>
			<if test="isDel != null and isDel != null">is_del = #{isDel},</if>
			update_time = sysdate()
		</set>
		where id = #{id}
	</update>

	<delete id="deleteGenTableRelation" parameterType="GenTableRelation">
		delete from gen_table_relation
		where id in
		<foreach item="ids" collection="array" open="(" separator="," close=")">
			#{ids}
		</foreach>
	</delete>

	<delete id="deleteGenTableRelationByParentId" parameterType="GenTableRelation">
		delete from gen_table_relation
		where relation_parent_id in
		<foreach item="ids" collection="array" open="(" separator="," close=")">
			#{ids}
		</foreach>
	</delete>
</mapper> 