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







	<resultMap id="SupplierUnAccountGoodsResultMap" type="cn.superdesk.uniorder.dto.vo.unified.UnifiedOrderSupplierNotCheckDetail2GoodsVo">
		<id column="id" property="id" jdbcType="BIGINT"/>
		<result column="order_sn" property="orderSn" jdbcType="VARCHAR"/>
		<result column="purchase_virt_name" property="purchaseVirtName" jdbcType="VARCHAR"/>
		<result column="sale_name" property="saleName" jdbcType="VARCHAR"/>

		<result column="create_time" property="createTime" jdbcType="TIMESTAMP"/>
		<result column="goods_name" property="goodsName" jdbcType="VARCHAR"/>
		<result column="unit" property="unit" jdbcType="VARCHAR"/>

		<result column="quantity" property="quantity" jdbcType="INTEGER"/>

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

		<result column="amount" property="amount" jdbcType="DECIMAL"/>
		<result column="goods_cost_amount" property="goodsCostAmount" jdbcType="DECIMAL"/>
	</resultMap>


<!--	弃用-->
<!--	<update id="updateAccountingStateSupplier">-->
<!--		update unified_order-->
<!--		set accounting_state_supplier = 1-->
<!--		where id in-->
<!--		<foreach collection="ids" index="index" item="id" open="(" close=")" separator=",">-->
<!--			#{id}-->
<!--		</foreach>-->
<!--	</update>-->

	<resultMap id="UnifiedOrderSupplierHeadSumResultMap" type="cn.superdesk.uniorder.dto.vo.accounting.unified.supplier.notyet.UnifiedOrderSupplierHeadSumVo">

		<result column="total" property="total" jdbcType="INTEGER"/>

		<result column="order_amount" property="orderAmount" jdbcType="DECIMAL"/>
		<result column="order_cost_amount" property="orderCostAmount" jdbcType="DECIMAL"/>

		<result column="goods_cost_amount" property="goodsCostAmount" jdbcType="DECIMAL"/>
		<result column="transport_cost_amount" property="transportCostAmount" jdbcType="DECIMAL"/>
		<result column="refund_cost_amount" property="refundCostAmount" jdbcType="DECIMAL"/>

	</resultMap>

	<!-- 供应商结算 - 未对账订单 Head 20201021 修改-->
	<select id="getSupplierUnAccountingDimOrderHeadSum" resultMap="UnifiedOrderSupplierHeadSumResultMap">
		SELECT
			count(uo.id) as total,
			IFNULL(sum(uo.amount),0) order_amount,
			IFNULL(sum(uo.cost_amount),0) order_cost_amount,
			IFNULL(sum(uog1.goods_cost_amount),0) as goods_cost_amount,
			IFNULL(sum(uog0.goods_cost_amount),0) as transport_cost_amount,
			IFNULL(sum(uog2.goods_cost_amount),0) as refund_cost_amount
		from
			unified_order uo
			LEFT JOIN (
				SELECT
					order_sn,
					sum(cost_price*quantity) as goods_cost_amount,
					sum(price*quantity) as goods_amount
				FROM unified_order_goods
				<where>
				AND direction = 1
				AND goods_id !=0
				</where>
				GROUP by order_sn
			) uog1 on uo.order_sn = uog1.order_sn
			LEFT JOIN (
				SELECT
					order_sn,
					sum(cost_price*quantity) as goods_cost_amount,
					sum(price*quantity) as goods_amount
				FROM unified_order_goods
				<where>
				AND direction = -1
				</where>
				GROUP by order_sn
			) uog2 on uo.order_sn = uog2.order_sn
			LEFT JOIN (
				SELECT
					order_sn,
					sum(cost_price*quantity) as goods_cost_amount,
					sum(price*quantity) as goods_amount
				FROM unified_order_goods
				<where>
					AND direction = 1
					AND goods_id =0
				</where>
				GROUP by order_sn
			) uog0 on uo.order_sn = uog0.order_sn
		<where>
			<include refid="supplierUnAccountWhereIf"></include>
		</where>
	</select>


	<resultMap id="SupplierNcResultMap" type="cn.superdesk.uniorder.dto.vo.unified.UnifiedOrderSupplierDimOrderNotCheckVo">

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

		<!-- C位 -->
		<result column="order_amount" property="orderAmount" jdbcType="DECIMAL"/>
		<result column="order_cost_amount" property="orderCostAmount" jdbcType="DECIMAL"/>

		<result column="goods_cost_amount" property="goodsCostAmount" jdbcType="DECIMAL"/>
		<result column="transport_cost_amount" property="transportCostAmount" jdbcType="DECIMAL"/>
		<result column="refund_cost_amount" property="refundCostAmount" jdbcType="DECIMAL"/>

		<result column="goods_amount" property="goodsAmount" jdbcType="DECIMAL"/>
		<result column="transport_amount" property="transportAmount" jdbcType="DECIMAL"/>
		<result column="refund_amount" property="refundAmount" jdbcType="DECIMAL"/>

		<result column="purchase_virt_name" property="purchaseVirtName" jdbcType="VARCHAR"/>
		<result column="sale_name" property="saleName" jdbcType="VARCHAR"/>

		<result column="create_time" property="createTime" jdbcType="TIMESTAMP"/>
	</resultMap>

	<!-- 供应商结算 - 未对账订单 List -->
	<select id="getSupplierUnAccountingDimOrderList" resultMap="SupplierNcResultMap">
		SELECT
			uo.id,
			uo.order_sn,

			uo.amount as order_amount,
			uo.cost_amount as order_cost_amount,

			IFNULL(uog1.goods_cost_amount,0) as goods_cost_amount,
			IFNULL(uog0.goods_cost_amount,0) as transport_cost_amount,
			IFNULL(uog2.goods_cost_amount,0) as refund_cost_amount,

			IFNULL(uog1.goods_amount,0) as goods_amount,
			IFNULL(uog0.goods_amount,0) as transport_amount,
			IFNULL(uog2.goods_amount,0) as refund_amount,

			uo.sale_name,
			uo.purchase_virt_name,
			uo.create_time
		from
			unified_order uo
			LEFT JOIN (
				SELECT
				order_sn,
				sum(cost_price*quantity) as goods_cost_amount,
				sum(price*quantity) as goods_amount
				FROM unified_order_goods
				WHERE direction = 1
				GROUP by order_sn
			) uog1 on uo.order_sn = uog1.order_sn
			LEFT JOIN (
				SELECT
				order_sn,
				sum(cost_price*quantity) as goods_cost_amount,
				sum(price*quantity) as goods_amount
				FROM unified_order_goods
				WHERE direction = -1
				GROUP by order_sn
			) uog2 on uo.order_sn = uog2.order_sn
			LEFT JOIN (
				SELECT
				order_sn,
				sum(cost_price*quantity) as goods_cost_amount,
				sum(price*quantity) as goods_amount
				FROM unified_order_goods
				WHERE goods_id = 0
				GROUP by order_sn
			) uog0 on uo.order_sn = uog0.order_sn
		<where>
			<include refid="supplierUnAccountWhereIf"></include>
		</where>
		order by create_time desc
	</select>

	<!-- 供应商结算 - 未对账订单 opt 导出报表 (未对账列表-导出) -->
	<select id="getSupplierUnAccountingDetail2Goods" resultMap="SupplierUnAccountGoodsResultMap">
		SELECT
			uo.id,
			uo.order_sn,
			uo.sale_name,
			uo.purchase_virt_name,
			uo.amount,
			uo.create_time,
			uog.goods_name,
			uog.unit,
			uog.cost_price,
			uog.quantity,
			uog.cost_price*uog.quantity as goods_cost_amount
		from
			unified_order_goods uog
			LEFT JOIN unified_order uo on uog.order_sn = uo.order_sn
		<where>
			<include refid="supplierUnAccountWhereIf"></include>
		</where>

	</select>


	<sql id="supplierUnAccountWhereIf">
		and uo.accounting_state_supplier=2
		and uo.complet_state != 3

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


		<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="supplierId != null and supplierId != '' ">
			and uo.sale_id = #{supplierId}
		</if>
		<if test="supplierName != null and supplierName != '' ">
			and uo.sale_name like CONCAT('%',#{supplierName},'%')
		</if>


		<if test="amount != null and amount != '' ">
			and uo.amount = #{amount}
		</if>

		<!-- 时分秒 -->
		<if test="startTime != null and startTime != ''">
			and DATE_FORMAT(uo.create_time,'%Y-%m-%d %H:%i:%S') &gt;= #{startTime,jdbcType=VARCHAR}
		</if>
		<if test="endTime != null and endTime != ''">
			and DATE_FORMAT(uo.create_time,'%Y-%m-%d %H:%i:%S') &lt;= #{endTime,jdbcType=VARCHAR}
		</if>


		<if test="orderIds != null and orderIds != ''">
			and uo.id in
			<foreach collection="orderIds" index="index" item="id" open="(" close=")" separator=",">
				#{id,jdbcType=BIGINT}
			</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
			(
				SELECT
					service_no
				from
					unified_service_group
				where
					group_code in
					<foreach collection="outServices" index="index" item="serviceno" open="(" close=")" separator=",">
						#{serviceno}
					</foreach>
			)
		</if>
	</sql>



</mapper>