<?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._statistics.ReceivableStatisticsMapper">


	<resultMap id="BaseResultMap" type="cn.superdesk.uniorder.dao.entity._dashboard.TempDashboard">
		<id column="id" property="id" jdbcType="BIGINT"/>
	</resultMap>
	<resultMap id="DashboardProjectPurchaseResultMap" type="cn.superdesk.uniorder.dto.vo._dashboard.UnifiedDashboardProjectPurchaseVo">
		<result column="projectId" property="projectId" jdbcType="BIGINT"/>
		<result column="projectName" property="projectName" jdbcType="VARCHAR"/>
		<result column="orderAmount" property="orderAmount" jdbcType="DECIMAL"/>
		<result column="lng" property="lng" jdbcType="DECIMAL"/>
		<result column="lat" property="lat" jdbcType="DECIMAL"/>
	</resultMap>



	<select id="queryReceivableDetailData" resultType="cn.superdesk.uniorder.dto.vo._statistics.ReceivableDetailDataVo">

		SELECT
		ascu.accounting_order_sn accountingOrderSn,
		ascu.update_user_id chargeUserId,
		ascu.update_time chargeCreatedAt,
		sysUser.fullname chargeUserName,

		uoc.id AS creditId,
		uo.purchase_virt_id settlementId,
		uo.purchase_virt_name settlementName,
		uo.order_sn orderSn,
		uoc.id AS creditNo,
		uo.service_no serviceNo,
		uo.service_name serviceNoName,
		uo.refund_order_sn refundOrderSn,
		uo.service_name serviceName,
		uo.order_state orderState,
		uo.order_state_name orderStateName,
		uo.create_time billCreatedAt,
		uo.finish_time finishTime,
		uog.id goodsId,
		uog.goods_name goodsName,
		uog.unit goodsUnit,
		uog.quantity goodsQuantity,
		uog.price goodsPrice,
		uog.price * uog.quantity AS goodsTotalAmount,
		uoc.credit_type creditType, #0为微信 其他福利积分
		uoc.credit_name  creditName, #积分类型名称
		uoc.balance balance,
		uog.operation_tax_code operationTaxCode,
		uog.operation_tax_rate operationTaxRate,
		ascu.accounting_status accountingState, #对账(结算)单 状态:0-未确认;1-已确认;-1-已取消
		uoc.invoice_make_status makeInvoiceStatus,
		uoc.back_status backStatus,

		uoc.balance / (1 + uog.operation_tax_rate/100) incomeNoTax,

		uoc.balance - ( uoc.balance / (1 + uog.operation_tax_rate/100) ) taxAmount,


		0 receivableAmount

		FROM
		( accounting_score_customer ascu
		RIGHT JOIN accounting_score_customer_x_credit ascuxc on ascu.accounting_order_sn = ascuxc.accounting_order_sn )
		LEFT JOIN unified_order_credit uoc ON ascuxc.unified_order_credit_id = uoc.id
		LEFT JOIN unified_order_goods uog ON ascuxc.unified_order_goods_id = uog.id
		LEFT JOIN unified_order uo ON ascuxc.unified_order_id = uo.id
		LEFT JOIN sys_enterprise se ON ascu.enterprise_id = se.enterprise_id
		LEFT JOIN sys_oder_user AS sou ON uo.purchase_user_id = sou.user_id
		LEFT join sys_user sysUser on ascu.update_user_id=sysUser.id

		where 1=1
		#ascu.accounting_order_sn = 'DZ2020102100004'

		#and uo.service_no = 'FLSC_QCH'
		#and uog.receivable_statistics_state = 0
		#and uo.complet_state = 1 #1-已完成
		#and uo.finish_time is not null
		 and uog.operation_tax_rate is not null
		#and uoc.accounting_state_customer in (1,2)


		<if test="accountingOrderSn != null and accountingOrderSn != ''" >
			and ascu.accounting_order_sn = #{accountingOrderSn}
		</if>


	</select>


	<!--查询应收每日数据-->
	<select id="queryReceivableDayData" resultType="cn.superdesk.uniorder.dto.vo._statistics.ReceivableDayDataVo">

        SELECT
            GROUP_CONCAT(id) detail_id,accounting_order_sn,DATE_FORMAT(finish_time,'%Y-%m') pertain_month,DATE_FORMAT(finish_time,'%Y-%m-%d') account_at,settlement_id,service_no,service_no_name,credit_type,operation_tax_rate,

            credit_id,
            settlement_name,
            credit_name,
            sum(income_no_tax) income_no_tax,
            sum(tax_amount) tax_amount,
            sum(receivable_amount)receivable_amount

        FROM
            st_receivable_detail_data

         where  1=1

		<if test="accountState != null and accountState != ''" >
			and account_state = #{accountState}
		</if>

		<if test="accountingOrderSn != null and accountingOrderSn != ''" >
			and accounting_order_sn = #{accountingOrderSn}
		</if>

        GROUP BY DATE_FORMAT(finish_time,'%Y-%m-%d'),settlement_id,service_no,credit_type,operation_tax_rate


	</select>

	<!--应收汇总报表-->
	<select id="getReceivableStatisticsPageVoList" resultType="cn.superdesk.uniorder.dto.vo._statistics.ReceivableStatisticsPageVo">

		SELECT * from
		(

			SELECT
			settlement_id,
			settlement_name,
			operation_tax_rate,
			sum(income_no_tax) income_no_tax,
			sum(tax_amount) tax_amount,
			sum(receivable_amount) receivable_amount
			FROM
			st_receivable_day_data t1
			WHERE
			1 = 1
		    and credit_type =1
		    and account_type in (1,2)

			<if test="startTime != null and startTime != ''">
				and t1.account_at &gt;= concat(#{startTime}, ' 00:00:00')
			</if>

			<if test="endTime != null and endTime != ''">
				and t1.account_at &lt;= concat(#{endTime}, ' 23:59:59')
			</if>

			<if test="settlementId != null and settlementId != ''" >
				and settlement_id = #{settlementId}
			</if>

			<if test="serviceNo != null and serviceNo != ''" >
				and t1.service_no = #{serviceNo}
			</if>

			GROUP BY
			settlement_id,
			operation_tax_rate

			UNION

			SELECT
			settlement_id,
			'小计',
			999 operation_tax_rate,
			sum(income_no_tax) income_no_tax,
			sum(tax_amount) tax_amount,
			sum(receivable_amount) receivable_amount
			FROM
			st_receivable_day_data t1
			WHERE
			1 = 1
		    and credit_type =1
		    and account_type in (1,2)

			<if test="startTime != null and startTime != ''">
				and t1.account_at &gt;= concat(#{startTime}, ' 00:00:00')
			</if>

			<if test="endTime != null and endTime != ''">
				and t1.account_at &lt;= concat(#{endTime}, ' 23:59:59')
			</if>

			<if test="settlementId != null and settlementId != ''" >
				and t1.settlement_id = #{settlementId}
			</if>

			<if test="serviceNo != null and serviceNo != ''" >
				and t1.service_no = #{serviceNo}
			</if>

			GROUP BY
			settlement_id

		    UNION

		    <!--零星客户-->
			SELECT
		     -1 settlement_id,
			'零星客户' settlement_name,
			operation_tax_rate,
			sum(income_no_tax) income_no_tax,
			sum(tax_amount) tax_amount,
			sum(receivable_amount) receivable_amount
			FROM
			st_receivable_day_data t1
			WHERE
			1 = 1
			and credit_type =0
		    and account_type in (1,2)

			<if test="startTime != null and startTime != ''">
				and t1.account_at &gt;= concat(#{startTime}, ' 00:00:00')
			</if>

			<if test="endTime != null and endTime != ''">
				and t1.account_at &lt;= concat(#{endTime}, ' 23:59:59')
			</if>

			<if test="settlementId != null and settlementId != ''" >
				and t1.settlement_id = #{settlementId}
			</if>

			<if test="serviceNo != null and serviceNo != ''" >
				and t1.service_no = #{serviceNo}
			</if>

			GROUP BY
			operation_tax_rate

		    UNION


		    <!--零星客户小计-->
			SELECT
			-1 settlement_id,
			'小计' settlement_name,
			999 operation_tax_rate,
			sum(income_no_tax) income_no_tax,
			sum(tax_amount) tax_amount,
			sum(receivable_amount) receivable_amount
			FROM
			st_receivable_day_data t1
			WHERE
			1 = 1
			and credit_type =0
		    and account_type in (1,2)

			<if test="startTime != null and startTime != ''">
				and t1.account_at &gt;= concat(#{startTime}, ' 00:00:00')
			</if>

			<if test="endTime != null and endTime != ''">
				and t1.account_at &lt;= concat(#{endTime}, ' 23:59:59')
			</if>

			<if test="settlementId != null and settlementId != ''" >
				and t1.settlement_id = #{settlementId}
			</if>

			<if test="serviceNo != null and serviceNo != ''" >
				and t1.service_no = #{serviceNo}
			</if>

			GROUP BY
			credit_type

		) t
		where 1=1

		<if test="listSettlementId != null and listSettlementId.size() &gt; 0">
			and settlement_id in (
			<foreach collection="listSettlementId" index="index" item="item" separator=",">
				#{item}
			</foreach>
			)
		</if>


		<!--ORDER BY t.settlement_id,t.operation_tax_rate-->
		ORDER BY
		field(t.settlement_id,-1),t.settlement_id,	t.operation_tax_rate


	</select>

	<!--获得应收报表分页后的settlementId-->
	<select id="getReceivableStatisticsPageLimitSettlementId" resultType="java.lang.Long">

		SELECT
		settlement_id
		FROM
		st_receivable_day_data t1
		WHERE
		1 = 1
		<if test="startTime != null and startTime != ''">
			and t1.account_at &gt;= concat(#{startTime}, ' 00:00:00')
		</if>

		<if test="endTime != null and endTime != ''">
			and t1.account_at &lt;= concat(#{endTime}, ' 23:59:59')
		</if>

		<if test="settlementId != null and settlementId != ''" >
			and t1.settlement_id = #{settlementId}
		</if>

		<if test="serviceNo != null and serviceNo != ''" >
			and t1.service_no = #{serviceNo}
		</if>

		GROUP BY 		settlement_id


	</select>



	<!--查询应收报表总页数-->
	<select id="getReceivableStatisticsTotalPageNum" resultType="java.lang.Integer">

		SELECT count(*) totalNum from
		(
			SELECT
			settlement_id
			FROM
			st_receivable_day_data t1
			WHERE
			1 = 1
			<if test="startTime != null and startTime != ''">
				and t1.account_at &gt;= concat(#{startTime}, ' 00:00:00')
			</if>

			<if test="endTime != null and endTime != ''">
				and t1.account_at &lt;= concat(#{endTime}, ' 23:59:59')
			</if>

			<if test="settlementId != null and settlementId != ''" >
				and t1.settlement_id = #{settlementId}
			</if>

			<if test="serviceNo != null and serviceNo != ''" >
				and t1.service_no = #{serviceNo}
			</if>

			GROUP BY 		settlement_id
		)t



	</select>


	<!--查询应收报表合计Data-->
	<select id="getReceivableStatisticsTotalData" resultType="java.util.HashMap">

		SELECT
		sum(income_no_tax) incomeNoTax,
		sum(tax_amount) taxAmount,
		sum(receivable_amount) receivableAmount
		FROM
		st_receivable_day_data t1
		WHERE
		1 = 1
		<if test="startTime != null and startTime != ''">
			and t1.account_at &gt;= concat(#{startTime}, ' 00:00:00')
		</if>

		<if test="endTime != null and endTime != ''">
			and t1.account_at &lt;= concat(#{endTime}, ' 23:59:59')
		</if>

		<if test="settlementId != null and settlementId != ''" >
			and t1.settlement_id = #{settlementId}
		</if>

		<if test="serviceNo != null and serviceNo != ''" >
			and t1.service_no = #{serviceNo}
		</if>


	</select>

	<select id="getReceivableStatisticsDayDataByExcel" resultType="cn.superdesk.uniorder.dto.vo._statistics.ReceivableStatisticsDayDataByExcelVo">

		SELECT
		   *,
		   CASE WHEN t1.account_way =1 then '系统入账' else '手工记账' end accountWayName,
		   CASE WHEN t1.account_type =1 then '正常入账' WHEN t1.account_type =2 then '撤销入账' WHEN t1.account_type =3 then '冲销账款' else '' end accountTypeName,
		DATE_FORMAT(account_at,'%Y-%m-%d') formatAccountAt,
		    operation_tax_rate/100 format_operation_tax_rate
		FROM
		st_receivable_day_data t1
		WHERE
		1 = 1
		<if test="startTime != null and startTime != ''">
			and t1.account_at &gt;= concat(#{startTime}, ' 00:00:00')
		</if>

		<if test="endTime != null and endTime != ''">
			and t1.account_at &lt;= concat(#{endTime}, ' 23:59:59')
		</if>

		<if test="settlementId != null and settlementId != ''" >
			and t1.settlement_id = #{settlementId}
		</if>

		<if test="serviceNo != null and serviceNo != ''" >
			and t1.service_no = #{serviceNo}
		</if>


	</select>


	<select id="getReceivableStatisticsDetailDataByExcel" resultType="cn.superdesk.uniorder.dto.vo._statistics.ReceivableStatisticsDetailDataByExcelVo">

		SELECT *,
		  CASE WHEN t1.account_state =0 then '未入账' WHEN t1.account_state =1 then '已入账' WHEN t1.account_state =3 then '撤销入账'  else '已撤销' end accountStateName,
		  CASE WHEN t1.accounting_state =1 then '已确认' else '已取消' end accountingTypeName,
		  CASE WHEN t1.charge_status =1 then '' WHEN t1.charge_status =2 then '已冲销'  else '已撤销' end chargeStatusName,
		 operation_tax_rate/100 format_operation_tax_rate
		from  st_receivable_detail_data t1
		where 1=1
		<!--and t1.account_state =1-->

		<if test="startTime != null and startTime != ''">
			and t1.account_at &gt;= concat(#{startTime}, ' 00:00:00')
		</if>

		<if test="endTime != null and endTime != ''">
			and t1.account_at &lt;= concat(#{endTime}, ' 23:59:59')
		</if>

		<if test="settlementId != null and settlementId != ''" >
			and t1.settlement_id = #{settlementId}
		</if>

		<if test="serviceNo != null and serviceNo != ''" >
			and t1.service_no = #{serviceNo}
		</if>

		ORDER BY created_at


	</select>
	<select id="getSettlementIdsPage" resultType="java.lang.Long">

			SELECT
			settlement_id
			FROM
			st_receivable_day_data t1
			WHERE
			1 = 1
			<if test="startDate != null and startDate != ''">
				and t1.account_at &gt;= concat(#{startDate}, ' 00:00:00')
			</if>

			<if test="endDate != null and endDate != ''">
				and t1.account_at &lt;= concat(#{endDate}, ' 23:59:59')
			</if>

			<if test="settlementId != null and settlementId != ''" >
				and t1.settlement_id = #{settlementId}
			</if>
			<if test="serviceNo != null and serviceNo != ''">
				and t1.service_no = #{serviceNo}
			</if>
			GROUP BY 		settlement_id


	</select>
	<select id="getReceivableBalanceVOList"
			resultType="cn.superdesk.uniorder.dto.vo._statistics.ReceivableBalanceDetail">
		SELECT
		    t.settlementName,
		    t.serviceNoName,
		    t.receivableAmount,
		    t.actualReceipt,
		    (t.receivableAmount - t.actualReceipt) as endingBalance
		FROM
		    (
		    SELECT t1.settlement_id                                            as settlement_id,
					 t2.enterprise_name                                          as settlementName,
					 t1.service_no_name                                          as serviceNoName,
					 sum(IF(t1.account_type in (1, 2), t1.receivable_amount, 0)) as receivableAmount,
					 sum(IF(t1.account_type = 3, t1.receivable_amount, 0))       as actualReceipt
			  FROM st_receivable_day_data t1
					   left join sys_enterprise t2 on t1.settlement_id = t2.enterprise_id
			  WHERE t1.credit_type = 1
			<if test="serviceNo != null and serviceNo !='' ">
				and t1.service_no = #{serviceNo}
			</if>
			<if test="startDate != null and startDate != ''">
				and t1.account_at &gt;= concat(#{startDate}, ' 00:00:00')
			</if>

			<if test="endDate != null and endDate != ''">
				and t1.account_at &lt;= concat(#{endDate}, ' 23:59:59')
			</if>

			<if test="settlementId != null and settlementId != ''" >
				and t1.settlement_id = #{settlementId}
			</if>
			  GROUP BY t1.settlement_id
			  UNION
			  SELECT 1000000                                                  settlement_id,
					 '零星客户'                                                   settlementName,
					 '侨城汇福利商城'                                                serviceNoName,
					 sum(IF(account_type in (1, 2), receivable_amount, 0)) as receivableAmount,
					 sum(IF(account_type in (1, 2), receivable_amount, 0)) as actualReceipt
			  FROM st_receivable_day_data
			  WHERE credit_type = 0
			<if test="serviceNo != null and serviceNo !='' ">
				and service_no = #{serviceNo}
			</if>
			<if test="startDate != null and startDate != ''">
				and account_at &gt;= concat(#{startDate}, ' 00:00:00')
			</if>
			<if test="endDate != null and endDate != ''">
				and account_at &lt;= concat(#{endDate}, ' 23:59:59')
			</if>
			<if test="settlementId != null and settlementId != ''" >
				and settlement_id = #{settlementId}
			</if>
			  group by credit_type
			) t
		where 1=1

		<if test="settlementIds != null and settlementIds.size() > 0">
			and settlement_id in (
			<foreach collection="settlementIds" index="index" item="item" separator=",">
				#{item}
			</foreach>
			)
		</if>
		order by endingBalance desc
	</select>
	<select id="selectReceivableBalanceByCondition"
			resultType="cn.superdesk.uniorder.dto.vo._statistics.ReceivableStatisticsDetailDataByExcelVo">
		SELECT
			t1.id,
			t1.pertain_month,
			date_format(t1.account_at, '%Y-%m-%d')     as accountAtStr,
			case t1.account_type
			when 1 then '正常入账'
			when 2 then '撤销入账'
			when 3 then '冲销账款'
			else '未知类型' end                     as accountTypeName,
			case t1.account_way
			when 0 then '系统入账'
			when 1 then '手工记账'
			else '未知' end                       as accountWayName,
			t1.accounting_order_sn as accountingOrderSn,
			t1.settlement_id as settlementId,
			t2.enterprise_name as settlementName,
			t1.service_no as serviceNo,
			t1.credit_name as creditName,
			format(t1.operation_tax_rate / 100, 2)     as operationTaxRate,
			t1.income_no_tax as incomeNoTax,
			t1.tax_amount as taxAmount,
			t1.receivable_amount as receivableAmount,
			t1.create_user_name as createUserName,
			date_format(t1.created_at, '%Y-%m-%d %T')  as createTimeStr,
			t1.update_user_name as updateUserName,
			date_format(t1.update_time, '%Y-%m-%d %T') as updateTimeStr
			from st_receivable_day_data t1
			left join sys_enterprise t2 on t1.settlement_id = t2.enterprise_id
		where 1=1
		<if test="queryType == 1 ">
			and t1.account_type in (1,2)
		</if>

		<if test="queryType == 2">
			and t1.account_type = 3
		</if>

		<if test="customerType == 1">
			and t1.credit_type = 1
		</if>
		<if test="customerType == 2">
		  	and t1.credit_type = 0
		</if>

		<if test="startDate != null and startDate != ''">
			and t1.account_at &gt;= concat(#{startTime}, ' 00:00:00')
		</if>

		<if test="endDate != null and endDate != ''">
			and t1.account_at &lt;= concat(#{endTime}, ' 23:59:59')
		</if>

		<if test="settlementId != null and settlementId != ''" >
			and t1.settlement_id = #{settlementId}
		</if>

		<if test="serviceNo != null and serviceNo != ''" >
			and t1.service_no = #{serviceNo}
		</if>

		<if test="settlementIds != null and settlementIds.size() > 0">
			and settlement_id in (
			<foreach collection="settlementIds" index="index" item="item" separator=",">
				#{item}
			</foreach>
			)
		</if>
		ORDER BY t1.account_at asc , t1.created_at asc


	</select>

</mapper>