<?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.TextLibraryMapper">

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

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

	<!-- 字段 -->
	<sql id="Field">
		f.REMARKS,
		f.TYPE,
		f.TYPE_NAME,
		f.FILE_NAME,
		f.UPLOAD_TIME,
		f.UPLOAD_USER,
		f.UPLOAD_USER_NAME,
		f.PATH,
		f.REMOTE_PATH,
		f.CREATED_TIME,
		f.ISDELETE,
		f.REMOTE_FILE_NAME,
		f.LOCKTOOL,
		f.ISTOPTIME,
		f.ASSOCIATION,
		f.STATUS,
		f.BUS_TEXT_LIBRARY_ID,
		f.CORPINFO_ID
	</sql>

	<!-- 字段用于新增 -->
	<sql id="Field2">
		REMARKS,
		TYPE,
		TYPE_NAME,
		FILE_NAME,
		UPLOAD_TIME,
		UPLOAD_USER,
		UPLOAD_USER_NAME,
		PATH,
		REMOTE_PATH,
		CREATED_TIME,
		ISDELETE,
		REMOTE_FILE_NAME,
		LOCKTOOL,
		ISTOPTIME,
		ASSOCIATION,
		STATUS,
		BUS_TEXT_LIBRARY_ID,
		CORPINFO_ID
	</sql>

	<!-- 字段值 -->
	<sql id="FieldValue">
		#{REMARKS},
		#{TYPE},
		#{TYPE_NAME},
		#{FILE_NAME},
		#{UPLOAD_TIME},
		#{UPLOAD_USER},
		#{UPLOAD_USER_NAME},
		#{PATH},
		#{REMOTE_PATH},
		#{CREATED_TIME},
		#{ISDELETE},
		#{REMOTE_FILE_NAME},
		#{LOCKTOOL},
		#{ISTOPTIME},
		#{ASSOCIATION},
		#{STATUS},
		#{BUS_TEXT_LIBRARY_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_TEXT_LIBRARY_ID = #{BUS_TEXT_LIBRARY_ID}
	</delete>

	<!-- 修改 -->
	<update id="edit" parameterType="pd">
		update
		<include refid="tableName"></include>
		set
		REMARKS = #{REMARKS},
		TYPE = #{TYPE},
		FILE_NAME = #{FILE_NAME},
		REMOTE_FILE_NAME = #{REMOTE_FILE_NAME},
		UPLOAD_TIME = #{UPLOAD_TIME},
		UPLOAD_USER = #{UPLOAD_USER},
		UPLOAD_USER_NAME = #{UPLOAD_USER_NAME},
		PATH = #{PATH},
		REMOTE_PATH = #{REMOTE_PATH},
		CREATED_TIME = #{CREATED_TIME},
		LOCKTOOL = #{LOCKTOOL},
		ISTOPTIME = #{ISTOPTIME},
		TYPE_NAME = #{TYPE_NAME},
		ASSOCIATION = #{ASSOCIATION},
		STATUS = #{STATUS},
		CORPINFO_ID = #{CORPINFO_ID},
		BUS_TEXT_LIBRARY_ID = BUS_TEXT_LIBRARY_ID
		where
		BUS_TEXT_LIBRARY_ID = #{BUS_TEXT_LIBRARY_ID}
	</update>

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

	<!-- 列表 -->
	<select id="datalistPage" parameterType="page" resultType="pd">
		select
		<include refid="Field"></include>,
		bci.CORP_NAME
		from
		<include refid="tableName"></include> f
		left join sys_user su on f.UPLOAD_USER = su.USER_ID
		left join bus_corp_info bci on bci.CORPINFO_ID = su.CORPINFO_ID
		where f.ISDELETE = '0'
		<if test="pd.KEYWORDS != null and pd.KEYWORDS != ''"><!-- 关键词检索 -->
			and f.REMARKS LIKE CONCAT(CONCAT('%', #{pd.KEYWORDS}),'%')
		</if>
		<if test="pd.LABELS != null and pd.LABELS.length > 0">
			and exists(select 1 from bus_library_labels b where b.BUS_TEXT_LIBRARY_ID = f.BUS_TEXT_LIBRARY_ID and
			b.BUS_LABEL_FACTORY_ID in
			<foreach item="item" index="index" collection="pd.LABELS" open="(" separator="," close=")">
				#{pd.LABELS[${index}]}
			</foreach>)
		</if>
		<if test="pd.ASSOCIATION != null and pd.ASSOCIATION != ''">
			and f.ASSOCIATION = #{pd.ASSOCIATION}
		</if>
		<if test="pd.STATUS != null and pd.STATUS != ''">
			and f.STATUS = #{pd.STATUS}
		</if>
		<if test="pd.CATEGORY_IDS != null and pd.CATEGORY_IDS.length > 0">
			and exists(select 1 from bus_library_labels b where b.BUS_TEXT_LIBRARY_ID = f.BUS_TEXT_LIBRARY_ID and
			b.CATEGORY_ID in
			<foreach item="item" index="index" collection="pd.CATEGORY_IDS" open="(" separator="," close=")">
				#{pd.CATEGORY_IDS[${index}]}
			</foreach>)
		</if>
		<if test="pd.CORPINFO_ID != null and pd.CORPINFO_ID != ''">
			and f.CORPINFO_ID = #{pd.CORPINFO_ID}
		</if>
		<if test="pd.ENTERPRISE_SIDE != null and pd.ENTERPRISE_SIDE != ''">
			and f.CORPINFO_ID != #{pd.ENTERPRISE_SIDE}
		</if>
		order by f.ISTOPTIME desc,f.CREATED_TIME desc
	</select>

	<!-- 列表(全部) -->
	<select id="listAll" parameterType="pd" resultType="pd">
		select
		<include refid="Field"></include>
		from
		<include refid="tableName"></include> f
	</select>

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

</mapper>
