<?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.pcloud.book.book.dao.impl.BookLabelDaoImpl">

	<resultMap id="BaseResultMap" type="com.pcloud.book.book.entity.BookLabel">
		<id column="id" property="id" jdbcType="BIGINT" />
		<result column="name" property="name" jdbcType="VARCHAR" />
		<result column="type" property="type" jdbcType="INTEGER" />
		<result column="create_user" property="createUser" jdbcType="BIGINT" />
		<result column="audit_state" property="auditState" jdbcType="INTEGER" />
		<result column="create_time" property="createTime" jdbcType="TIMESTAMP" />
		<result column="update_label_id" property="updateLabelId" jdbcType="BIGINT" />
		<result column="remark" property="remark" jdbcType="VARCHAR" />
		<result column="seq" property="seq" jdbcType="INTEGER" />
	</resultMap>

	<sql id="Base_Column_List">
		id,`name`,`type`, create_user, audit_state, create_time, update_label_id, remark, seq
	</sql>

	<insert id="insert" parameterType="com.pcloud.book.book.entity.BookLabel" useGeneratedKeys="true" keyProperty="id">
		insert into book_label
		<trim prefix="(" suffix=")" suffixOverrides=",">
			`name`,`type`, create_user, audit_state, create_time, remark, seq
		</trim>
		<trim prefix="values (" suffix=")" suffixOverrides=",">
			#{name},
			#{type,jdbcType=INTEGER},
			#{createUser},
			#{auditState,jdbcType=INTEGER},
			NOW(),
			#{remark},
			#{seq}
		</trim>
	</insert>

	<update id="update" parameterType="com.pcloud.book.book.entity.BookLabel">
		update book_label
		<set>
			<if test="auditState != null">
				audit_state = #{auditState,jdbcType=INTEGER},
			</if>
			<if test="updateLabelId != null">
				update_label_id = #{updateLabelId},
			</if>
			<if test="seq != null">
				seq = #{seq},
			</if>
		</set>
		where id = #{id,jdbcType=BIGINT}
	</update>

	<delete id="deleteById" parameterType="long">
		delete from
		book_label
		where id = #{id,jdbcType=BIGINT}
	</delete>

	<select id="getMaxSeqByType" parameterType="integer" resultType="integer">
		SELECT MAX(seq) FROM book_label WHERE type=#{type}
	</select>

	<select id="getListByType" parameterType="integer" resultMap="BaseResultMap">
		SELECT 
			<include refid="Base_Column_List"/>
		FROM 
			book_label
		where type=#{type}
	</select>

	<!--获取全部-->
	<select id="getAll" resultMap="BaseResultMap" parameterType="map">
		select <include refid="Base_Column_List"/>
		from book_label
		WHERE audit_state=1
		<if test="createUser >0 ">
			OR create_user = #{createUser}
		</if>
		ORDER BY type ASC, seq ASC, id ASC
	</select>

	<!--根据id查询map-->
	<select id="getMapByIds" parameterType="list" resultMap="BaseResultMap">
		select <include refid="Base_Column_List"/>
		from book_label
		where id in
		<foreach collection="list" item="item" open="(" separator="," close=")">
			#{item}
		</foreach>
	</select>

	<select id="getByLabelIds" parameterType="list" resultMap="BaseResultMap">
		select <include refid="Base_Column_List"/>
		from book_label
		where id in
		<foreach collection="list" item="item" open="(" separator="," close=")">
			#{item}
		</foreach>
	</select>

	<select id="getByNameType" resultType="long" parameterType="map">
		SELECT id FROM book_label
		WHERE `name`= #{name}
		AND type = #{type}
		LIMIT 1
	</select>

	<select id="listBookLabel" parameterType="map" resultType="com.pcloud.book.book.vo.BookLabelVO">
		select
		id labelId,
		`name` name,
		type type,
		audit_state auditState,
		seq seq,
		create_time createTime,
		remark AS remark,
		create_user createUser
		from book_label
		WHERE 1=1
		<if test="auditState != null">
			and audit_state = #{auditState}
		</if>
		<if test="type != null">
			AND type = #{type}
		</if>
		<if test="name != null">
			AND `name` LIKE CONCAT('%',#{name},'%')
		</if>
		<if test="startTime != null">
			AND create_time >= #{startTime}
		</if>
		<if test="endTime != null">
			AND create_time &lt;= #{endTime}
		</if>
		<if test=" filterApply == 1">
			AND create_user>0
		</if>
		ORDER BY type ASC, seq ASC, id ASC
	</select>

</mapper>