<?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.sys.SysInvoiceMapper">
	<resultMap id="BaseResultMap" type="cn.superdesk.uniorder.dao.entity.sys.SysInvoice">
		<id column="id" property="id" jdbcType="BIGINT"/>
		<result column="dz_main_id" property="dzMainId" jdbcType="BIGINT"/>
		<result column="dz_order_id" property="dzOrderId" jdbcType="BIGINT"/>
		<result column="invoice_number" property="invoiceNumber" jdbcType="VARCHAR"/>
		<result column="back_status" property="backStatus" jdbcType="INTEGER"/>
		<result column="back_account" property="backAccount" jdbcType="DECIMAL"/>
		<result column="back_operation_time" property="backOperationTime" jdbcType="TIMESTAMP"/>
		<result column="back_account_time" property="backAccountTime" jdbcType="TIMESTAMP"/>
		<result column="invoice_time" property="invoiceTime" jdbcType="TIMESTAMP"/>
		<result column="mail_tick_time" property="mailTickTime" jdbcType="TIMESTAMP"/>
		<result column="invoice_status" property="invoiceStatus" jdbcType="INTEGER"/>
		<result column="make_invoice_status" property="makeInvoiceStatus" jdbcType="INTEGER"/>
		<result column="express_company_id" property="expressCompanyId" jdbcType="BIGINT"/>
		<result column="express_no" property="expressNo" jdbcType="VARCHAR"/>
	</resultMap>
	<sql id="Base_Column_List">
    id, dz_main_id, dz_order_id, my_ordersn,invoice_number, back_status, back_account, back_operation_time, 
    back_account_time, invoice_time, mail_tick_time, invoice_status,make_invoice_status,express_company_id, 
    express_no
  </sql>
	<select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Long">
		select
		<include refid="Base_Column_List"/>
		from sys_invoice
		where id = #{id,jdbcType=BIGINT}
	</select>


	<delete id="deleteByPrimaryKey" parameterType="java.lang.Long">
    delete from sys_invoice
    where id = #{id,jdbcType=BIGINT}
  </delete>
	<insert id="insert" parameterType="cn.superdesk.uniorder.dao.entity.sys.SysInvoice">
		<selectKey resultType="java.lang.Long" keyProperty="id" order="AFTER">
			SELECT LAST_INSERT_ID()
		</selectKey>
		insert into sys_invoice (dz_main_id, dz_order_id, invoice_number,
		back_status, back_account, back_operation_time,
		back_account_time, invoice_time, mail_tick_time,
		invoice_status, make_invoice_status,express_company_id, express_no
		)
		values (#{dzMainId,jdbcType=BIGINT}, #{dzOrderId,jdbcType=VARCHAR}, #{invoiceNumber,jdbcType=VARCHAR},
		#{backStatus,jdbcType=INTEGER}, #{backAccount,jdbcType=DECIMAL}, #{backOperationTime,jdbcType=TIMESTAMP},
		#{backAccountTime,jdbcType=TIMESTAMP}, #{invoiceTime,jdbcType=TIMESTAMP}, #{mailTickTime,jdbcType=TIMESTAMP},
		#{invoiceStatus,jdbcType=INTEGER},#{make_invoice_status,jdbcType=INTEGER}, #{expressCompanyId,jdbcType=BIGINT},
		#{expressNo,jdbcType=VARCHAR}
		)
	</insert>
	<insert id="insertSelective" parameterType="cn.superdesk.uniorder.dao.entity.sys.SysInvoice">
		<selectKey resultType="java.lang.Long" keyProperty="id" order="AFTER">
			SELECT LAST_INSERT_ID()
		</selectKey>
		insert into sys_invoice
		<trim prefix="(" suffix=")" suffixOverrides=",">
			<if test="dzMainId != null">
				dz_main_id,
			</if>
			<if test="dzOrderId != null">
				dz_order_id,
			</if>
			<if test="invoiceNumber != null">
				invoice_number,
			</if>
			<if test="backStatus != null">
				back_status,
			</if>
			<if test="backAccount != null">
				back_account,
			</if>
			<if test="backOperationTime != null">
				back_operation_time,
			</if>
			<if test="backAccountTime != null">
				back_account_time,
			</if>
			<if test="invoiceTime != null">
				invoice_time,
			</if>
			<if test="mailTickTime != null">
				mail_tick_time,
			</if>
			<if test="invoiceStatus != null">
				invoice_status,
			</if>
			<if test="makeInvoiceStatus != null">
				make_invoice_status,
			</if>
			<if test="expressCompanyId != null">
				express_company_id,
			</if>
			<if test="expressNo != null">
				express_no,
			</if>
		</trim>
		<trim prefix="values (" suffix=")" suffixOverrides=",">
			<if test="dzMainId != null">
				#{dzMainId,jdbcType=BIGINT},
			</if>
			<if test="dzOrderId != null">
				#{dzOrderId,jdbcType=VARCHAR},
			</if>
			<if test="invoiceNumber != null">
				#{invoiceNumber,jdbcType=VARCHAR},
			</if>
			<if test="backStatus != null">
				#{backStatus,jdbcType=INTEGER},
			</if>
			<if test="backAccount != null">
				#{backAccount,jdbcType=DECIMAL},
			</if>
			<if test="backOperationTime != null">
				#{backOperationTime,jdbcType=TIMESTAMP},
			</if>
			<if test="backAccountTime != null">
				#{backAccountTime,jdbcType=TIMESTAMP},
			</if>
			<if test="invoiceTime != null">
				#{invoiceTime,jdbcType=TIMESTAMP},
			</if>
			<if test="mailTickTime != null">
				#{mailTickTime,jdbcType=TIMESTAMP},
			</if>
			<if test="invoiceStatus != null">
				#{invoiceStatus,jdbcType=INTEGER},
			</if>
			<if test="makeInvoiceStatus != null">
				#{makeInvoiceStatus,jdbcType=INTEGER},
			</if>
			<if test="expressCompanyId != null">
				#{expressCompanyId,jdbcType=BIGINT},
			</if>
			<if test="expressNo != null">
				#{expressNo,jdbcType=VARCHAR},
			</if>
		</trim>
	</insert>
	<update id="updateByPrimaryKeySelective" parameterType="cn.superdesk.uniorder.dao.entity.sys.SysInvoice">
		update sys_invoice
		<set>
			<if test="dzMainId != null">
				dz_main_id = #{dzMainId,jdbcType=BIGINT},
			</if>
			<if test="dzOrderId != null">
				dz_order_id = #{dzOrderId,jdbcType=BIGINT},
			</if>
			<if test="invoiceNumber != null and invoiceNumber != '' ">
				invoice_number = #{invoiceNumber,jdbcType=VARCHAR},
			</if>
			<if test="backStatus != null">
				back_status = #{backStatus,jdbcType=INTEGER},
			</if>
			<if test="backAccount != null">
				back_account = #{backAccount,jdbcType=DECIMAL},
			</if>
			<if test="backOperationTime != null">
				back_operation_time = #{backOperationTime,jdbcType=TIMESTAMP},
			</if>
			<if test="backAccountTime != null">
				back_account_time = #{backAccountTime,jdbcType=TIMESTAMP},
			</if>
			<if test="invoiceTime != null">
				invoice_time = #{invoiceTime,jdbcType=TIMESTAMP},
			</if>
			<if test="mailTickTime != null">
				mail_tick_time = #{mailTickTime,jdbcType=TIMESTAMP},
			</if>
			<if test="invoiceStatus != null">
				invoice_status = #{invoiceStatus,jdbcType=INTEGER},
			</if>
			<if test="makeInvoiceStatus != null">
				make_invoice_status = #{makeInvoiceStatus,jdbcType=INTEGER},
			</if>
			<if test="expressCompanyId != null">
				express_company_id = #{expressCompanyId,jdbcType=BIGINT},
			</if>
			<if test="expressNo != null">
				express_no = #{expressNo,jdbcType=VARCHAR},
			</if>
		</set>
		where id = #{id,jdbcType=BIGINT}
	</update>
	<update id="updateByPrimaryKey" parameterType="cn.superdesk.uniorder.dao.entity.sys.SysInvoice">
    update sys_invoice
    set dz_main_id = #{dzMainId,jdbcType=BIGINT},
      dz_order_id = #{dzOrderId,jdbcType=BIGINT},
      invoice_number = #{invoiceNumber,jdbcType=VARCHAR},
      back_status = #{backStatus,jdbcType=INTEGER},
      back_account = #{backAccount,jdbcType=DECIMAL},
      back_operation_time = #{backOperationTime,jdbcType=TIMESTAMP},
      back_account_time = #{backAccountTime,jdbcType=TIMESTAMP},
      invoice_time = #{invoiceTime,jdbcType=TIMESTAMP},
      mail_tick_time = #{mailTickTime,jdbcType=TIMESTAMP},
      invoice_status = #{invoiceStatus,jdbcType=INTEGER},
      make_invoice_status = #{makeInvoiceStatus,jdbcType=INTEGER},
      express_company_id = #{expressCompanyId,jdbcType=BIGINT},
      express_no = #{expressNo,jdbcType=VARCHAR}
    where id = #{id,jdbcType=BIGINT}
  </update>


	<!--自定义 -->
	<select id="findAll" resultMap="BaseResultMap">
		select
		<include refid="Base_Column_List"/>
		from sys_invoice
		<where>
			<if test="mdzId != null and mdzId != '' ">
				and dz_main_id = #{mdzId,jdbcType=BIGINT}
			</if>
			<if test="orderId != null and orderId != '' ">
				and dz_order_id = #{orderId,jdbcType=BIGINT}
			</if>
			<if test="myOrderSn != null and myOrderSn != '' ">
				and my_ordersn = #{myOrderSn,jdbcType=VARCHAR}
			</if>
		</where>
	</select>

	<!--自定义 -->
	<resultMap id="ViewInvoiceManageResultMap" type="cn.superdesk.uniorder.dto.vo.inv.ViewInvoiceManage">

		<result column="dz_sn" property="dzSn" jdbcType="VARCHAR"/>
		<result column="order_sn" property="orderSn" jdbcType="VARCHAR"/>

		<result column="buyerInfo" property="buyerInfo" jdbcType="VARCHAR"/>
		<result column="billOrReceiptTime" property="billOrReceiptTime" jdbcType="VARCHAR"/>

		<result column="orderTime" property="orderTime" jdbcType="VARCHAR"/>
		<result column="recipientGoodsTime" property="recipientGoodsTime" jdbcType="VARCHAR"/>

		<result column="invoiceId" property="invoiceId" jdbcType="VARCHAR"/>
		<result column="mdz_id" property="mdzId" jdbcType="VARCHAR"/>
		<result column="order_id" property="orderId" jdbcType="VARCHAR"/>
		<result column="invoice_number" property="invoiceNumber" jdbcType="VARCHAR"/>

		<!-- 企业名称 -->
		<result column="enterprise_name" property="enterpriseName" jdbcType="VARCHAR"/>
		<!-- 发票抬头 -->
		<result column="companyName" property="companyName" jdbcType="VARCHAR"/>

		<result column="order_num" property="orderNum" jdbcType="INTEGER"/>
		<result column="invoice_account" property="invoiceAccount" jdbcType="DECIMAL"/>
		<result column="invoice_time" property="invoiceTime" jdbcType="TIMESTAMP"/>
		<result column="invoiceStatus" property="invoiceStatus" jdbcType="INTEGER"/>
		<result column="makeInvoiceStatus" property="makeInvoiceStatus" jdbcType="INTEGER"/>
		<result column="back_status" property="backStatus" jdbcType="INTEGER"/>

		<result column="bill_status" property="billStatus" jdbcType="INTEGER"/>
		<result column="paytype" property="payType" jdbcType="INTEGER"/>
		<result column="source_name" property="sourceName" jdbcType="VARCHAR"/>
		<result column="remark" property="remark" jdbcType="VARCHAR"/>

		<result column="express_company_id" property="expressCompanyId" jdbcType="VARCHAR"/>
		<result column="express_no" property="expressNo" jdbcType="VARCHAR"/>

	</resultMap>


	<!-- 发票管理或寄票登记列表   发票登记也用此功能-->
	<select id="findInvoiceManageViewPage" resultMap="ViewInvoiceManageResultMap">


		#发票管理
		select m.dz_sn, #帐单编码
		o.order_sn order_sn, #'订单号'
		CONCAT(orderU.realname,' ',orderU.mobile) buyerInfo, #'买家信息'
		CONCAT(o.create_time,' ',o.finish_time) billOrReceiptTime, #'下单时间收货时间'
		o.create_time orderTime, #'下单时间'
		o.finish_time recipientGoodsTime, #'收货时间'

		inv.id invoiceId, #'发票表ID'
		m.id mdz_id,
		o.id order_id, #'用dz_order.id'
		inv.invoice_number invoice_number, #'发票号'
		e.enterprise_name, #'企业名称'
		o.companyName companyName , #开票抬头
		(select count(id) FROM accounting_unified_customer_order where dz_id=m.id) order_num, #'订单数'
		(select sum(price) FROM accounting_unified_customer_order where dz_id=m.id and id=o.id) invoice_account, # '发票金额'\订单金额
		inv.invoice_time invoice_time, #'开票时间'
		IFNULL(inv.invoice_status,0) invoiceStatus, #'发票状态 0-未邮寄 1-已邮寄 2-已作废'
		IFNULL(inv.make_invoice_status,0) makeInvoiceStatus, #'开票状态 0-未开票 1-已开票'
		IFNULL(inv.back_status,0) back_status, #'回款状态'

		o.status bill_status, #'订单状态'
		o. paytype, #'支付方式'
		os.source_name, #'订单类型'

		inv.express_company_id , #'快递公司ID'
		inv.express_no , #'快递单号'

		o.remark #'备注'
		from accounting_unified_customer m
		left JOIN sys_enterprise e on m.enterprise_id=e.enterprise_id
		#dz订单dz_id = m.id
		left join accounting_unified_customer_order o on m.id=o.dz_id
		left join sys_invoice inv on (inv.dz_main_id=m.id and inv.dz_order_id=o.id)
		left join order_source_type os on os.id=o.service_name
		left join sys_oder_user orderU on orderU.id=o.realname_id
		where 1=1

		<!-- 发票管理或寄票登记列表 要先登记发票 findInvoiceManageByPage 方法有赋值 findConfirmInvoice发票登记不用赋值-->
		<if test="invoiceNumberNotNull != null ">
			and inv.invoice_number is not null
		</if>

		<if test="mdzId != null ">
			and m.id = #{mdzId,jdbcType=BIGINT}
		</if>
		<if test="invoiceIds != null ">
			and inv.id in

			<foreach item="item" index="index" collection="invoiceIds" open="(" separator="," close=")">
				#{item}
			</foreach>

		</if>

		<!-- 开票登记过滤 -->
		<if test="mdzIds != null ">
			and m.id in

			<foreach item="item" index="index" collection="mdzIds" open="(" separator="," close=")">
				#{item}
			</foreach>

		</if>

		<!-- 发票管理或寄票登记 列表过滤 -->

		<if test="dateType != null ">
			<choose>

				<when test="dateType == 1"><!-- 1下单时间 2开票时间 3对帐时间 4寄票时间 -->
					<if test="startTime != null and startTime != ''">
						and DATE_FORMAT(o.create_time,'%Y-%m-%d %H:%i:%s') &gt;= #{startTime,jdbcType=VARCHAR}

					</if>
					<if test="endTime != null and endTime != ''">
						and DATE_FORMAT(o.create_time,'%Y-%m-%d %H:%i:%s') &lt;= #{endTime,jdbcType=VARCHAR}
					</if>
				</when>

				<when test="dateType == 2"><!-- 1下单时间 2开票时间 3对帐时间 4寄票时间 -->
					<if test="startTime != null and startTime != ''">
						and DATE_FORMAT(inv.invoice_time,'%Y-%m-%d %H:%i:%s') &gt;= #{startTime,jdbcType=VARCHAR}

					</if>
					<if test="endTime != null and endTime != ''">
						and DATE_FORMAT(inv.invoice_time,'%Y-%m-%d %H:%i:%s') &lt;= #{endTime,jdbcType=VARCHAR}
					</if>
				</when>

				<when test="dateType == 3"><!-- 1下单时间 2开票时间 3对帐时间 4寄票时间 -->
					<if test="startTime != null and startTime != ''">
						and DATE_FORMAT(m.create_time,'%Y-%m-%d %H:%i:%s') &gt;= #{startTime,jdbcType=VARCHAR}

					</if>
					<if test="endTime != null and endTime != ''">
						and DATE_FORMAT(m.create_time,'%Y-%m-%d %H:%i:%s') &lt;= #{endTime,jdbcType=VARCHAR}
					</if>
				</when>

				<when test="dateType == 4"><!-- 1下单时间 2开票时间 3对帐时间 4寄票时间 -->
					<if test="startTime != null and startTime != ''">
						and DATE_FORMAT(inv.mail_tick_time,'%Y-%m-%d %H:%i:%s') &gt;= #{startTime,jdbcType=VARCHAR}

					</if>
					<if test="endTime != null and endTime != ''">
						and DATE_FORMAT(inv.mail_tick_time,'%Y-%m-%d %H:%i:%s') &lt;= #{endTime,jdbcType=VARCHAR}
					</if>
				</when>

			</choose>
		</if>

		<if test="enterpriseName != null and enterpriseName != '' ">
			and e.enterprise_name like CONCAT('%',#{enterpriseName,jdbcType=VARCHAR},'%')
		</if>
		<if test="dzSn != null and dzSn != '' ">
			and m.dz_sn like CONCAT('%',#{dzSn,jdbcType=VARCHAR},'%')
		</if>
		<if test="orderSn != null and orderSn != '' ">
			and o.ordersn like CONCAT('%',#{orderSn,jdbcType=VARCHAR},'%')
		</if>


		<if test="backStatus != null "><!-- 回款状态 0未回款\1部分回款\2全部回款 -->
			and inv.back_status = #{backStatus,jdbcType=TINYINT}
		</if>
		<if test="invoiceStatus != null "><!-- 发票状态 0未邮寄 1已邮寄 2已作废 -->
			and inv.invoice_status = #{invoiceStatus,jdbcType=TINYINT}
		</if>


		<if test="companyName != null and companyName != '' "><!-- 开票抬头 -->
			and o.companyName like CONCAT('%',#{companyName,jdbcType=VARCHAR},'%')
		</if>
		<if test="merchName != null and merchName != '' "><!-- 商户名称 -->
			and o.merchName like CONCAT('%',#{merchName,jdbcType=VARCHAR},'%')
		</if>

		<if test="realname != null and realname != '' "><!-- 下单人 -->
			and orderU.realname like CONCAT('%',#{realname,jdbcType=VARCHAR},'%')
		</if>

		<if test="address_realname != null and address_realname != '' "><!-- 收件人 -->
			and o.address_realname like CONCAT('%',#{address_realname,jdbcType=VARCHAR},'%')
		</if>

		order by inv.invoice_time desc

		<!-- 发票管理或寄票登记 列表过滤 -->

	</select>


	<!--自定义 发票详情分录-->
	<resultMap id="ViewInvoiceDetailsListResultMap" type="cn.superdesk.uniorder.dto.vo.inv.ViewInvoiceDetailsList">

		<result column="dz_sn" property="dzSn" jdbcType="VARCHAR"/>
		<result column="ordersn" property="ordersn" jdbcType="VARCHAR"/>

		<result column="buyerInfo" property="buyerInfo" jdbcType="VARCHAR"/>
		<result column="billOrReceiptTime" property="billOrReceiptTime" jdbcType="VARCHAR"/>

		<result column="orderTime" property="orderTime" jdbcType="VARCHAR"/>
		<result column="recipientGoodsTime" property="recipientGoodsTime" jdbcType="VARCHAR"/>

		<result column="mdz_id" property="mdzId" jdbcType="VARCHAR"/>
		<result column="order_id" property="orderId" jdbcType="VARCHAR"/>
		<result column="invoice_number" property="invoiceNumber" jdbcType="VARCHAR"/>
		<result column="enterprise_name" property="enterpriseName" jdbcType="VARCHAR"/>
		<result column="order_num" property="orderNum" jdbcType="INTEGER"/>
		<result column="invoice_account" property="invoiceAccount" jdbcType="DECIMAL"/>
		<result column="invoice_time" property="invoiceTime" jdbcType="TIMESTAMP"/>
		<result column="invoice_status" property="invoiceStatus" jdbcType="INTEGER"/>
		<result column="back_status" property="backStatus" jdbcType="INTEGER"/>

		<result column="bill_status" property="billStatus" jdbcType="INTEGER"/>
		<result column="paytype" property="payType" jdbcType="INTEGER"/>
		<result column="source_name" property="sourceName" jdbcType="VARCHAR"/>
		<result column="remark" property="remark" jdbcType="VARCHAR"/>

		<result column="goodName" property="goodName" jdbcType="VARCHAR"/>
		<result column="unit" property="unit" jdbcType="VARCHAR"/>
		<result column="total" property="total" jdbcType="INTEGER"/>
		<result column="unit_price" property="unitPrice" jdbcType="DECIMAL"/>
		<result column="dispatch_price" property="dispatchPrice" jdbcType="DECIMAL"/>
		<result column="total_price" property="totalPrice" jdbcType="DECIMAL"/>


	</resultMap>


	<!-- 发票详情分录  因有商品数据 单独查询-->
	<select id="findInvoiceDetailsList" resultMap="ViewInvoiceDetailsListResultMap">

		#发票详情分录
		select m.dz_sn, #帐单编码
		o.ordersn ordersn, #'订单号'
		orderU.realname realname, #'下单人'
		CONCAT(orderU.realname,' ',orderU.mobile) buyerInfo, #'买家信息'
		CONCAT(o.create_time,' ',o.finish_time) billOrReceiptTime, #'下单时间收货时间'
		o.create_time orderTime, #'下单时间'
		o.finish_time recipientGoodsTime, #'收货时间'
		m.id mdz_id,
		o.id order_id,
		inv.invoice_number invoice_number, #'发票号'
		e.enterprise_name,
		(select count(id) FROM accounting_unified_customer_order where dz_id=m.id) order_num, #'订单数'
		(select sum(price) FROM accounting_unified_customer_order where dz_id=m.id) invoice_account, # '发票金额'\订单金额
		inv.invoice_time invoice_time, #'开票时间'
		IFNULL(inv.invoice_status,0) invoice_status, #'邮票状态'
		IFNULL(inv.back_status,0) back_status, #'回款状态'

		o.status bill_status, #'订单状态'
		o. paytype, #'支付方式'
		os.source_name, #'订单类型'
		o.remark, #'备注'

		og.other_field1 goodName, #'商品名称'
		og.unit , #'单位'
		og.total , #'数量'
		og.unit_price , #'单价'
		og.dispatch_price , #'运费'
		og.total_price #'小计'

		from accounting_unified_customer m
		left JOIN sys_enterprise e on m.enterprise_id=e.enterprise_id
		#dz订单dz_id = m.id
		left join accounting_unified_customer_order o on m.id=o.dz_id
		left join sys_invoice inv on (inv.dz_main_id=m.id and inv.dz_order_id=o.id)
		left join order_source_type os on os.id=o.service_name
		left join sys_oder_user orderU on orderU.id=o.realname_id
		left join dz_order_good og on og.dz_order_id=o.id
		<where>

		<if test="invoiceId != null ">
			and inv.id = #{invoiceId,jdbcType=BIGINT}
		</if>
		</where>

	</select>


	<!--自定义 -->
	<resultMap id="ViewInvoiceDetailResultMap" type="cn.superdesk.uniorder.dto.vo.inv.ViewInvoiceDetailsHead">

		<result column="mdzId" property="mdzId" jdbcType="VARCHAR"/>
		<result column="invoiceNumber" property="invoiceNumber" jdbcType="VARCHAR"/>
		<result column="expressCompany" property="expressCompany" jdbcType="VARCHAR"/>
		<result column="expressNo" property="expressNo" jdbcType="VARCHAR"/>
		<result column="enterpriseName" property="enterpriseName" jdbcType="VARCHAR"/>
		<result column="companyName" property="companyName" jdbcType="VARCHAR"/>
		<result column="addressRealname" property="addressRealname" jdbcType="VARCHAR"/>
		<result column="addressMobile" property="addressMobile" jdbcType="VARCHAR"/>
		<result column="addressAddress" property="addressAddress" jdbcType="VARCHAR"/>
		<result column="invoice_account" property="invoiceAccount" jdbcType="DECIMAL"/>

	</resultMap>


	<select id="getInvoiceDetailByInvoiceId" resultMap="ViewInvoiceDetailResultMap">


		#发票详情
		select
		inv.dz_main_id mdzId, #帐单ID
		inv.invoice_number invoiceNumber , #发票号
		ec.company_name expressCompany, #快递公司
		inv.express_no expressNo , #快递单号
		e.enterprise_name enterpriseName , #企业名称
		o.companyName companyName , #开票抬头
		o.address_realname addressRealname, #账单联系人
		o.address_mobile addressMobile, #账单联系人电话
		o.address_address addressAddress, #账单地址
		(select sum(price) FROM accounting_unified_customer_order where dz_id=m.id) invoice_account, # 订单金额
		o.realname_id # 下单人ID

		from accounting_unified_customer m
		left JOIN sys_enterprise e on m.enterprise_id=e.enterprise_id
		#dz订单dz_id = m.id
		left join accounting_unified_customer_order o on m.id=o.dz_id
		left join sys_invoice inv on (inv.dz_main_id=m.id and inv.dz_order_id=o.id)
		left join express_company ec on ec.id=inv.express_company_id
		where 1=1

		<if test="invoiceId != null ">
			and inv.id = #{invoiceId,jdbcType=BIGINT}
		</if>

	</select>


	<!-- 未回款  -->


	<!--自定义 -->
	<resultMap id="ViewBackAccountResultMap" type="cn.superdesk.uniorder.dto.vo.inv.ViewBackAccountManage">

		<result column="ordersn" property="ordersn" jdbcType="VARCHAR"/>
		<result column="invoiceNumber" property="invoiceNumber" jdbcType="VARCHAR"/>
		<result column="dz_sn" property="dzSn" jdbcType="VARCHAR"/>
		<result column="mdz_id" property="mdzId" jdbcType="VARCHAR"/>
		<result column="order_id" property="orderId" jdbcType="VARCHAR"/>
		<!-- 企业名称 -->
		<result column="enterprise_name" property="enterpriseName" jdbcType="VARCHAR"/>

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

		<result column="order_num" property="orderNum" jdbcType="INTEGER"/>
		<result column="invoice_account" property="invoiceAccount" jdbcType="DECIMAL"/>
		<result column="back_account" property="backAccount" jdbcType="DECIMAL"/>

		<result column="noBackAccount" property="noBackAccount" jdbcType="DECIMAL"/>

		<result column="create_time" property="createTime" jdbcType="TIMESTAMP"/>
		<result column="invoice_time" property="invoiceTime" jdbcType="TIMESTAMP"/>
		<result column="mail_tick_time" property="mailTickTime" jdbcType="TIMESTAMP"/>
		<result column="back_status" property="backStatus" jdbcType="INTEGER"/>

		<result column="back_account_time" property="backAccountTime" jdbcType="TIMESTAMP"/>
		<result column="back_operation_time" property="backOperationTime" jdbcType="TIMESTAMP"/>
		<result column="payType" property="payType" jdbcType="INTEGER"/>
		<result column="source_name" property="sourceName" jdbcType="VARCHAR"/>

	</resultMap>

	<!--未回款 已回款界面数据  按帐单级汇总-->
	<select id="getBackAccountManageData" resultMap="ViewBackAccountResultMap">


		#未回款查询
		#这里是按帐单查询的
		select
		distinct
		m.id mdz_id,
		m.dz_sn, #帐单编码
		e.enterprise_name, #'企业名称'
		(select count(id) FROM accounting_unified_customer_order where dz_id=m.id) order_num, #'订单数'
		(select sum(price) FROM accounting_unified_customer_order where dz_id=m.id) invoice_account, # '对账单金额

		(select sum(back_account) FROM sys_back_account ba where ba.dz_main_id=m.id ) back_account, #已回款额
		#未回款额

		m.create_time, #'对帐时间'

		#inv.invoice_time invoice_time, #'开票时间'
		#inv.mail_tick_time mail_tick_time , #'寄票时间'
		(select max(inv.invoice_time) FROM sys_invoice inv where inv.dz_main_id=m.id ) invoice_time, #开票时间
		(select max(inv.mail_tick_time) FROM sys_invoice inv where inv.dz_main_id=m.id ) mail_tick_time, #寄票时间
		0 noBackAccount, #未回款额
		IFNULL(m.back_account_status,0) back_status #'回款状态'

		from accounting_unified_customer m
		left JOIN sys_enterprise e on m.enterprise_id=e.enterprise_id
		<!-- 加订单表后会有重复 上面 distinct 去重  加入作用 查询-->
		left join accounting_unified_customer_order o on m.id=o.dz_id
		left join sys_invoice inv on (inv.dz_main_id=m.id and inv.dz_order_id=o.id)
		left join sys_oder_user orderU on orderU.id=o.realname_id
		where 1=1
		and m.dz_invstatus=1 and invoice_status=1 #'开票后和快递票后才能到未回款'

		<if test="backStatusList != null ">
			and m.back_account_status in

			<foreach item="item" index="index" collection="backStatusList" open="(" separator="," close=")">
				#{item}
			</foreach>

		</if>

		<if test="mdzId != null ">
			and m.id = #{mdzId,jdbcType=BIGINT}
		</if>

		<!-- 发票管理或寄票登记 列表过滤 -->

		<if test="dateType != null ">
			<choose>

				<when test="dateType == 1"><!--1下单时间 2对帐时间 3开票时间 4寄票时间 5回款时间 -->
					<if test="startTime != null and startTime != ''">
						and DATE_FORMAT(o.create_time,'%Y-%m-%d %H:%i:%s') &gt;= #{startTime,jdbcType=VARCHAR}

					</if>
					<if test="endTime != null and endTime != ''">
						and DATE_FORMAT(o.create_time,'%Y-%m-%d %H:%i:%s') &lt;= #{endTime,jdbcType=VARCHAR}
					</if>
				</when>

				<when test="dateType == 2">
					<if test="startTime != null and startTime != ''">
						and DATE_FORMAT(m.create_time,'%Y-%m-%d %H:%i:%s') &gt;= #{startTime,jdbcType=VARCHAR}

					</if>
					<if test="endTime != null and endTime != ''">
						and DATE_FORMAT(m.create_time,'%Y-%m-%d %H:%i:%s') &lt;= #{endTime,jdbcType=VARCHAR}
					</if>
				</when>

				<when test="dateType == 3">
					<if test="startTime != null and startTime != ''">
						and DATE_FORMAT(inv.invoice_time,'%Y-%m-%d %H:%i:%s') &gt;= #{startTime,jdbcType=VARCHAR}

					</if>
					<if test="endTime != null and endTime != ''">
						and DATE_FORMAT(inv.invoice_time,'%Y-%m-%d %H:%i:%s') &lt;= #{endTime,jdbcType=VARCHAR}
					</if>
				</when>
				<when test="dateType == 4">
					<if test="startTime != null and startTime != ''">
						and DATE_FORMAT(inv.mail_tick_time,'%Y-%m-%d %H:%i:%s') &gt;= #{startTime,jdbcType=VARCHAR}

					</if>
					<if test="endTime != null and endTime != ''">
						and DATE_FORMAT(inv,'%Y-%m-%d %H:%i:%s') &lt;= #{endTime,jdbcType=VARCHAR}
					</if>
				</when>
				<when test="dateType == 5"><!--5回款时间  -->
					<if test="startTime != null and startTime != ''">
						and DATE_FORMAT(inv.back_account_time,'%Y-%m-%d %H:%i:%s') &gt;= #{startTime,jdbcType=VARCHAR}

					</if>
					<if test="endTime != null and endTime != ''">
						and DATE_FORMAT(inv.back_account_time,'%Y-%m-%d %H:%i:%s') &lt;= #{endTime,jdbcType=VARCHAR}
					</if>
				</when>


			</choose>
		</if>

		<if test="enterpriseName != null and enterpriseName != '' ">
			and e.enterprise_name like CONCAT('%',#{enterpriseName,jdbcType=VARCHAR},'%')
		</if>
		<if test="dzSn != null and dzSn != '' ">
			and m.dz_sn like CONCAT('%',#{dzSn,jdbcType=VARCHAR},'%')
		</if>
		<if test="orderSn != null and orderSn != '' ">
			and o.ordersn like CONCAT('%',#{orderSn,jdbcType=VARCHAR},'%')
		</if>

		<if test="invoiceNumber != null and invoiceNumber != '' "><!-- 发票号 -->
			and inv.invoice_number like CONCAT('%',#{invoiceNumber,jdbcType=VARCHAR},'%')
		</if>

		<if test="backStatus != null "><!-- 回款状态 0未回款\1部分回款\2全部回款 -->
			and inv.back_status = #{backStatus,jdbcType=TINYINT}
		</if>
		<if test="invoiceStatus != null "><!-- 发票状态 0未邮寄 1已邮寄 2已作废 -->
			and inv.invoice_status = #{invoiceStatus,jdbcType=TINYINT}
		</if>


		<if test="companyName != null and companyName != '' "><!-- 开票抬头 -->
			and o.companyName like CONCAT('%',#{companyName,jdbcType=VARCHAR},'%')
		</if>
		<if test="merchName != null and merchName != '' "><!-- 商户名称 -->
			and o.merch_name like CONCAT('%',#{merchName,jdbcType=VARCHAR},'%')
		</if>

		<if test="realname != null and realname != '' "><!-- 下单人 -->
			and orderU.realname like CONCAT('%',#{realname,jdbcType=VARCHAR},'%')
		</if>

		<if test="address_realname != null and address_realname != '' "><!-- 收件人 -->
			and o.address_realname like CONCAT('%',#{address_realname,jdbcType=VARCHAR},'%')
		</if>


		<!-- 发票管理或寄票登记 列表过滤 -->


	</select>


	<!-- 未回款  -->


	<!-- 回款登记界面  -->

	<!-- 用的未回款ResultMap -->
	<select id="getBackAccountDataRegisterData" resultMap="ViewBackAccountResultMap">

		#未回款 回款登记 回款相关数据
		#按订单级查询
		select
		m.id mdz_id,
		o.id order_id,
		m.dz_sn, #帐单编码
		o.ordersn ordersn, #'订单号'
		inv.invoice_number invoiceNumber , #发票号
		e.enterprise_name, #'企业名称'
		o.merch_name, #'商户名称'
		CONCAT(orderU.realname,' ',orderU.mobile) buyerInfo, #'买家信息'
		(select count(id) FROM accounting_unified_customer_order where dz_id=m.id) order_num, #'订单数'
		o.price invoice_account, # '订单金额'
		#(select sum(price) FROM accounting_unified_customer_order where dz_id=m.id and order_id=o.id) invoice_account, # '订单金额'

		(select sum(back_account) FROM sys_back_account ba where ba.dz_main_id=m.id and ba.dz_order_id=o.id)
		back_account, #已回款额


		0 noBackAccount, #未回款额
		m.create_time , #'对帐时间'
		inv.invoice_time invoice_time, #'开票时间'
		inv.mail_tick_time mail_tick_time , #'寄票时间'
		inv.back_account_time , #'回款时间'
		inv.back_operation_time , #'操作时间'
		o. paytype payType, #'支付方式'
		IFNULL(inv.back_status,0) back_status, #'回款状态'
		os.source_name #'订单类型'

		from accounting_unified_customer m
		left JOIN sys_enterprise e on m.enterprise_id=e.enterprise_id
		#dz订单dz_id = m.id
		left join accounting_unified_customer_order o on m.id=o.dz_id
		left join sys_invoice inv on (inv.dz_main_id=m.id and inv.dz_order_id=o.id)
		left join order_source_type os on os.id=o.service_name
		left join sys_oder_user orderU on orderU.id=o.realname_id

		where 1=1
		and inv.id is not null
		#and m.id =16

		<if test="mdzId != null ">
			and m.id = #{mdzId,jdbcType=BIGINT}
		</if>


		order by m.id,o.id


	</select>

	<!-- 回款登记界面  -->


</mapper>