<?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.TbHsprCommunityMapper">
    <resultMap id="TbHsprCommunityResultMap" type="cn.com.countrygarden.bi.main.model.twerp.hm.TbHsprCommunity">
        <id column="CommID" jdbcType="INTEGER" property="commid"/>
        <result column="BranchID" jdbcType="INTEGER" property="branchid"/>
        <result column="CorpID" jdbcType="INTEGER" property="corpid"/>
        <result column="CommName" jdbcType="NVARCHAR" property="commname"/>
        <result column="CommAddress" jdbcType="NVARCHAR" property="commaddress"/>
        <result column="Province" jdbcType="NVARCHAR" property="province"/>
        <result column="City" jdbcType="NVARCHAR" property="city"/>
        <result column="Borough" jdbcType="NVARCHAR" property="borough"/>
        <result column="Street" jdbcType="NVARCHAR" property="street"/>
        <result column="GateSign" jdbcType="NVARCHAR" property="gatesign"/>
        <result column="CorpGroupCode" jdbcType="NVARCHAR" property="corpgroupcode"/>
        <result column="CorpRegionCode" jdbcType="VARCHAR" property="corpregioncode"/>
        <result column="CommSpell" jdbcType="NVARCHAR" property="commspell"/>
        <result column="RegDate" jdbcType="TIMESTAMP" property="regdate"/>
        <result column="IsDelete" jdbcType="SMALLINT" property="isdelete"/>
        <result column="OrganCode" jdbcType="NVARCHAR" property="organcode"/>
        <result column="UseBeginDate" jdbcType="TIMESTAMP" property="usebegindate"/>
        <result column="UseEndDate" jdbcType="TIMESTAMP" property="useenddate"/>
        <result column="CommKind" jdbcType="NVARCHAR" property="commkind"/>
        <result column="ManageTime" jdbcType="TIMESTAMP" property="managetime"/>
        <result column="ManageKind" jdbcType="NVARCHAR" property="managekind"/>
        <result column="CommSynchCode" jdbcType="CHAR" property="commsynchcode"/>
        <result column="SynchFlag" jdbcType="SMALLINT" property="synchflag"/>
        <result column="ContractBeginDate" jdbcType="TIMESTAMP" property="contractbegindate"/>
        <result column="ContractEndDate" jdbcType="TIMESTAMP" property="contractenddate"/>
        <result column="SysStartDate" jdbcType="TIMESTAMP" property="sysstartdate"/>
        <result column="SysLogDate" jdbcType="TIMESTAMP" property="syslogdate"/>
        <result column="IsFees" jdbcType="SMALLINT" property="isfees"/>
        <result column="Memo" jdbcType="NVARCHAR" property="memo"/>
        <result column="CommType" jdbcType="SMALLINT" property="commtype"/>
        <result column="Num" jdbcType="INTEGER" property="num"/>
        <result column="CommunityName" jdbcType="NVARCHAR" property="communityname"/>
        <result column="CommunityCode" jdbcType="NVARCHAR" property="communitycode"/>
        <result column="CRMCode" jdbcType="VARCHAR" property="crmcode"/>
        <result column="fdNo" jdbcType="NVARCHAR" property="fdno"/>
        <result column="TimeStamp" jdbcType="BINARY" property="timestamp"/>
        <result column="Comms" jdbcType="INTEGER" property="comms"/>
        <result column="organs" jdbcType="INTEGER" property="organs"/>
    </resultMap>


    <resultMap id="communityBaseResultMap" type="cn.com.countrygarden.bi.main.model.twerp.ErpCommunity">
        <id column="CommID" jdbcType="VARCHAR" property="commId"/>
        <result column="CommName" jdbcType="VARCHAR" property="commName"/>
        <result column="OrganCode" jdbcType="VARCHAR" property="organCode"/>
        <result column="OrganName" jdbcType="VARCHAR" property="organName"/>
        <result column="NCSystemNumber" jdbcType="VARCHAR" property="ncSystemNumber"/>
    </resultMap>

    <resultMap id="projectBaseResultMap"
               type="cn.com.countrygarden.bi.main.model.financial.dto.Project">
        <id column="CommID" jdbcType="VARCHAR" property="commID"/>
        <result column="CommName" jdbcType="VARCHAR" property="commName"/>
        <result column="NCSystemNumber" jdbcType="VARCHAR" property="NCSystemNumber"/>
        <result column="OrganCode" jdbcType="VARCHAR" property="organCode"/>
        <result column="PropertyArea" jdbcType="VARCHAR" property="propertyArea"/>
        <result column="CityCounty" jdbcType="VARCHAR" property="cityCounty"/>
        <result column="DevelopmentState" jdbcType="VARCHAR" property="developmentState"/>
        <result column="DevelopmentSubject" jdbcType="VARCHAR" property="developmentSubject"/>
        <result column="ActualProperty" jdbcType="VARCHAR" property="actualProperty"/>
    </resultMap>

    <select id="slectErpCommunityByOrganCode" resultMap="communityBaseResultMap">
        <![CDATA[




          select c.CommID, c.CommName, o.OrganName, o.OrganCode from
            [HM_wygl_new_1853].[dbo].[Tb_HSPR_Community] c
            INNER JOIN [HM_wygl_new_1853].[dbo].[Tb_Sys_Organ] o ON o.OrganCode = c.OrganCode AND o.IsDelete=0
            where c.IsDelete = 0 AND o.OrganCode=#{organCode} and c.rentid=1853 and o.rentid=1853




        ]]>
    </select>

    <sql id="countByUserCode">
        <![CDATA[




SELECT COUNT
	( 1 ) AS Comms
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 c.rentid=1853 and o.rentid=1853 and r.rentid=1853 and d.rentid=1853
	and u.rentid=1853




        ]]>
    </sql>

    <select id="countThisMonthByUserCode"
            fetchSize="1"
            resultMap="TbHsprCommunityResultMap"
            parameterType="string">
        <include refid="countByUserCode"/>
        AND c.ManageTime >= CAST (
        dateadd(
        DAY,- DAY (
        getdate()) + 1,
        getdate()) AS DATE)
    </select>

    <select id="countAllByUserCode"
            fetchSize="1"
            resultMap="TbHsprCommunityResultMap"
            parameterType="string">
        <include refid="countByUserCode"/>
    </select>

    <select id="findAllBaseComm" resultMap="communityBaseResultMap">
        <![CDATA[




          select c.CommID, c.CommName, o.OrganName, o.OrganCode from
            [HM_wygl_new_1853].[dbo].[Tb_HSPR_Community] c
            INNER JOIN [HM_wygl_new_1853].[dbo].[Tb_Sys_Organ] o ON o.OrganCode = c.OrganCode AND o.IsDelete=0 and c.rentid=o.rentid
            where c.IsDelete = 0 and c.rentid=1853




        ]]>
	</select>
	<select id="findAllComm" resultMap="communityBaseResultMap">
        <![CDATA[




         select  o.OrganName, o.OrganCode from
            [HM_wygl_new_1853].[dbo].[Tb_Sys_Organ] o
            where o.IsDelete = 0 and o.rentid=1853;





        ]]>
	</select>

	<select id="findAllCommByUserCode"
		resultMap="communityBaseResultMap" parameterType="String">
    
     <![CDATA[


         select  o.OrganName, o.OrganCode from
            [HM_wygl_new_1853].[dbo].[Tb_Sys_Organ] o
            where o.IsDelete = 0 and o.OrganCode=#{organCode} and o.rentid=1853;



        ]]>


	</select>
	<select id="findAllCommByUserCode1"
		resultMap="communityBaseResultMap" parameterType="String">
    
     <![CDATA[


         select  o.OrganName, o.OrganCode from
            [HM_wygl_new_1853].[dbo].[Tb_Sys_Organ] o
            where o.IsDelete = 0 and o.rentid=1853
            



        ]]>
		<if test="organCode!='01'">
			and o.OrganCode=#{organCode}
		</if>

	</select>
	<select id="findBaseCommByCode"
		resultMap="communityBaseResultMap">
        <![CDATA[
          select c.CommID, c.CommName, o.OrganName, o.OrganCode from
            [HM_wygl_new_1853].[dbo].[Tb_HSPR_Community] c
            INNER JOIN [HM_wygl_new_1853].[dbo].[Tb_Sys_Organ] o ON o.OrganCode = c.OrganCode AND o.IsDelete=0 and c.rentid=o.rentid
            where c.IsDelete = 0 and c.rentid=1853 AND c.CommID IN
        ]]>
		<foreach item="item" index="index" collection="commCodes"
			open="(" separator="," close=")">
			#{item}
		</foreach>
	</select>

	<select id="findBaseCommByUserCode"
		resultMap="communityBaseResultMap">
        <![CDATA[
          select d.CommID, '' as CommName, '' as OrganName, d.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 )
          INNER JOIN [HM_wygl_new_1853].[dbo].[Tb_Sys_RoleData] AS d ON ( d.RoleCode = r.RoleCode )
          WHERE
	      u.UserCode = #{userCode} and u.rentid=1853 and r.rentid=1853 and d.rentid=1853

        ]]>
		<if test="organCode != null and organCode != '' ">
			AND d.OrganCode = #{organCode}
		</if>
	</select>

    <select id="findByCommID" resultMap="TbHsprCommunityResultMap">
        SELECT * FROM [HM_wygl_new_1853].[dbo].[Tb_HSPR_Community] WHERE CommID = #{commId} and rentid=1853
    </select>

    <select id="findOrganBaseByUserCode" resultMap="communityBaseResultMap">
        <![CDATA[
          select d.CommID, '' as CommName, '' as OrganName, d.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.rentid=r.rentid
          INNER JOIN [HM_wygl_new_1853].[dbo].[Tb_Sys_RoleData] AS d ON ( d.RoleCode = r.RoleCode ) and d.rentid=r.rentid
          INNER JOIN [HM_wygl_new_1853].[dbo].[Tb_Sys_Organ] AS o ON o.OrganCode = d.OrganCode AND o.isDelete = 0 and d.rentid=o.rentid
          WHERE
	      u.UserCode = #{userCode} AND d.CommID=0 and u.rentid=1853

        ]]>
		<if test="organCode != null and organCode != '' ">
			AND d.OrganCode = #{organCode}
		</if>
	</select>
	<select id="findCommBaseByUserCode"
		resultMap="communityBaseResultMap">
        <![CDATA[
          select c.CommID, c.CommName, o.OrganName, o.OrganCode,i.NCSystemNumber 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.rentid=r.rentid
          INNER JOIN [HM_wygl_new_1853].[dbo].[Tb_Sys_RoleData] AS d ON ( d.RoleCode = r.RoleCode ) and u.rentid=d.rentid
          INNER JOIN [HM_wygl_new_1853].[dbo].[Tb_HSPR_Community] AS c ON c.CommID = d.CommID AND c.isDelete = 0 and c.rentid=d.rentid
          INNER JOIN [HM_wygl_new_1853].[dbo].[Tb_Sys_Organ] AS o ON o.OrganCode = c.OrganCode AND o.isDelete = 0 and c.rentid=o.rentid
          LEFT JOIN [HM_wygl_new_1853].[dbo].[Tb_HSPR_BasicInfo] i ON i.CommID = c.CommID AND ISNULL(i.IsDelete,0) = 0 and c.rentid=i.rentid
          WHERE
	      u.UserCode = #{userCode} AND d.CommID>0 and u.rentid=1853
        ]]>
		<if test="organCode != null and organCode != '' ">
			AND d.OrganCode = #{organCode}
		</if>
	</select>
	<select id="findCommBaseByOrgCodes"
		resultMap="communityBaseResultMap">
        <![CDATA[
          select c.CommID, c.CommName, o.OrganName, o.OrganCode, i.NCSystemNumber
          from [HM_wygl_new_1853].[dbo].[Tb_HSPR_Community] c
            INNER JOIN [HM_wygl_new_1853].[dbo].[Tb_Sys_Organ] o ON o.OrganCode = c.OrganCode AND o.IsDelete=0
            LEFT JOIN [HM_wygl_new_1853].[dbo].[Tb_HSPR_BasicInfo] i ON i.CommID = c.CommID AND ISNULL(i.IsDelete,0) = 0
            where c.rentid=1853 and o.rentid=1853 and i.rentid=1853 and c.IsDelete = 0 AND o.OrganCode IN
        ]]>
		<foreach item="item" index="index" collection="organCodes"
			open="(" separator="," close=")">
			#{item}
		</foreach>
	</select>

	<select id="findBaseCommByName"
		resultMap="communityBaseResultMap">
        <![CDATA[




          select c.CommID, c.CommName, o.OrganName, o.OrganCode
          from [HM_wygl_new_1853].[dbo].[Tb_HSPR_Community] c
            INNER JOIN [HM_wygl_new_1853].[dbo].[Tb_Sys_Organ] o ON o.OrganCode = c.OrganCode AND o.IsDelete=0
            where c.IsDelete = 0 AND c.CommName =#{commName} and c.rentid=1853 and o.rentid=1853





        ]]>
	</select>

    <select id="findHSPRCommByName"
            resultMap="TbHsprCommunityResultMap">
        <![CDATA[

          select TOP 1  c.CommID, c.CommName
          from [HM_wygl_new_1853].[dbo].[Tb_HSPR_Community] c
            where c.IsDelete = 0 AND c.CommName =#{commName} and c.rentid=1853

        ]]>
	</select>

	<select id="findBaseCommByCommId"
		resultMap="communityBaseResultMap">
        <![CDATA[




          select c.CommID, c.CommName, o.OrganName, o.OrganCode
          from [HM_wygl_new_1853].[dbo].[Tb_HSPR_Community] c
            INNER JOIN [HM_wygl_new_1853].[dbo].[Tb_Sys_Organ] o ON o.OrganCode = c.OrganCode AND o.IsDelete=0 and o.rentid=c.rentid
            where c.IsDelete = 0 AND c.CommID =#{commID} and c.rentid=1853





        ]]>
	</select>

	<!--当前收费面积区域排名 -->
	<select id="chargeAreaRanking"
		resultMap="TbHsprCommunityResultMap" parameterType="string">

		select q.organname,sum(buildarea)as area from
		HM_wygl_new_1853.dbo.Tb_HSPR_Room m
		,HM_wygl_new_1853.dbo.Tb_HSPR_Community p
		,HM_wygl_new_1853.dbo.Tb_Sys_Organ q
		where
		m.commid= p.commid and
		p.organcode = q.organcode and
		m.roomid !='0' and
        m.rentid=1853 and p.rentid=1853 and q.rentid=1853 and
		m.roomid in
		(
		select distinct(roomid)
		from Rts_FeesSerial..Tb_HSPR_FeesSerial a,
		HM_wygl_new_1853.dbo.Tb_HSPR_BasicInfo c
		where a.CommID=c.CommID and c.rentid=1853 and a.rentid=1853
		and c.DevelopmentSubject ='外拓'
		and (a.BussType = '新增' or a.BussType = '删除')
		<if test="lastMonth != null and lastMonth != '' ">
			--上月
			and CONVERT(char(7),ChargesModeFeesDueDate,120) =
			CONVERT(char(7),DATEADD(MONTH,-1,GETDATE()),120)
		</if>
		<if test="lastMonth ==null or lastMonth ==''">
			--本月
			and CONVERT(char(7),ChargesModeFeesDueDate,120) =
			CONVERT(char(7),DATEADD(MONTH,0,GETDATE()),120)
		</if>
		and isnull(ParkID,0) = 0
		and a.CorpCostID in
		(100000251,100000253,100000299,100000444,100000445,100000464,100000465,100000466,100000469,100000474)
		group by roomid having sum(a.DealAmount)>'0'
		) group by q.organname order by sum(buildarea) desc

	</select>
	<!--本年每月收费面积趋势 -->
	<select id="monthOfYearArea"
		resultMap="TbHsprCommunityResultMap">
        <![CDATA[




           set statistics io on
            select n.month as month,sum(buildarea)as area from HM_wygl_new_1853.dbo.Tb_HSPR_Room m,
                            (select distinct roomid,datepart(mm,ChargesModeFeesDueDate) as month
                        from Rts_FeesSerial..Tb_HSPR_FeesSerial a, HM_wygl_new_1853.dbo.Tb_HSPR_BasicInfo  c
                        where  a.CommID=c.CommID
                                and c.DevelopmentSubject ='外拓'
                                and (a.BussType = '新增' or a.BussType = '删除')
                 and datepart(YYYY,ChargesModeFeesDueDate) =datepart(YYYY,GETDATE())
                 and datepart(mm,ChargesModeFeesDueDate) <=datepart(mm,GETDATE())
                 --and ChargesModeFeesDueDate<GETDATE()
                                and isnull(ParkID,0) = 0
                                and a.CorpCostID in (100000251,100000253,100000299,100000444,100000445,100000464,100000465,100000466,100000469,100000474)
                                group by roomid,datepart(mm,ChargesModeFeesDueDate) having sum(a.DealAmount)>'0') n
                        where
                            n.roomid <>'0' and
                              m.roomid =n.roomid group by n.month order by n.month
            set statistics io off



        ]]>
	</select>


	<!--收费区域数和项目数 -->
	<select id="countMarketingExpansionCharge"
		resultMap="TbHsprCommunityResultMap">
        <![CDATA[



        select count(distinct q.organname) as organs,count(distinct p.commid) as comms from HM_wygl_new_1853.dbo.Tb_HSPR_Room m
            ,HM_wygl_new_1853.dbo.Tb_HSPR_Community p
            ,HM_wygl_new_1853.dbo.Tb_Sys_Organ q
        where
            m.commid= p.commid and
            p.organcode = q.organcode and
            q.isdelete = '0' and
            m.roomid <>'0' and
              m.roomid in
        (
        select distinct(roomid)
        from Rts_FeesSerial..Tb_HSPR_FeesSerial a, HM_wygl_new_1853.dbo.Tb_HSPR_BasicInfo  c
        where  a.CommID=c.CommID
                and c.DevelopmentSubject ='外拓'
                and (a.BussType = '新增' or a.BussType = '删除')
                and isnull(ParkID,0) = 0
                and a.CorpCostID in (100000251,100000253,100000299,100000444,100000445,100000464,100000465,100000466,100000469,100000474)
                group by roomid having sum(a.DealAmount)>'0'
                    )



        ]]>
    </select>


    <!--查询所有的纯案场或者案场常规混合的项目-->
    <select id="findCaseFieldCommByOrganCode" resultMap="TbHsprCommunityResultMap">

        select c.CommName,c.CommID
        FROM HM_wygl_new_1853.dbo.Tb_HSPR_BasicInfo i WITH(NOLOCK)
        INNER JOIN HM_wygl_new_1853.dbo.Tb_HSPR_Community c WITH(NOLOCK)
        ON c.CommID = i.CommID AND ISNULL(c.IsDelete,0) = 0
        where (i.DevelopmentState='案场常规混合' or i.DevelopmentState='纯案场')
        and i.rentid=1853 and c.rentid=1853
        <if test="organCode!=null and organCode !='' and organCode != '01'">
            and c.OrganCode=#{organCode}
        </if>

        group by c.CommName,c.CommID

    </select>


    <!--通过用户ID查询所有纯案场或者案场常规混合的区域-->
    <select id="findOrgansByUserCode" resultMap="communityBaseResultMap">
       	select o.OrganName, o.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 r.rentid=u.rentid
        INNER JOIN [HM_wygl_new_1853].[dbo].[Tb_Sys_RoleData] AS d ON ( d.RoleCode = r.RoleCode ) and d.rentid=r.rentid
        INNER JOIN [HM_wygl_new_1853].[dbo].[Tb_HSPR_Community] AS c ON c.CommID = d.CommID AND c.isDelete = 0 and c.rentid=d.rentid
        INNER JOIN [HM_wygl_new_1853].[dbo].[Tb_Sys_Organ] AS o ON o.OrganCode = c.OrganCode AND o.isDelete = 0 and o.rentid=c.rentid
        LEFT JOIN [HM_wygl_new_1853].[dbo].[Tb_HSPR_BasicInfo] i ON i.CommID = c.CommID AND ISNULL(i.IsDelete,0) = 0 and i.rentid=c.rentid
        WHERE
        u.UserCode = #{userCode} AND d.CommID>0
        and (i.DevelopmentState='纯案场' or i.DevelopmentState='案场常规混合') and u.rentid=1853
        group by o.organcode,o.organname
    </select>

    <!--通过用户ID和区域ID查询所有纯案场或者案场常规混合的项目-->
    <select id="findCommsByUserCodeAndOrganCode" resultMap="communityBaseResultMap">
        select c.CommID, c.CommName 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 r.rentid=u.rentid
        INNER JOIN [HM_wygl_new_1853].[dbo].[Tb_Sys_RoleData] AS d ON ( d.RoleCode = r.RoleCode ) and d.rentid=r.rentid
        INNER JOIN [HM_wygl_new_1853].[dbo].[Tb_HSPR_Community] AS c ON c.CommID = d.CommID AND c.isDelete = 0 and c.rentid=d.rentid
        INNER JOIN [HM_wygl_new_1853].[dbo].[Tb_Sys_Organ] AS o ON o.OrganCode = c.OrganCode AND o.isDelete = 0 and o.rentid=c.rentid
        LEFT JOIN [HM_wygl_new_1853].[dbo].[Tb_HSPR_BasicInfo] i ON i.CommID = c.CommID AND ISNULL(i.IsDelete,0) = 0 and i.rentid=c.rentid
        WHERE
        u.UserCode = #{userCode} AND d.CommID>0  and (i.DevelopmentState='纯案场' or i.DevelopmentState='案场常规混合') and u.rentid=1853
        and o.OrganCode = #{organCode}
        group by c.CommID, c.CommName
    </select>
    
    <!--通过用户ID和区域ID查询所有纯案场或者案场常规混合的项目-->
    <select id="findCommsByUserCodeAndOrganCodefenghuang" resultType="java.lang.String">
        select c.CommID 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.rentid=r.rentid
        INNER JOIN [HM_wygl_new_1853].[dbo].[Tb_Sys_RoleData] AS d ON ( d.RoleCode = r.RoleCode ) and u.rentid=d.rentid
        INNER JOIN [HM_wygl_new_1853].[dbo].[Tb_HSPR_Community] AS c ON c.CommID = d.CommID AND c.isDelete = 0 and c.rentid=d.rentid
        INNER JOIN [HM_wygl_new_1853].[dbo].[Tb_Sys_Organ] AS o ON o.OrganCode = c.OrganCode AND o.isDelete = 0 and o.rentid=c.rentid
        LEFT JOIN [HM_wygl_new_1853].[dbo].[Tb_HSPR_BasicInfo] i ON i.CommID = c.CommID AND ISNULL(i.IsDelete,0) = 0 and i.rentid=c.rentid
        WHERE
        u.UserCode = #{userCode} AND d.CommID>0  and (i.DevelopmentState='纯案场') and u.rentid=1853
        <if test="organCode!=null and organCode!=''">
        and o.OrganCode = #{organCode}
        </if>
        
         <!-- <if test="CommID!=null">
           and c.CommID in 
            <foreach item="CommID" index="index" collection="CommID" open="(" separator="," close=")">
                #{CommID}
            </foreach>
        </if> --> 
        group by c.CommID, c.CommName
    </select>

    <!--通过用户ID查询所有的纯案场或者常规混合的项目-->
    <select id="findCommsByUserCode" resultMap="communityBaseResultMap">
        select c.CommID, c.CommName 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 r.rentid=u.rentid
        INNER JOIN [HM_wygl_new_1853].[dbo].[Tb_Sys_RoleData] AS d ON ( d.RoleCode = r.RoleCode ) and d.rentid=r.rentid
        INNER JOIN [HM_wygl_new_1853].[dbo].[Tb_HSPR_Community] AS c ON c.CommID = d.CommID AND c.isDelete = 0 and c.rentid=d.rentid
        INNER JOIN [HM_wygl_new_1853].[dbo].[Tb_Sys_Organ] AS o ON o.OrganCode = c.OrganCode AND o.isDelete = 0 and o.rentid=c.rentid
        LEFT JOIN [HM_wygl_new_1853].[dbo].[Tb_HSPR_BasicInfo] i ON i.CommID = c.CommID AND ISNULL(i.IsDelete,0) = 0 and i.rentid=c.rentid
        WHERE
        u.UserCode = #{userCode} AND d.CommID>0  and (i.DevelopmentState='纯案场' or i.DevelopmentState='案场常规混合') and u.rentid=1853
        group by c.CommID, c.CommName
    </select>


    <select id="findAllBaseOrgan" resultType="cn.com.countrygarden.bi.main.dto.ManagerMapDTO">
        <![CDATA[
          select o.OrganCode,REPLACE(o.OrganName, '/', '') OrganName,
            (SELECT count(1) from [HM_wygl_new_1853].[dbo].[Tb_HSPR_Community] c WHERE  c.rentid = '1853' AND IsDelete = 0 AND c.OrganCode = o.OrganCode) commCount,
            (SELECT count(1) FROM [dbo].[TF_View_HSPR_Customer] WHERE rent_id=1853 AND IsDelete = 0 and CommID in ( select CommID from Tb_HSPR_Community WHERE OrganCode=O.OrganCode AND IsDelete = 0 AND rentid = '1853')) manageNumCount
            from [HM_wygl_new_1853].[dbo].[Tb_Sys_Organ] AS o
            WHERE o.isDelete=0 AND o.OrganCode <>'01' AND o.rentid = '1853' 
        ]]>
        <if test="organName!=null and organName !=''">
            and o.OrganName like CONCAT('%',#{organName},'%')
        </if>
    </select>
    <select id="selectCommunityNamesByOrganCode" resultType="java.lang.String">
        SELECT c.communityName from  [HM_wygl_new_1853].[dbo].[Tb_HSPR_Community]  c WHERE c.OrganCode = #{organCode} and c.rentid=1853
    </select>


    <select id="selectAllBaseOrganTree" resultType="cn.com.countrygarden.bi.main.dto.ManagerMapOrganTreeDTO">
        <![CDATA[


          select o.OrganCode code,o.OrganName name
          from [HM_wygl_new_1853].[dbo].[Tb_Sys_Organ] AS o WHERE o.isDelete=0 and o.rentid=1853


        ]]>
	</select>

	<select id="selectCommunityTreeByOrganCode"
		resultType="cn.com.countrygarden.bi.main.dto.ManagerMapOrganTreeDTO">
		SELECT c.commID code,c.commName name from
		[HM_wygl_new_1853].[dbo].[Tb_HSPR_Community] c WHERE c.OrganCode =
		#{organCode} and c.rentid=1853
	</select>
	<select id="selectCommunityTreeByOrganCode1"
		resultType="cn.com.countrygarden.bi.main.dto.ManagerMapOrganTreeDTO">
		select c.commid code,c.commname name from
		[hm_wygl_new_1853].[dbo].[tb_hspr_community] c where c.organcode =
		#{organCode} and c.rentid=1853
	</select>

    <select id="selectProjectByUserCode" resultType="cn.com.countrygarden.bi.main.model.twerp.ErpNcData">
        select u.UserName,
        r.RoleCode,
        o.OrganCode,
        o.OrganName,
        i.CommID,
        i.PropertyProject,
        i.NCSystemNumber 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 r.rentid=u.rentid
        INNER JOIN [HM_wygl_new_1853].[dbo].[Tb_Sys_RoleData] AS d ON ( d.RoleCode = r.RoleCode ) and d.rentid=r.rentid
        INNER JOIN [HM_wygl_new_1853].[dbo].[Tb_HSPR_Community] AS c ON c.CommID = d.CommID AND c.isDelete = 0 and c.rentid=d.rentid
        INNER JOIN [HM_wygl_new_1853].[dbo].[Tb_Sys_Organ] AS o ON o.OrganCode = c.OrganCode AND o.isDelete = 0 and o.rentid=c.rentid
        INNER JOIN [HM_wygl_new_1853].[dbo].[Tb_HSPR_BasicInfo] i ON i.CommID = c.CommID AND ISNULL(i.IsDelete,0) = 0 AND i.NCSystemNumber LIKE 'F%' and i.rentid=c.rentid
        WHERE
        u.UserCode = #{userCode} AND d.CommID>0 and u.rentid=1853
    </select>
    <!--通过userCode获取区域账套编码信息-->
    <select id="selectRegionByUserCode" resultType="cn.com.countrygarden.bi.main.model.twerp.ErpNcData">
        SELECT
        u.UserName,
        r.RoleCode,
        o.OrganCode,
        o.OrganName,
        i.CommID,
        i.PropertyProject,
        i.NCSystemNumber
        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 r.rentid=u.rentid
        INNER JOIN [HM_wygl_new_1853].[dbo].[Tb_Sys_RoleData] AS d ON (d.RoleCode = r.RoleCode) and d.rentid=r.rentid
        INNER JOIN [HM_wygl_new_1853].[dbo].[Tb_Sys_Organ] AS o ON (o.OrganCode = d.OrganCode) and o.rentid=d.rentid
        AND o.IsDelete = 0
        INNER JOIN [HM_wygl_new_1853].[dbo].[Tb_HSPR_Community] AS c ON (o.OrganCode = c.OrganCode) and c.rentid=o.rentid
        AND c.IsDelete = 0
        INNER JOIN [HM_wygl_new_1853].[dbo].[Tb_HSPR_BasicInfo] i ON i.CommID = c.CommID
        AND ISNULL(i.IsDelete, 0) = 0 AND i.NCSystemNumber LIKE 'F%' and i.rentid=c.rentid
        WHERE
        u.UserCode = #{userCode} and u.rentid=1853
        AND d.CommID = 0
    </select>
    <!--通过commId获取项目账套编码信息-->
    <select id="selectProjectByCommId" resultType="cn.com.countrygarden.bi.main.model.twerp.ErpNcData">
        select
        o.OrganCode,
        o.OrganName,
        i.CommID,
        i.PropertyProject,
        i.NCSystemNumber from
        [HM_wygl_new_1853].[dbo].[Tb_HSPR_Community] AS c
        INNER JOIN [HM_wygl_new_1853].[dbo].[Tb_Sys_Organ] AS o ON o.OrganCode = c.OrganCode AND o.isDelete = 0 and c.rentid=o.rentid
        INNER JOIN [HM_wygl_new_1853].[dbo].[Tb_HSPR_BasicInfo] i ON i.CommID = c.CommID AND ISNULL(i.IsDelete,0) = 0 AND i.NCSystemNumber LIKE 'F%' and i.rentid=c.rentid
        WHERE c.CommID = #{commId} AND c.IsDelete = 0 and c.rentid=1853
    </select>
    <!--获取账簿编号对应多项目的NCSystemNumber-->
    <select id="selectCodoOneToMany" resultType="java.lang.String">
        SELECT
        CONCAT(NCSystemNumber,'-0001')
        FROM
        [HM_wygl_new_1853].[dbo].[Tb_HSPR_BasicInfo]
        WHERE IsDelete = 0
        GROUP BY
        NCSystemNumber
        HAVING
        COUNT (CommID) > 1
    </select>
    <!--通过OrganCode获取区域对应账簿编码-->
    <select id="selectRegionByOrganCode" resultType="cn.com.countrygarden.bi.main.model.twerp.ErpNcData">
        SELECT
        o.OrganCode,
        o.OrganName,
        i.CommID,
        i.PropertyProject,
        i.NCSystemNumber
        FROM
        [HM_wygl_new_1853].[dbo].[Tb_Sys_Organ] AS o
        INNER JOIN [HM_wygl_new_1853].[dbo].[Tb_HSPR_Community] AS c ON (o.OrganCode = c.OrganCode)
        AND c.IsDelete = 0 and c.rentid=o.rentid
        INNER JOIN [HM_wygl_new_1853].[dbo].[Tb_HSPR_BasicInfo] i ON i.CommID = c.CommID
        AND ISNULL(i.IsDelete, 0) = 0 AND i.NCSystemNumber LIKE 'F%' and i.rentid=c.rentid
        WHERE
        o.OrganCode = #{organCode} AND o.IsDelete = 0 and o.rentid=1853;
    </select>
    <!--通过commIdList获取项目对应账簿编码-->
    <select id="selectProjectByCommIdList" parameterType="java.lang.String"
            resultType="cn.com.countrygarden.bi.main.model.twerp.ErpNcData">
        SELECT
        c.CommID,
        o.OrganCode,
        i.NCSystemNumber
        FROM
        [HM_wygl_new_1853].[dbo].[Tb_HSPR_Community] AS c
        INNER JOIN [HM_wygl_new_1853].[dbo].[Tb_Sys_Organ] AS o ON (o.OrganCode = c.OrganCode)
        AND o.IsDelete = 0
        INNER JOIN [HM_wygl_new_1853].[dbo].[Tb_HSPR_BasicInfo] i ON i.CommID = c.CommID
        AND ISNULL(i.IsDelete, 0) = 0 AND i.NCSystemNumber LIKE 'F%'
        WHERE
        c.CommID IN
        <foreach item="commId" index="index" collection="commIdList" open="(" separator="," close=")">
            #{commId}
        </foreach>
        AND c.IsDelete = 0 and o.rentid=1853 and c.rentid=1853 and i.rentid=1853;
    </select>
    <!--通过 commIdList 获取PL按项目横向 Project Bean -->
    <select id="selectProjectBeanByCommIdList" parameterType="java.lang.String"
            resultMap="projectBaseResultMap">
        SELECT c.CommID,c.CommName,i.NCSystemNumber,c.organCode AS OrganCode,i.PropertyArea,(i.City +'/'+ i.County) AS
        CityCounty,i.DevelopmentState,i.DevelopmentSubject,i.ActualProperty
        FROM [HM_wygl_new_1853].[dbo].[Tb_HSPR_Community] AS c
        INNER JOIN [HM_wygl_new_1853].[dbo].[Tb_HSPR_BasicInfo] AS i ON i.CommID = c.CommID
        AND ISNULL(i.IsDelete, 0) = 0 AND i.NCSystemNumber LIKE 'F%'
        WHERE 1=1
        <if test="commIdList !=null">
            and c.CommID IN
            <foreach item="commId" index="index" collection="commIdList" open="(" separator="," close=")">
                #{commId}
            </foreach>
        </if>
        AND c.IsDelete = 0 and c.rentid=1853 and i.rentid=1853;
    </select>
    <!--获取所有organCode-->
    <select id="selectAllOrganCode" resultType="java.lang.String">
        select OrganCode from [HM_wygl_new_1853].[dbo].[Tb_Sys_Organ] WHERE isDelete=0 and rentid=1853
    </select>
    <!--通过COMMID获取组团信息 RegionID RegionName-->
    <select id="selectGroupsByCommId" parameterType="java.lang.String" resultType="cn.com.countrygarden.bi.main.model.twerp.ErpNcData">
        select RegionID,RegionName from [HM_wygl_new_1853].[dbo].[Tb_HSPR_Region]
          WHERE CommID = #{commId} AND isDelete=0 and rentid=1853
    </select>

    <select id="findBaseCommByCommIdOrArea" resultMap="communityBaseResultMap">
        <![CDATA[
          select c.CommID, c.CommName, o.OrganName, o.OrganCode
          from [HM_wygl_new_1853].[dbo].[Tb_HSPR_Community] c
            INNER JOIN [HM_wygl_new_1853].[dbo].[Tb_Sys_Organ] o ON o.OrganCode = c.OrganCode AND o.IsDelete=0 and c.rentid=o.rentid
            where c.IsDelete = 0 AND c.CommID =#{commID} AND o.OrganName=#{area} and c.rentid=1853
        ]]>
    </select>
    
    <select id="findAllRoleOrganCode"
		resultMap="communityBaseResultMap" parameterType="String">
    
     <![CDATA[


         select  o.OrganName, o.OrganCode from
            [HM_wygl_new_1853].[dbo].[Tb_Sys_Organ] o
            where o.IsDelete = 0 and o.rentid=1853
            



        ]]>
		<if test="organCode!='all'">
			and o.OrganCode=#{organCode}
		</if>

	</select>


    <select id="selectCommNamesByOrganCode" resultType="java.lang.String">
        SELECT c.CommName from  [HM_wygl_new_1853].[dbo].[Tb_HSPR_Community]  c
        WHERE c.OrganCode = #{organCode} AND ISNULL(c.IsDelete,0) = 0 and c.rentid=1853
    </select>

    <select id="commNum" resultType="int">
         SELECT count(CommID)
         FROM [dbo].[Tb_HSPR_Community]
         WHERE rentid=1853
    </select>
</mapper>