<?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="cn.com.countrygarden.bi.main.dao.mediaResource.CustomerMapper">

	<select id="selectCustomer" resultType="map">
		select 
		CAST(customer_id AS CHAR) as customerId,
		case when customer_type = 0 then '集团内部' when customer_type = 1 then '代理公司' 
		when customer_type = 2 then '直客' else '' end as customerType,
	  	case when customer_industry = 15 then '其他' when customer_industry = 0 then '建材' when customer_industry = 1 then '家居'
	    when customer_industry = 2 then '装修' when customer_industry = 3 then '电器' when customer_industry = 4 then '3C'
	    when customer_industry = 5 then '电商' when customer_industry = 6 then '商超' when customer_industry = 7 then '餐饮'
	    when customer_industry = 8 then '饮料' when customer_industry = 9 then '食品' when customer_industry = 10 then '教育'
		when customer_industry = 11 then '金融' when customer_industry = 12 then '交通' when customer_industry = 13 then '旅游'
		when customer_industry = 14 then '文本' else '' end as customerIndustry,
		customer_name as customerName,
		contact_phone as contactPhone,
		contact_name as contactName,
		customer_no as customerNo,
		remark 
		from t_media_customer where is_delete = 0  
		<if test="customerName!=null and customerName!=''">
		 	and customer_name like concat('%',#{customerName},'%')
		</if>
		<if test="contactName!=null and contactName!=''">
			and contact_name like concat('%',#{contactName},'%')
		</if>
		<if test="customerType!=null and customerType!=''">
			and customer_type = #{customerType}
		</if>
		<if test="customerIndustry!=null and customerIndustry!=''">
			and customer_industry = #{customerIndustry}
		</if>
		<if test="createBy!=null and createBy!=''">
			and create_by = #{createBy} 
		</if>
		order by customer_type asc
		limit #{pageOffset}, #{pageSize}
	</select>
	
	<select id="selectCustomerCount" resultType="long">
		select count(customer_id)
		from t_media_customer where is_delete = 0 
		<if test="customerName!=null and customerName!=''">
		 	and customer_name like concat('%',#{customerName},'%')
		</if>
		<if test="contactName!=null and contactName!=''">
			and contact_name like concat('%',#{contactName},'%')
		</if>
		<if test="customerType!=null and customerType!=''">
			and customer_type = #{customerType}
		</if>
		<if test="customerIndustry!=null and customerIndustry!=''">
			and customer_industry = #{customerIndustry}
		</if>
		<if test="createBy!=null and createBy!=''">
			and create_by = #{createBy} 
		</if>
	</select>
	
	<update id="dels">
		update t_media_customer set is_delete = 1 where customer_id in 
		<foreach collection="customerIds" index="index" item="item" open="(" separator="," close=")">
              #{item}       
        </foreach> 
	</update>
	
	<select id="findOne" resultType="map">
		select CAST(customer_id AS CHAR) as customerId,CAST(customer_type AS CHAR)as customerType,customer_name as customerName,
		    contact_phone as contactPhone,contact_name as contactName,remark,version,CAST(customer_industry AS CHAR) as customerIndustry 
		    from t_media_customer
	  	    where customer_id = #{customerId}
	</select>

	<update id="updateOne" parameterType="cn.com.countrygarden.bi.main.model.mediaResource.Customer">
		UPDATE t_media_customer
			SET 
			 customer_type = #{customerType},
			 customer_name = #{customerName},
			 contact_phone = #{contactPhone},
			 contact_name = #{contactName},
			 customer_industry = #{customerIndustry},
			 remark = #{remark},
			 version = (version+1),
			 mod_by = #{modBy},
			 mod_time = #{modTime},
			 mod_by_name = #{modByName} 
		WHERE customer_id =#{customerId}
	</update>

	<select id="verifyCustomerName" resultType="int">
		select count(customer_id) from t_media_customer where is_delete = 0 and customer_name = #{customerName}
		<if test="customerId!=null and customerId!=''">
			and customer_id != #{customerId}
		</if>
		 
	</select>
</mapper>