<?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.AdvertisingResourcesMapper">

	<select id="findAdvertisingPage" resultType="map">
		select 
		CAST(media_id AS CHAR) as mediaId,
		media_code as mediaCode,
		case when media_type = 1 then '电梯内广告' when media_type = 2 then '道闸广告' when media_type = 3 then '道旗广告' when media_type = 4 then '大牌广告'
		when media_type = 5 then '灯箱广告' when media_type = 0 then '其他广告' when media_type = 6 then '电梯外广告' 
		when media_type = 7 then '步梯楼广告' else '' end as mediaType,
		case when media_form = 0 then '框架广告' when media_form = 1 then '电子屏' when media_form = 2 then '梯内投影' when media_form = 3 then '梯外门贴' 
		when media_form = 4 then '数码海报' when media_form = 5 then 'LCD电视' when media_form = 6 then '翻页道闸' when media_form = 7 then '横杆道闸' when media_form = 8 then '单边道旗' 
		when media_form = 9 then '双边道旗' when media_form = 10 then '地面大牌' when media_form = 11 then '墙体大牌' when media_form = 12 then '楼顶大牌' when media_form = 13 then '三面翻' 
		when media_form = 14 then '户外滚动灯箱' when media_form = 15 then '户外固定灯箱' when media_form = 16 then '室内固定灯箱' when media_form = 17 then '公交亭海报' when media_form = 18 then '易拉宝'
	    when media_form = 20 then '梯内门贴' when media_form = 21 then '梯外投影' 
	    when media_form = 22 then '户外滚动灯箱' when media_form = 23 then '公告栏海报' when media_form = 24 then '户外LED' 
        when media_form = 25 then '门禁灯箱' when media_form = 26 then '横幅' when media_form = 27 then 'X展架' 
        when media_form = 28 then '快递柜广告' when media_form = 29 then '地贴/墙贴/玻璃贴' when media_form = 30 then '地贴/墙贴/玻璃贴' 
        when media_form = 31 then '立式广告机' when media_form = 32 then '门岗亭海报' when media_form = 33 then '饮水机体广告' 
        else '' end as mediaForm,
		media_width as mediaWidth,
		media_height as mediaHeight,
		media_unit as mediaUnit,
		-- meida_number as meidaNumber,
		media_status as mediaStatus,
		site_one as siteOne,
		site_two as siteTwo,
		organ_code as organCode,
		organ_name as organName,
		comm_id as commId,
		comm_name as commName,
		comm_address as commAddress,
		sort,
		version,
		is_delete as isDelete,
		create_time as createTime,
		create_by as createBy,
		create_by_name as createByName,
		mod_time as modTime,
		mod_by as modBy,
		mod_by_name as  modByName
		from t_media_resource 
		where is_delete = 0
		
		<if test="organCode!=null and organCode!=''">
			and organ_code = #{organCode}
		</if>
		
		<if test="commId!=null and commId!=''">
			and comm_id like CONCAT('%',#{commId},'%')
		</if>
		<if test="organName!=null and organName!=''">
			and organ_name like concat('%',#{organName},'%')
		</if>
		<if test="commName!=null and commName!=''">
			and comm_name like concat('%',#{commName},'%')
		</if>
		<if test="mediaType!=null and mediaType!=''">
			and media_type = #{mediaType}
		</if>
		<if test="mediaForm!=null and mediaForm!=''">
			and media_form = #{mediaForm}
		</if>
		<if test="organSearch!=null and organSearch!=''">
			and organ_code like CONCAT('%',#{organSearch},'%')		
		</if>
		<if test="siteOne!=null and siteOne!=''">
			and site_one like CONCAT('%',#{siteOne},'%')		
		</if>
		
		order by organ_name,comm_name,media_code asc 
		
		limit #{pageOffset}, #{pageSize}
	</select>

	<select id="findAdvertisingCount" resultType="long">
		select count(media_id) from t_media_resource 
		where is_delete = 0
		<if test="organCode!=null and organCode!=''">
			and organ_code = #{organCode}
		</if>
		<if test="commId!=null and commId!=''">
			and comm_id like CONCAT('%',#{commId},'%')
		</if>
		<if test="organName!=null and organName!=''">
			and organ_name like concat('%',#{organName},'%')
		</if>
		<if test="commName!=null and commName!=''">
			and comm_name like concat('%',#{commName},'%')
		</if>
		<if test="mediaType!=null and mediaType!=''">
			and media_type = #{mediaType}
		</if>
		<if test="mediaForm!=null and mediaForm!=''">
			and media_form = #{mediaForm}
		</if>
		<if test="organSearch!=null and organSearch!=''">
			and organ_code like CONCAT('%',#{organSearch},'%')		
		</if>
		<if test="siteOne!=null and siteOne!=''">
			and site_one like CONCAT('%',#{siteOne},'%')		
		</if>
	</select>
	
	<update id="updateState">
		update t_media_resource set media_status = #{state} where media_id in 
		<foreach collection="ids" index="index" item="item" open="(" separator="," close=")">
               #{item}       
        </foreach> 
	</update>
	
	<select id="findAdvertising" resultType="map">
		select 
		organ_code as organCode,
		comm_id as commId,
		media_code as mediaCode,
		organ_name as organName,
		comm_name as commName,
		comm_address as commAddress,
		case when media_type = 1 then '电梯内广告' when media_type = 2 then '道闸广告' when media_type = 3 then '道旗广告' when media_type = 4 then '大牌广告'
		when media_type = 5 then '灯箱广告' when media_type = 0 then '其他广告' when media_type = 6 then '电梯外广告' 
		when media_type = 7 then '步梯楼广告' else '' end as mediaType,
		case when media_form = 0 then '框架广告' when media_form = 1 then '电子屏' when media_form = 2 then '梯内投影' when media_form = 3 then '梯外门贴' 
		when media_form = 4 then '数码海报' when media_form = 5 then 'LCD电视' when media_form = 6 then '翻页道闸' when media_form = 7 then '横杆道闸' when media_form = 8 then '单边道旗' 
		when media_form = 9 then '双边道旗' when media_form = 10 then '地面大牌' when media_form = 11 then '墙体大牌' when media_form = 12 then '楼顶大牌' when media_form = 13 then '三面翻' 
		when media_form = 14 then '户外滚动灯箱' when media_form = 15 then '户外固定灯箱' when media_form = 16 then '室内固定灯箱' when media_form = 17 then '公交亭海报' when media_form = 18 then '易拉宝'
	    when media_form = 20 then '梯内门贴' when media_form = 21 then '梯外投影' 
	    when media_form = 22 then '户外滚动灯箱' when media_form = 23 then '公告栏海报' when media_form = 24 then '户外LED' 
        when media_form = 25 then '门禁灯箱' when media_form = 26 then '横幅' when media_form = 27 then 'X展架' 
        when media_form = 28 then '快递柜广告' when media_form = 29 then '地贴/墙贴/玻璃贴' when media_form = 30 then '地贴/墙贴/玻璃贴' 
        when media_form = 31 then '立式广告机' when media_form = 32 then '门岗亭海报' when media_form = 33 then '饮水机体广告'  
        else '' end as mediaForm,
		media_code as mediaCode,
		site_one as siteOne,
		site_two as siteTwo,
		media_width as mediaWidth,
		media_height as mediaHeight,
		media_unit as mediaUnit,
		case when media_status = 0 then '启用' when media_status = 1 then '停用' else '' end as mediaStatus 
		from t_media_resource 
		where is_delete = 0
		<if test="organCode!=null and organCode!=''">
			and organ_code = #{organCode}
		</if>
		<if test="commId!=null and commId!=''">
			and comm_id like CONCAT('%',#{commId},'%')
		</if>
		<if test="organName!=null and organName!=''">
			and organ_name like concat('%',#{organName},'%')
		</if>
		<if test="commName!=null and commName!=''">
			and comm_name like concat('%',#{commName},'%')
		</if>
		<if test="mediaType!=null and mediaType!=''">
			and media_type = #{mediaType}
		</if>
		<if test="mediaForm!=null and mediaForm!=''">
			and media_form = #{mediaForm}
		</if>
		<if test="organSearch!=null and organSearch!=''">
			and organ_code like CONCAT('%',#{organSearch},'%')		
		</if>
		<if test="siteOne!=null and siteOne!=''">
			and site_one like CONCAT('%',#{siteOne},'%')		
		</if>
		order by organ_name,comm_name,media_code asc 
	</select>
	
	<select id="findNextMediaCode" resultType="String">
		select case when length(next) = 1 then concat('00',next) 
		when length(next) = 2 then concat('0',next) 
		when length(next) >= 3 then next 
		else '001' end as next from (
			select substring(max(media_code),length(#{str})+1)+1 as next from t_media_resource 
			where media_code like concat(#{str},'%')
		) as temp
	</select>
	
	<select id="findExportNextSerial" resultType="String">
		select 
			concat(
				(select concat(comm_id,'-',organ_code,'-',#{variate}) from basic_info 
				 where property_project = #{project} 
				 and property_area = #{area}),
					(select 
								case when length(next) = 1 then concat('00',next) 
								when length(next) = 2 then concat('0',next) 
								when length(next) = 3 then next 
								else '001' end as next
					from (
								select right(max(serial_number),3)+1 as next from advertising_resources 
								where serial_number like (select concat(comm_id,'-',organ_code,'-',#{variate},'%') 
								from basic_info where property_project = #{project} and property_area = #{area})
					) as temp 
				)
		)

	</select>
	<!-- <selectKey resultType="java.lang.Long" order="AFTER" keyProperty="mediaId">
          	SELECT LAST_INSERT_ID()
      	</selectKey> -->
	
	<insert id="insertCallBack" >
      	insert into t_media_resource(media_id,media_code,media_type,media_form,media_width,media_height,media_unit,media_price_unit,meida_number,media_status,
      	site_one,site_two,organ_code,organ_name,comm_id,comm_name,comm_address,sort,version,is_delete,create_time,create_by,create_by_name) 
      	values(#{mediaId},#{mediaCode},#{mediaType},#{mediaForm},#{mediaWidth},#{mediaHeight},#{mediaUnit},#{mediaPriceUnit},#{meidaNumber},#{mediaStatus},#{siteOne},
      	#{siteTwo},#{organCode},#{organName},#{commId},#{commName},#{commAddress},#{sort},#{version},#{isDelete},#{createTime},#{createBy},#{createByName})
	</insert>
	  
	<delete id="dels">
		update t_media_resource set is_delete = 1 where media_id in 
		<foreach collection="mediaIds" item="item" index="index" open="(" close=")" separator=",">
            #{item}
        </foreach>
	</delete>
	
	
	<select id="findMediaCode" resultType="string">
		select media_code from t_media_resource where is_delete = 0 and media_id in 
		<foreach collection="cycleaList" item="item" index="index" open="(" close=")" separator=",">
            #{item}
        </foreach>
	</select>

		
	<select id="findOrgAndAddess" resultType="map">
		select organ_code as code,ifnull(comm_address,' ') as address,
		organ_name as organName,comm_name as commName 
		from t_media_comm_info where 1=1 
		and ifnull(is_delete,0) = 0 
		and comm_id = #{commId} 
		limit 1
	</select>
	
	
	<select id="verifyCommIdAndSite" resultType="int">
		select count(t.cot) from (
			select concat(comm_id,site_one,site_two) as cot from t_media_resource where is_delete = 0
		) as t 
		where t.cot = #{cot}
	</select>
	
	<select id="verifyCommIdAndSiteById" resultType="int">
		select count(t.cot) from (
			select concat(comm_id,site_one,site_two) as cot,media_id as id from t_media_resource where is_delete = 0
		) as t 
		where t.cot = #{cot}
		<if test="id!=null and id!=''">
			and t.id != #{id}
		</if>
		
	</select>
	
	<insert id="insertOne" parameterType="cn.com.countrygarden.bi.main.model.mediaResource.AdvertisingResources">
		INSERT INTO t_media_resource (
			media_id,
			media_type,
			media_form,
			media_width,
			media_height,
			media_unit,
			meida_number,
			media_status,
			site_one,
			site_two,
			media_code,
			comm_id,
			comm_name,
			organ_code,
			organ_name,
			create_by,
			create_by_name,
			create_time
		)
		values(#{mediaId},#{mediaType},#{mediaForm},#{mediaWidth},#{mediaHeight},#{mediaUnit},#{mediaStatus},
		#{siteOne},#{siteTwo},#{mediaCode},#{commId},#{commName},#{organCode},#{organName},
		#{createBy},#{createByName},#{createTime})
		
		

 
	</insert>
	
	<select id="verifyLocation" resultType="int">
		select count(media_id) from t_media_resource where is_delete = 0 and comm_id = #{commId} and site_one = #{siteOne} and site_two = #{siteTwo}
	</select>
	
	<select id="verifyLocation2" resultType="int">
		select count(media_id) from t_media_resource where is_delete = 0 and comm_id = #{commId} and site_one = #{siteOne} and site_two = #{siteTwo}
		<if test="mediaId!=null and mediaId!=''">
			and media_id != #{mediaId}
		</if>
	</select>
	
	
	<select id="findNextCode2" resultType="string">
		select ifnull(max(REPLACE(media_code,#{mediaCode},''))+1,'001') from t_media_resource 
			  where media_code like concat(#{mediaCode},'%') and is_delete = 0 
	</select>
	
	<select id="findOne" resultType="map">
		select media_id as mediaId,media_type as mediaType,media_code as mediaCode,
		comm_id as commId,organ_code as organCode,
		media_form as mediaForm,media_width as mediaWidth,media_height as mediaHeight,
		media_unit as mediaUnit,media_price_unit as mediaPriceUnit,meida_number as meidaNumber,
		media_status as mediaStatus,site_one as siteOne,site_two as siteTwo,organ_name as organName,
		comm_name as commName,version from t_media_resource where media_id = #{mediaId} 
	</select>
	
</mapper>