<?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.bootdo.data.dao.DataRelationDao">

	<select id="get" resultType="com.bootdo.data.domain.DataRelationDO">
		select `id`,`APPLY_ID`,`DATA_ID` from tf_data_relation where id = #{value}
	</select>

	<select id="list" resultType="com.bootdo.data.domain.DataRelationDO">
		select `id`,`APPLY_ID`,`DATA_ID` from tf_data_relation
        <where>  
		  <if test="id != null and id != ''"> and id = #{id} </if>
		  <if test="applyId != null and applyId != ''"> and APPLY_ID = #{applyId} </if>
		  <if test="dataId != null and dataId != ''"> and DATA_ID = #{dataId} </if>
		</where>
	</select>
	
 	<select id="count" resultType="int">
		select count(*) from tf_data_relation
		 <where>  
		  		  <if test="id != null and id != ''"> and id = #{id} </if>
		  		  <if test="applyId != null and applyId != ''"> and APPLY_ID = #{applyId} </if>
		  		  <if test="dataId != null and dataId != ''"> and DATA_ID = #{dataId} </if>
		  		</where>
	</select>
	 
	<insert id="save" parameterType="com.bootdo.data.domain.DataRelationDO" useGeneratedKeys="true" keyProperty="id">
		insert into tf_data_relation
		(
			`APPLY_ID`, 
			`DATA_ID`
		)
		values
		(
			#{applyId}, 
			#{dataId}
		)
	</insert>
	 
	<update id="update" parameterType="com.bootdo.data.domain.DataRelationDO">
		update tf_data_relation 
		<set>
			<if test="applyId != null">`APPLY_ID` = #{applyId}, </if>
			<if test="dataId != null">`DATA_ID` = #{dataId}</if>
		</set>
		where id = #{id}
	</update>
	
	<delete id="remove">
		delete from tf_data_relation where id = #{value}
	</delete>

	<!--删除备案表主键-->
	<delete id="removeByApplyId">
		DELETE FROM tf_data_relation WHERE APPLY_ID=#{applyId}
	</delete>

	<delete id="batchRemoveByApplyId">
		delete from tf_data_relation where APPLY_ID in
		<foreach item="applyId" collection="array" open="(" separator="," close=")">
			#{applyId}
		</foreach>
	</delete>

	<!--删除数据表主键-->
	<delete id="removeByDataId">
		DELETE FROM tf_data_relation WHERE DATA_ID=#{dataId}
	</delete>

	<delete id="batchRemoveByDataId">
		delete from tf_data_relation where DATA_ID in
		<foreach item="dataId" collection="array" open="(" separator="," close=")">
			#{dataId}
		</foreach>
	</delete>

	<insert id="batchSave">
		INSERT INTO tf_data_relation(APPLY_ID, DATA_ID,CREATE_TIME) values
		<foreach item="item" index="index" collection="list"
				 separator=",">
			(#{item.applyId},#{item.dataId},now())
		</foreach>
	</insert>
	
	<delete id="batchRemove">
		delete from tf_data_relation where id in 
		<foreach item="id" collection="array" open="(" separator="," close=")">
			#{id}
		</foreach>
	</delete>

</mapper>