<?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.mediaResource.BasicInfoMapper">

	<resultMap type="cn.com.countrygarden.bi.main.model.mediaResource.ErpBasicInfo" id="erpBasicInfoMap">
		<id column="basicsId" property="basicsId"/>
		<result column="commId" property="commId"/>
		<result column="organCode" property="organCode"/>
		<result column="propertyArea" property="propertyArea"/>
		<result column="propertyProject" property="propertyProject"/>
		<result column="developmentSubject" property="developmentSubject"/>
		<result column="region" property="region"/>
		<result column="province" property="province"/>
		<result column="city" property="city"/>
		<result column="county" property="county"/>
		<result column="actualProperty" property="actualProperty"/>
		<result column="occRate" property="occRate"/>

		<result column="propertyName" property="propertyName"/>
		<result column="propertyForm" property="propertyForm"/>
		<result column="developmentState" property="developmentState"/>
		<result column="takeoverState" property="takeoverState"/>
		<result column="actualEnterTime" property="actualEnterTime"/>
		<result column="stateProperty" property="stateProperty"/>
		<result column="plannedOpen" property="plannedOpen"/>
		<result column="actualOpen" property="actualOpen"/>
		<result column="contractProperty" property="contractProperty"/>
		<result column="totalResidentialArea" property="totalResidentialArea"/>
		<result column="developmentSubjectNames" property="developmentSubjectNames"/>

		<collection property="planInfos" ofType="cn.com.countrygarden.bi.main.model.mediaResource.ErpPlanInfo">
			<result column="planNumber" property="planNumbers"/>
		</collection>
		
	</resultMap>
	
	<select id="findErpBasicInfo" resultMap="erpBasicInfoMap">
		select 
		b.basicsId,
		b.commId,
		b.organCode,
		b.propertyArea,
		b.propertyProject,
		b.developmentSubject,
		b.region,
		b.province,
		b.city,
		b.county,
		b.actualProperty,
		isnull((f.HouseQuantity+f.CommercialAuantity + f.ParkeNumber + f.OtherQuantity),0) as planNumber,
		isnull(t.OccRate,0) as occRate,
		b.propertyName,
		b.propertyForm,
		b.developmentState,
		b.takeoverState,
		b.actualEnterTime,
		b.stateProperty,
		b.plannedOpen,
		b.actualOpen,
		b.contractProperty,
		isnull(f.totalResidentialArea,0) as totalResidentialArea,
		 stuff(
		(
			SELECT ',' + ConstUnitName
			FROM [HM_wygl_new_1853].[dbo].[Tb_HSPR_BasicInfoCom] c WHERE c.basid = b.basicsId FOR xml path ('')
		),1,1,'') AS developmentSubjectNames 	
		from [HM_wygl_new_1853].[dbo].[Tb_HSPR_BasicInfo] b
		left join [HM_wygl_new_1853].[dbo].[Tb_HSPR_PlanInfo] f on b.commid = f.commid and isnull(f.isdelete,0) = 0
		left join (SELECT  AA.commid, 
		CASE WHEN SUM(AA.Amount)=0 THEN 0 
		    ELSE SUM(CASE WHEN AA.livestates IN('0001','0004') THEN AA.Amount ELSE 0 END)/SUM(AA.Amount) END AS OccRate
		FROM (
				SELECT r.commid,r.livestates,CAST(COUNT(1) as decimal) AS amount 
				FROM [HM_wygl_new_1853].[dbo].[tb_hspr_room] r  
				WHERE coalesce(r.IsDelete,0) = 0 
						--AND r.commid IN (b.commid) 
						AND r.IsSplitUnite != 1 AND r.IsSplitUnite != 3 
						AND r.takeoverdate <![CDATA[ < ]]> (select CONVERT(varchar(10),GETDATE(),120)) 
				GROUP BY r.commid,r.livestates
		) AA  group by commid) 
		as t on t.commid = b.commid
		where isnull(b.isdelete,0) = 0 and b.commid is not null  and b.organcode is not null
	</select>
	
	
	<select id="findErpUserOrgan" resultType="map">
		SELECT top 1 d2.DepName as regionName,
		isnull((select organcode from [HM_wygl_new_1853].[dbo].[Tb_Sys_Organ] where isdelete = 0 and d2.DepName = organName),'0') as organCode 
		FROM [HM_wygl_new_1853].[dbo].[Tb_Sys_User] AS u
		JOIN [HM_wygl_new_1853].[dbo].[Tb_Sys_Department] as d on d.DepCode = u.DepCode  and (d.IsDelete is null OR d.IsDelete!='1')
		JOIN [HM_wygl_new_1853].[dbo].[Tb_Sys_Department] d2 on d2.SortDepCode = LEFT(d.SortDepCode, 8)
		WHERE u.IsDelete = 0 AND u.UserCode = #{userCode} 
	</select>
	
	
	
	<select id="findErpBasicInfo2" resultType="cn.com.countrygarden.bi.main.model.mediaResource.BasicInfo">
		select 
		basicsId,
		commId,
		organCode,
		propertyArea as organName,
		propertyProject as commName,
		developmentSubject as developUnitName,
		region as commAddress,
		province,
		city,
		county,
		actualProperty 
		from [HM_wygl_new_1853].[dbo].[Tb_HSPR_BasicInfo] 
		where 1=1 
		and isdelete = 0 
		<if test="idList!=null and idList.size() > 0">
			and basicsid not in 
			<foreach collection="idList" index="index" item="item" open="(" separator="," close=")">
               	#{item}       
        	</foreach>   
		</if>
	</select>
	
	
	<resultMap type="cn.com.countrygarden.bi.main.model.mediaResource.Regional" id="regionalMap">
		
		<result column="OrganName" property="name"/>
		<result column="OrganCode" property="id"/>

		<collection property="projects" ofType="cn.com.countrygarden.bi.main.model.mediaResource.Projects">
			<result column="CommID" property="id"/>
			<result column="CommName" property="name"/>
		</collection>
		
	</resultMap>
	
	<select id="findRegionalProject" resultMap="regionalMap">
		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.isdelete = 0
		where c.isdelete = 0
		order by a.OrganName
	</select>
	
	<select id="findCompany" resultMap="regionalMap">
		SELECT c.CommID,c.CommName,a.OrganName,c.OrganCode
		FROM [HM_wygl_new_1853].[dbo].Tb_Sys_User u
		INNER JOIN [HM_wygl_new_1853].[dbo].Tb_Sys_UserRole ur ON ur.UserCode = u.UserCode
		INNER JOIN [HM_wygl_new_1853].[dbo].Tb_Sys_Role r ON r.RoleCode = ur.RoleCode
		INNER JOIN [HM_wygl_new_1853].[dbo].Tb_Sys_RoleData rd ON rd.RoleCode = r.RoleCode
		INNER JOIN [HM_wygl_new_1853].[dbo].Tb_HSPR_Community c ON c.CommID = rd.CommID and COALESCE(c.IsDelete,0) = 0 
		left join [HM_wygl_new_1853].[dbo].[Tb_Sys_Organ] a ON	a.OrganCode = c.OrganCode and a.isdelete = 0
		where u.UserCode = #{userCode}
	</select>
	
	<!-- <select id="queryCommInfo" resultType="cn.com.countrygarden.bi.main.service.mediaResource.CommunityInfo">
		SELECT c.CommID,c.CommName,o.OrganCode,o.OrganName
		,COALESCE(ci.CityCompany,'0002') AS CompanyCode
		,COALESCE(cc.DictionaryName,'碧桂园物业服务有限公司') AS CompanyName
		,COALESCE(cc.DictionaryOrderID,1) AS CompanyOrder
		,c.Province,c.City
		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)
		INNER JOIN [HM_wygl_new_1853].[dbo].tb_hspr_coreindex AS ci ON (ci.OrganCode = o.OrganCode)
		LEFT JOIN [HM_wygl_new_1853].[dbo].tb_dictionary_citycompany AS cc ON (cc.DictionaryCode = ci.CityCompany)
		WHERE (COALESCE(c.IsDelete,0) = 0) AND (COALESCE(o.IsDelete,0) = 0)
		AND (o.OrganName != '测试区域') AND (c.CommName != '测试项目')
	</select>
	
	<select id="queryCommId" resultType="Integer">
		SELECT DISTINCT c.CommID
        FROM [HM_wygl_new_1853].[dbo].Tb_Sys_User u 
        INNER JOIN [HM_wygl_new_1853].[dbo].Tb_Sys_UserRole ur ON ur.UserCode = u.UserCode
        INNER JOIN [HM_wygl_new_1853].[dbo].Tb_Sys_Role r ON r.RoleCode = ur.RoleCode
        INNER JOIN [HM_wygl_new_1853].[dbo].Tb_Sys_RoleData rd ON rd.RoleCode = r.RoleCode
        INNER JOIN [HM_wygl_new_1853].[dbo].Tb_HSPR_Community c ON c.CommID = rd.CommID
        WHERE COALESCE(c.IsDelete,0) = 0 AND c.CommName != '测试项目'
        AND u.UserCode = #{userCode}
	</select> -->
	
</mapper>