<?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.TbDesktopOperationSubjectMapper">
    <sql id="sqlSumAll">
    <![CDATA[
        SELECT
            ISNULL( SUM (s.面积), 0 ) Area,
            ISNULL( SUM (s.业主数), 0 ) Owner,
            ISNULL( SUM (s.住宅数), 0 ) House,
            ISNULL( SUM (s.车位), 0 ) Parking,
            ISNULL( SUM (s.商铺), 0 ) Business,
            ISNULL( SUM (s.楼宇数), 0 ) Building,
            ISNULL( SUM (s.房屋数), 0 ) Room,
            ISNULL( SUM (s.客户数), 0 ) Customer,
            ISNULL( SUM (s.套内面积), 0 ) InnerArea,
            ISNULL( SUM (s.公摊面积), 0 ) ShareArea,
            ISNULL( SUM (s.花园面积), 0 ) GardenArea,
            ISNULL( SUM (s.商铺面积), 0 ) BusinessArea,
            ISNULL( SUM (s.地下室面积), 0 ) BasementArea,
            ISNULL( SUM (s.住宅面积), 0 ) HouseArea,
            ISNULL( SUM (s.本月增加面积), 0 ) MonthNewArea,
            ISNULL( SUM (s.本月增加业主数), 0 ) MonthNewOwner,
            ISNULL( SUM (s.本月增加住宅数), 0 ) MonthNewHouse,
            ISNULL( SUM (s.本月增加车位), 0 ) MonthNewParking,
            ISNULL( SUM (s.本月增加商铺), 0 ) MonthNewBusiness,
            ISNULL( SUM (s.本月增加楼宇数), 0 ) MonthNewBuilding,
            ISNULL( SUM (s.本月增加房屋数), 0 ) MonthNewRoom,
            ISNULL( SUM (s.本月增加客户数), 0 ) MonthNewCustomer
        FROM
            [Rts].[dbo].[Tb_DeskTop_Resource] AS s
        ]]>
    </sql>

    <sql id="sqlSumAreaByOrgan">
        <![CDATA[
        SELECT
            t.OrganName,
            SUM (s.面积) Area,
            SUM (s.车位) Parking,
            SUM (s.套内面积) InnerArea,
            SUM (s.公摊面积) ShareArea,
            SUM (s.花园面积) GardenArea,
            SUM (s.商铺面积) BusinessArea,
            SUM (s.地下室面积) BasementArea,
            SUM (s.住宅面积) HouseArea,
            SUM (s.住宅面积 + s.商铺面积 + s.地下室面积) Total
        FROM
            [Rts].[dbo].[Tb_DeskTop_Resource] AS s
        ]]>
    </sql>

    <sql id="sqlSumAreaByOrganGroupBy">
        <![CDATA[
        GROUP BY
            t.OrganName
        ORDER BY
            Total DESC
        ]]>
    </sql>

    <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}
	) AS t ON ( t.OrganCode = s.OrganCode AND t.CommID = s.CommID )
        ]]>
    </sql>

    <select id="sumAll"
            resultType="cn.com.countrygarden.bi.main.model.twerp.hm.TbDesktopOperationSubject">
        <include refid="sqlSumAll"/>
    </select>

    <select id="sumByUserCode"
            fetchSize="1"
            parameterType="string"
            resultType="cn.com.countrygarden.bi.main.model.twerp.hm.TbDesktopOperationSubject">
        <include refid="sqlSumAll"/>
        <include refid="sqlInnerJoinUserRoleData"/>
    </select>

    <select id="sumByUserCodeAndOrganCode"
            fetchSize="1"
            parameterType="map"
            resultType="cn.com.countrygarden.bi.main.model.twerp.hm.TbDesktopOperationSubject">
        <include refid="sqlSumAll"/>
        <include refid="sqlInnerJoinUserRoleData"/>
        <![CDATA[
          WHERE s.organcode = #{organCode}
        ]]>
    </select>

    <select id="sumByUserCodeAndOrganCodeAndCommID"
            fetchSize="1"
            parameterType="map"
            resultType="cn.com.countrygarden.bi.main.model.twerp.hm.TbDesktopOperationSubject">
        <include refid="sqlSumAll"/>
        <include refid="sqlInnerJoinUserRoleData"/>
        <![CDATA[
          WHERE s.organcode = #{organCode} AND s.CommID = #{commID}
        ]]>
    </select>


    <!-- 接管面积一览 -->
    <select id="sumByOrganByUserCode"
            parameterType="string"
            resultType="cn.com.countrygarden.bi.main.model.twerp.hm.TbDesktopOperationSubject">
        <include refid="sqlSumAreaByOrgan"/>
        <include refid="sqlInnerJoinUserRoleData"/>
        <include refid="sqlSumAreaByOrganGroupBy"/>
    </select>

    <select id="sumByOrganByUserCodeAndOrganCode"
            parameterType="map"
            resultType="cn.com.countrygarden.bi.main.model.twerp.hm.TbDesktopOperationSubject">
        <include refid="sqlSumAreaByOrgan"/>
        <include refid="sqlInnerJoinUserRoleData"/>
        <![CDATA[
          WHERE s.organcode = #{organCode}
        ]]>
        <include refid="sqlSumAreaByOrganGroupBy"/>
    </select>

    <select id="sumByOrganByOrganByUserCodeAndOrganCodeAndCommID"
            parameterType="map"
            resultType="cn.com.countrygarden.bi.main.model.twerp.hm.TbDesktopOperationSubject">
        <include refid="sqlSumAreaByOrgan"/>
        <include refid="sqlInnerJoinUserRoleData"/>
        <![CDATA[
          WHERE s.organcode = #{organCode} AND s.CommID = #{commID}
        ]]>
        <include refid="sqlSumAreaByOrganGroupBy"/>
    </select>
</mapper>