<?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.nbclass.activity.mapper.DataDictMapper">
  <resultMap id="BaseResultMap" type="com.nbclass.activity.model.DataDict">
    <!--
      WARNING - @mbg.generated
    -->
    <id column="id" jdbcType="BIGINT" property="id" />
    <result column="createtime" jdbcType="TIMESTAMP" property="createtime" />
    <result column="updatetime" jdbcType="TIMESTAMP" property="updatetime" />
    <result column="datakey" jdbcType="VARCHAR" property="datakey" />
    <result column="name" jdbcType="VARCHAR" property="name" />
    <result column="isvalid" jdbcType="TINYINT" property="isvalid" />
    <result column="description" jdbcType="VARCHAR" property="description" />
  </resultMap>

  	<select id="delCheck" parameterType="java.util.List" resultType="java.lang.Long">
  		select count(*) from data_dict_item
  		where dictid in
  		<foreach item="item" collection="list" open="(" separator="," close=")">
			#{item}
		</foreach>
  	</select>

	<delete id="deleteByIds" parameterType="java.util.List">
		delete from data_dict where id in
	    <foreach item="item" collection="list" open="(" separator="," close=")">
			#{item}
		</foreach>
	</delete>

	<select id="getList" resultType="com.nbclass.activity.model.DataDict" parameterType="com.nbclass.activity.model.DataDict">
		SELECT * FROM data_dict where 1=1
	  	<if test="isvalid != null and isvalid >= 0">
		    AND data_dict.isvalid = #{isvalid}
		</if>
	  	<if test="name != null and name != ''">
	    	AND (data_dict.datakey like CONCAT('%',#{name},'%') or data_dict.name like CONCAT('%',#{name},'%') )
	  	</if>
		order by data_dict.id desc
	</select>

	<select id="getItemsByKey" resultType="com.nbclass.activity.model.DataDictItem" parameterType="com.nbclass.activity.model.DataDictItem">
       select t.* from data_dict_item t
       left join data_dict on t.dictid = data_dict.id
       where data_dict.isvalid = 1 and data_dict.datakey = #{datakey}
       order by t.sort
	</select>

	<select id="getItemsByNames" parameterType="java.util.List" resultType="com.nbclass.activity.model.DataDictItem">
		select data_dict_item.* from data_dict_item
		left join data_dict on data_dict_item.dictid = data_dict.id
		where data_dict.datakey = #{datakey}
		<if test="tagNames != null">
			and data_dict_item.name in
			<foreach item="tag_name" collection="tagNames" open="(" separator="," close=")">
              #{tag_name}
			</foreach>
		</if>
	</select>

	<select id="getDataDictTemName" resultType="com.nbclass.activity.model.DataDictItem">
		SELECT
			item.*
		FROM
			data_dict_item item
		LEFT JOIN data_dict dict ON item.dictid = dict.id
		WHERE dict.`name` = '标签'
		<if test="tagNames != null">
			and item.`name` in
			<foreach item="tag_name" collection="tagNames" open="(" separator="," close=")">
				#{tag_name}
			</foreach>
		</if>
	</select>

  	<insert id="insertDataDictItem" parameterType="com.nbclass.activity.model.DataDictItem" useGeneratedKeys="true" keyProperty="id">
		INSERT ignore INTO data_dict_item(dictid, `value`, `name`) VALUES (#{dictid}, #{value}, #{name} )
	</insert>

	<select id="getDataDictID" resultType="java.lang.Long">
		select
		       id
		from data_dict
		where name = '标签' or name = '案例类型'
		   or name = '案例用途' or name = '创意形式'
		   or name = '节日事件' or name = '品牌公司'
		   or name = '广告公司' or name = '关联部门'
	</select>

	<select id="getDataDicName" resultType="int">
		select count(*) from data_dict where name = #{name}
	</select>

	<select id="getDataDictIDLabels" resultType="java.lang.Long">
		select
			id
		from data_dict
		where name = '标签'
	</select>

</mapper>
