<?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.MediaOrderBillMapper">
	<resultMap id="BaseOrderMap" type="cn.com.countrygarden.bi.main.dto.mediaResource.MediaOrderDto">
		<id property="orderId" column="order_id"/>
		<result property="orderCode" column="order_code"/>
		<result property="orderStartDate" column="order_start_date"/>
		<result property="orderEndDate" column="order_end_date"/>
		<result property="orderAmount" column="order_amount"/>
		<result property="orderStatus" column="order_status"/>
		<result property="customerId" column="customer_id"/>
		<result property="remark" column="remark"/>
		<result property="sort" column="sort"/>
		<result property="version" column="version"/>
		<result property="isDelete" column="is_delete"/>
		<result property="createTime" column="create_time"/>
		<result property="createBy" column="create_by"/>
		<result property="createByName" column="create_by_name"/>
		<result property="modTime" column="mod_time"/>
		<result property="modBy" column="mod_by"/>
		<result property="modByName" column="mod_by_name"/>

		<result property="mediaType" column="media_type"/>
		<result property="mediaCode" column="media_code"/>
		<result property="mediaForm" column="media_form"/>
		<result property="mediaWidth" column="media_width"/>
		<result property="mediaHeight" column="media_height"/>
		<result property="mediaUnit" column="media_unit"/>
		<result property="mediaStatus" column="media_status"/>
		<result property="siteOne" column="site_one"/>
		<result property="siteTwo" column="site_two"/>
		<result property="organCode" column="organ_code"/>
		<result property="organName" column="organ_name"/>
		<result property="commId" column="comm_id"/>
		<result property="commName" column="comm_name"/>
		<result property="commAddress" column="comm_address"/>

		<result property="customerType" column="customer_type"/>
		<result property="customerName" column="customer_name"/>
		<result property="contactPhone" column="contact_phone"/>
		<result property="contactName" column="contact_name"/>

	</resultMap>

	<resultMap id="BaseOrderBillMap" type="cn.com.countrygarden.bi.main.dto.mediaResource.MediaOrderBillDto">
		<id property="orderBillId" column="order_Bill_id"/>
		<result property="mediaId" column="media_id"/>
		<result property="orderBillStartDate" column="order_bill_start_date"/>
		<result property="orderBillEndDate" column="order_bill_end_date"/>
		<result property="orderId" column="order_id"/>
		<result property="orderCode" column="order_code"/>
		<result property="orderStartDate" column="order_start_date"/>
		<result property="orderEndDate" column="order_end_date"/>
		<result property="orderAmount" column="order_amount"/>
		<result property="orderBillAmount" column="order_bill_amount"/>
		<result property="orderStatus" column="order_status"/>
		<result property="customerId" column="customer_id"/>
		<result property="remark" column="remark"/>
		<result property="sort" column="sort"/>
		<result property="version" column="version"/>
		<result property="isDelete" column="is_delete"/>
		<result property="createTime" column="create_time"/>
		<result property="createBy" column="create_by"/>
		<result property="createByName" column="create_by_name"/>
		<result property="modTime" column="mod_time"/>
		<result property="modBy" column="mod_by"/>
		<result property="modByName" column="mod_by_name"/>

		<result property="mediaType" column="media_type"/>
		<result property="mediaCode" column="media_code"/>
		<result property="mediaForm" column="media_form"/>
		<result property="mediaWidth" column="media_width"/>
		<result property="mediaHeight" column="media_height"/>
		<result property="mediaUnit" column="media_unit"/>
		<result property="mediaStatus" column="media_status"/>
		<result property="mediaPriceUnit" column="media_price_unit"/>
		<result property="siteOne" column="site_one"/>
		<result property="siteTwo" column="site_two"/>
		<result property="organCode" column="organ_code"/>
		<result property="organName" column="organ_name"/>
		<result property="commId" column="comm_id"/>
		<result property="commName" column="comm_name"/>
		<result property="commAddress" column="comm_address"/>

		<result property="customerType" column="customer_type"/>
		<result property="customerName" column="customer_name"/>
		<result property="contactPhone" column="contact_phone"/>
		<result property="contactName" column="contact_name"/>
		<result property="cycleStatus" column="cycle_status"/>
		<result property="orderBillUnitAmount" column="order_bill_unit_amount"/>
	</resultMap>

	<select id="findList" resultMap="BaseOrderBillMap">
		SELECT
		o.order_amount,
		r.media_id,
		r.media_type,
		r.media_code,
		r.media_form,
		r.media_width,
		r.media_height,
		r.media_unit,
		r.media_status,
		r.media_price_unit,
		r.site_one,
		r.site_two,
		r.organ_code,
		r.organ_name,
		r.comm_id,
		r.comm_name,
		r.comm_address,
		ob.order_bill_unit_amount,
		ob.order_bill_amount,
		ob.order_bill_id,
		ob.order_id,
		ob.media_id,
		ob.prepare_start_date,
		ob.prepare_end_date,
		ob.lock_start_date,
		ob.lock_end_date,
		ob.use_start_date,
		ob.use_end_date,
		ob.order_bill_start_date,
		ob.order_bill_end_date,
		ob.final_date,
		ob.is_final,
		ob.cycle_status
		FROM t_media_order o
		JOIN t_media_order_bill ob on o.order_id = ob.order_id and ob.is_delete = 0
		JOIN t_media_resource r ON r.media_id = ob.media_id
		WHERE
		<include refid="BaseFindSql"/>
		<if test="pageSize != '-1'">
			limit #{pageOffset}, #{pageSize}
		</if>
	</select>

	<select id="findCount" resultType="long">
		SELECT
			COUNT(1)
		FROM t_media_order o
		JOIN t_media_order_bill ob on o.order_id = ob.order_id and ob.is_delete = 0
		JOIN t_media_resource r ON r.media_id = ob.media_id
		WHERE
		<include refid="BaseFindSql"/>
	</select>

	<sql id="BaseFindSql">
		1 = 1
		AND o.is_delete = 0
		AND o.order_id = #{orderId}
	</sql>

	<select id="findByOrderId" resultMap="BaseOrderBillMap">
		select * from t_media_order_bill ob where ob.order_id = #{orderId}
	</select>

    <update id="delByOrderId">
        update t_media_order_bill set is_delete = 1, mod_time = #{modTime}, mod_by = #{userCode}, mod_by_name = #{userName}  where order_id = #{orderId}
    </update>
</mapper>