<?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.zcloud.mapper.dsno2.depository.LabelFactoryMapper">

	<!--表名 -->
	<sql id="tableName">
		BUS_LABEL_FACTORY
	</sql>

	<!--数据字典表名 -->
	<sql id="dicTableName">
		SYS_DICTIONARIES
	</sql>

	<!-- 字段 -->
	<sql id="Field">
		f.PARENT_ID,
		f.ANCESTORS_ID,
		f.IS_ANCESTORS_FLAG,
		f.LEVEL,
		f.NAME,
		f.CREATOR_ID,
		f.CREATED_TIME,
		f.ISDELETE,
		f.SORT,
		f.TYPE,
		f.BUS_LABEL_FACTORY_ID,
		f.CORPINFO_ID
	</sql>

	<!-- 字段用于新增 -->
	<sql id="Field2">
		PARENT_ID,
		ANCESTORS_ID,
		IS_ANCESTORS_FLAG,
		LEVEL,
		NAME,
		CREATOR_ID,
		CREATED_TIME,
		ISDELETE,
		SORT,
		TYPE,
		BUS_LABEL_FACTORY_ID,
		CORPINFO_ID
	</sql>

	<!-- 字段值 -->
	<sql id="FieldValue">
		#{PARENT_ID},
		#{ANCESTORS_ID},
		#{IS_ANCESTORS_FLAG},
		#{LEVEL},
		#{NAME},
		#{CREATOR_ID},
		#{CREATED_TIME},
		#{ISDELETE},
		#{SORT},
		#{TYPE},
		#{BUS_LABEL_FACTORY_ID},
		#{CORPINFO_ID}
	</sql>

	<!-- 新增-->
	<insert id="save" parameterType="pd">
		insert into
	<include refid="tableName"></include>
		(
	<include refid="Field2"></include>
		) values (
	<include refid="FieldValue"></include>
		)
	</insert>

	<!-- 删除-->
	<delete id="delete" parameterType="pd">
		update
		<include refid="tableName"></include>
		set
			ISDELETE = '1'
		where
			BUS_LABEL_FACTORY_ID = #{BUS_LABEL_FACTORY_ID}
	</delete>
	<delete id="deleteByAncestors" parameterType="pd">
		delete from
		<include refid="tableName"></include>
		where TYPE = #{TYPE}
		<if test="CORPINFO_ID != '' and CORPINFO_ID != null">
			and CORPINFO_ID = #{CORPINFO_ID}
		</if>
	</delete>

	<!-- 修改 -->
	<update id="edit" parameterType="pd">
		update
		<include refid="tableName"></include>
		set
			PARENT_ID = #{PARENT_ID},
			ANCESTORS_ID = #{ANCESTORS_ID},
			IS_ANCESTORS_FLAG = #{IS_ANCESTORS_FLAG},
			LEVEL = #{LEVEL},
			NAME = #{NAME},
			CREATOR_ID = #{CREATOR_ID},
			CREATED_TIME = #{CREATED_TIME},
			SORT = #{SORT},
			TYPE = #{TYPE},
			CORPINFO_ID = #{CORPINFO_ID},
			BUS_LABEL_FACTORY_ID = BUS_LABEL_FACTORY_ID
		where
			BUS_LABEL_FACTORY_ID = #{BUS_LABEL_FACTORY_ID}
	</update>

	<!-- 通过ID获取数据 -->
	<select id="findById" parameterType="pd" resultType="pd">
		select
		<include refid="Field"></include>
		from
		<include refid="tableName"></include> f
		where
			f.BUS_LABEL_FACTORY_ID = #{BUS_LABEL_FACTORY_ID}
	</select>

	<select id="findByAncestors" parameterType="pd" resultType="pd">
		select
		<include refid="Field"></include>
		from
		<include refid="tableName"></include>
		f
		where f.ISDELETE = '0'
		<if test="ANCESTORS_ID != null and ANCESTORS_ID != ''">
			and f.ANCESTORS_ID = #{ANCESTORS_ID}
		</if>
		<if test="TYPE != null and TYPE != ''">
			and f.TYPE = #{TYPE}
		</if>
		<if test="CORPINFO_ID != '' and CORPINFO_ID != null">
			and f.CORPINFO_ID = #{CORPINFO_ID}
		</if>
		<if test="IS_ANCESTORS_FLAG != null and IS_ANCESTORS_FLAG != ''">
			and f.IS_ANCESTORS_FLAG = #{IS_ANCESTORS_FLAG}
		</if>
	</select>
	<select id="findAllAncestors" parameterType="pd" resultType="pd">
		select
		<include refid="Field"></include>
		from
		<include refid="tableName"></include> f
		where
			f.IS_ANCESTORS_FLAG = '1' and f.ISDELETE = '0' and TYPE  = #{TYPE}
		<if test="NAME != null and NAME != ''">
			and f.NAME LIKE CONCAT(CONCAT('%', #{NAME}),'%')
		</if>
	</select>

	<!-- 列表 -->
	<select id="datalistPage" parameterType="page" resultType="pd">
		select
		<include refid="Field"></include>
		from
		<include refid="tableName"></include>
		f
		where f.ISDELETE = '0'
		<if test="pd.NAME != null and pd.NAME != ''"><!-- 关键词检索 -->
			and f.NAME LIKE CONCAT(CONCAT('%', #{pd.NAME}),'%')
		</if>
		<if test="pd.IS_ANCESTORS_FLAG != null and pd.IS_ANCESTORS_FLAG != ''"><!-- 关键词检索 -->
			and f.IS_ANCESTORS_FLAG = #{pd.IS_ANCESTORS_FLAG}
		</if>
		<if test="pd.TYPE != null and pd.TYPE != ''">
			and f.TYPE = #{pd.TYPE}
		</if>
	</select>

	<!-- 列表(全部) -->
	<select id="listAll" parameterType="pd" resultType="pd">
		select
		<include refid="Field"></include>
		from
		<include refid="tableName"></include> f where f.ISDELETE = '0'
		<if test="NAME != '' and NAME != null">
			and f.NAME = #{NAME}
		</if>
	</select>

	<!-- 批量删除 -->
	<delete id="deleteAll" parameterType="String">
		update
		<include refid="tableName"></include>
		set
			ISDELETE = '1'
		where
			BUS_LABEL_FACTORY_ID in
		<foreach item="item" index="index" collection="array" open="(" separator="," close=")">
                 #{item}
		</foreach>
	</delete>

</mapper>
