<?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.superdesk.uniorder.dao.mapper.accounting.unified.customer.AccountingUnifiedCustomerOrderGoodsMapperEx">



	<resultMap id="ViewDzExcelResultMap" type="cn.superdesk.uniorder.dto.vo.accounting.unified.customer.ViewDzOrderExcel">

		<result column="id" property="id" jdbcType="BIGINT"/>
		<result column="dz_order_id" property="dzOrderId" jdbcType="BIGINT"/>
		<result column="goods_id" property="goodsId" jdbcType="BIGINT"/>
		<result column="goods_no" property="goodsNo" jdbcType="VARCHAR"/>
		<result column="price" property="price" jdbcType="DECIMAL"/>
		<result column="total" property="total" jdbcType="INTEGER"/>
		<result column="unit_price" property="unitPrice" jdbcType="DECIMAL"/>
		<result column="optionname" property="optionname" jdbcType="VARCHAR"/>
		<result column="unit" property="unit" jdbcType="VARCHAR"/>
		<result column="goods_name" property="goodsName" jdbcType="VARCHAR"/>
		<result column="goods_alias" jdbcType="VARCHAR" property="goodsAlias" />
		<result column="dispatch_price" property="dispatchPrice" jdbcType="DECIMAL"/>
		<result column="total_price" property="totalPrice" jdbcType="DECIMAL"/>
		<result column="tax_code" property="taxCode" jdbcType="VARCHAR"/>
		<result column="fiscal_code" property="fiscalCode" jdbcType="VARCHAR"/>
		<result column="finish_time" property="finishTime" jdbcType="TIMESTAMP"/>
		<result column="create_time" property="createTime" jdbcType="TIMESTAMP"/>
		<result column="merch_name" property="merchName" jdbcType="VARCHAR"/>
		<result column="order_sn" property="orderSn" jdbcType="VARCHAR"/>
		<result column="company_name" property="companyName" jdbcType="VARCHAR"/>
		<result column="invoice_taxpayers_i_d_code" property="invoiceTaxpayersIDCode" jdbcType="VARCHAR"/>
		<result column="address_mobile" property="addressMobile" jdbcType="VARCHAR"/>

		<result column="remark" property="remark" jdbcType="VARCHAR"/>

		<result column="status" property="status" jdbcType="TINYINT"/>

		<result column="realname_id" property="realnameId" jdbcType="INTEGER"/>
		<result column="realname" property="realname" jdbcType="VARCHAR"/>

		<result column="order_price" property="orderPrice" jdbcType="DECIMAL"/>

		<result column="enterprise_id" jdbcType="BIGINT" property="enterpriseId"/>
		<result column="enterprise_name" jdbcType="VARCHAR" property="enterpriseName"/>

		<result column="accounting_order_sn" property="accountingOrderSn" jdbcType="VARCHAR"/>
		<result column="accounting_status" property="accountingStatus" jdbcType="TINYINT"/>

		<result column="invoice_type" property="invoiceType" jdbcType="TINYINT"/>
		<result column="invoice_status" property="invoiceStatus" jdbcType="TINYINT"/>
		<result column="invoice_account" property="invoiceAccount" jdbcType="VARCHAR"/>
	</resultMap>

	<!-- 订单维度 -->
	<select id="getDzOrderExcel" resultMap="ViewDzExcelResultMap">
		SELECT
			uog.id AS id,
			aucuo.id AS dz_order_id,
			uog.id AS goods_id,

			'' AS goods_no,
			uog.price AS price,
			uog.price AS unit_price,
			uog.quantity AS total,
			uog.quantity * uog.price AS total_price,
			uog.unit AS unit,
			uog.goods_name AS goods_name,
			uog.goods_alias,<!-- 未添加订单商品属性 -->
			'' AS optionname,
			'' AS fiscal_code,


			aucuo.dispatch_price AS dispatch_price,


			aucuo.create_time AS create_time,
			aucuo.finish_time AS finish_time,

			aucuo.merch_name AS merch_name,
			aucuo.order_sn AS order_sn,

			aucuo.address_mobile AS address_mobile,
			aucuo.invoice_account AS invoice_account,
			aucuo.remark AS remark,

			aucuo.tax_code AS tax_code,

			<!-- 支付方式 -->
			aucuo.pay_type AS pay_type,

			<!-- 订单状态 -->
			aucuo.status AS status,
			aucuo.realname_id AS realname_id,

			aucuo.price AS order_price,

			uo.purchase_virt_name AS enterprise_name,
			<!-- 错错错 -->
			uo.purchase_virt_name AS company_name,

			aucu.enterprise_id AS enterprise_id,
			aucu.invoice_taxpayers_i_d_code AS invoice_taxpayers_i_d_code,

			aucu.accounting_order_sn AS accounting_order_sn,
			aucu.accounting_status AS accounting_status,

			aucu.invoice_type AS invoice_type,
			aucu.invoice_status AS invoice_status,

			uo.purchase_user_name AS realname
		FROM
			unified_order_goods uog
			LEFT JOIN accounting_unified_customer_order aucuo ON uog.order_sn = aucuo.order_sn
			LEFT JOIN accounting_unified_customer aucu ON aucuo.dz_id = aucu.id
			LEFT JOIN sys_enterprise se ON se.enterprise_id = aucu.enterprise_id
			LEFT JOIN unified_order uo ON uog.order_sn = uo.order_sn
		<where>


		<!-- 订单类型 -->
		<if test="serviceNo != null and serviceNo != '' ">
			and aucu.service_no =#{serviceNo,jdbcType=VARCHAR}
		</if>

		<if test="purchaseVirtId != null and purchaseVirtId != '' ">
			and aucu.enterprise_id =#{purchaseVirtId,jdbcType=BIGINT}
		</if>
		<if test="purchaseVirtName != null and purchaseVirtName != '' ">
			and se.enterprise_name like CONCAT('%',#{purchaseVirtName},'%')
		</if>


		<if test="invoiceCompanyName != null and invoiceCompanyName != '' ">
			and aucu.invoice_company_name like CONCAT('%',#{invoiceCompanyName,jdbcType=VARCHAR},'%')
		</if>
		<!-- dzInvstatus -->
		<if test="invoiceStatus != null ">
			and aucu.invoice_status = #{invoiceStatus,jdbcType=TINYINT}
		</if>


		<!-- 下单人 -->
		<if test="purchaseUserId != null and purchaseUserId != '' ">
			and aucu.purchase_user_id =#{purchaseUserId,jdbcType=INTEGER}
		</if>
		<if test="purchaseUserName != null and purchaseUserName != '' ">
			and aucu.purchase_user_name like CONCAT('%',#{purchaseUserName,jdbcType=VARCHAR},'%')
		</if>
		<if test="purchasePhone != null and purchasePhone != '' ">
			and aucu.purchase_phone like CONCAT('%',#{purchasePhone,jdbcType=VARCHAR},'%')
		</if>


		<!-- 对账单SN -->
		<if test="accountingOrderSn != null and accountingOrderSn != '' ">
			and aucu.accounting_order_sn like CONCAT('%',#{accountingOrderSn,jdbcType=VARCHAR},'%')
		</if>

		<!-- dzStatus
		// 对账单状态，0-未确认，1-已确认，-1-已取消,2-已拒绝  3待提交
		// 如果不传 默认为 accounting_status not in (-1,2)
		-->
		<if test="accountingStatus != null ">
			and aucu.accounting_status = #{accountingStatus,jdbcType=TINYINT}
		</if>


		<if test="isSendEmail != null ">
			and aucu.is_send_email = #{isSendEmail,jdbcType=TINYINT}
		</if>




		<!-- startTime -->
		<if test="startTime != null and startTime != ''">
			and DATE_FORMAT(aucuo.create_time,'%Y-%m-%d') &gt;= #{startTime,jdbcType=VARCHAR}
		</if>
		<if test="endTime != null and endTime != ''">
			and DATE_FORMAT(aucuo.create_time,'%Y-%m-%d') &lt;= #{endTime,jdbcType=VARCHAR}
		</if>
		<if test="outVirtIds != null and outVirtIds != ''"><!-- 已修正 数据权限 by linjinyu at 2020090818 -->
			and order_sn in (
				SELECT
					order_sn
				from
					unified_order
				where
					<!-- project_id -->
					purchase_virt_id in
					<foreach collection="outVirtIds" index="index" item="pid" open="(" close=")" separator=",">
						#{pid}
					</foreach>
					<if test="outServices != null and outServices != ''">
						and service_no in
						(
						SELECT service_no from unified_service_group where group_code in
						<foreach collection="outServices" index="index" item="service_no" open="(" close=")" separator=",">
							#{service_no}
						</foreach>
						)
					</if>
			)
		</if>
		</where>
		order by aucuo.create_time desc
	</select>


	<resultMap id="dzOrderGoodResultMap" type="cn.superdesk.uniorder.dto.vo.invoice.DzInvoiceOrderVo">
		<result column="dz_id" property="dzId" jdbcType="BIGINT"/>
		<result column="dz_order_id" property="dzOrderId" jdbcType="BIGINT"/>
	</resultMap>

	<!-- 大概的意思就是

	通过 unified_order_goods.id(但不是accounting_unified_customer_order_goods )
	去查回 订单accounting_unified_customer_order (但不是 unified_order ) X 对账单 (accounting_unified_customer)

	这个为什么要这样做 反查 select id
	invoiceApplySelectFocusUnified
	invoiceApplySelectFocusScore
	-->
	<select id="selectDzByGoodId" resultMap="dzOrderGoodResultMap" parameterType="java.lang.Long">
		SELECT
			aucu.id as dz_id,
			aucuo.id as dz_order_id
		FROM
			accounting_unified_customer aucu
			LEFT JOIN accounting_unified_customer_order aucuo ON aucu.id = aucuo.dz_id
			LEFT JOIN unified_order_goods uog ON aucuo.order_sn = uog.order_sn
		WHERE
			uog.id = #{id,jdbcType=BIGINT}
  </select>

</mapper>