<?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.ozs.kpim.mapper.topic.TopicMapper">

	<sql id="base_column_list">
		id,topic_code,name,description,level,update_time,update_user_id,create_time
	</sql>

	<!--插入(所有字段)-->
	<insert id="insert" parameterType="com.ozs.kpim.dto.topic.TopicDTO">
		INSERT INTO kpim_info_topic(id,topic_code,name,description,level,update_time,update_user_id,create_time)
		VALUE(UUID(),#{topicCode},#{name},#{description},#{level},NOW(),#{updateUserId},NOW());
	</insert>

	<!--更新（所有字段,id,topic_code，level除外）-->
	<update id="update" parameterType="com.ozs.kpim.dto.topic.TopicDTO">
		UPDATE kpim_info_topic
		SET
			name = #{name},
			description = #{description},
			update_time = NOW(),
			update_user_id = #{updateUserId}
		WHERE 1 = 1
		<if test="id != null and id != '' ">
			and id = #{id}
		</if>
		<if test="topicCode != null and topicCode != '' ">
			and topic_code = #{topicCode}
		</if>
	</update>

	<!--删除主题-->
	<delete id="delete">
		DELETE FROM kpim_info_topic WHERE id = #{id}
	</delete>

	<!--删除主题及子主题,根据主题编码-->
	<delete id="deleteByTopicCode">
		DELETE FROM kpim_info_topic WHERE topic_code LIKE  CONCAT(#{topicCode},'%')
	</delete>

	<!--根据ID查询详情-->
	<select id="getById" resultType="com.ozs.kpim.dto.topic.TopicDTO">
		SELECT
			A.id,A.topic_code,
			A.name,A.description,
			A.level,A.update_time,A.create_time,
			A.update_user_id,
			/*B.topic_code as parentTopicCode,*/
			IFNULL(B.topic_code,'0') AS parentTopicCode,
			B.name as parentTopic,
			SU.nickname as updateUserName

		FROM kpim_info_topic A
		LEFT JOIN kpim_info_topic B
		ON LEFT(A.topic_code,LENGTH(A.topic_code)-3) = B.topic_code
		LEFT JOIN dc_info_user SU
		ON A.update_user_id = SU.id
		WHERE A.id = #{id}
	</select>

	<!--根据topic_code查询详情-->
	<select id="getByTopicCode" resultType="com.ozs.kpim.dto.topic.TopicDTO">
		SELECT
			A.id,A.topic_code,
			A.name,A.description,
			A.level,A.update_time,A.create_time,
			A.update_user_id,
			/*B.topic_code as parentTopicCode,*/
			IFNULL(B.topic_code,'0') AS parentTopicCode,
			B.name as parentTopic ,
			SU.nickname as updateUserName
		FROM kpim_info_topic A
		LEFT JOIN kpim_info_topic B
		ON LEFT(A.topic_code,LENGTH(A.topic_code)-3) = B.topic_code
		LEFT JOIN dc_info_user SU
		ON A.update_user_id = SU.id
		WHERE A.topic_code = #{topicCode}
	</select>

	<!--根据topic_code查询主题列表-->
	<select id="listTopicsByTopicCodes" resultType="com.ozs.kpim.dto.topic.TopicDTO">
		SELECT
			A.id,A.topic_code,
			A.name,A.description,
			A.level,A.update_time,A.create_time,
			A.update_user_id,
			B.topic_code as parentTopicCode,
			B.name as parentTopic ,
			SU.nickname as updateUserName
		FROM kpim_info_topic A
		LEFT JOIN kpim_info_topic B
		ON LEFT(A.topic_code,LENGTH(A.topic_code)-3) = B.topic_code
		LEFT JOIN dc_info_user SU
		ON A.update_user_id = SU.id

		<if test="list != null and list.size > 0 ">
			WHERE A.topic_code IN
			<foreach collection="list" item="item" open="(" close=")" separator=",">
				#{item}
			</foreach>
		</if>


	</select>

	<!--根据KpiId查询主题列表-->
	<select id="listTopicsByKpiId" resultType="com.ozs.kpim.dto.topic.TopicDTO">
		SELECT
		A.id,A.topic_code,
		A.name,A.description,
		A.level,A.update_time,A.create_time,
		A.update_user_id,
		B.topic_code as parentTopicCode,
		B.name as parentTopic ,
		SU.nickname as updateUserName
		FROM kpim_info_topic A
		LEFT JOIN kpim_info_topic B
		ON LEFT(A.topic_code,LENGTH(A.topic_code)-3) = B.topic_code
		LEFT JOIN dc_info_user SU
		ON A.update_user_id = SU.id
		LEFT JOIN kpim_rel_kpi_topic D
		ON A.id = D.topic_id
		WHERE D.kpi_id = #{kpiId}

	</select>

	<!--根据ID查询数量-->
	<select id="getCountById" resultType="java.lang.Integer">
	    SELECT  COUNT(1) FROM  kpim_info_topic WHERE id = #{id}
	</select>

	<!--根据topicCode查询数量-->
	<select id="getCountByTopicCode" resultType="java.lang.Integer">
	    SELECT  COUNT(1) FROM  kpim_info_topic WHERE topic_code = #{topicCode}
	</select>


	<!--判断主题是否在使用中-->
	<select id="ifTopicInfoUse" resultType="java.lang.Integer">
	    SELECT  COUNT(1) FROM  kpim_rel_kpi_topic WHERE topic_id = #{topicId}
	</select>

	<!--判断主题或者主题下的子主题是否被使用-->
	<select id="ifTopicOrSubTopicsInUse" resultType="java.lang.Integer">
		SELECT COUNT(1) FROM kpim_rel_kpi_topic WHERE topic_id IN
		<foreach collection="list" item="item"  open="(" close=")" separator=",">
			#{item}
		</foreach>
	</select>

	<!--主题列表查询-->
	<select id="listTopics" resultType="com.ozs.kpim.dto.topic.TopicDTO">
		SELECT
			A.id,A.topic_code,
			A.name,A.description,
			A.level,A.update_time,A.create_time,
			A.update_user_id,
			B.topic_code as parentTopicCode,
			B.name as parentTopic ,
			SU.nickname as updateUserName
		FROM kpim_info_topic A
		LEFT JOIN kpim_info_topic B
		ON LEFT(A.topic_code,LENGTH(A.topic_code)-3) = B.topic_code
		LEFT JOIN dc_info_user SU
		ON A.update_user_id = SU.id
		<where>
			<if test="name != null and name != '' ">
				A.name LIKE CONCAT('%',#{name},'%')
			</if>
		</where>

	</select>

	<!--查询上级主题编码查询下级主题-->
	<select id="listSubTopics" resultType="com.ozs.kpim.dto.topic.TopicDTO">
		SELECT
			<include refid="base_column_list"/>
		FROM kpim_info_topic
		<where>
			<if test=" parentTopicCode != null and parentTopicCode !='' ">
				topic_code LIKE CONCAT(#{parentTopicCode},'%')
				AND topic_code != #{parentTopicCode}
				AND level = LENGTH(#{parentTopicCode})/3 + 1
			</if>
			<if test=" parentTopicCode == null or parentTopicCode == '' ">
				level = 1
			</if>
		</where>
	</select>

	<!--20190331查询主题的所有子主题不分级别-->
	<select id="listAllSubTopics" resultType="com.ozs.kpim.dto.topic.TopicDTO">
		SELECT
		<include refid="base_column_list"/>
		FROM kpim_info_topic
		WHERE topic_code LIKE CONCAT(#{parentTopicCode},'%') AND topic_code != #{parentTopicCode}
	</select>

	<!--给ztree使用的格式-->
	<select id="topicForZtree" resultType="com.ozs.kpim.dto.topic.TopicZtreeDTO">
		SELECT
            A.id,
			A.topic_code as code,
			A.name as name,
			/*B.topic_code as pcode*/
			IFNULL(B.topic_code,'0') AS pcode
		FROM kpim_info_topic A
		LEFT JOIN kpim_info_topic B
		ON LEFT(A.topic_code,LENGTH(A.topic_code)-3) = B.topic_code
		<where>
			<if test="name != null and name != '' ">
				A.name LIKE CONCAT('%',#{name},'%')
			</if>
		</where>
		order by A.create_time asc
	</select>


</mapper>