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

	<!-- 已修正 20200702 -->
	<resultMap id="ViewResultMap" type="cn.superdesk.uniorder.dto.vo.accounting.unified.customer.DzMainAutoPage">
		<id column="id" property="id" jdbcType="BIGINT"/>
		<result column="accounting_order_sn" jdbcType="VARCHAR" property="accountingOrderSn" />

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

		<result column="invoice_type" jdbcType="TINYINT" property="invoiceType" />
		<result column="invoice_status" jdbcType="TINYINT" property="invoiceStatus" />
		<result column="invoice_company_name" jdbcType="VARCHAR" property="invoiceCompanyName" />
		<result column="invoice_taxpayers_i_d_code" jdbcType="VARCHAR" property="invoiceTaxpayersIDCode" />

		<result column="enterprise_id" property="enterpriseId" jdbcType="BIGINT"/>
		<result column="purchase_user_id" jdbcType="INTEGER" property="purchaseUserId" />
		<result column="purchase_address" jdbcType="VARCHAR" property="purchaseAddress" />

		<result column="is_send_email" jdbcType="TINYINT" property="isSendEmail" />
		<result column="send_email_time" jdbcType="TIMESTAMP" property="sendEmailTime" />

		<result column="date_type" jdbcType="TINYINT" property="dateType" />
		<result column="start_time" jdbcType="TIMESTAMP" property="startTime" />
		<result column="end_time" jdbcType="TIMESTAMP" property="endTime" />

		<result column="create_time" jdbcType="TIMESTAMP" property="createTime" />
		<result column="create_user_id" jdbcType="BIGINT" property="createUserId" />
		<result column="update_time" jdbcType="TIMESTAMP" property="updateTime" />
		<result column="update_user_id" jdbcType="BIGINT" property="updateUserId" />

	</resultMap>

	<!-- 已修正 20200702 批量对账记录 list-->
	<select id="findAllView" resultMap="ViewResultMap">
		SELECT
		<include refid="cn.superdesk.uniorder.dao.mapper.accounting.unified.customer.AccountingUnifiedCustomerMapper.Base_Column_List" />
		FROM
			accounting_unified_customer
		<where>
		<if test="dateType != null ">
			and date_type = #{dateType,jdbcType=TINYINT}
		</if>
		<if test="enterpriseName != null and enterpriseName != '' ">
			and enterprise_id in (
			select enterprise_id from sys_enterprise
			where
				enterprise_name like CONCAT('%',#{enterpriseName,jdbcType=VARCHAR},'%')
			)
		</if>
		<if test="startTime != null and startTime != ''">
			and DATE_FORMAT(create_time,'%Y-%m-%d %H:%i:%s') &gt;= #{startTime,jdbcType=VARCHAR}
		</if>
		<if test="endTime != null and endTime != ''">
			and DATE_FORMAT(create_time,'%Y-%m-%d %H:%i:%s') &lt;= #{endTime,jdbcType=VARCHAR}
		</if>
		</where>
		order by create_time DESC
	</select>

	<!-- 待修正 20200702 -->
	<resultMap id="CustomerStatementOfAccountDetailItemResultMap" type="cn.superdesk.uniorder.dto.vo.accounting.unified.customer.statement.CustomerStatementOfAccountDetailItemVo">
		<!-- 订单维度 -->
<!--		auco.dz_id AS accounting_order_id,-->
<!--		'' as accounting_order_sn,-->
<!--		uo.id AS unified_order_id,-->
<!--		uo.order_sn AS order_sn,-->
<!--		uog.id as unified_order_goods_id,-->
<!--		uog.goods_id,-->
<!--		uog.out_snapshot_order_goods_id,-->
		<!-- 商品维度 -->
<!--		aucuxg.accounting_order_id,-->
<!--		aucuxg.accounting_order_sn,-->
<!--		aucuxg.unified_order_id,-->
<!--		aucuxg.order_sn,-->
<!--		aucuxg.unified_order_goods_id,-->
<!--		aucuxg.goods_id,-->
<!--		aucuxg.out_snapshot_order_goods_id,-->

		<result column="accounting_order_id" property="accountingOrderId" jdbcType="BIGINT"/>
		<result column="accounting_order_sn" property="accountingOrderSn" jdbcType="VARCHAR"/>
		<result column="unified_order_id" property="unifiedOrderId" jdbcType="BIGINT"/>
		<result column="order_sn" property="orderSn" jdbcType="VARCHAR"/>
		<result column="unified_order_goods_id" property="unifiedOrderGoodsId" jdbcType="BIGINT"/>


		<!--
		<result column="order_id" property="orderId" jdbcType="BIGINT"/>
		<result column="id" property="id" jdbcType="BIGINT"/>
		-->


		<result column="order_sn_out_trade" property="orderSnOutTrade" jdbcType="VARCHAR"/>

		<result column="service_name" property="serviceName" jdbcType="VARCHAR"/>
		<result column="service_no" property="serviceNo" jdbcType="VARCHAR"/>

		<result column="project_id" property="projectId" jdbcType="BIGINT"/>
		<result column="project_name" property="projectName" jdbcType="VARCHAR"/>

		<result column="purchase_virt_id" property="purchaseVirtId" jdbcType="BIGINT"/>
		<result column="purchase_virt_name" property="purchaseVirtName" jdbcType="VARCHAR"/>

		<result column="purchase_user_id" property="purchaseUserId" jdbcType="BIGINT"/>
		<result column="purchase_user_name" property="purchaseUserName" jdbcType="VARCHAR"/>
		<result column="purchase_phone" property="purchasePhone" jdbcType="VARCHAR"/>

		<result column="sale_id" property="saleId" jdbcType="BIGINT"/>
		<result column="sale_name" property="saleName" jdbcType="VARCHAR"/>

		<result column="amount" property="amount" jdbcType="DECIMAL"/>
		<result column="cost_amount" property="costAmount" jdbcType="DECIMAL"/>
		<result column="transport_amount" property="transportAmount" jdbcType="DECIMAL"/>
		<result column="complet_state" property="completState" jdbcType="TINYINT"/>

		<!-- 客户对账:1-已对账;2-未对账 -->
		<result column="accounting_state_customer" property="accountingStateCustomer" jdbcType="TINYINT"/>
		<!-- 供应商对账:1-已对账;2-未对账 -->
		<result column="accounting_state_supplier" property="accountingStateSupplier" jdbcType="TINYINT"/>

		<result column="pay_type" property="payType" jdbcType="TINYINT"/>
		<result column="pay_type_name" jdbcType="VARCHAR" property="payTypeName" />
		<result column="pay_state" property="payState" jdbcType="TINYINT"/>
		<result column="pay_state_name" jdbcType="VARCHAR" property="payStateName" />

		<result column="order_state" property="orderState" jdbcType="TINYINT"/>
		<result column="order_state_name" property="orderStateName" jdbcType="VARCHAR"/>

		<result column="settlement_type" property="settlementType" jdbcType="TINYINT"/>
		<result column="appraise_state" property="appraiseState" jdbcType="TINYINT"/>
		<result column="source_channel" property="sourceChannel" jdbcType="TINYINT"/>
		<result column="other_params" property="otherParams" jdbcType="VARCHAR"/>
		<result column="user_type" property="userType" jdbcType="VARCHAR"/>
		<result column="user_name" property="userName" jdbcType="VARCHAR"/>


		<result column="create_user_id" property="createUserId" jdbcType="BIGINT"/>
		<result column="rec_user_name" property="recUserName" jdbcType="VARCHAR"/>
		<result column="rec_user_id" property="recUserId" jdbcType="BIGINT"/>
		<result column="rec_user_phone" property="recUserPhone" jdbcType="VARCHAR"/>
		<result column="rec_address" property="recAddress" jdbcType="VARCHAR"/>



		<result column="create_time" property="createTime" jdbcType="TIMESTAMP"/>
		<result column="update_time" property="updateTime" jdbcType="TIMESTAMP"/>
		<result column="pay_time"     property="payTime"    jdbcType="TIMESTAMP"/>
		<result column="send_time"    property="sendTime"   jdbcType="TIMESTAMP"/>
		<result column="finish_time"  property="finishTime" jdbcType="TIMESTAMP"/>

		<result column="express_sn" jdbcType="VARCHAR" property="expressSn" />
		<result column="express" jdbcType="VARCHAR" property="express" />
		<result column="express_company" jdbcType="VARCHAR" property="expressCompany" />

		<result column="invoice_status" property="invoiceStatus" jdbcType="TINYINT"/>
		<result column="invoice_number" property="invoiceNumber" jdbcType="VARCHAR"/>
		<result column="invoice_apply_sn" property="invSn" jdbcType="VARCHAR"/>

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


		<!--		<result column="goods_id" jdbcType="BIGINT" property="goodsId" />-->
		<!--		<result column="out_snapshot_order_goods_id" jdbcType="BIGINT" property="outSnapshotOrderGoodsId" />-->
		<result column="goods_id" property="goodsId" jdbcType="BIGINT"/>
		<result column="out_snapshot_order_goods_id" jdbcType="BIGINT" property="outSnapshotOrderGoodsId" />
		<result column="goods_name" property="goodsName" jdbcType="VARCHAR"/>
		<result column="goods_alias" jdbcType="VARCHAR" property="goodsAlias" />

		<result column="quantity" property="quantity" jdbcType="INTEGER"/>
		<result column="price" property="price" jdbcType="DECIMAL"/>
		<result column="cost_price" property="costPrice" jdbcType="DECIMAL"/>
		<result column="goods_amount" property="goodsAmount" jdbcType="DECIMAL"/>
		<result column="goods_cost_amount" property="goodsCostAmount" jdbcType="DECIMAL"/>

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

		<result column="cate_id_lv1" property="cateIdLv1" jdbcType="BIGINT"/>
		<result column="cate_name_lv1" property="cateNameLv1" jdbcType="VARCHAR"/>
		<result column="cate_id_lv2" property="cateIdLv2" jdbcType="BIGINT"/>
		<result column="cate_name_lv2" property="cateNameLv2" jdbcType="VARCHAR"/>
		<result column="cate_id_lv3" property="cateIdLv3" jdbcType="BIGINT"/>
		<result column="cate_name_lv3" property="cateNameLv3" jdbcType="VARCHAR"/>
		<result column="self_option" jdbcType="VARCHAR" property="selfOption"/>
		<result column="unit" property="unit" jdbcType="VARCHAR"/>

		<result column="fiscal_code" jdbcType="VARCHAR" property="fiscalCode" />
		<result column="tax_code" jdbcType="VARCHAR" property="taxCode" />
		<result column="tax_rate" jdbcType="DECIMAL" property="taxRate" />
		<result column="merch_tax_id" jdbcType="BIGINT" property="merchTaxId" />
		<result column="merch_tax_code" jdbcType="VARCHAR" property="merchTaxCode" />
		<result column="merch_tax_rate" jdbcType="DECIMAL" property="merchTaxRate" />
		<result column="operation_tax_id" jdbcType="BIGINT" property="operationTaxId" />
		<result column="operation_tax_code" jdbcType="VARCHAR" property="operationTaxCode" />
		<result column="operation_tax_rate" jdbcType="DECIMAL" property="operationTaxRate" />

		<result column="goods_params" property="goodsParams" jdbcType="VARCHAR"/>
	</resultMap>

	<!-- 待修正 20200702  对账单 详情(订单维度) -->
	<select id="getCustomerStatementOfAccountDetailFillItemDimOrder2Goods" resultMap="CustomerStatementOfAccountDetailItemResultMap">
		SELECT




			auco.dz_id AS accounting_order_id,
			'' as accounting_order_sn,
			uo.id AS unified_order_id,
			uo.order_sn AS order_sn,
			uog.id as unified_order_goods_id,


			<!--
			aucuxg.accounting_order_id,
			aucuxg.accounting_order_sn,
			aucuxg.unified_order_id,
			aucuxg.order_sn,
			aucuxg.unified_order_goods_id,
			aucuxg.goods_id,
			aucuxg.out_snapshot_order_goods_id,
			-->


			uo.order_sn_out_trade AS order_sn_out_trade,

			uo.service_name AS service_name,
			uo.service_no AS service_no,

			uo.project_id AS project_id,
			uo.project_name AS project_name,

			uo.purchase_virt_id AS purchase_virt_id,
			uo.purchase_virt_name AS purchase_virt_name,

			uo.purchase_user_id AS purchase_user_id,
			uo.purchase_user_name AS purchase_user_name,
			uo.purchase_phone AS purchase_phone,

			uo.sale_id AS sale_id,
			uo.sale_name AS sale_name,

			uo.amount AS amount,
			uo.cost_amount AS cost_amount,
			uo.transport_amount AS transport_amount,
			uo.complet_state AS complet_state,
			uo.accounting_state_supplier AS accounting_state_supplier,
			uo.accounting_state_customer AS accounting_state_customer,

			uo.pay_type,
			uo.pay_type_name,
			uo.pay_state,
			uo.pay_state_name,

			uo.order_state,
			uo.order_state_name,

			uo.settlement_type AS settlement_type,
			uo.appraise_state AS appraise_state,
			uo.source_channel AS source_channel,
			uo.other_params AS other_params,
			uo.user_type AS user_type,
			uo.user_name AS user_name,


			uo.create_user_id AS create_user_id,
			uo.rec_user_name AS rec_user_name,
			uo.rec_user_id AS rec_user_id,
			uo.rec_user_phone AS rec_user_phone,
			uo.rec_address AS rec_address,

			uo.create_time,
			uo.update_time,
			uo.pay_time,
			uo.send_time,
			uo.finish_time,

			uo.invoice_status AS invoice_status,
			uo.invoice_number AS invoice_number,
			uo.invoice_apply_sn AS invoice_apply_sn,

			uo.remarks AS remarks,

			uog.goods_id,
			uog.out_snapshot_order_goods_id,
			uog.goods_name,
			uog.goods_alias,<!-- 已添加订单商品属性 -->

			uog.quantity,
			uog.price,
			uog.cost_price,
			uog.price * uog.quantity as goods_amount,
			uog.cost_price * uog.quantity as goods_cost_amount,

			uog.direction,

			uog.cate_id_lv1,
			uog.cate_name_lv1,
			uog.cate_id_lv2,
			uog.cate_name_lv2,
			uog.cate_id_lv3,
			uog.cate_name_lv3,
			uog.self_option,
			uog.unit,

			uog.fiscal_code,
			uog.tax_code,
			uog.tax_rate,
			uog.merch_tax_id,
			uog.merch_tax_code,
			uog.merch_tax_rate,
			uog.operation_tax_id,
			uog.operation_tax_code,
			uog.operation_tax_rate,

			uog.goods_params
		FROM
			accounting_unified_customer_order auco
			LEFT JOIN unified_order uo ON auco.order_sn = uo.order_sn
			LEFT JOIN unified_order_goods uog ON uo.order_sn = uog.order_sn
		<where>
			<if test="dzId != null and dzId !=''">
				and auco.dz_id = #{dzId}
			</if>
			<choose>
				<when test="orderBy != null and orderBy != '' ">
					order by ${orderBy}
				</when>
				<otherwise>
					<!--
					ORDER BY uo.create_time ASC
					-->
					order by uo.create_time desc, uo.order_sn desc, uog.id asc
				</otherwise>
			</choose>
		</where>

	</select>


	<select id="getCustomerStatementOfAccountDetailFillItemDimGoods2Goods" resultMap="CustomerStatementOfAccountDetailItemResultMap">
		SELECT
			<!--
			auco.dz_id AS dz_id,
			auco.order_id AS order_id,
			auco.id AS id,
			uo.order_sn AS order_sn,
			-->

			aucuxg.accounting_order_id,
			aucuxg.accounting_order_sn,
			aucuxg.unified_order_id,
			aucuxg.order_sn,
			aucuxg.unified_order_goods_id,

			uo.order_sn_out_trade AS order_sn_out_trade,

			uo.service_name AS service_name,
			uo.service_no AS service_no,

			uo.project_id AS project_id,
			uo.project_name AS project_name,

			uo.purchase_virt_id AS purchase_virt_id,
			uo.purchase_virt_name AS purchase_virt_name,

			uo.purchase_user_id AS purchase_user_id,
			uo.purchase_user_name AS purchase_user_name,
			uo.purchase_phone AS purchase_phone,

			uo.sale_id AS sale_id,
			uo.sale_name AS sale_name,

			uo.amount AS amount,
			uo.cost_amount AS cost_amount,
			uo.transport_amount AS transport_amount,
			uo.complet_state AS complet_state,

			uo.accounting_state_supplier AS accounting_state_supplier,
			uo.accounting_state_customer AS accounting_state_customer,

			uo.pay_type,
			uo.pay_type_name,
			uo.pay_state,
			uo.pay_state_name,

			uo.order_state AS order_state,
			uo.order_state_name AS order_state_name,

			uo.settlement_type AS settlement_type,
			uo.appraise_state AS appraise_state,
			uo.source_channel AS source_channel,
			uo.other_params AS other_params,
			uo.user_type AS user_type,
			uo.user_name AS user_name,

			uo.create_user_id AS create_user_id,

			uo.rec_user_name AS rec_user_name,
			uo.rec_user_id AS rec_user_id,
			uo.rec_user_phone AS rec_user_phone,
			uo.rec_address AS rec_address,

			uo.create_time,
			uo.update_time,
			uo.pay_time,
			uo.send_time,
			uo.finish_time,

			uo.express_sn,
			uo.express,
			uo.express_company,

			uo.invoice_status AS invoice_status,
			uo.invoice_number AS invoice_number,
			uo.invoice_apply_sn AS invoice_apply_sn,

			uo.remarks AS remarks,


			<!--
			uog.goods_id,
			uog.out_snapshot_order_goods_id,
			-->
			aucuxg.goods_id,
			aucuxg.out_snapshot_order_goods_id,
			uog.goods_name,
			uog.goods_alias,<!-- 已添加订单商品属性 -->

			uog.quantity,
			uog.price,
			uog.cost_price,
			uog.price * uog.quantity as goods_amount,
			uog.cost_price * uog.quantity as goods_cost_amount,

			uog.direction,

			uog.cate_id_lv1,
			uog.cate_name_lv1,
			uog.cate_id_lv2,
			uog.cate_name_lv2,
			uog.cate_id_lv3,
			uog.cate_name_lv3,
			uog.self_option,
			uog.unit,

			uog.fiscal_code,
			uog.tax_code,
			uog.tax_rate,
			uog.merch_tax_id,
			uog.merch_tax_code,
			uog.merch_tax_rate,
			uog.operation_tax_id,
			uog.operation_tax_code,
			uog.operation_tax_rate,

			uog.goods_params
		FROM
			accounting_unified_customer_x_goods aucuxg
			LEFT JOIN unified_order uo ON aucuxg.unified_order_id = uo.id
			LEFT JOIN unified_order_goods uog ON aucuxg.unified_order_goods_id = uog.id
		<where>
			<if test="accountingOrderId != null and accountingOrderId !=''">
				and aucuxg.accounting_order_id = #{accountingOrderId}
			</if>
			<choose>
				<when test="orderBy != null and orderBy != '' ">
					order by ${orderBy}
				</when>
				<otherwise>
					<!--
					ORDER BY uo.create_time ASC
					-->
					order by uo.create_time desc, uo.order_sn desc, uog.id asc
				</otherwise>
			</choose>
		</where>

	</select>

	<!-- 已修正 20200702 -->
	<resultMap id="ViewDzOrderResultMap" type="cn.superdesk.uniorder.dto.vo.invoice.customer.InvoiceApplyDetailDimOrderItemVo">
		<result column="id" property="id" jdbcType="BIGINT"/>
		<result column="dz_id" property="dzId" jdbcType="BIGINT"/>
		<result column="order_id" property="orderId" jdbcType="BIGINT"/>
		<result column="order_sn" property="orderSn" jdbcType="VARCHAR"/>
		<result column="parent_order_id" property="parentOrderId" jdbcType="INTEGER"/>
		<result column="parent_order_sn" property="parentOrderSn" jdbcType="VARCHAR"/>

		<result column="address_realname" property="addressRealname" jdbcType="VARCHAR"/>
		<result column="address_mobile" property="addressMobile" jdbcType="VARCHAR"/>
		<result column="address_address" property="addressAddress" jdbcType="VARCHAR"/>
		<result column="companyName" property="companyname" jdbcType="VARCHAR"/>
		<result column="taxpayersIDcode" property="taxpayersidcode" jdbcType="VARCHAR"/>

		<result column="merch_name" property="merchName" jdbcType="VARCHAR"/>
		<result column="order_type" property="orderType" jdbcType="VARCHAR"/>

		<result column="pay_type" property="payType" jdbcType="TINYINT"/>
		<result column="pay_type_name" property="payTypeName" jdbcType="VARCHAR"/>

		<result column="status" property="status" jdbcType="TINYINT"/>
		<result column="status_name" property="statusName" jdbcType="VARCHAR"/>

		<result column="create_time" property="createTime" jdbcType="TIMESTAMP"/>
		<result column="pay_time" property="payTime" jdbcType="TIMESTAMP"/>
		<result column="send_time" property="sendTime" jdbcType="TIMESTAMP"/>
		<result column="finish_time" property="finishTime" jdbcType="TIMESTAMP"/>

		<result column="search_field1" property="searchField1" jdbcType="VARCHAR"/>
		<result column="search_field2" property="searchField2" jdbcType="VARCHAR"/>
		<result column="search_field3" property="searchField3" jdbcType="VARCHAR"/>

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


		<result column="invoice_account" property="invoiceAccount" jdbcType="VARCHAR"/>
		<result column="invoice_bank" property="invoiceBank" jdbcType="VARCHAR"/>
		<result column="invoice_type" property="invoiceType" jdbcType="TINYINT"/>
		<result column="invoice_type_name" property="invoiceTypeName" jdbcType="VARCHAR"/>

		<result column="invoice_number" property="invoiceNumber" jdbcType="VARCHAR"/>
		<result column="invoice_apply_sn" property="invoiceApplySn" jdbcType="VARCHAR"/>

		<result column="examine_id" property="examineId" jdbcType="BIGINT"/>
		<result column="realname_id" property="realnameId" jdbcType="BIGINT"/>


		<result column="detail_url" property="detailUrl" jdbcType="VARCHAR"/>
		<result column="organization_id" property="organizationId" jdbcType="BIGINT"/>
		<result column="enterprise_id" property="enterpriseId" jdbcType="BIGINT"/>

		<result column="dispatchname" property="dispatchname" jdbcType="VARCHAR"/>
		<result column="sync_time" property="syncTime" jdbcType="TIMESTAMP"/>


		<result column="dz_order_good_id" property="dzOrderGoodId" jdbcType="BIGINT"/>
		<result column="dz_order_id" property="dzOrderId" jdbcType="BIGINT"/>
		<result column="good_id" property="goodId" jdbcType="BIGINT"/>
		<result column="goodno" property="goodno" 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="other_field1" property="otherField1" jdbcType="VARCHAR"/>
		<result column="other_field2" property="otherField2" jdbcType="VARCHAR"/>

		<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="realname" property="realname" jdbcType="VARCHAR"/>
		<result column="examine_name" property="examineName" jdbcType="VARCHAR"/>

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



	</resultMap>

	<!-- 已修正 20200702 accounting_unified_customer_order 已弃用 逻辑不清 数据无法用了 -->
	<select id="findAllDzOrderView" resultMap="ViewDzOrderResultMap">
		SELECT
		accounting_unified_customer_order.id AS id,
		accounting_unified_customer_order.dz_id AS dz_id,
		accounting_unified_customer_order.order_id AS order_id,
		accounting_unified_customer_order.order_sn AS order_sn,
		accounting_unified_customer_order.parent_order_id AS parent_order_id,
		accounting_unified_customer_order.parent_order_sn AS parent_order_sn,
		accounting_unified_customer_order.create_time AS create_time,
		accounting_unified_customer_order.address_realname AS address_realname,
		accounting_unified_customer_order.address_mobile AS address_mobile,
		accounting_unified_customer_order.address_address AS address_address,

		accounting_unified_customer_order.companyName AS companyName,
		accounting_unified_customer_order.taxpayersIDcode AS taxpayersIDcode,

		accounting_unified_customer_order.send_time AS send_time,
		accounting_unified_customer_order.finish_time AS finish_time,
		accounting_unified_customer_order.status AS status,
		CASE
		WHEN accounting_unified_customer_order.status = -1 THEN
		'已关闭'
		WHEN accounting_unified_customer_order.status = 0 THEN
		'待付款'
		WHEN accounting_unified_customer_order.status = 1 THEN
		'待发货'
		WHEN accounting_unified_customer_order.status = 2 THEN
		'待收货'
		WHEN accounting_unified_customer_order.status = 3 THEN
		'已完成'
		ELSE
		'已关闭'
		END AS status_name,
		accounting_unified_customer_order.merch_name AS merch_name,
		accounting_unified_customer_order.order_type AS order_type,
		accounting_unified_customer_order.pay_type AS pay_type,
		CASE
		WHEN accounting_unified_customer_order.pay_type = 0 THEN
		'未支付'
		WHEN accounting_unified_customer_order.pay_type = 1 THEN
		'余额支付'
		WHEN accounting_unified_customer_order.pay_type = 2 THEN
		'在线支付'
		WHEN accounting_unified_customer_order.pay_type = 3 THEN
		'企业月结'
		WHEN accounting_unified_customer_order.pay_type = 11 THEN
		'后台付款'
		WHEN accounting_unified_customer_order.pay_type = 21 THEN
		'微信支付'
		WHEN accounting_unified_customer_order.pay_type = 22 THEN
		'支付宝支付'
		WHEN accounting_unified_customer_order.pay_type = 23 THEN
		'银联支付'
		ELSE
		'未支付'
		END AS pay_type_name,
		accounting_unified_customer_order.search_field1 AS search_field1,
		accounting_unified_customer_order.search_field2 AS search_field2,
		accounting_unified_customer_order.search_field3 AS search_field3,
		accounting_unified_customer_order.remark AS remark,

		accounting_unified_customer_order.examine_id AS examine_id,
		accounting_unified_customer_order.realname_id AS realname_id,
		accounting_unified_customer_order.detail_url AS detail_url,
		accounting_unified_customer_order.organization_id AS organization_id,
		accounting_unified_customer_order.enterprise_id AS enterprise_id,


		accounting_unified_customer_order.invoice_type AS invoice_type,
		CASE
		WHEN accounting_unified_customer_order.invoice_type = 1 THEN
		'增值税普票'
		ELSE
		'增值税专票'
		END AS invoice_type_name,
		accounting_unified_customer_order.invoice_account AS invoice_account,
		accounting_unified_customer_order.invoice_bank AS invoice_bank,


		accounting_unified_customer_order.dispatchname AS dispatchname,
		accounting_unified_customer_order.sync_time AS sync_time,
		accounting_unified_customer_order.pay_time AS pay_time,

		accounting_unified_customer_order_goods.id AS dz_order_good_id,
		accounting_unified_customer_order_goods.dz_order_id AS dz_order_id,
		accounting_unified_customer_order_goods.good_id AS good_id,
		accounting_unified_customer_order_goods.goodno AS goodno,
		accounting_unified_customer_order_goods.price AS price,
		accounting_unified_customer_order_goods.total AS total,
		accounting_unified_customer_order_goods.unit_price AS unit_price,
		accounting_unified_customer_order_goods.optionname AS optionname,
		accounting_unified_customer_order_goods.unit AS unit,
		accounting_unified_customer_order_goods.other_field1 AS other_field1,
		accounting_unified_customer_order_goods.other_field2 AS other_field2,
		accounting_unified_customer_order_goods.dispatch_price AS dispatch_price,
		accounting_unified_customer_order_goods.total_price AS total_price,
		accounting_unified_customer_order_goods.tax_code AS tax_code,
		accounting_unified_customer_order_goods.fiscal_code AS fiscal_code,

		real_order_user.realname AS realname,
		examine_order_user.realname AS examine_name,
		(
		SELECT
		t2.invoice_number
		FROM
		invoice_apply_x_goods t1
		LEFT JOIN invoice_apply t2 ON t1.invoice_apply_id = t2.id
		WHERE
		t1.dz_good_id = accounting_unified_customer_order_goods.id
		AND t2.audit_status IN (1, 2) GROUP BY t1.dz_good_id
		) AS invoice_number,
		(
		SELECT
		t2.invoice_apply_sn
		FROM
		invoice_apply_x_goods t1
		LEFT JOIN invoice_apply t2 ON t1.invoice_apply_id = t2.id
		WHERE
		t1.dz_good_id = accounting_unified_customer_order_goods.id
		AND t2.audit_status IN (1, 2) GROUP BY t1.dz_good_id
		) AS invoice_apply_sn,
		accounting_unified_customer_order.price AS order_price
		FROM
			(
			accounting_unified_customer_order
			RIGHT JOIN accounting_unified_customer_order_goods ON accounting_unified_customer_order.id = accounting_unified_customer_order_goods.dz_order_id
			)
			LEFT JOIN sys_oder_user AS real_order_user ON accounting_unified_customer_order.realname_id = real_order_user.user_id
			LEFT JOIN sys_oder_user AS examine_order_user ON accounting_unified_customer_order.examine_id = examine_order_user.user_id
			LEFT JOIN sys_invoice ON accounting_unified_customer_order.id = sys_invoice.dz_order_id
		<where>
		<if test="dzId != null ">
			and accounting_unified_customer_order.dz_id = #{dzId,jdbcType=BIGINT}
		</if>
		<if test="dzIds !=null and dzIds != ''">
			and accounting_unified_customer_order.dz_id in
			<foreach collection="dzIds" index="index" item="mainid" open="(" close=")" separator=",">
				#{mainid}
			</foreach>
		</if>
		</where>
		order by id
	</select>

	<!-- 已修正 20200702 xml 与 vo NND 这不就是accounting_unified_customer吗?还这样写,唉,没空改,但这有点弱智 -->
	<resultMap id="ViewDzMainResultMap" type="cn.superdesk.uniorder.dto.vo.accounting.unified.customer.statement.CustomerStatementOfAccountDetailVo">

		<result column="id" property="id" jdbcType="BIGINT"/>

		<result column="dim_create_by" jdbcType="TINYINT" property="dimCreateBy" />
		<result column="logic_mode" jdbcType="TINYINT" property="logicMode" />

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

		<result column="service_no" jdbcType="VARCHAR" property="serviceNo" />
		<result column="quantity_order" jdbcType="INTEGER" property="quantityOrder" />
		<result column="accounting_amount" jdbcType="DECIMAL" property="accountingAmount" />
		<result column="accounting_status" jdbcType="TINYINT" property="accountingStatus" />

		<result column="invoice_type" jdbcType="TINYINT" property="invoiceType" />
		<result column="invoice_status" jdbcType="TINYINT" property="invoiceStatus" />
		<result column="invoice_company_name" jdbcType="VARCHAR" property="invoiceCompanyName" />
		<result column="invoice_taxpayers_i_d_code" jdbcType="VARCHAR" property="invoiceTaxpayersIDCode" />

		<result column="enterprise_id" jdbcType="BIGINT" property="enterpriseId" />
		<result column="purchase_user_id" jdbcType="BIGINT" property="purchaseUserId" />
		<result column="purchase_user_name" jdbcType="VARCHAR" property="purchaseUserName" />
		<result column="purchase_phone" jdbcType="VARCHAR" property="purchasePhone" />
		<result column="purchase_address" jdbcType="VARCHAR" property="purchaseAddress" />

		<result column="is_send_email" jdbcType="TINYINT" property="isSendEmail" />
		<result column="send_email_time" jdbcType="TIMESTAMP" property="sendEmailTime" />

		<result column="date_type" jdbcType="TINYINT" property="dateType" />
		<result column="start_time" jdbcType="TIMESTAMP" property="startTime" />
		<result column="end_time" jdbcType="TIMESTAMP" property="endTime" />

		<result column="back_account_status" jdbcType="TINYINT" property="backAccountStatus" />
		<result column="bank_info_id" jdbcType="BIGINT" property="bankInfoId" />

		<result column="create_time" jdbcType="TIMESTAMP" property="createTime" />
		<result column="create_user_id" jdbcType="BIGINT" property="createUserId" />
		<result column="update_time" jdbcType="TIMESTAMP" property="updateTime" />
		<result column="update_user_id" jdbcType="BIGINT" property="updateUserId" />

		<result column="pay_type" jdbcType="TINYINT" property="payType" />
		<result column="pay_status" jdbcType="TINYINT" property="payStatus" />

		<result column="wx_qrcode_imgurl" jdbcType="VARCHAR" property="wxQrcodeImgurl" />


	</resultMap>

	<!-- 客户结算 - 对账单 List 已修正 20200702 -->
	<select id="getAccountingUnifiedCustomerView" resultMap="ViewDzMainResultMap">
		SELECT
		<include refid="cn.superdesk.uniorder.dao.mapper.accounting.unified.customer.AccountingUnifiedCustomerMapper.Base_Column_List"/>
		FROM
			accounting_unified_customer
		<where>
			<if test="id != null ">
				and id = #{id,jdbcType=BIGINT}
			</if>

			<if test="orderSn != null and orderSn != ''">
				and id in (
					SELECT
						accounting_order_id
					from
						accounting_unified_customer_x_goods
					<where>
						order_sn = #{orderSn,jdbcType=VARCHAR}
					</where>

				)
			</if>

			<if test="dimCreateBy != null ">
				and dim_create_by = #{dimCreateBy,jdbcType=TINYINT}
			</if>

			<if test="purchaseVirtId != null and purchaseVirtId != '' ">
				and enterprise_id = #{purchaseVirtId}
			</if>
			<if test="purchaseVirtName != null and purchaseVirtName != '' ">
				and enterprise_id in (
				SELECT enterprise_id from sys_enterprise where enterprise_name like CONCAT('%',#{purchaseVirtName},'%')
				)
			</if>
			<if test="outVirtIds != null and outVirtIds != ''"><!-- 已修正 数据权限 by linjinyu at 2020090818 -->
				and enterprise_id in (
					SELECT
						enterprise_id
					from
						sys_enterprise
					where
						<!-- organization_id -->
						enterprise_id in
						<foreach collection="outVirtIds" index="index" item="pid" open="(" close=")" separator=",">
							#{pid}
						</foreach>
				)
			</if>

			<if test="serviceNo != null and serviceNo != ''">
				and service_no = #{serviceNo}
			</if>
			<if test="outServices != null and outServices != ''">
				and service_no in
				<foreach collection="outServices" index="index" item="service_no" open="(" close=")" separator=",">
					#{service_no}
				</foreach>
			</if>






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

			<if test="invoiceCompanyName != null and invoiceCompanyName != '' ">
				and invoice_company_name like CONCAT('%',#{invoiceCompanyName,jdbcType=VARCHAR},'%')
			</if>


			<if test="accountingOrderSn != null and accountingOrderSn != '' ">
				and accounting_order_sn like CONCAT('%',#{accountingOrderSn,jdbcType=VARCHAR},'%')
			</if>


			<!-- 对账单状态，0-未确认，1-已确认，-1-已取消,2-已拒绝  3待提交 -->
			<choose>
				<when test="accountingStatus != null ">
					and accounting_status = #{accountingStatus}
				</when>
				<otherwise>
					and accounting_status not in (-1,2)
				</otherwise>
			</choose>

			<if test="dzStatusWx != null ">
				and accounting_status not in (-1,2)
			</if>
			<if test="isSendEmail != null ">
				and is_send_email = #{isSendEmail,jdbcType=TINYINT}
			</if>
			<if test="invoiceStatus != null ">
				and invoice_status = #{invoiceStatus,jdbcType=TINYINT}
			</if>

			<if test="searchYear != null">
				and YEAR(create_time) =#{searchYear}
			</if>
			<if test="searchMonth != null">
				and MONTH(create_time) =#{searchMonth}
			</if>

			<if test="dateType != null ">
				<choose>
					<when test="dateType == 1">
						<if test="startTime != null and startTime != ''">
							and DATE_FORMAT(create_time,'%Y-%m-%d') &gt;= #{startTime,jdbcType=VARCHAR}
						</if>
						<if test="endTime != null and endTime != ''">
							and DATE_FORMAT(create_time,'%Y-%m-%d') &lt;= #{endTime,jdbcType=VARCHAR}
						</if>
					</when>
					<otherwise>
						<if test="startTime != null and startTime != ''">
							and DATE_FORMAT(send_email_time,'%Y-%m-%d') &gt;= #{startTime,jdbcType=VARCHAR}
						</if>
						<if test="endTime != null and endTime != ''">
							and DATE_FORMAT(send_email_time,'%Y-%m-%d') &lt;= #{endTime,jdbcType=VARCHAR}
						</if>
					</otherwise>
				</choose>
			</if>
		</where>
		order by create_time DESC
	</select>


	<!-- 弃用 mark by linjinyu at 20210105 -->
	<!-- 已修正 20200702 -->
	<select id="findByAccountingOrderSn"
	        resultMap="cn.superdesk.uniorder.dao.mapper.accounting.unified.customer.AccountingUnifiedCustomerMapper.BaseResultMap"
	        parameterType="java.lang.String">
		select
		<include refid="cn.superdesk.uniorder.dao.mapper.accounting.unified.customer.AccountingUnifiedCustomerMapper.Base_Column_List"/>
		from
			accounting_unified_customer
		where
			accounting_order_sn = #{dzSn}
	</select>







</mapper>