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

	<resultMap id="ViewGoodsResultMap" type="cn.superdesk.uniorder.dto.vo.unified.UnifiedOrderGoodsVo">
		<id column="id" property="id" jdbcType="BIGINT"/>
		<result column="order_sn" property="orderSn" jdbcType="VARCHAR"/>

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

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

		<result column="pay_state" jdbcType="TINYINT" property="payState" />
		<result column="pay_state_name" jdbcType="VARCHAR" property="payStateName" />

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

<!--		uo.amount-->
<!--		<result column="amount" property="amount" jdbcType="DECIMAL"/>-->
<!--		uo.amount AS order_amount,-->
<!--		uo.cost_amount AS order_cost_amount,-->
		<result column="order_amount" property="orderAmount" jdbcType="DECIMAL"/>
		<result column="order_cost_amount" property="orderCostAmount" jdbcType="DECIMAL"/>


		<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_name" property="purchaseUserName" jdbcType="VARCHAR"/>
		<result column="purchase_phone" property="purchasePhone" jdbcType="VARCHAR"/>

		<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="sale_id" property="saleId" jdbcType="BIGINT"/>
		<result column="sale_name" property="saleName" jdbcType="VARCHAR"/>

		<result column="oper_account" jdbcType="VARCHAR" property="operAccount" />
		<result column="oper_name" jdbcType="VARCHAR" property="operName" />

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

		<result column="rec_user_name" property="recUserName" jdbcType="VARCHAR"/>
		<result column="rec_user_phone" property="recUserPhone" jdbcType="VARCHAR"/>
		<result column="rec_address" property="recAddress" jdbcType="VARCHAR"/>

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

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


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

	<resultMap id="UnifiedOrderCommonHeadSumResultMap" type="cn.superdesk.uniorder.dto.vo.unified.UnifiedOrderCommonHeadSum">

		<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="order_transport_amount" property="orderTransportAmount" jdbcType="DECIMAL"/>

		<result column="order_refund_amount" property="orderRefundAmount" jdbcType="DECIMAL"/>
		<result column="order_refund_cost_amount" property="orderRefundCostAmount" jdbcType="DECIMAL"/>

		<result column="goods_amount" property="goodsAmount" jdbcType="DECIMAL"/>
		<result column="goods_cost_amount" property="goodsCostAmount" jdbcType="DECIMAL"/>

		<result column="goods_transport_amount" property="goodsTransportAmount" jdbcType="DECIMAL"/>

		<result column="goods_refund_amount" property="goodsRefundAmount" jdbcType="DECIMAL"/>
		<result column="goods_refund_cost_amount" property="goodsRefundCostAmount" jdbcType="DECIMAL"/>


	</resultMap>

	<select id="getOrderCount" resultType="java.lang.Integer">
		SELECT
			sum(B.quantity) AS quantity
		from
			unified_order A
			left join unified_order_goods B ON A.sn = B.sn
		<where>

			and A.complet_state != 3
		<if test="orgId != null and orgId != ''">
			and A.project_id in
			<foreach collection="orgId" index="index" item="oid" open="(" close=")" separator=",">
				#{oid}
			</foreach>
		</if>
		<if test="saleId != null and saleId != ''">
			and A.sale_id = #{saleId}
		</if>
		<if test="payState != null and payState != ''">
			and A.pay_state = #{payState}
		</if>
		<if test="beginTime != null and beginTime != ''">
			and DATE_FORMAT(A.create_time,'%Y-%m-%d %H:%i:%s') &gt;= #{beginTime}
		</if>
		<if test="endTime != null and endTime != ''">
			and DATE_FORMAT(A.create_time,'%Y-%m-%d %H:%i:%s') &lt;= #{endTime}
		</if>
		</where>

	</select>

	<select id="findAll" resultMap="cn.superdesk.uniorder.dao.mapper.base.UnifiedOrderMapper.BaseResultMap">
		select
			<include refid="cn.superdesk.uniorder.dao.mapper.base.UnifiedOrderMapper.Base_Column_List"/>
		from
			unified_order
		<where>
		<if test="ids != '' and ids != null">
			and id in
			<foreach collection="ids" index="index" item="id" open="(" close=")" separator=",">
				#{id}
			</foreach>
		</if>
		<if test="serviceNo != null and serviceNo != ''">
			and service_no = #{serviceNo}
		</if>
		<if test="dzId != null and dzId != ''">
			and id in (SELECT order_id from accounting_unified_customer_order where dz_id =#{dzId})
		</if>
		</where>
	</select>

	<select id="countAll" resultType="java.lang.Integer">
		select
			count(1)
		from
			unified_order
		<where>
			<if test="ids != '' and ids != null">
				and id in
				<foreach collection="ids" index="index" item="id" open="(" close=")" separator=",">
					#{id}
				</foreach>
			</if>
			<if test="serviceNo != null and serviceNo != ''">
				and service_no = #{serviceNo}
			</if>
			<if test="dzId != null and dzId != ''">
				and id in (SELECT order_id from accounting_unified_customer_order where dz_id =#{dzId})
			</if>
		</where>
	</select>

	<select id="getAmount" resultType="java.math.BigDecimal">
		SELECT
			sum(amount) as order_amount
		from
			unified_order
		<where>
			complet_state != 3
		<if test="virtId != null and virtId != ''">
			and purchase_virt_id = #{virtId,jdbcType=BIGINT}
		</if>
		<if test="projectId != null and projectId != ''">
			and project_id = #{projectId,jdbcType=BIGINT}
		</if>
		<if test="yearly != null and monthly != null">
			and YEAR(create_time) = #{yearly}
			and MONTH(create_time) = #{monthly}
		</if>
		<if test="onlyyear != null and onlyyear != ''">
			and YEAR(create_time)=#{onlyyear}
		</if>
		<if test="onlymonth != null and onlymonth != ''">
			and MONTH(create_time)=#{onlymonth}
		</if>

		<if test="serviceNo != null and serviceNo != ''">
			and service_no = #{serviceNo}
		</if>
		<if test="groupserviceNo != null and groupserviceNo != ''">
			and service_no in (
			SELECT service_no FROM unified_service_group where group_code = #{groupserviceNo}
			)
		</if>
		</where>
	</select>

	<select id="getPayAmount" resultType="java.math.BigDecimal">
		SELECT
			*
		from
			accounting_unified_customer_order_goods
		<where>
			id in (
				SELECT dz_good_id
				from invoice_apply_x_goods
				where back_status=1
				)
			and dz_order_id in (
				SELECT id
				from
					accounting_unified_customer_order
				where
					order_sn in (
					select
						order_sn
					from
						unified_order
					where
						complet_state!=3
					<if test="virtId != null and virtId != ''">
						and purchase_virt_id = #{virtId,jdbcType=BIGINT}
					</if>
					<if test="onlyyear != null and onlyyear != ''">
						and YEAR(create_time)=#{onlyyear}
					</if>
					<if test="onlymonth != null and onlymonth != ''">
						and MONTH(create_time)=#{onlymonth}
					</if>
					<if test="serviceNo != null and serviceNo != ''">
						and service_no = #{serviceNo}
					</if>
					<if test="groupserviceNo != null and groupserviceNo != ''">
						and service_no = #{groupserviceNo}
					</if>
					)
			)
		</where>
	</select>

	<select id="findByOrderSn"
	        resultMap="cn.superdesk.uniorder.dao.mapper.base.UnifiedOrderMapper.BaseResultMap"
	        parameterType="java.lang.String">
		select
		<include refid="cn.superdesk.uniorder.dao.mapper.base.UnifiedOrderMapper.Base_Column_List"/>
		from
			unified_order
		<where>
			<if test="orderSn != null and orderSn != ''">
			and order_sn = #{orderSn,jdbcType=VARCHAR}
			</if>
		</where>
	</select>

	<select id="findByRefundOrderSn"
			resultMap="cn.superdesk.uniorder.dao.mapper.base.UnifiedOrderMapper.BaseResultMap"
			parameterType="java.lang.String">
		select
		<include refid="cn.superdesk.uniorder.dao.mapper.base.UnifiedOrderMapper.Base_Column_List"/>
		from
			unified_order
		<where>
			<if test="refundOrderSn != null and refundOrderSn != ''">
				and refund_order_sn = #{refundOrderSn,jdbcType=VARCHAR}
			</if>
		</where>
	</select>

	<select id="findByJdOrderSn"
			resultMap="cn.superdesk.uniorder.dao.mapper.base.UnifiedOrderMapper.BaseResultMap">
		select
			<include refid="cn.superdesk.uniorder.dao.mapper.base.UnifiedOrderMapper.Base_Column_List"/>
		from
			unified_order
		<where>
			<if test="jdOrderSn != null and jdOrderSn != ''">
			and express_sn = #{jdOrderSn,jdbcType=VARCHAR}
			and express = 'jd'
			</if>
		</where>
	</select>

	<sql id="orderMamagerWhereIfAliasUOGGoodsName">
		<if test="goodsName != null and goodsName != '' ">
			and uog.goods_name like CONCAT('%',#{goodsName},'%')
		</if>
	</sql>
	<sql id="orderMamagerWhereIfGoodsName">
		<if test="goodsName != null and goodsName != '' ">
			and goods_name like CONCAT('%',#{goodsName},'%')
		</if>
	</sql>
	<sql id="orderMamagerWhereIf">

			and uo.complet_state != 3

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

		<if test="projectId != null and projectId != '' ">
			and uo.project_id = #{projectId}
		</if>
		<if test="projectName != null and projectName != '' ">
			and uo.project_name like CONCAT('%',#{projectName},'%')
		</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="serviceNo != null and serviceNo != '' ">
			and uo.service_no = #{serviceNo}
		</if>

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

		<if test="purchaseUserId != null and purchaseUserId != '' ">
			and uo.purchase_user_id = #{purchaseUserId}
		</if>
		<if test="purchaseUserName != null and purchaseUserName != '' ">
			and uo.purchase_user_name = #{purchaseUserName}
		</if>
		<if test="purchasePhone != null and purchasePhone != '' ">
			and uo.purchase_phone = #{purchasePhone}
		</if>

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

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

<!-- outVirtIds != '' -->
<!-- outVirtIds.size > 0 -->

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

	<resultMap id="getOrg2VirtStatisticsResultMap" type="cn.superdesk.uniorder.dto.vo.order.Org2VirtStatisticsVo">
		<id column="virt_id" property="virtId" jdbcType="BIGINT"/>
		<result column="org_id" property="orgId" jdbcType="BIGINT"/>

		<result column="org_name" property="orgName" jdbcType="VARCHAR"/>
		<result column="virt_name" property="virtName" jdbcType="VARCHAR"/>

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

	<select id="getOrg2VirtStatistics" resultMap="getOrg2VirtStatisticsResultMap">

		select
			b.*
			from (
				select
					sum(price * quantity) as price,
					sum(cost_price * quantity) as cost_price,

					<choose>
						<when test="mode != null and mode == 1">
							project_id as org_id,
							project_name as org_name
						</when>
						<when test="mode != null and mode == 2">
							project_id as org_id,
							project_name as org_name,
							purchase_virt_id as virt_id,
							purchase_virt_name as virt_name
						</when>
						<otherwise>
							project_id as org_id,
							project_name as org_name,
							purchase_virt_id as virt_id,
							purchase_virt_name as virt_name
						</otherwise>
					</choose>

				from (
					<include refid="getOrderManagerGoodsViewSQL"></include>
				) a

				<choose>
					<when test="mode != null and mode == 1">
						<!--
						group by a.project_id
						-->
						group by a.project_name
					</when>
					<when test="mode != null and mode == 2">
						<!--
						group by a.purchase_virt_id
						-->
						group by a.purchase_virt_name
					</when>
					<otherwise>
						<!--
						group by a.purchase_virt_id
						-->
						group by a.purchase_virt_name
					</otherwise>
				</choose>
		) b
		order by b.price desc
	</select>

<!--	<result column="order_amount" property="orderAmount" jdbcType="DECIMAL"/>-->
<!--	<result column="order_cost_amount" property="orderCostAmount" jdbcType="DECIMAL"/>-->
	<sql id="getOrderManagerGoodsViewSQL">

		SELECT
			uog.id AS id,
			uog.order_sn AS order_sn,

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

			uo.amount AS order_amount,
			uo.cost_amount AS order_cost_amount,

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

			uo.pay_type AS pay_type,
			uo.pay_type_name AS pay_type_name,

			uo.pay_state AS pay_state,
			uo.pay_state_name AS pay_state_name,

			uo.complet_state AS complet_state,

			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.express_sn,
			uo.express,
			uo.express_company,

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

			uo.remarks AS remarks,



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

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

			uo.oper_account AS oper_account,
			uo.oper_name AS oper_name,

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

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

			uog.quantity AS quantity,
			uog.price AS price,
			uog.cost_price AS 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
			unified_order_goods uog
			LEFT JOIN unified_order uo ON uog.order_sn = uo.order_sn
		<where>
			<include refid="orderMamagerWhereIf"></include>
			<include refid="orderMamagerWhereIfAliasUOGGoodsName"></include>

			<choose>
				<when test="orderBy != null and orderBy != '' ">
					order by ${orderBy}
				</when>
				<otherwise>
					<!--
					order by uog.create_time desc
					-->
					order by uo.create_time desc , uo.order_sn desc, uog.id asc
				</otherwise>
			</choose>
		</where>

	</sql>

	<select id="getOrderManagerGoodsView" resultMap="ViewGoodsResultMap">
		<include refid="getOrderManagerGoodsViewSQL"></include>
	</select>

	<!-- 错误标志 20201022 -->
	<select id="getOrderManagerGoodsPageHead" resultMap="UnifiedOrderCommonHeadSumResultMap">
		SELECT
			count(uo.id) as total,

			IFNULL(sum(uo.amount),0) order_amount,
			IFNULL(sum(uo.cost_amount),0) order_cost_amount,

			IFNULL(sum(uor.order_refund_amount),0) order_refund_amount,
			IFNULL(sum(uor.order_refund_cost_amount),0) order_refund_cost_amount,

			IFNULL(sum(uo.transport_amount),0) order_transport_amount,

			ifnull(sum(uog1.goods_amount),0) as goods_amount,
			ifnull(sum(uog1.goods_cost_amount),0) as goods_cost_amount,

			ifnull(sum(uog2.goods_amount),0) as goods_refund_amount,
			ifnull(sum(uog2.goods_cost_amount),0) as goods_refund_cost_amount,

			ifnull(sum(uog0.goods_amount),0) as goods_transport_amount
		from
			unified_order uo

			RIGHT JOIN (
				SELECT
					sum(price * quantity) as goods_amount,
					sum(cost_price * quantity) as goods_cost_amount,
					order_sn
				FROM
					unified_order_goods
				<where>
					AND direction = 1
					AND goods_id != 0
					<include refid="orderMamagerWhereIfGoodsName"></include>
				</where>
				GROUP by order_sn
			) uog1 on uo.order_sn = uog1.order_sn

			LEFT JOIN (
				SELECT
					sum(price * quantity) as goods_amount,
					sum(cost_price * quantity) as goods_cost_amount,
					order_sn
				FROM
					unified_order_goods
				<where>
					AND direction = -1
					<include refid="orderMamagerWhereIfGoodsName"></include>
				</where>
				GROUP by order_sn
			) uog2 on uo.order_sn = uog2.order_sn

			LEFT JOIN (
				SELECT
					sum(price * quantity) as goods_amount,
					sum(cost_price * quantity) as goods_cost_amount,
					order_sn
				FROM
					unified_order_goods
				<where>
					AND direction = 1
					AND goods_id = 0
					<include refid="orderMamagerWhereIfGoodsName"></include>
				</where>
				GROUP by order_sn
			) uog0 on uo.order_sn = uog0.order_sn

			LEFT JOIN (
				SELECT
					sum(amount) order_refund_amount,
					0 as order_refund_cost_amount,
					order_sn
				from
					unified_order_return
				GROUP BY order_sn
			) uor on uo.order_sn = uor.order_sn
		<where>
			<include refid="orderMamagerWhereIf"></include>
		</where>
	</select>

	<resultMap id="VirtOrderResultMap" type="cn.superdesk.uniorder.dto.vo.wechat.WxBillOrder">
		<id column="id" property="id" jdbcType="BIGINT"/>
		<result column="order_sn" property="orderSn" jdbcType="VARCHAR"/>
		<result column="service_name" property="serviceName" jdbcType="VARCHAR"/>
		<result column="service_no" property="serviceNo" jdbcType="VARCHAR"/>
		<result column="purchase_user_name" property="purchaseUserName" jdbcType="VARCHAR"/>
		<result column="amount" property="amount" jdbcType="DECIMAL"/>
		<result column="create_time" property="createTime" jdbcType="TIMESTAMP"/>
		<result column="back_account_status" property="backAccountStatus" jdbcType="VARCHAR"/>
	</resultMap>

	<select id="getVirtOrder" resultMap="VirtOrderResultMap">
		SELECT
			ta.id,
			ta.order_sn,
			ta.service_name,
			ta.service_no,
			ta.purchase_user_name,
			ta.amount,
			ta.create_time,
			CASE
			WHEN tb.amount is null then '未付款'
			WHEN tb.amount = 0 then '未付款'
			WHEN tb.amount = ta.amount then '已付款'
			WHEN tb.amount &gt; 0 and tb.amount &lt; ta.amount then '部分付款'
			ELSE '未付款'
			END as back_account_status
		from unified_order ta left join
		(
		SELECT t.amount,a.ordersn from (
		SELECT sum(price) amount,dz_order_id from accounting_unified_customer_order_goods where id in (
		SELECT dz_good_id from invoice_apply_x_goods where back_status=1
		) GROUP BY dz_order_id
		) t LEFT JOIN accounting_unified_customer_order a on a.id = t.dz_order_id
		) tb on ta.order_sn=tb.order_sn
		<where>
			and ta.complet_state!=3
		<if test="virtId != null and virtId != ''">
			and ta.purchase_virt_id = #{virtId,jdbcType=BIGINT}
		</if>

		<if test="onlyyear != null and onlyyear != ''">
			and YEAR(ta.create_time)=#{onlyyear}
		</if>
		<if test="onlymonth != null and onlymonth != ''">
			and MONTH(ta.create_time)=#{onlymonth}
		</if>

		<if test="serviceNo != null and serviceNo != ''">
			and ta.service_no = #{serviceNo}
		</if>
		<if test="groupserviceNo != null and groupserviceNo != ''">
			and ta.service_no =#{groupserviceNo}
		</if>
		</where>
	</select>



	<resultMap id="getSelectPurchaseDepartmentResultMap" type="cn.superdesk.uniorder.dto.vo._function.select.SelectOptionDepartment">

		<id column="department_name" property="departmentName" jdbcType="VARCHAR"/>
		<result column="department_id" property="departmentId" jdbcType="BIGINT"/>

	</resultMap>

	<select id="getSelectPurchaseDepartment"
			parameterType="cn.superdesk.uniorder.dto.param._function.select.SelectOptionDepartmentCriteria"
			resultMap="getSelectPurchaseDepartmentResultMap">
		SELECT
			DISTINCT uo.purchase_department_name as department_name,
			0 as department_id
		FROM
			unified_order uo
		<where>

			<if test="purchaseDepartmentName != null and purchaseDepartmentName != ''">
				and uo.purchase_department_name LIKE CONCAT('%',#{purchaseDepartmentName},'%')
			</if>
		</where>
		GROUP by uo.purchase_department_name
	</select>

</mapper>