<?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.twerp.hm.TbDesktop经营主题Mapper">

    <sql id="sqlInnerJoinUserRoleData">
        <![CDATA[
	INNER JOIN (
SELECT
	u.UserCode,
	d.CommID,
	c.OrganCode,
	c.CommName,
	o.OrganName
FROM
	[HM_wygl_new_1853].[dbo].[Tb_Sys_User] AS u
	INNER JOIN [HM_wygl_new_1853].[dbo].[Tb_Sys_UserRole] AS r ON ( r.UserCode = u.UserCode AND u.IsDelete = 0 )
	INNER JOIN [HM_wygl_new_1853].[dbo].[Tb_Sys_RoleData] AS d ON ( d.RoleCode = r.RoleCode AND d.CommID > 0 )
	INNER JOIN [HM_wygl_new_1853].[dbo].[Tb_HSPR_Community] AS c ON ( c.CommID = d.CommID AND c.isDelete = 0 )
	INNER JOIN [HM_wygl_new_1853].[dbo].[Tb_Sys_Organ] AS o ON ( o.OrganCode = c.OrganCode AND o.isDelete = 0 )
WHERE
	u.UserCode = #{userCode} and u.rentid=1853 and r.rentid=1853 and d.rentid=1853 and o.rentid=1853 and c.rentid=1853
	) AS t ON ( t.OrganCode = m.OrganCode AND t.CommID = m.CommID )
        ]]>
    </sql>


    <select id="findByUserSysOrgan"
            parameterType="map"
            resultType="cn.com.countrygarden.bi.main.model.twerp.hm.TbDesktop经营主题">
        <![CDATA[
SELECT
	本年累计追缴率 ,本年综合收费率 ,本年欠费追缴率,
	ISNULL( recoveryrate, 0.00 ) AS recoveryrate,
	ISNULL( chargerate, 0.00 ) AS chargerate,
	ISNULL( arrearsrecoveryrate, 0.00 ) AS arrearsrecoveryrate
FROM
	Tb_DeskTop_经营主题 t
	LEFT JOIN [HM_wygl_new_1853].[dbo].Tb_HSPR_CoreIndex i ON ( t.organcode = i.organcode )
WHERE
	t.OrganCode = #{organCode}
	AND t.CommID = #{commID}
        ]]>
    </select>


    <sql id="sqlSumHomeMainIncome">
        <![CDATA[
SELECT
	isnull( SUM (本年理论应收), 0 ) AS 本年理论应收,
	isnull( SUM (本年累计实收), 0 ) AS 本年累计实收,
	isnull( SUM (年初往年欠费), 0 ) AS 年初往年欠费,
	isnull( SUM (本年累计追缴金额), 0 ) AS 本年累计追缴金额,
	isnull( SUM (车位收入), 0 ) AS 车位收入,
	isnull( SUM (本月应收), 0 ) AS 本月应收,
	isnull( SUM (本月实收), 0 ) AS 本月实收,
	isnull( SUM (车位数量 ), 0 ) AS 车位数量
FROM
	tb_desktop_经营主题 AS m
        ]]>
    </sql>
    <select id="sumHomeMainIncomeByUserCode"
            fetchSize="1"
            parameterType="map"
            resultType="cn.com.countrygarden.bi.main.model.twerp.hm.TbDesktop经营主题">

        <include refid="sqlSumHomeMainIncome"/>
        <include refid="sqlInnerJoinUserRoleData"/>
    </select>
    <select id="sumHomeMainIncomeByUserCodeAndOrganCode"
            fetchSize="1"
            parameterType="map"
            resultType="cn.com.countrygarden.bi.main.model.twerp.hm.TbDesktop经营主题">

        <include refid="sqlSumHomeMainIncome"/>
        <include refid="sqlInnerJoinUserRoleData"/>
        <![CDATA[
        WHERE m.OrganCode = #{organCode}
        ]]>
    </select>
    <select id="sumHomeMainIncomeByUserCodeAndOrganCodeAndCommID"
            fetchSize="1"
            parameterType="map"
            resultType="cn.com.countrygarden.bi.main.model.twerp.hm.TbDesktop经营主题">

        <include refid="sqlSumHomeMainIncome"/>
        <include refid="sqlInnerJoinUserRoleData"/>
        <![CDATA[
        WHERE m.OrganCode = #{organCode} AND m.CommID = #{commID}
        ]]>
    </select>


    <!-- 区域收费率排行 -->
    <select id="rankingChargeRate"
            parameterType="Map"
            resultType="cn.com.countrygarden.bi.main.model.twerp.hm.TbDesktop经营主题">
        <choose>
        <when test="isHeadquarters != 1">
        <![CDATA[
SELECT
	m.CommName AS OrganName,
	m.本年综合收费率,
	m.本年已收收费率,
	m.本年应收未收收费率,
	m.本年空置收费率,
	m.OrganManagePhoto
FROM
	Tb_DeskTop_经营主题 AS m
	INNER JOIN (
SELECT SUBSTRING
	( d.OrganCode, 1, 4 ) AS OrganCode
FROM
	[HM_wygl_new_1853].[dbo].[Tb_Sys_User] AS u
	INNER JOIN [HM_wygl_new_1853].[dbo].[Tb_Sys_UserRole] AS r ON ( r.UserCode = u.UserCode AND u.IsDelete = 0 )
	INNER JOIN [HM_wygl_new_1853].[dbo].[Tb_Sys_RoleData] AS d ON ( d.RoleCode = r.RoleCode AND d.OrganCode != '01' )
WHERE
	u.UserCode = #{userCode}
GROUP BY
	SUBSTRING ( d.OrganCode, 1, 4 )
	) AS t ON ( t.OrganCode = m.OrganCode AND m.CommID <> '-1' )
WHERE
	m.OrganCode <> '01'
ORDER BY
	m.本年综合收费率 DESC
        ]]>
        </when>
        <otherwise>
            <![CDATA[
SELECT
	m.OrganName,
	m.本年综合收费率,
	m.本年已收收费率,
	m.本年应收未收收费率,
	m.本年空置收费率,
	m.OrganManagePhoto
FROM
	Tb_DeskTop_经营主题 AS m
	INNER JOIN (
SELECT SUBSTRING
	( d.OrganCode, 1, 4 ) AS OrganCode
FROM
	[HM_wygl_new_1853].[dbo].[Tb_Sys_User] AS u
	INNER JOIN [HM_wygl_new_1853].[dbo].[Tb_Sys_UserRole] AS r ON ( r.UserCode = u.UserCode AND u.IsDelete = 0 )
	INNER JOIN [HM_wygl_new_1853].[dbo].[Tb_Sys_RoleData] AS d ON ( d.RoleCode = r.RoleCode AND d.OrganCode != '01' )
WHERE
	u.UserCode = #{userCode}
GROUP BY
	SUBSTRING ( d.OrganCode, 1, 4 )
	) AS t ON ( t.OrganCode = m.OrganCode AND m.CommID = '-1' )
WHERE
	m.OrganCode <> '01'
ORDER BY
	m.本年综合收费率 DESC
        ]]>
        </otherwise>
        </choose>
    </select>

    <!-- 区域追缴率排行 -->
    <select id="rankingChaseRate"
            parameterType="Map"
            resultType="cn.com.countrygarden.bi.main.model.twerp.hm.TbDesktop经营主题">
        <choose>
        <when test="isHeadquarters != 1">
            <![CDATA[
SELECT
	m.CommName AS OrganName,
	m.本年累计追缴率,
	m.本年累计追缴已收追缴率,
	m.本年累计追缴应收未收追缴率,
	m.本年累计追缴空置追缴率,
	m.OrganManagePhoto
FROM
	Tb_DeskTop_经营主题 AS m
	INNER JOIN (
SELECT SUBSTRING
	( d.OrganCode, 1, 4 ) AS OrganCode
FROM
	[HM_wygl_new_1853].[dbo].[Tb_Sys_User] AS u
	INNER JOIN [HM_wygl_new_1853].[dbo].[Tb_Sys_UserRole] AS r ON ( r.UserCode = u.UserCode AND u.IsDelete = 0 )
	INNER JOIN [HM_wygl_new_1853].[dbo].[Tb_Sys_RoleData] AS d ON ( d.RoleCode = r.RoleCode AND d.OrganCode != '01' )
WHERE
	u.UserCode = #{userCode}
GROUP BY
	SUBSTRING ( d.OrganCode, 1, 4 )
	) AS t ON ( t.OrganCode = m.OrganCode AND m.CommID <> '-1' )
WHERE
	m.OrganCode <> '01'
ORDER BY
	m.本年累计追缴率 DESC
        ]]>
        </when>
        <otherwise>
            <![CDATA[
SELECT
	m.OrganName,
	m.本年累计追缴率,
	m.本年累计追缴已收追缴率,
	m.本年累计追缴应收未收追缴率,
	m.本年累计追缴空置追缴率,
	m.OrganManagePhoto
FROM
	Tb_DeskTop_经营主题 AS m
	INNER JOIN (
SELECT SUBSTRING
	( d.OrganCode, 1, 4 ) AS OrganCode
FROM
	[HM_wygl_new_1853].[dbo].[Tb_Sys_User] AS u
	INNER JOIN [HM_wygl_new_1853].[dbo].[Tb_Sys_UserRole] AS r ON ( r.UserCode = u.UserCode AND u.IsDelete = 0 )
	INNER JOIN [HM_wygl_new_1853].[dbo].[Tb_Sys_RoleData] AS d ON ( d.RoleCode = r.RoleCode AND d.OrganCode != '01' )
WHERE
	u.UserCode = #{userCode}
GROUP BY
	SUBSTRING ( d.OrganCode, 1, 4 )
	) AS t ON ( t.OrganCode = m.OrganCode AND m.CommID = '-1' )
WHERE
	m.OrganCode <> '01'
ORDER BY
	m.本年累计追缴率 DESC
        ]]>
        </otherwise>
        </choose>
    </select>

</mapper>