<?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="industrySqlMapper">

	<sql id="base_column">
		id,
		name,
		father_id as fatherId
	</sql>
	
	<select id="findAllFatherIndustry" resultType="com.cloudcross.ssp.model.Industry" parameterType="java.util.Map">
		select
	    <include refid="base_column" />
	    from t_industry where father_id = 0
	    <if test="pager != null">
	    	limit #{pager.offset}, #{pager.limit}
	    </if>
	</select>
	
	<select id="countAllFatherIndustry" resultType="java.lang.Integer" parameterType="java.util.Map">
		select count(id) from t_industry where father_id = 0
	</select>
	
	<select id="findIndustryByFatherId" parameterType="java.util.Map" resultType="com.cloudcross.ssp.model.Industry">
		select
	    <include refid="base_column" />
	    from t_industry where father_id = #{fatherId}
	    limit #{pager.offset}, #{pager.limit}
	</select>
	
	<select id="countIndustryByFatherId" parameterType="java.util.Map" resultType="java.lang.Integer">
		select count(id) from t_industry where father_id = #{fatherId}
	</select>
	
	<select id="findAllIndustry" resultType="com.cloudcross.ssp.model.Industry">
		select a.id,a.name,b.name as fatherName from t_industry a,t_industry b 
		where a.father_id = b.id
	</select>
	
	<select id="findAllUseIndustry" parameterType="java.lang.Long" resultType="com.cloudcross.ssp.model.Industry">
		select tiy.id,tiy.name,tiy.father_id as fatherId
		from t_advertiser tar 
		LEFT JOIN t_industry tiy ON tar.industry_id = tiy.id 
		where tar.status != -1
		<if test="_parameter != null">	
			and tar.agent_id = #{_parameter}
		</if>
		group by tar.industry_id
	</select>
	
	<select id="findByIndustryId" parameterType="int" resultType="com.cloudcross.ssp.model.Industry">
		select 
		 <include refid="base_column" />
		from t_industry
		<where>
			id = #{id}
		</where>
	</select>
	
	<select id="findAllIndustryByZoneIdOperator"  resultType="com.cloudcross.ssp.model.Industry">
	    select
	    <include refid="base_column" />
	    from t_industry 
	    <where>
		id not in( select industry_id from t_zone_industry where zone_id = #{zoneId} and
		 t_zone_industry.status != -1)
		</where>
	</select>
	
</mapper>