<?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.base.UnifiedOrderMapperExCustomerDimCredit">


	<!--  autoMapping="true"  -->
	<resultMap id="ScoreOrderReconciliationNotYetVoResultMap"
	           type="cn.superdesk.uniorder.dto.vo.invoice.customer.InvoiceApplyDetailDimCreditItemVo">
		<id column="unified_order_credit_id" property="unifiedOrderCreditId" jdbcType="BIGINT"/>
<!--		uoc.out_snapshot_order_credit_id,-->
		<result column="out_snapshot_order_credit_id" property="outSnapshotOrderCreditId" jdbcType="BIGINT"/>

		<result column="unified_order_goods_id" property="unifiedOrderGoodsId" jdbcType="BIGINT"/>
		<result column="out_snapshot_order_goods_id" jdbcType="BIGINT" property="outSnapshotOrderGoodsId" />
		<result column="goods_id" property="goodsId" jdbcType="BIGINT"/>


		<result column="unified_order_id" property="unifiedOrderId" jdbcType="BIGINT"/>


		<result column="credit_type" jdbcType="INTEGER" property="creditType" />
		<result column="credit_name" property="creditName" jdbcType="VARCHAR"/>
		<result column="daily_account_type" jdbcType="INTEGER" property="dailyAccountType" />

		<result column="batchs" property="batchs" jdbcType="VARCHAR"/>
		<result column="balance" property="balance" jdbcType="DECIMAL"/>


		<result column="goods_name" property="goodsName" jdbcType="VARCHAR"/>

		<!-- 弃用 改为 三级分类都记录 20200904 by linjinyu-->
		<!-- uog.cate_name,-->
		<!-- <result column="cate_name" property="cateName" jdbcType="VARCHAR"/>-->
		<!-- ****************************************** 三级分类 20200904 by linjinyu 20200904_林志鹏_三极分类 ****************************************** -->
		<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" />
		<!-- ****************************************** 三级分类 20200904 by linjinyu 20200904_林志鹏_三极分类 ****************************************** -->


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


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


		<result column="order_locking_customer" property="orderLockingCustomer" jdbcType="INTEGER"/>
		<result column="order_locking_supplier" property="orderLockingSupplier" jdbcType="INTEGER"/>
		<result column="goods_locking_customer" property="goodsLockingCustomer" jdbcType="INTEGER"/>
		<result column="goods_locking_supplier" property="goodsLockingSupplier" jdbcType="INTEGER"/>



		<!-- 发票申请状态 是否已申请 0-未申请;1-已申请; -->
<!--		uoc.invoice_apply_status,-->
		<result column="invoice_apply_status" property="invoiceApplyStatus" jdbcType="INTEGER"/>
		<!-- 开票状态 是否已登记开票 0-未登记;1-已登记; -->
<!--		uoc.invoice_make_status,-->
		<result column="invoice_make_status" property="invoiceMakeStatus" jdbcType="INTEGER"/>
		<!-- 回款状态 是否回款 0-未回款;1-已回款; -->
<!--		uoc.back_status,-->
		<result column="back_status" property="backStatus" jdbcType="INTEGER"/>



		<result column="price" property="price" jdbcType="DECIMAL"/>
		<result column="cost_price" property="costPrice" jdbcType="DECIMAL"/>
		<result column="quantity" property="quantity" jdbcType="INTEGER"/>

		<!-- uog.price*uog.quantity as total_price,-->
		<result column="total_price" property="totalPrice" jdbcType="DECIMAL"/>
		<result column="total_cost_price" property="totalCostPrice" jdbcType="DECIMAL"/>

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


		<!-- uog.tax_rate,-->
		<result column="tax_rate" property="taxRate" jdbcType="DECIMAL"/>
		<!-- uog.tax_code,-->
		<result column="tax_code" property="taxCode" jdbcType="VARCHAR"/>
		<!-- uog.fiscal_code,-->
		<result column="fiscal_code" property="fiscalCode" jdbcType="VARCHAR"/>

		<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" />

        <!-- uo.amount,-->
        <result column="amount" property="amount" jdbcType="DECIMAL"/>
        <!-- uo.cost_amount,-->
        <result column="cost_amount" property="costAmount" jdbcType="DECIMAL"/>
        <!-- uo.transport_amount,-->
        <result column="transport_amount" property="transportAmount" jdbcType="DECIMAL"/>

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

		<result column="purchase_department_name" property="purchaseDepartmentName" 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="express_sn" property="expressSn" jdbcType="VARCHAR"/>
		<result column="express" property="express" jdbcType="VARCHAR"/>
		<result column="express_company" property="expressCompany" 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="accounting_time" property="accountingTime" jdbcType="TIMESTAMP"/>
		<result column="update_time" property="updateTime" jdbcType="TIMESTAMP"/>

		<result column="complet_state" property="completState" jdbcType="TINYINT"/>

		<!-- add 20200723 附件1：对账单导出格式与字段.xls "订单状态","供货方","快递公司","快递单号","更新时间"-->

		<!-- uo.order_state,-->
		<result column="order_state" property="orderState" jdbcType="INTEGER"/>
		<!-- uo.order_state_name,-->
		<result column="order_state_name" property="orderStateName" jdbcType="VARCHAR"/>

		<!-- uo.sale_id,-->
		<result column="sale_id" property="saleId" jdbcType="BIGINT"/>
		<!-- uo.sale_name,-->
		<result column="sale_name" property="saleName" jdbcType="VARCHAR"/>



		<!-- add 20200723 附件1：对账单导出格式与字段.xls "订单状态","供货方","快递公司","快递单号","更新时间"-->



		<!-- 20200810 start -->
<!--		uo.pay_type,-->
		<result column="pay_type" property="payType" jdbcType="INTEGER"/>
<!--		uo.pay_state,-->
		<result column="pay_state" property="payState" jdbcType="INTEGER"/>


		<!-- uo.service_name, 弃用 改用定义 mark by linjinyu at 20220624 -->
		<result column="service_name" property="serviceName" jdbcType="VARCHAR"/>
		<!-- uo.service_no,-->
		<result column="service_no" property="serviceNo" jdbcType="VARCHAR"/>

<!--		uo.rec_user_name,-->
		<result column="rec_user_name" property="recUserName" jdbcType="VARCHAR"/>
<!--		uo.rec_user_id,-->
		<result column="rec_user_id" property="recUserId" jdbcType="BIGINT"/>
<!--		uo.rec_user_phone,-->
		<result column="rec_user_phone" property="recUserPhone" jdbcType="VARCHAR"/>
<!--		uo.rec_address,-->
		<result column="rec_address" property="recAddress" jdbcType="VARCHAR"/>

		<!-- 20200810   end  -->


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

	<!-- 未结算商品 共用 -->
	<sql id="getScoreOrderReconciliationNotYetWhereIf">

		<!--
			1.积分支付;2.微信支付;
			清除 and uoc.pay_type = 1
		-->
		and uo.complet_state != 3
		<choose>
			<when test="payType != null and payType != '' or payType == 0">
				and uoc.pay_type = #{payType}
			</when>
			<otherwise>

			</otherwise>
		</choose>

		<!-- 客户对账:1-已对账;2-未对账 只作用未结算流水 -->
		<!-- 积分结算2.1 and accountingStateCustomer != '' or accountingStateCustomer == 0 -->
		<choose>
			<when test="accountingStateCustomer != null">
				and uoc.accounting_state_customer = #{accountingStateCustomer}
			</when>
			<otherwise>
				<!--
				and uoc.accounting_state_customer = 2
				-->
			</otherwise>
		</choose>



		<if test="orderSnEq != null and orderSnEq != '' ">
			and (uo.order_sn = #{orderSnEq} or uo.refund_order_sn = #{orderSnEq} )
		</if>

		<if test="orderSn != null and orderSn != '' ">
			<!--
			and uoc.order_sn like CONCAT('%',#{orderSn},'%')
			-->
			and (uo.order_sn like CONCAT('%',#{orderSn},'%') or uo.refund_order_sn like CONCAT('%',#{orderSn},'%'))
		</if>

		<!-- 订单编号 uoc.order_sn -->
		<if test="autoComplete != null and autoComplete == 0 and orderSnsSelected != null and orderSnsSelected != ''">
			and uoc.order_sn in
			<foreach collection="orderSnsSelected" index="index" item="order_sn" open="(" close=")" separator=",">
				#{order_sn}
			</foreach>
		</if>



		<!-- TODO 标记 20201021 and uoc.id in  () 不见了  order by -->
		<!-- autoComplete != null and autoComplete == x 写法为了只能存在一种情况问题 -->
		<if test="autoComplete != null and autoComplete == 2 and idsCredit != null and idsCredit != ''">
			and uoc.id in
			<foreach collection="idsCredit" index="index" item="unified_order_credit_id" open="(" close=")" separator=",">
				#{unified_order_credit_id}
			</foreach>
		</if>
		<if test="autoComplete != null and autoComplete == -1 and idsCredit != null and idsCredit != ''">
			and uoc.id in
			<foreach collection="idsCredit" index="index" item="unified_order_credit_id" open="(" close=")" separator=",">
				#{unified_order_credit_id}
			</foreach>
		</if>

		<if test="outVirtIds != null and outVirtIds != ''"><!-- 已修正 数据权限 by linjinyu at 2020090818 -->
			<!-- project_id -->
			and uo.purchase_virt_id in
			<foreach collection="outVirtIds" index="index" item="pid" open="(" close=")" separator=",">
				#{pid}
			</foreach>
		</if>
		<if test="outServices != null and outServices != ''">
			and uo.service_no in
			<foreach collection="outServices" index="index" item="serviceNo" open="(" close=")" separator=",">
				#{serviceNo}
			</foreach>
		</if>
		<if test="serviceNosSelected != null and serviceNosSelected != '' and serviceNosSelected.size > 0">
			and uo.service_no in
			<foreach collection="serviceNosSelected" index="index" item="service_no" open="(" close=")" separator=",">
				#{service_no}
			</foreach>
		</if>

		<!-- 下单时间 uo.create_time -->
		<if test="startTime != null and startTime != ''">
			and DATE_FORMAT(uo.create_time,'%Y-%m-%d') &gt;= #{startTime,jdbcType=VARCHAR}
		</if>
		<if test="endTime != null and endTime != ''">
			and DATE_FORMAT(uo.create_time,'%Y-%m-%d') &lt;= #{endTime,jdbcType=VARCHAR}
		</if>

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


		<!-- 企业名称 uo.purchase_virt_name -->
		<if test="purchaseVirtId != null and purchaseVirtId != '' ">
			and uo.purchase_virt_id = #{purchaseVirtId}
		</if>
		<if test="purchaseVirtName != null and purchaseVirtName != '' ">
			and uo.purchase_virt_name like CONCAT('%',#{purchaseVirtName},'%')
		</if>

		<if test="purchaseDepartmentName != null and purchaseDepartmentName != '' ">
			and uo.purchase_department_name like CONCAT('%',#{purchaseDepartmentName},'%')
		</if>



		<if test="goodsName != null and goodsName != '' ">
			and uog.goods_name like CONCAT('%',#{goodsName},'%')
		</if>
		<if test="goodsId != null and goodsId != '' ">
			and uog.goods_id = #{goodsId}
		</if>
		<if test="unifiedOrderGoodsId != null and unifiedOrderGoodsId != '' ">
			and uog.id = #{unifiedOrderGoodsId}
		</if>
		<if test="operationTaxCode != null and operationTaxCode != '' ">
			and uog.operation_tax_code = #{operationTaxCode}
		</if>



		<!-- 积分类型 uoc.credit_name -->
		<if test="creditName != null and creditName != '' ">
			and uoc.credit_name like CONCAT('%',#{creditName},'%')
		</if>
		<!-- 积分批次 uoc.batchs -->
		<if test="batchs != null and batchs != '' ">
			and
			(
				<!--
				uoc.batchs like CONCAT('%',#{batchs},'%')
				-->
				uoc.batchs = #{batchs}

		        <choose>
		            <when test="autoComplete != null and autoComplete == -1 and idsCredit != null and idsCredit != ''">
						or (
								uoc.batchs = 'wechat'

								<choose>
									<when test="accountingStateCustomer != null">
										and uoc.accounting_state_customer = #{accountingStateCustomer}
									</when>
									<otherwise>
										<!-- -->
										and uoc.accounting_state_customer = 2

									</otherwise>
								</choose>
							)
					</when>
		            <!--
					<when test="autoComplete != null and autoComplete == 0"></when>
					<when test="autoComplete != null and autoComplete == 1"></when>
					<when test="autoComplete != null and autoComplete == 2"></when>
					-->
		            <otherwise>
						or (
							uoc.batchs = 'wechat'
							and uoc.order_sn in (
								SELECT
									DISTINCT uoc.order_sn
								FROM
									unified_order_credit uoc
								WHERE
									uoc.accounting_state_customer = 2
									<!--
									AND uoc.batchs like CONCAT('%',#{batchs},'%')
									-->
									AND uoc.batchs = #{batchs}
							)
						)

		            </otherwise>
		        </choose>

			)
		</if>

	</sql>

	<!-- 积分结算 -> 未结算商品 列表 | 此页面是以商品流水的维度去展示的 opt 搜索 下单时间 只能以订单维度 最终 -->
	<select id="getScoreOrderReconciliationNotYet"
			parameterType="Map"
	        resultMap="ScoreOrderReconciliationNotYetVoResultMap">
		SELECT
			uoc.id as unified_order_credit_id,
			uoc.out_snapshot_order_credit_id,

			uog.id as unified_order_goods_id,
			uog.out_snapshot_order_goods_id,
			uoc.goods_id,

			uo.id as unified_order_id,

			uoc.credit_type,
			uoc.credit_name,
			uoc.daily_account_type,

			uoc.batchs,
			uoc.balance,


			uog.goods_name,

			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,

			uoc.order_sn,

			<!-- 客户对账:1-已对账;2-未对账 -->
			uoc.accounting_state_customer,
			<!-- 供应商对账:1-已对账;2-未对账 -->
			uoc.accounting_state_supplier,

			<!-- 发票申请状态 是否已申请 0-未申请;1-已申请; -->
			uoc.invoice_apply_status,
			<!-- 开票状态 是否已登记开票 0-未登记;1-已登记; -->
			uoc.invoice_make_status,
			<!-- 回款状态 是否回款 0-未回款;1-已回款; -->
			uoc.back_status,


			uog.price,
			uog.cost_price,
			uog.quantity,

			uog.price * uog.quantity as total_price,
			uog.cost_price * uog.quantity as total_cost_price,

			uog.unit,

			uog.tax_rate,
			uog.tax_code,
			uog.fiscal_code,

			uog.merch_tax_id,
			uog.merch_tax_code,
			uog.merch_tax_rate,
			uog.operation_tax_id,
			uog.operation_tax_code,
			uog.operation_tax_rate,

			uo.amount,
			uo.cost_amount,
			uo.transport_amount,

			uo.purchase_virt_id,
			uo.purchase_virt_name,
			uo.purchase_department_name,
			uo.purchase_user_id,
			uo.purchase_user_name,
			uo.purchase_phone,

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

			uog.goods_locking_customer,
			uog.goods_locking_supplier,
			uo.order_locking_customer,
			uo.order_locking_supplier,

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

			uo.complet_state,

			<!-- add 20200723 附件1：对账单导出格式与字段.xls "订单状态","供货方","快递公司","快递单号","更新时间", -->

			uo.order_state,
			uo.order_state_name,

			uo.sale_id,
			uo.sale_name,


			<!-- 20200810 start -->
			uo.pay_type,
			uo.pay_state,

		    <!-- 弃用 改用定义 mark by linjinyu at 20220624 -->
			uo.service_name,
			uo.service_no,

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

			<!-- 20200810   end  -->

			uo.remarks
		FROM
			unified_order_credit uoc
			LEFT JOIN unified_order_goods uog on uoc.goods_id = uog.goods_id and uoc.order_sn = uog.order_sn and uoc.direction = uog.direction
			LEFT JOIN unified_order uo on uo.order_sn = uoc.order_sn
		<where>
			<include refid="getScoreOrderReconciliationNotYetWhereIf"></include>
		</where>
		order by uo.create_time desc , uo.order_sn desc, uoc.id asc
	</select>

	<!-- 积分结算 -> 未结算商品 头部 | 此页面是以商品流水的维度去展示的 opt 搜索 下单时间 只能以订单维度 最终 -->
	<select id="getScoreOrderReconciliationNotYetHeadSum" parameterType="Map"
	        resultType="cn.superdesk.uniorder.dto.vo.score.ScoreOrderReconciliationNotYetHeadVo">
		SELECT
			COUNT(socg.order_quantity) as order_quantity,
			SUM(socg.goods_quantity) as goods_quantity,
			SUM(socg.credit_quantity) as credit_quantity,
			SUM(socg.total_balance) as total_balance_amount
		FROM (
			SELECT
				COUNT(DISTINCT uoc.order_sn) as order_quantity,
				COUNT(DISTINCT uog.id) as goods_quantity,
				COUNT(uoc.id) as credit_quantity,
				SUM(uoc.balance) total_balance
			FROM
				unified_order_credit uoc
				LEFT JOIN unified_order_goods uog on uoc.goods_id = uog.goods_id and uoc.order_sn = uog.order_sn and uoc.direction = uog.direction
				LEFT JOIN unified_order uo on uo.order_sn = uoc.order_sn
			<where>
				<include refid="getScoreOrderReconciliationNotYetWhereIf"></include>
			</where>

			GROUP by uoc.order_sn,uoc.goods_id
		) socg
	</select>


	<resultMap id="AutoCompleteUnifiedOrderCreditDimOrderStep01ResultMap"
			   type="cn.superdesk.uniorder.dto.vo.order.AutoCompleteUnifiedOrderCreditDimOrderStep01Vo">
		<id column="unified_order_id" property="unifiedOrderId" jdbcType="BIGINT"/>
		<result column="order_sn" property="orderSn" jdbcType="VARCHAR"/>
		<result column="refund_order_sn" jdbcType="VARCHAR" property="refundOrderSn"/>
		<result column="order_type" jdbcType="TINYINT" property="orderType"/>
	</resultMap>

	<!-- -->
	<select id="autoCompleteUnifiedOrderCreditDimOrderStep01" parameterType="Map"
			resultMap="AutoCompleteUnifiedOrderCreditDimOrderStep01ResultMap">
		SELECT
			id as unified_order_id,
			order_sn,
			refund_order_sn,
			order_type
		FROM
			unified_order
		<where>
			id in
			<foreach collection="unifiedOrderIds" index="index" item="unifiedOrderId" open="(" close=")" separator=",">
				#{unifiedOrderId}
			</foreach>

		</where>
	</select>

	<resultMap id="AutoCompleteUnifiedOrderCreditDimOrderGoodsStep01ResultMap"
			   type="cn.superdesk.uniorder.dto.vo.order.AutoCompleteUnifiedOrderCreditDimOrderGoodsStep01Vo">

		<result column="order_sn" property="orderSn" jdbcType="VARCHAR"/>
		<result column="goods_id" property="goodsId" jdbcType="BIGINT"/>
	</resultMap>

	<select id="autoCompleteUnifiedOrderCreditDimOrderGoodsStep01" parameterType="Map"
			resultMap="AutoCompleteUnifiedOrderCreditDimOrderGoodsStep01ResultMap">
		SELECT
			order_sn,
			goods_id
		FROM
			unified_order_credit
		<where>
			and id IN
			<foreach collection="unifiedOrderCreditIds" index="index" item="unifiedOrderCreditId" open="(" close=")" separator=",">
				#{unifiedOrderCreditId}
			</foreach>
		</where>
		GROUP BY order_sn,goods_id
	</select>

	<select id="autoCompleteUnifiedOrderCreditDimOrderGoodsStep02"
			parameterType="Map" resultType="java.lang.Long">
		SELECT
			uoc.id
		FROM
			unified_order_credit uoc
		<where>
			and uoc.order_sn IN
			<foreach collection="outOrderSns" index="index" item="orderSn" open="(" close=")" separator=",">
				#{orderSn}
			</foreach>
			and uoc.goods_id IN
			<foreach collection="outGoodsIds" index="index" item="goodsId" open="(" close=")" separator=",">
				#{goodsId}
			</foreach>
		</where>
	</select>

</mapper>
