<?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.com.countrygarden.bi.main.dao.dup.WeekIncomeMapper">
	
	<resultMap type="cn.com.countrygarden.bi.main.model.dup.WeekIncome" id="WeekIncomeMap">
		
		<result column="CommID" property="commID"/>
		<result column="CommName" property="commName"/>
		<result column="OrganName" property="organName"/>
		<result column="OrganCode" property="organCode"/>
		<result column="ss_by_tax_amount" property="receipts"/>
		<result column="ss_by_ntax_amount" property="weekAmount"/>
		<result column="NCSystemNumber" property="NCSystemNumber"/>
		
	</resultMap>
	
	<!-- 查询项目名称 -->
	 <select id="findAllProject" resultMap="WeekIncomeMap">
		 
		 SELECT
			 	c.CommID,
				c.CommName,
				a.OrganName,
				c.OrganCode
			FROM
				[HM_wygl_new_1853].[dbo].[Tb_HSPR_Community] c
				LEFT JOIN
				[HM_wygl_new_1853].[dbo].[Tb_Sys_Organ] a 
				ON
				a.OrganCode = c.OrganCode and a.rentid=c.rentid
				where c.rentid=1853
	</select>
	
	<!-- 查询区域名称 -->
	 <select id="findAllOrgan" resultMap="WeekIncomeMap">
		 
		 SELECT
			 	
				a.OrganName,
				a.OrganCode
			FROM
				
				[HM_wygl_new_1853].[dbo].[Tb_Sys_Organ] a 
			WHERE 
				 a.isDelete = 0 and a.rentid=1853
				 
	
	</select>
	
	<select id="findOrgan" resultMap="WeekIncomeMap">
		SELECT c.OrganCode,c.CommID,c.CommName,a.OrganName
		FROM 
		[HM_wygl_new_1853].[dbo].[Tb_HSPR_Community] c
		LEFT JOIN
		[HM_wygl_new_1853].[dbo].[Tb_Sys_Organ] a 
		ON
		a.OrganCode = c.OrganCode
		WHERE  a.rentid=1853 and c.rentid=1853 and c.CommID IN
		<foreach collection="array" item="commId" index="index" open="(" close=")" separator=",">
			#{commId}
		</foreach>
		
	</select>
	
		
	<!-- 查询周收入统计 -->
   <select id="findWeekIncome" parameterType="java.util.Map" resultMap="WeekIncomeMap">
	;WITH C AS (
	SELECT CommID,NCSystemNumber
		,ROW_NUMBER() OVER (PARTITION BY CommID ORDER BY CreateTime DESC) rn
	FROM [HM_wygl_new_1853].[dbo].[Tb_HSPR_BasicInfo]
	WHERE COALESCE(IsDelete,0) = 0
)
SELECT A.OrganCode,B.OrganName,A.CommID,A.CommName,C.NCSystemNumber,
		ROUND(SUM(AA.ss_by_tax_amount),2) AS ss_by_tax_amount,
		ROUND(SUM(AA.ss_by_ntax_amount),2) AS ss_by_ntax_amount
	FROM  [HM_wygl_new_1853].[dbo].[Tb_HSPR_Community] A
	LEFT JOIN  [HM_wygl_new_1853].[dbo].[Tb_Sys_Organ] B ON B.OrganCode=A.OrganCode
	LEFT JOIN C ON C.CommID=A.CommID AND C.rn = 1
	LEFT JOIN (
		SELECT AA.CommID,
			ROUND(SUM(COALESCE(AA.tax_amount,0)-COALESCE(fees.CommisionAmount,0)),2)  AS ss_by_tax_amount,
			ROUND(SUM((COALESCE(AA.tax_amount,0)-COALESCE(fees.CommisionAmount,0))/(1+AA.tax) ),2) AS ss_by_ntax_amount
		FROM (		
			SELECT A.CommID,A.FeesID,B.增值税率 AS tax,
				SUM(A.DealAmount)*-1 AS tax_amount
			FROM  [Rts_FeesSerial].[dbo].[Tb_HSPR_FeesSerial] A
			LEFT JOIN  [HM_wygl_new_1853].[dbo].[Tb_HSPR_FeesProcessLog] B 
			ON A.FeesID=B.FeesID 
			<if test='lists != null and lists.size > 0'>
				  AND B.CommID IN
				  <foreach collection="lists" index="index" open="(" close=")" separator="," item="CommID">
				     #{CommID}
				  </foreach>
				</if>
			WHERE  A.CorpCostID IN(100000586,100000589,100000588,100000585,100000587,100000584)
				<if test='lists != null and lists.size > 0'>
				  AND A.CommID IN
				  <foreach collection="lists" index="index" open="(" close=")" separator="," item="CommID">
				     #{CommID}
				  </foreach>
				</if>
				AND A.DealDate <![CDATA[>=]]> #{startDate} AND A.DealDate <![CDATA[<]]> #{endDate} 
				AND A.BussType IN('实收','撤销实收') AND A.ChargesModeFeesDueDate <![CDATA[>=]]> #{startDate} AND A.ChargesModeFeesDueDate <![CDATA[<]]> #{endDate} 
			GROUP BY A.CommID,A.FeesID,B.增值税率
		) AA
		LEFT JOIN (
				SELECT FeesID,CommisionAmount FROM  [HM_wygl_new_1853].[dbo].[Tb_HSPR_Fees] F 
				WHERE 
				<if test='lists != null and lists.size > 0'>
				  F.CommID IN
				  <foreach collection="lists" index="index" open="(" close=")" separator="," item="CommID">
				     #{CommID}
				  </foreach>
				</if> 
				AND EXISTS(SELECT A.FeesID FROM  [Rts_FeesSerial].[dbo].[Tb_HSPR_FeesSerial] A
					WHERE F.FeesID=A.FeesID 
					<if test='lists != null and lists.size > 0'>
					  AND A.CommID IN
					  <foreach collection="lists" index="index" open="(" close=")" separator="," item="CommID">
					     #{CommID}
					  </foreach>
					</if>
						
					AND A.CorpCostID  IN(100000586,100000589,100000588,100000585,100000587,100000584) 
					AND A.DealDate <![CDATA[>=]]> #{startDate} AND A.DealDate <![CDATA[<]]> #{endDate} )
			) fees ON fees.FeesID=AA.FeesID AND AA.tax_amount<![CDATA[<>]]> 0
		GROUP BY AA.CommID
		UNION ALL
		SELECT AA.CommID,
			ROUND(SUM(fees.CommisionAmount),2) AS ss_by_tax_amount,
			ROUND(SUM( (COALESCE(fees.CommisionAmount,0))/(1+AA.tax)),2) AS ss_by_ntax_amount
		FROM (		
			SELECT A.CommID,A.FeesID,B.增值税率 AS tax,
				SUM(A.DealAmount)*-1 AS tax_amount
			FROM  [Rts_FeesSerial].[dbo].[Tb_HSPR_FeesSerial] A
			LEFT JOIN  [HM_wygl_new_1853].[dbo].[Tb_HSPR_FeesProcessLog] B 
			ON A.FeesID=B.FeesID 
			<if test='lists != null and lists.size > 0'>
			  AND B.CommID IN
			  <foreach collection="lists" index="index" open="(" close=")" separator="," item="CommID">
			     #{CommID}
			  </foreach>
			</if>
			WHERE A.CorpCostID IN(100000586,100000589,100000588,100000585,100000587,100000584)
					<if test='lists != null and lists.size > 0'>
				  AND A.CommID IN
				  <foreach collection="lists" index="index" open="(" close=")" separator="," item="CommID">
				     #{CommID}
				  </foreach>
				</if>
				
				AND A.DealDate <![CDATA[>=]]> #{startDate} AND A.DealDate <![CDATA[<]]> #{endDate}   
				AND A.BussType IN('实收','撤销实收') AND A.ChargesModeFeesDueDate <![CDATA[>=]]> #{startDate} AND A.ChargesModeFeesDueDate <![CDATA[<]]> #{endDate} 
			GROUP BY A.CommID,A.FeesID,B.增值税率
		) AA
		LEFT JOIN (SELECT FeesID,CommisionCostID,CommisionAmount FROM  [HM_wygl_new_1853].[dbo].[Tb_HSPR_Fees] F 
		
		WHERE
		<if test='lists != null and lists.size > 0'>
		   F.CommID IN
		  <foreach collection="lists" index="index" open="(" close=")" separator="," item="CommID">
		     #{CommID}
		  </foreach>
		</if>  
		AND
		EXISTS(SELECT A.FeesID FROM  [Rts_FeesSerial].[dbo].[Tb_HSPR_FeesSerial] A 
		WHERE  F.FeesID=A.FeesID
			<if test='lists != null and lists.size > 0'>
			  AND A.CommID IN
			  <foreach collection="lists" index="index" open="(" close=")" separator="," item="CommID">
			     #{CommID}
			  </foreach>
			</if> 
			AND A.CorpCostID IN(100000586,100000589,100000588,100000585,100000587,100000584) 
			AND A.DealDate <![CDATA[>=]]> #{startDate} AND A.DealDate <![CDATA[<]]> #{endDate}  )
			) fees ON fees.FeesID=AA.FeesID AND AA.tax_amount<![CDATA[<>]]>0
		LEFT JOIN  [HM_wygl_new_1853].[dbo].[Tb_HSPR_CostItem] costItem ON costItem.CostID=fees.CommisionCostID AND costItem.CostID>0
		WHERE costItem.CorpCostID IN(100000586,100000589,100000588,100000585,100000587,100000584)
		GROUP BY AA.CommID
		UNION ALL
		SELECT A.CommID,
			0 AS ss_by_tax_amount,
			SUM(B.LateFeeAmount) AS ss_by_ntax_amount
		FROM  [Rts_FeesSerial].[dbo].[Tb_HSPR_FeesSerial] A
		LEFT JOIN  [HM_wygl_new_1853].[dbo].[Tb_HSPR_FeesDetail] B 
		ON A.FeesID=B.FeesID 
		<if test='lists != null and lists.size > 0'>
		  AND B.CommID IN
		  <foreach collection="lists" index="index" open="(" close=")" separator="," item="CommID">
		     #{CommID}
		  </foreach>
		</if>
		WHERE  A.CorpCostID IN(100000586,100000589,100000588,100000585,100000587,100000584)
			AND A.DealDate <![CDATA[>=]]> #{startDate} AND A.DealDate <![CDATA[<]]> #{endDate}  AND A.BussType IN('实收','撤销实收')
			AND A.ChargesModeFeesDueDate <![CDATA[>=]]> #{startDate} AND A.ChargesModeFeesDueDate <![CDATA[<]]> #{endDate} 
			<if test='lists != null and lists.size > 0'>
			  AND A.CommID IN
			  <foreach collection="lists" index="index" open="(" close=")" separator="," item="CommID">
			     #{CommID}
			  </foreach>
			</if> 
		GROUP BY A.CommID
	) AA ON AA.CommID=A.CommID
	WHERE  A.IsDelete=0 
	<if test='lists != null and lists.size > 0'>
	  AND A.CommID IN
	  <foreach collection="lists" index="index" open="(" close=")" separator="," item="CommID">
	     #{CommID}
	  </foreach>
	</if> 
	GROUP BY A.OrganCode,B.OrganName,A.CommID,A.CommName,C.NCSystemNumber		          
   </select>

</mapper>





 