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

	<select id="findAdvertisingPage" resultType="cn.com.countrygarden.bi.main.model.mediaResource.MediaOrderParam">
		select 
			media_id as mediaId, 
			media_code as mediaCode,
			media_type as mediaType,
			media_form as mediaForm,
			media_width as mediaWidth,
			media_height as mediaHeight,
			media_unit as mediaUnit,
			media_status mediaStatusStr,
			media_price_unit as priceUnit,
			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 
		and media_status = 0 <!-- 启用 -->
		<if test="commId!=null and commId!=''">
			and comm_id = #{commId}
		</if>
		<if test="organName!=null and organName!=''">
			and organ_name like concat('%',#{organName},'%')
		</if>
		<if test="organCode!=null and organCode!=''">
			and organ_code = #{organCode}
		</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="startTime!=null and startTime!=''">
			and media_id not in (
			SELECT media_id from t_media_cycle WHERE cycle_time &gt;=#{startTime}
			)
		</if>
		<if test="endTime!=null and endTime!=''">
			and media_id not in (
			SELECT media_id from t_media_cycle WHERE cycle_time &lt;= #{endTime}
			)
		</if>
		<if test="ids!=null and ids.size>0">
		and media_id in
			<foreach collection="ids" index="index" item="ids"
			open="(" separator="," close=")">
		 	 #{ids}
			</foreach>
		</if>
		<if test="organSearch!=null and organSearch!=''">
			and organ_code = #{organSearch}
		</if>
		 order by organ_name,comm_name asc 
		<if test="pageSize != '-1'">
			limit #{pageOffset}, #{pageSize}
		</if>
	</select>

	<select id="findAdvertisingCount" resultType="long">
		select count(media_id) from t_media_resource 
		where is_delete = 0
		and media_status = 0
		<if test="commId!=null and commId!=''">
			and comm_id = #{commId}
		</if>
		<if test="organName!=null and organName!=''">
			and organ_name like concat('%',#{organName},'%')
		</if>
		<if test="organCode!=null and organCode!=''">
			and organ_code =#{organCode}
		</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="startTime!=null and startTime!=''">
			and media_id not in (
			SELECT media_id from t_media_cycle WHERE cycle_time &gt;=#{startTime}
			)
		</if>
		<if test="endTime!=null and endTime!=''">
			and media_id not in (
			SELECT media_id from t_media_cycle WHERE cycle_time &lt;= #{endTime}
			)
		</if>
		<if test="ids!=null and ids.size>0">
		and media_id in
			<foreach collection="ids" index="index" item="ids"
			open="(" separator="," close=")">
			#{ids}
			</foreach>
		</if>
		<if test="organSearch!=null and organSearch!=''">
			and organ_code = #{organSearch}
		</if>
	</select>
	<select id="getMaxSort" resultType="int">
		SELECT
			ifnull(max(sort), 0)
		FROM
			t_media_order_bill
	</select>
	<select id="getMaxOrderSort" resultType="int">
		SELECT
			ifnull(max(sort), 0)
		FROM
			t_media_order
	</select>
	<insert id="insertOrderBillDetail" useGeneratedKeys="false" keyProperty="orderBillId" parameterType="cn.com.countrygarden.bi.main.model.mediaResource.MediaOrderBill">
		INSERT INTO t_media_order_bill
		(
		order_id,
		order_bill_id,
		media_id,
		prepare_start_date,
		prepare_end_date,
		order_bill_start_date,
		order_bill_end_date,
		is_final,
		version,
		is_delete,
		create_time,
		create_by,
		create_by_name,
		mod_time,
		mod_by,
		mod_by_name,
		cycle_status,
		sort
		)
		values(
		#{orderId},
		#{orderBillId},
		#{mediaId},
		#{prepareStartDate},
		#{prepareEndDate},
		#{orderBillStartDate},
		#{orderBillEndDate},
		#{isFinal},
		#{version},
		#{isDelete},
		#{createTime},
		#{createBy},
		#{createByName},
		#{modTime},
		#{modBy},
		#{modByName},
		#{cycleStatus},
		#{sort}
		)
	</insert>
	<insert id="insertOrderInfo" useGeneratedKeys="false" keyProperty="orderId" parameterType="cn.com.countrygarden.bi.main.model.mediaResource.MediaOrderBill">
			INSERT into t_media_order 
				(
				order_id, 
				order_code,
				order_start_date,
				order_end_date,
				order_amount,
				order_status,
				customer_id,
				remark,
				sort,
				version,
				is_delete,
				create_time,
				create_by,
				create_by_name,
				mod_time,
				mod_by,
				mod_by_name,
				organ_code
				)
			values(
			    #{orderId}, 
				#{orderCode},
				#{orderStartDate},
				#{orderEndDate},
				#{orderAmount},
				#{orderStatus},
				#{customerId},
				#{remark},
				#{sort},
				#{version},
				#{isDelete},
				#{createTime},
				#{createBy},
				#{createByName},
				#{modTime},
				#{modBy},
				#{modByName},
				#{organCode}
			)
	</insert>
	<select id="getCancelOrderListForPrint" resultType="cn.com.countrygarden.bi.main.model.mediaResource.MediaOrderParam">
	SELECT
		orderInfo.order_status orderStatus,
		media.organ_name organName,
		media.comm_name commName,
		media.comm_address commAddress,
		media.media_type mediaType,
		media.media_form mediaForm,
		media.media_code mediaCode,
		media.site_one siteOne,
		media.site_two siteTwo,
		media.media_width mediaWidth,
		media.media_height mediaHeight,
		media.media_unit mediaUnit,
		media_price_unit as priceUnit,
		bill.order_bill_id orderBillId,
		bill.cycle_status cycleStatus,
		date_format(bill.prepare_start_date,'%Y-%m-%d') startDate,
		date_format(bill.prepare_end_date,'%Y-%m-%d') endDate,
		bill.create_by_name createByName
	FROM
		t_media_order orderInfo
	LEFT JOIN t_media_order_bill bill ON bill.order_id = orderInfo.order_id
	LEFT JOIN t_media_resource media ON media.media_id = bill.media_id
	where 1=1
	and	orderInfo.order_id = #{orderId} 
	</select>
	<select id="getCancelOrderInfo" resultType="cn.com.countrygarden.bi.main.model.mediaResource.MediaOrderParam">
	SELECT
		orderInfo.order_code orderCode,
		date_format(orderInfo.order_start_date,'%Y-%m-%d') orderStartDate,
		date_format(orderInfo.order_end_date,'%Y-%m-%d') orderEndDate,
		orderInfo.remark remark,
		customer.customer_type customerType,
		customer.customer_name customerName,
		customer.contact_phone contactPhone
	FROM
		t_media_order orderInfo
	LEFT JOIN t_media_customer customer ON orderInfo.customer_id = customer.customer_id
	where 1=1
	<if test="orderId!=null and orderId!=''">
		and	orderInfo.order_id = #{orderId} 
	</if> 
	</select>
	<delete id="cancelOrder">
	delete from t_media_cycle
	<!-- SET is_delete = 1 -->
	WHERE
		media_id = #{mediaId}
	AND cycle_time &gt;= (
		SELECT
			prepare_start_date
		FROM
			t_media_order_bill
		WHERE
			order_bill_id = #{orderBillId}
	)
	AND cycle_time &lt;= (
		SELECT
			prepare_end_date
		FROM
			t_media_order_bill
		WHERE
			order_bill_id = #{orderBillId}
	) 
	<!-- UPDATE t_media_cycle
	SET 
		is_delete = 1
	WHERE
		media_id =#{mediaId}
	AND is_delete = 0  -->
	</delete>
	<insert id="insertCycle" useGeneratedKeys="false" keyProperty="cycleId" parameterType="cn.com.countrygarden.bi.main.model.mediaResource.MediaCycle">
	insert into t_media_cycle
	(
		cycle_id,
		cycle_time,
		cycle_status,
		comm_id,
		organ_code,
		media_id,
		sort,
		version,
		is_delete
	)
	values
	(
		#{cycleId},
		#{cycleTime},
		#{cycleStatus},
		#{commId},
		#{organCode},
		#{mediaId},
		#{sort},
		#{version},
		#{isDelete}
	)
	</insert>
	<select id="getMaxLogSort" resultType="int">
	SELECT
		ifnull(max(sort), 0)
	FROM
		t_media_cycle_log
	</select>
	<update id="delOrderBillDetail">
	UPDATE t_media_order_bill
	SET 
		is_delete = 1,
		mod_time=#{modTime},
		mod_by = #{modBy},
		mod_by_name = #{modByName}
	WHERE
		order_bill_id = #{orderBillId}
	</update>
	<select id="findMediaOutPage" resultType="Map">
		select 
		media_id as mediaId, 
		media_code as mediaCode,
		media_type as mediaType,
		media_form as mediaForm,
		media_width as mediaWidth,
		media_height as mediaHeight,
		media_unit as mediaUnit,
		media_price_unit as priceUnit,
		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,
		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 
		and media_status = 0<!-- 启用 -->
		<if test="commId!=null and commId!=''">
			and comm_id = #{commId}
		</if>
		<if test="organName!=null and organName!=''">
			and organ_name like concat('%',#{organName},'%')
		</if>
		<if test="organCode!=null and organCode!=''">
			and organ_code = #{organCode}
		</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="startTime!=null and startTime!=''">
			and media_id not in (
			SELECT media_id from t_media_cycle WHERE cycle_time &lt;=#{startTime}
			)
		</if>
		<if test="endTime!=null and endTime!=''">
			and media_id not in (
			SELECT media_id from t_media_cycle WHERE cycle_time >= #{endTime}
			)
		</if>
		order by organ_name,comm_name asc 
	</select>
	<select id="findOrderListOutPage"  resultType="Map">
			SELECT
			orderInfo.order_id orderId,
			orderInfo.order_id orderIdStr,
			orderInfo.order_code orderCode,
			customer.customer_type customerType,
			customer.customer_name customerName,
			customer.contact_name contactName,
			customer.contact_phone contactPhone,
			DATE_FORMAT(orderInfo.order_start_date,"%Y-%m-%d") orderStartDate,
			DATE_FORMAT(orderInfo.order_end_date,"%Y-%m-%d") orderEndDate,
			orderInfo.order_amount orderAmount,
			orderInfo.order_status orderStatus,
			media.organ_name organName,
			media.comm_name commName,
			media.comm_address commAddress,
			media.media_type mediaType,
			media.media_form mediaForm,
			media.media_code mediaCode,
			media.site_one siteOne,
			media.site_two siteTwo,
			media.media_width mediaWidth,
			media.media_height mediaHeight,
			media.media_unit mediaUnit,
			media.media_price_unit priceUnit,
			orderBill.cycle_status cycleStatus,
		 	orderBill.create_by_name createByName
		FROM
			t_media_order_bill orderBill
		LEFT JOIN t_media_order orderInfo ON orderBill.order_id = orderInfo.order_id
		LEFT JOIN t_media_customer customer ON orderInfo.customer_id = customer.customer_id
		LEFT JOIN t_media_resource media ON media.media_id = orderBill.media_id
		WHERE
			orderInfo.is_delete = 0
		AND orderBill.is_delete = 0
		AND media.is_delete = 0
	<if test="orderCode!=null and orderCode!=''">
		and	orderInfo.order_code =#{orderCode}
	</if>
	<if test="customerName!=null and customerName!=''">
		and	customer.customer_name like concat('%',#{customerName},'%')
	</if>
	<if test="contactPhone!=null and contactPhone!=''">
		and	customer.contact_phone like concat('%',#{contactPhone},'%')
	</if>
	<if test="orderStartDate!=null and orderStartDate!=''">
		and	orderInfo.order_start_date >= STR_TO_DATE(#{orderStartDate},"%Y-%m-%d") 
	</if>
	<if test="orderEndDate!=null and orderEndDate!=''">
		and	orderInfo.order_end_date &lt;=  STR_TO_DATE(#{orderEndDate},"%Y-%m-%d") 
	</if>
	<if test="orderStatus!=null and orderStatus!=''">
		and	orderInfo.order_status = #{orderStatus} 
	</if>
	<if test="createBy != null and createBy != ''">
		and orderInfo.create_by = #{createBy}
	</if>
	order by media.organ_name,media.comm_name asc 
	</select>
	<insert id="insertCycleLog">
	INSERT INTO t_media_cycle_log 
		(
		 cycle_log_id,
		 comm_id,
		 cycle_status,
		 order_bill_id,
		 start_date,
		 end_date,
		 organ_code,
		 media_id,
		 sort,
		 create_time,
		 create_by,
		 create_by_name
		 
		)
	VALUES
		(
		#{cycleLogId},
		#{commId},
		#{cycleStatus},
		#{orderBillId},
		#{startDate},
		#{endDate},
		#{organCode},
		#{mediaId},
		#{sort},
		#{createTime},
		#{createBy},
		#{createByName}
		
		)
	
	</insert>
	<select id="getOverDateCycle" resultType="String">
		SELECT
			order_id orderId
		FROM
			t_media_order
		WHERE
			is_delete = 0
		AND order_status = '0'
		AND TIMESTAMPDIFF(
			DAY,
			create_time,
			date_format(now(), '%Y-%m-%d')
		) >= 7
	</select>
	<select id="getMediaStatus" resultType="Map">
	<!-- SELECT
		cycle_status cycleStatus
	FROM
		t_media_cycle
	WHERE
		is_delete = 0
	AND cycle_time in 
	<foreach collection="dateList" item="item" index="index" open="(" close=")" separator=",">
            #{item}
    </foreach>
	AND media_id = #{mediaId}  -->
	SELECT
		cycle_status cycleStatus
	FROM
		t_media_cycle
	WHERE
		is_delete = 0
	AND cycle_time = #{cycleTime}
	AND media_id = #{mediaId}
	</select>
	
	<select id="findCycleTime" resultType="string">
		select cycle_id from t_media_cycle where is_delete = 0
		and cycle_time >= date_format(now(),'%Y-%m-%d')
		and media_id in 
		<foreach collection="mediaIds" item="item" index="index" open="(" close=")" separator=",">
            #{item}
        </foreach>
	</select>
	
	<select id="findCycleTimeOne" resultType="string">
		select media_id from t_media_cycle where is_delete = 0
		and cycle_time >= date_format(now(),'%Y-%m-%d')
		and media_id = #{mediaId}
	</select>
	<select id="checkInSchdule" resultType="int">
	SELECT
		count(cycle_id)
	FROM
		t_media_cycle
	WHERE
		is_delete = 0
	AND media_id = #{mediaId}
	AND cycle_time = #{cycleTime}
	</select>
	<select id="checkMediaCycle" resultType="int">
	SELECT
		count(1)
	FROM
		t_media_order_bill
	WHERE
		is_delete=0
		<!-- order_id != #{orderIdStr} -->
	AND media_id = #{mediaId}
	AND #{cycleTime} &gt;= prepare_start_date
	AND #{cycleTime} &lt;= prepare_end_date
	OR (
		#{cycleTime}&lt;= lock_start_date
		AND #{cycleTime} &gt;= lock_end_date
	)
	OR (
		#{cycleTime} &lt;= use_start_date
		AND #{cycleTime} &gt;= use_end_date
	)
	</select>
	<update id="updateOrderStatus">
	 UPDATE t_media_order
	 SET mod_by = #{modBy},
	 mod_by_name = #{modByName},
	 mod_time = #{modTime},
	 order_status = #{orderStatus}
	 <if test="remark !=null and remark !=''">
	 ,remark = #{remark}
	 </if>
	WHERE
	 order_id = #{orderId}
	</update>
	<update id="updateOrderBillStatus">
	UPDATE t_media_order_bill
	SET cycle_status = #{cycleStatus},
	 mod_by = #{modBy},
	 mod_by_name = #{modByName},
	 mod_time = #{modTime}
	WHERE
		order_id = #{orderId}
	</update>
	<update id="delCycleMedia">
	<!-- UPDATE t_media_cycle a
	SET a.is_delete = 1
	WHERE
		a.cycle_id IN (
			SELECT
				c.cycle_id
			FROM
				(
					SELECT
						cycle.cycle_id
					FROM
						t_media_cycle cycle
					LEFT JOIN t_media_order_bill bill ON bill.media_id = cycle.media_id
					WHERE
						bill.order_id = #{orderId}
					AND cycle.cycle_time &gt;= bill.prepare_start_date
					AND cycle.cycle_time &lt;= bill.prepare_end_date
				) c
		) -->
		
		delete from t_media_cycle 
		WHERE	cycle_id IN (
				SELECT	cycle_id FROM
					(
						SELECT cycle.cycle_id FROM	t_media_cycle cycle
						LEFT JOIN t_media_order_bill bill ON bill.media_id = cycle.media_id
						WHERE 	bill.order_id = #{orderId}
						AND cycle.cycle_time &gt;= bill.prepare_start_date
						AND cycle.cycle_time &lt;= bill.prepare_end_date
					) c
			)
	</update>
	<select id="getOrderBillDetail" resultType="cn.com.countrygarden.bi.main.model.mediaResource.MediaCycleLog">
	SELECT
		bill.media_id AS mediaId,
		bill.order_bill_id AS orderBillId,
		resource.comm_id AS commId,
		bill.prepare_start_date AS startDate,
		bill.prepare_end_date AS endDate,
		resource.organ_code AS organCode
	FROM
		t_media_order_bill bill
	INNER JOIN t_media_resource resource ON resource.media_id = bill.media_id
	WHERE
		 bill.order_id = #{orderId}
	</select>
	<select id="getDicItem" resultType="Map">
	SELECT
		item.dict_item_name as value,
	    item.dict_item_num_value as code
	FROM
		t_sys_dict_item item
	INNER JOIN t_sys_dict dict ON dict.dict_id = item.dict_id
	where dict.dict_code=#{dictCode}
	</select>
	
	<select id="findOrderLockResource" resultType="String">
		select b.order_bill_id from t_media_order o 
		inner join t_media_order_bill b on b.order_id = o.order_id 
		and b.is_delete = 0 and b.cycle_status = 1 -- 预定
		where o.is_delete = 0 and o.order_status = 1 -- 锁定
		and datediff(DATE_FORMAT(NOW(),'%Y-%m-%d'),DATE_FORMAT(o.create_time,'%Y-%m-%d')) >= 7 
	</select>
	
	<update id="delOrderBillList">
		update t_media_order_bill set is_delete = 1 where order_bill_id in 
		<foreach collection="ids" index="index" item="ids" open="(" separator="," close=")">
		 	 #{ids}
		</foreach>
	</update>
	
	<delete id="delCycle">
		delete from t_media_cycle where media_id = (select media_id from t_media_order_bill where order_bill_id = #{billId})
		and cycle_time between (select prepare_start_date from t_media_order_bill where order_bill_id = #{billId})
		and (select prepare_end_date from t_media_order_bill where order_bill_id = #{billId})		
	</delete>
	
	<select id="queryTimeQuantum" resultType="int">
		select count(cycle_id) from t_media_cycle 
		where is_delete = 0 and media_id = #{mediaId}
		and cycle_time between DATE_FORMAT(#{beginDate},'%Y-%m-%d') and DATE_FORMAT(#{endDate},'%Y-%m-%d');
	</select>
	
	<select id="findOrderCode" resultType="String">
		select r.order_code from t_media_order_bill b 
		inner join 
		(select min(cycle_time) as mintime,max(cycle_time) as maxtime,media_id from t_media_cycle 
		  where is_delete = 0 and media_id = #{mediaId} 
		  and cycle_time between #{beginDate} and #{endDate} 
		) t on t.media_id = b.media_id
		inner join t_media_order r on r.order_id = b.order_id
		where b.is_delete = 0 
		and prepare_start_date between t.mintime and t.maxtime
		or prepare_end_date between t.mintime and t.maxtime
	</select>

</mapper>