<?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.MediaOrderRunBillMapper">
	<resultMap id="BaseOrderRunBillMap" type="cn.com.countrygarden.bi.main.dto.mediaResource.MediaOrderRunBillDto">
		<id property="orderId" column="order_id"/>
		<result property="orderRunCode" column="order_run_code"/>
		<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="orderBillId" column="order_bill_id"/>
		<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"/>
		<result property="orderRunBillId" column="order_run_bill_id"/>
		<result property="runBillStartDate" column="run_bill_start_date"/>
		<result property="runBillEndDate" column="run_bill_end_date"/>
		<result property="mediaId" column="media_id"/>
		<result property="unitAmount" column="unit_amount"/>
		<result property="orderSingleAmount" column="order_single_amount"/>
		<result property="orderRunId" column="order_run_id"/>
	</resultMap>

	<select id="findList" resultMap="BaseOrderRunBillMap">
		select
			o.order_code,
			o.order_status,
			r.order_id,
			r.order_run_code,
			c.customer_id,
			c.customer_type,
			c.customer_name,
			c.contact_phone,
			c.contact_name,
			rb.order_run_bill_id,
			rb.run_bill_start_date,
			rb.run_bill_end_date,
			rb.media_id,
			rb.unit_amount,
			rb.order_single_amount,
			rb.order_run_id,
			rb.order_bill_id,
			rb.remark,
			rb.sort,
			rb.version,
			rb.is_delete,
			rb.create_time,
			rb.create_by,
			rb.create_by_name,
			rb.mod_time,
			rb.mod_by,
			rb.mod_by_name
		FROM t_media_order o
		JOIN t_media_order_run r ON r.order_id = o.order_id
		JOIN t_media_order_run_bill rb ON r.order_run_id = rb.order_run_id AND rb.is_delete = 0
		JOIN t_media_customer c ON c.customer_id = o.customer_id
		WHERE
		<include refid="BaseFindSql"/>
		ORDER BY r.sort DESC
		<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_run r ON r.order_id = o.order_id
		JOIN t_media_order_run_bill rb ON r.order_run_id = rb.order_run_id AND rb.is_delete = 0
		JOIN t_media_customer c ON c.customer_id = o.customer_id
		WHERE
		<include refid="BaseFindSql"/>
	</select>

	<select id="findBillCount" resultType="long">
		select
			count(1)
		FROM t_media_order_run r
		JOIN t_media_order_run_bill rb ON r.order_run_id = rb.order_run_id AND rb.is_delete = 0
		WHERE
		<include refid="BaseFindSql"/>
	</select>

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

	<select id="findByRunId" resultMap="BaseOrderRunBillMap">
		select * from t_media_order_run_bill r where r.order_run_id = #{orderRunId}
	</select>
</mapper>