<?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.TbHsprContractMapper">

    <!-- 合同录入统计管理映射 -->
    <resultMap id="ContractStatisticalMap" type="cn.com.countrygarden.bi.main.model.dup.TbHsprContract">
        <result column="ContID" property="ContId"/>
        <result column="BipNum" property="BipNum"/>
        <result column="ContName" property="ContName"/>
        <result column="IsAuditing" property="IsAuditing"/>
        <result column="RegDate" property="RegDate"/>
        <result column="CommName" property="CommName"/>
        <result column="OrganCode" property="OrganCode"/>
        <result column="OrganName" property="OrganName"/>
    </resultMap>

    <!-- 合同录入统计管理映射 -->
    <resultMap id="ContractStatisticalMap1" type="cn.com.countrygarden.bi.main.model.dup.ContractStatistical">
        <result column="CommID" property="CommId"/>
        <result column="ContID" property="ContId"/>
        <result column="BipNum" property="BipNum"/>
        <result column="ContName" property="ContName"/>
        <result column="IsAuditing" property="IsAuditing"/>
        <result column="RegDate" property="RegDate"/>
        <result column="CommName" property="CommName"/>
        <result column="OrganCode" property="OrganCode"/>
        <result column="OrganName" property="OrganName"/>
        <result column="AuditData" property="finallyDate"/>
    </resultMap>


    <!-- 未审核 -->
    <select id="contractStatisticalSelectNoExamine" parameterType="cn.com.countrygarden.bi.main.model.dup.ContractStatistical"  resultMap="ContractStatisticalMap1" >
        select
        [HM_wygl_new_1853].[dbo].[Tb_HSPR_Contract].ContID,
        [HM_wygl_new_1853].[dbo].[Tb_HSPR_Contract].BipNum,
        [HM_wygl_new_1853].[dbo].[Tb_HSPR_Contract].ContName,
        [HM_wygl_new_1853].[dbo].[Tb_HSPR_Contract].IsAuditing,
        [HM_wygl_new_1853].[dbo].[Tb_HSPR_Contract].RegDate,
        [HM_wygl_new_1853].[dbo].[Tb_HSPR_Contract].AuditData,
        [HM_wygl_new_1853].[dbo].[Tb_HSPR_Community].CommName,
        [HM_wygl_new_1853].[dbo].[Tb_HSPR_Community].CommId,
        [HM_wygl_new_1853].[dbo].[Tb_Sys_Organ].OrganCode,
        [HM_wygl_new_1853].[dbo].[Tb_Sys_Organ].OrganName
        from
        [HM_wygl_new_1853].[dbo].[Tb_HSPR_Contract]
        LEFT JOIN
        [HM_wygl_new_1853].[dbo].[Tb_HSPR_Community]
        ON
        [HM_wygl_new_1853].[dbo].[Tb_HSPR_Community].CommID=[HM_wygl_new_1853].[dbo].[Tb_HSPR_Contract].CommID
        LEFT JOIN
        [HM_wygl_new_1853].[dbo].[Tb_Sys_Organ]
        ON
        [HM_wygl_new_1853].[dbo].[Tb_Sys_Organ].OrganCode=[HM_wygl_new_1853].[dbo].[Tb_HSPR_Community].OrganCode
        <where>
            <if test="BipNum !=null and BipNum != ''">AND [HM_wygl_new_1853].[dbo].[Tb_HSPR_Contract].BipNum LIKE CONCAT('%',#{BipNum},'%')</if>
            <if test="ContName !=null and ContName != ''">AND [HM_wygl_new_1853].[dbo].[Tb_HSPR_Contract].ContName LIKE CONCAT('%',#{ContName},'%')</if>
            <if test="IsAuditings !=null ">AND ([HM_wygl_new_1853].[dbo].[Tb_HSPR_Contract].IsAuditing = #{IsAuditings}
                OR [HM_wygl_new_1853].[dbo].[Tb_HSPR_Contract].IsAuditing is null)</if>
            <!-- <if test="RegDate !=null">AND [HM_wygl_new_1853].[dbo].[Tb_HSPR_Contract].RegDate &gt; #{RegDate}</if>	 -->
            <if test="CommName !=null and CommName != ''">AND [HM_wygl_new_1853].[dbo].[Tb_HSPR_Community].CommName LIKE CONCAT('%',#{CommName},'%')</if>
            <if test="OrganName !=null and OrganName != ''">AND [HM_wygl_new_1853].[dbo].[Tb_Sys_Organ].OrganName LIKE CONCAT('%',#{OrganName},'%')</if>
            <if test="CommId !=null and  CommId!=0">AND [HM_wygl_new_1853].[dbo].[Tb_HSPR_Contract].CommID = #{CommId}</if>
            <if test="OrganCode !=null and OrganCode != ''">AND [HM_wygl_new_1853].[dbo].[Tb_Sys_Organ].OrganCode LIKE CONCAT('%',#{OrganCode},'%')</if>
            <if test="RegDateStart !=null and RegDateStart!=''">AND [HM_wygl_new_1853].[dbo].[Tb_HSPR_Contract].RegDate &gt;= #{RegDateStart}</if>
            <if test="RegDateEnd !=null and RegDateEnd!=''">AND [HM_wygl_new_1853].[dbo].[Tb_HSPR_Contract].RegDate &lt;= #{RegDateEnd}</if>
            AND [HM_wygl_new_1853].[dbo].[Tb_HSPR_Contract].rentid=1853 and
            [HM_wygl_new_1853].[dbo].[Tb_HSPR_Community].rentid=1853 and
            [HM_wygl_new_1853].[dbo].[Tb_Sys_Organ].rentid=1853
        </where>
        order by [HM_wygl_new_1853].[dbo].[Tb_HSPR_Contract].RegDate	desc
    </select>

    <!-- 查询所有数据 -->
    <select id="contractStatisticalSelectNoExamineAll" parameterType="cn.com.countrygarden.bi.main.model.dup.ContractStatistical"  resultMap="ContractStatisticalMap1" >
        select  TOP 40
        [HM_wygl_new_1853].[dbo].[Tb_HSPR_Contract].ContID,
        [HM_wygl_new_1853].[dbo].[Tb_HSPR_Contract].BipNum,
        [HM_wygl_new_1853].[dbo].[Tb_HSPR_Contract].ContName,
        [HM_wygl_new_1853].[dbo].[Tb_HSPR_Contract].IsAuditing,
        [HM_wygl_new_1853].[dbo].[Tb_HSPR_Contract].RegDate,
        [HM_wygl_new_1853].[dbo].[Tb_HSPR_Community].CommName,
        [HM_wygl_new_1853].[dbo].[Tb_HSPR_Contract].AuditData,
        [HM_wygl_new_1853].[dbo].[Tb_HSPR_Contract].CommID,
        [HM_wygl_new_1853].[dbo].[Tb_Sys_Organ].OrganCode,
        [HM_wygl_new_1853].[dbo].[Tb_Sys_Organ].OrganName
        from
        [HM_wygl_new_1853].[dbo].[Tb_HSPR_Contract]
        LEFT JOIN
        [HM_wygl_new_1853].[dbo].[Tb_HSPR_Community]
        ON
        [HM_wygl_new_1853].[dbo].[Tb_HSPR_Community].CommID=[HM_wygl_new_1853].[dbo].[Tb_HSPR_Contract].CommID and [HM_wygl_new_1853].[dbo].[Tb_HSPR_Contract].rentid=[HM_wygl_new_1853].[dbo].[Tb_HSPR_Community].rentid
        LEFT JOIN
        [HM_wygl_new_1853].[dbo].[Tb_Sys_Organ]
        ON
        [HM_wygl_new_1853].[dbo].[Tb_Sys_Organ].OrganCode=[HM_wygl_new_1853].[dbo].[Tb_HSPR_Community].OrganCode AND [HM_wygl_new_1853].[dbo].[Tb_Sys_Organ].rentid=[HM_wygl_new_1853].[dbo].[Tb_HSPR_Community].rentid
        <where>
            <if test="BipNum !=null and BipNum != ''">AND [HM_wygl_new_1853].[dbo].[Tb_HSPR_Contract].BipNum LIKE CONCAT('%',#{BipNum},'%')</if>
            <if test="ContName !=null and ContName != ''">AND [HM_wygl_new_1853].[dbo].[Tb_HSPR_Contract].ContName LIKE CONCAT('%',#{ContName},'%')</if>
            <if test="IsAuditing !=null and IsAuditing!='' ">AND [HM_wygl_new_1853].[dbo].[Tb_HSPR_Contract].IsAuditing = #{IsAuditings}</if>
            <!-- 	<if test="RegDate !=null">AND [HM_wygl_new_1853].[dbo].[Tb_HSPR_Contract].RegDate &gt; #{RegDate}</if>	 -->
            <if test="CommName !=null and CommName != ''">AND [HM_wygl_new_1853].[dbo].[Tb_HSPR_Community].CommName LIKE CONCAT('%',#{CommName},'%')</if>
            <if test="OrganName !=null and OrganName != ''">AND [HM_wygl_new_1853].[dbo].[Tb_Sys_Organ].OrganName LIKE CONCAT('%',#{OrganName},'%')</if>
            <if test="CommId !=null and CommId !=0">AND [HM_wygl_new_1853].[dbo].[Tb_HSPR_Contract].CommID = #{CommId}</if>
            <if test="OrganCode !=null and OrganCode != ''">AND [HM_wygl_new_1853].[dbo].[Tb_Sys_Organ].OrganCode LIKE CONCAT('%',#{OrganCode},'%')</if>
            <if test="RegDateStart !=null and RegDateStart!=''">AND [HM_wygl_new_1853].[dbo].[Tb_HSPR_Contract].RegDate &gt;= #{RegDateStart}</if>
            <if test="RegDateEnd !=null and RegDateEnd!=''">AND [HM_wygl_new_1853].[dbo].[Tb_HSPR_Contract].RegDate &lt;= #{RegDateEnd}</if>
            and [HM_wygl_new_1853].[dbo].[Tb_HSPR_Contract].rentid=1853 and
            [HM_wygl_new_1853].[dbo].[Tb_HSPR_Community].rentid=1853 and
            [HM_wygl_new_1853].[dbo].[Tb_Sys_Organ].rentid=1853
        </where>
        order by [HM_wygl_new_1853].[dbo].[Tb_HSPR_Contract].RegDate desc
    </select>

    <!-- 查询所有数据 -->
    <select id="contractStatisticalSelectNoExamineAll1" parameterType="cn.com.countrygarden.bi.main.model.dup.ContractStatistical"  resultMap="ContractStatisticalMap1" >
        select
        [HM_wygl_new_1853].[dbo].[Tb_HSPR_Contract].ContID,
        [HM_wygl_new_1853].[dbo].[Tb_HSPR_Contract].BipNum,
        [HM_wygl_new_1853].[dbo].[Tb_HSPR_Contract].ContName,
        [HM_wygl_new_1853].[dbo].[Tb_HSPR_Contract].IsAuditing,
        [HM_wygl_new_1853].[dbo].[Tb_HSPR_Contract].RegDate,
        [HM_wygl_new_1853].[dbo].[Tb_HSPR_Community].CommName,
        [HM_wygl_new_1853].[dbo].[Tb_HSPR_Contract].AuditData,
        [HM_wygl_new_1853].[dbo].[Tb_HSPR_Community].CommID,
        [HM_wygl_new_1853].[dbo].[Tb_Sys_Organ].OrganCode,
        [HM_wygl_new_1853].[dbo].[Tb_Sys_Organ].OrganName
        from
        [HM_wygl_new_1853].[dbo].[Tb_HSPR_Contract]
        LEFT JOIN
        [HM_wygl_new_1853].[dbo].[Tb_HSPR_Community]
        ON
        [HM_wygl_new_1853].[dbo].[Tb_HSPR_Community].CommID=[HM_wygl_new_1853].[dbo].[Tb_HSPR_Contract].CommID
        LEFT JOIN
        [HM_wygl_new_1853].[dbo].[Tb_Sys_Organ]
        ON
        [HM_wygl_new_1853].[dbo].[Tb_Sys_Organ].OrganCode=[HM_wygl_new_1853].[dbo].[Tb_HSPR_Community].OrganCode
        <where>
            <if test="BipNum !=null and BipNum != ''">AND [HM_wygl_new_1853].[dbo].[Tb_HSPR_Contract].BipNum LIKE CONCAT('%',#{BipNum},'%')</if>
            <if test="ContName !=null and ContName != ''">AND [HM_wygl_new_1853].[dbo].[Tb_HSPR_Contract].ContName LIKE CONCAT('%',#{ContName},'%')</if>
            <if test="IsAuditing !=null and IsAuditing!=''">AND [HM_wygl_new_1853].[dbo].[Tb_HSPR_Contract].IsAuditing = #{IsAuditing}</if>
            <if test="CommName !=null and CommName != ''">AND [HM_wygl_new_1853].[dbo].[Tb_HSPR_Community].CommName LIKE CONCAT('%',#{CommName},'%')</if>
            <if test="OrganName !=null and OrganName != ''">AND [HM_wygl_new_1853].[dbo].[Tb_Sys_Organ].OrganName LIKE CONCAT('%',#{OrganName},'%')</if>
            <if test="CommId !=null and CommId!=0">AND [HM_wygl_new_1853].[dbo].[Tb_HSPR_Contract].CommID = #{CommId}</if>
            <if test="OrganCode !=null and OrganCode != ''">AND [HM_wygl_new_1853].[dbo].[Tb_Sys_Organ].OrganCode LIKE CONCAT('%',#{OrganCode},'%')</if>
            <if test="RegDateStart !=null and RegDateStart!=''">AND [HM_wygl_new_1853].[dbo].[Tb_HSPR_Contract].RegDate &gt;= #{RegDateStart}</if>
            <if test="RegDateEnd !=null and RegDateEnd!=''">AND [HM_wygl_new_1853].[dbo].[Tb_HSPR_Contract].RegDate &lt;= #{RegDateEnd}</if>
            and [HM_wygl_new_1853].[dbo].[Tb_HSPR_Contract].rentid=1853 and
            [HM_wygl_new_1853].[dbo].[Tb_HSPR_Community].rentid=1853 and
            [HM_wygl_new_1853].[dbo].[Tb_Sys_Organ].rentid=1853
        </where>
        order by [HM_wygl_new_1853].[dbo].[Tb_HSPR_Contract].RegDate desc
    </select>

    <!-- 已审核 -->
    <select id="contractStatisticalSelectYesExamine" parameterType="cn.com.countrygarden.bi.main.model.dup.ContractStatistical" resultMap="ContractStatisticalMap1" >
        select
        [HM_wygl_new_1853].[dbo].[Tb_HSPR_Contract].ContID,
        [HM_wygl_new_1853].[dbo].[Tb_HSPR_Contract].BipNum,
        [HM_wygl_new_1853].[dbo].[Tb_HSPR_Contract].ContName,
        [HM_wygl_new_1853].[dbo].[Tb_HSPR_Contract].IsAuditing,
        [HM_wygl_new_1853].[dbo].[Tb_HSPR_Contract].RegDate,
        [HM_wygl_new_1853].[dbo].[Tb_HSPR_Contract].AuditData,
        [HM_wygl_new_1853].[dbo].[Tb_HSPR_Community].CommName,
        [HM_wygl_new_1853].[dbo].[Tb_Sys_Organ].OrganCode,
        [HM_wygl_new_1853].[dbo].[Tb_Sys_Organ].OrganName
        from
        [HM_wygl_new_1853].[dbo].[Tb_HSPR_Contract]
        LEFT JOIN
        [HM_wygl_new_1853].[dbo].[Tb_HSPR_Community]
        ON
        [HM_wygl_new_1853].[dbo].[Tb_HSPR_Community].CommID=[HM_wygl_new_1853].[dbo].[Tb_HSPR_Contract].CommID
        LEFT JOIN
        [HM_wygl_new_1853].[dbo].[Tb_Sys_Organ]
        ON
        [HM_wygl_new_1853].[dbo].[Tb_Sys_Organ].OrganCode=[HM_wygl_new_1853].[dbo].[Tb_HSPR_Community].OrganCode
        <where>
            <if test="BipNum !=null and BipNum != ''">AND [HM_wygl_new_1853].[dbo].[Tb_HSPR_Contract].BipNum LIKE CONCAT('%',#{BipNum},'%')</if>
            <if test="ContName !=null and ContName != ''">AND [HM_wygl_new_1853].[dbo].[Tb_HSPR_Contract].ContName LIKE CONCAT('%',#{ContName},'%')</if>
            <if test="IsAuditings !=null">AND [HM_wygl_new_1853].[dbo].[Tb_HSPR_Contract].IsAuditing = #{IsAuditings}</if>
            <!--<if test="RegDate !=null">AND [HM_wygl_new_1853].[dbo].[Tb_HSPR_Contract].RegDate &gt; #{RegDate}</if>  -->
            <if test="CommName !=null and CommName != ''">AND [HM_wygl_new_1853].[dbo].[Tb_HSPR_Community].CommName LIKE CONCAT('%',#{CommName},'%')</if>
            <if test="OrganName !=null and OrganName != ''">AND [HM_wygl_new_1853].[dbo].[Tb_Sys_Organ].OrganName LIKE CONCAT('%',#{OrganName},'%')</if>
            <if test="CommId !=null and CommId!=0">AND [HM_wygl_new_1853].[dbo].[Tb_HSPR_Contract].CommID = #{CommId}</if>
            <if test="OrganCode !=null and OrganCode != ''">AND [HM_wygl_new_1853].[dbo].[Tb_Sys_Organ].OrganCode LIKE CONCAT('%',#{OrganCode},'%')</if>
            <if test="RegDateStart !=null and RegDateStart!=''">AND [HM_wygl_new_1853].[dbo].[Tb_HSPR_Contract].RegDate &gt;= #{RegDateStart}</if>
            <if test="RegDateEnd !=null and RegDateEnd!=''">AND [HM_wygl_new_1853].[dbo].[Tb_HSPR_Contract].RegDate &lt;= #{RegDateEnd}</if>
            and [HM_wygl_new_1853].[dbo].[Tb_HSPR_Contract].rentid=1853 and
            [HM_wygl_new_1853].[dbo].[Tb_HSPR_Community].rentid=1853 and
            [HM_wygl_new_1853].[dbo].[Tb_Sys_Organ].rentid=1853
        </where>
        order by [HM_wygl_new_1853].[dbo].[Tb_HSPR_Contract].RegDate desc
    </select>

    <!-- 已审核数量 -->
    <select id="contractStatisticalSelectYesExamineNumber" resultType="int">
    	select 
			COUNT([HM_wygl_new_1853].[dbo].[Tb_HSPR_Contract].CommID) as count1
		from  	
			[HM_wygl_new_1853].[dbo].[Tb_HSPR_Contract] 
		WHERE
			[HM_wygl_new_1853].[dbo].[Tb_HSPR_Contract].IsAuditing='1' and [HM_wygl_new_1853].[dbo].[Tb_HSPR_Contract].rentid=1853
    	
    </select>

    <!-- 未审核数量   审核状态为空的时候:默认为0-->
    <select id="contractStatisticalSelectNOExamineNumber"  resultType="int">
    SELECT
		COUNT (
			[HM_wygl_new_1853].[dbo].[Tb_HSPR_Contract].CommID
		) AS count1
	FROM
		[HM_wygl_new_1853].[dbo].[Tb_HSPR_Contract]
	WHERE
		([HM_wygl_new_1853].[dbo].[Tb_HSPR_Contract].IsAuditing = '0'
	OR [HM_wygl_new_1853].[dbo].[Tb_HSPR_Contract].IsAuditing is null) AND [HM_wygl_new_1853].[dbo].[Tb_HSPR_Contract].rentid=1853
    </select>

    <!-- 查询ERP数据库中是否存在该BIP流程单号 -->
    <select id="findBipNumber" parameterType="String" resultType="String">
    	select TOP 1
		  [HM_wygl_new_1853].[dbo].[Tb_HSPR_Contract].BipNum		
		from  	
			[HM_wygl_new_1853].[dbo].[Tb_HSPR_Contract] 
		WHERE
			[HM_wygl_new_1853].[dbo].[Tb_HSPR_Contract].BipNum =#{BipNum}
			and [HM_wygl_new_1853].[dbo].[Tb_HSPR_Contract].rentid=1853
		ORDER BY [HM_wygl_new_1853].[dbo].[Tb_HSPR_Contract].RegDate DESC
    </select>

    <!-- 查询所有的区域 -->
    <select id="findOrderCode" resultMap="ContractStatisticalMap1">
    	SELECT
			[HM_wygl_new_1853].[dbo].[Tb_Sys_Organ].OrganCode,
			[HM_wygl_new_1853].[dbo].[Tb_Sys_Organ].OrganName
		FROM
			[HM_wygl_new_1853].[dbo].[Tb_Sys_Organ] where rentid=1853
    </select>

    <!-- 查询对应区域下的项目 -->
    <select id="findCommName" parameterType="string" resultMap="ContractStatisticalMap1">
    	SELECT
			[HM_wygl_new_1853].[dbo].[Tb_HSPR_Community].CommName,
			[HM_wygl_new_1853].[dbo].[Tb_HSPR_Community].CommID,
			[HM_wygl_new_1853].[dbo].[Tb_HSPR_Community].OrganCode
		FROM
			[HM_wygl_new_1853].[dbo].[Tb_HSPR_Community]
		WHERE
			[HM_wygl_new_1853].[dbo].[Tb_HSPR_Community].OrganCode =#{OrganCode} AND [HM_wygl_new_1853].[dbo].[Tb_HSPR_Community].rentid=1853
    </select>

    <!-- 根据项目名称与区域名称获取对应的项目id与区域code -->
    <select id="findCommidAndOrganCode" parameterType="java.util.Map" resultType="java.util.Map">
    SELECT
		[HM_wygl_new_1853].[dbo].[Tb_HSPR_Community].CommName,
		[HM_wygl_new_1853].[dbo].[Tb_HSPR_Community].CommID,
		[HM_wygl_new_1853].[dbo].[Tb_HSPR_Community].OrganCode,
		[HM_wygl_new_1853].[dbo].[Tb_Sys_Organ].OrganName
	FROM
		[HM_wygl_new_1853].[dbo].[Tb_HSPR_Community]
	LEFT JOIN [HM_wygl_new_1853].[dbo].[Tb_Sys_Organ] 
	ON
	    [HM_wygl_new_1853].[dbo].[Tb_Sys_Organ].OrganCode = [HM_wygl_new_1853].[dbo].[Tb_HSPR_Community].OrganCode
	WHERE
	    [HM_wygl_new_1853].[dbo].[Tb_HSPR_Community].rentid=1853 and
	    [HM_wygl_new_1853].[dbo].[Tb_Sys_Organ].rentid=1853 and
		[HM_wygl_new_1853].[dbo].[Tb_Sys_Organ].OrganName =#{OrganName}
	AND [HM_wygl_new_1853].[dbo].[Tb_HSPR_Community].CommName =#{CommName}
    </select>

    <!-- 查询所有的项目 支持模糊查询项目 -->
    <select id="findAllCommName" parameterType="java.util.Map" resultMap="ContractStatisticalMap1">
        SELECT
        [HM_wygl_new_1853].[dbo].[Tb_HSPR_Community].CommName,
        [HM_wygl_new_1853].[dbo].[Tb_HSPR_Community].CommID,
        [HM_wygl_new_1853].[dbo].[Tb_HSPR_Community].OrganCode
        FROM
        [HM_wygl_new_1853].[dbo].[Tb_HSPR_Community]
        <where>
            <if test="CommName !=null and CommName!='' and CommName!='null'">[HM_wygl_new_1853].[dbo].[Tb_HSPR_Community].CommName LIKE CONCAT('%',#{CommName},'%')</if>
            and [HM_wygl_new_1853].[dbo].[Tb_HSPR_Community].rentid=1853
        </where>
    </select>

    <!-- 查询所有的流程编号, -->
    <select id="findAllBipNumber" resultType="String">
    SELECT 
  	  [HM_wygl_new_1853].[dbo].[Tb_HSPR_Contract].BipNum
    FROM
   		 [HM_wygl_new_1853].[dbo].[Tb_HSPR_Contract]
   		 where [HM_wygl_new_1853].[dbo].[Tb_HSPR_Contract].rentid=1853
    </select>
    <select id="findErpRegionList" resultType="java.lang.String">
   select DISTINCT o.OrganName region_name
         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_HSPR_Community] AS c ON c.CommID = d.CommID AND c.isDelete = 0 and d.rentid=c.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 u.rentid=1853
        group by o.organcode,o.organname
    </select>
    <select id="findErpRegionByUserCodeIn" resultType="java.lang.String">
        SELECT  top 3
        u.UserName
        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_HSPR_Community] AS c ON c.CommID = d.CommID and (c.rentid=d.rentid)
        AND c.isDelete = 0
        INNER JOIN [HM_wygl_new_1853].[dbo].[Tb_Sys_Organ] AS o ON o.OrganCode = c.OrganCode and (o.rentid=c.rentid)
        AND o.isDelete = 0
        LEFT JOIN [HM_wygl_new_1853].[dbo].[Tb_HSPR_BasicInfo] i ON i.CommID = c.CommID and (i.rentid=c.rentid)
        AND ISNULL( i.IsDelete, 0 ) = 0
        WHERE
        d.CommID> 0
        and u.rentid=1853
        and  u.IsDelete = 0   AND u.UserCode in
        <foreach item="item" index="index" collection="userCode" open="(" separator="," close=")">
            #{item}
        </foreach>
        and (o.OrganName LIKE CONCAT('%',#{regionName},'%'))
        GROUP BY u.UserName
    </select>
    <resultMap id="BaseRegionProjectDtoMap" type="cn.com.countrygarden.bi.main.dto.wcx.WcxRegionProjectDto">
        <result column="region_name" property="regionName"/>
        <result column="projectName" property="projectName"/>
        <result column="user_id" property="userId"/>
        <result column="regionPrizeInnNumber" property="regionPrizeInnNumber"/>
        <result column="projectPrizeInnNumber" property="projectPrizeInnNumber"/>
    </resultMap>

    <!-- 查询项目对应的区域 ferren -->
    <select id="findRegionByProjectName" resultMap="BaseRegionProjectDtoMap">
		SELECT DISTINCT
		region.OrganName region_name
		FROM [HM_wygl_new_1853].[dbo].[Tb_HSPR_Community] project
		JOIN [HM_wygl_new_1853].[dbo].[Tb_Sys_Organ] region ON region.OrganCode = project.OrganCode
		WHERE project.CommName = #{projectName} AND project.IsDelete = 0 and project.rentid=1853 and region.rentid=1853
	</select>

    <select id="getErpProjectCode" resultType="string">
        SELECT erpProject.CommID
        FROM [HM_wygl_new_1853].[dbo].[Tb_HSPR_Community] erpProject
        WHERE erpProject.CommName = #{erpProjectName} AND erpProject.IsDelete = 0 and erpProject.rentid=1853
    </select>
	<!-- 避免erp可能存在的数据问题这里返回list -->
	<select id="findErpRegionByUserCode" resultMap="BaseRegionProjectDtoMap">
       SELECT DISTINCT d2.DepName region_name
        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') and u.rentid=d.rentid
        JOIN [HM_wygl_new_1853].[dbo].[Tb_Sys_Department] d2 on d2.SortDepCode = LEFT(d.SortDepCode, 8) and u.rentid=d2.rentid
        WHERE u.IsDelete = 0 AND u.UserCode = #{userCode} and u.rentid=1853
    </select>

    <select id="findErpRegin" resultType="java.util.Map" parameterType="java.lang.String">
        SELECT
          d2.DepName commName,
          (select CommID from [HM_wygl_new_1853].[dbo].[Tb_HSPR_Community] where rentid=1853 and CommName=d2.DepName and COALESCE(IsDelete,0) = 0) commId
        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.rentid=u.rentid  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, 12) and d2.rentid=d.rentid
        WHERE
            u.IsDelete = 0 and u.rentid=1853
            AND u.UserCode = #{userCode}
    </select>
   <select id="getOrganName" resultType="string">
        select OrganCode FROM [HM_wygl_new_1853].[dbo].[Tb_Sys_Organ] where OrganName =#{userCode} AND rentid=1853
    </select>

       <select id="getOrgan" resultType="string">
        select OrganName FROM [HM_wygl_new_1853].[dbo].[Tb_Sys_Organ] where OrganCode =#{organCode} AND rentid=1853
    </select>
    
    
      <select id="getCommunity" resultType="java.util.Map">
        select CommName,OrganCode FROM [HM_wygl_new_1853].[dbo].[Tb_HSPR_Community] where CommID =#{commID} AND rentid=1853
    </select>

    <select id="findOrganByUserCode" resultType="java.util.Map" >
    	        select OrganCode,region_name as organName from (SELECT d2.DepName region_name
        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) and d.rentid=1853
        WHERE u.IsDelete = 0 AND u.UserCode = #{userCode} and u.rentid=1853) NAME,[HM_wygl_new_1853].[dbo].[Tb_Sys_Organ] organ where organ.OrganName=region_name and rentid=1853
    </select>
    
    <select id="getCommUser" resultType="java.util.Map" >
    	SELECT
			u.UserCode userCode,
			d.DepName depName,
			u.UserName handUser,
			d.DepName+'-'+u.UserName as  duName
		FROM
			HM_wygl_new_1853.[dbo].[Tb_Sys_Department] d
		LEFT JOIN HM_wygl_new_1853.[dbo].[Tb_Sys_User] u ON d.DepCode = u.DepCode and d.rentid=u.rentid
		WHERE
			u.IsDelete = '0' AND d.rentid=1853 and
		<if test="list.size() > 0 ">
		AND u.UserCode in
		<foreach collection="list" item="item" open="(" separator="," close=")">
			#{item}
		</foreach>
		</if>
    </select>

    <select id="areaquname" resultType="cn.com.countrygarden.bi.main.dto.ContractStatisticalarpojnameDTO" parameterType="string">
                SELECT o.OrganCode as areaid,o.OrganName as areaname,i.Names as arname
               FROM HM_wygl_new_1853.dbo.Tb_HSPR_RegionAvatarInfo i
                   INNER JOIN HM_wygl_new_1853.dbo.Tb_Sys_Organ o WITH(NOLOCK)
                           ON o.OrganCode = i.OrganCode AND i.CommID = 0 AND o.IsDelete = 0 and o.rentid=i.rentid
              where o.OrganCode=#{areaid} and i.rentid=1853
     
    </select>
    
    <select id="projectname" resultType="cn.com.countrygarden.bi.main.dto.ContractStatisticalarpojnameDTO" parameterType="java.lang.Integer">
          SELECT c.CommID as projectid,c.CommName as projectname,i.Names as prname
          FROM HM_wygl_new_1853.dbo.Tb_HSPR_RegionAvatarInfo i
            INNER JOIN HM_wygl_new_1853.dbo.Tb_HSPR_Community c WITH(NOLOCK)
            ON c.CommID = i.CommID AND c.IsDelete = 0 and c.rentid=i.rentid
           where c.CommID=#{projectId} and i.rentid=1853
    
    </select>
    
    <select id="findErpUserCodes" resultType="String" parameterType="String">
	SELECT DISTINCT UserCode 
        FROM [HM_wygl_new_1853].[dbo].[Tb_Sys_User] where DepCode in (
         SELECT d2.DepCode
        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') and d.rentid=u.rentid
        JOIN [HM_wygl_new_1853].[dbo].[Tb_Sys_Department] d2 on d2.SortDepCode = LEFT(d.SortDepCode, 8) and d.rentid=d2.rentid
        WHERE u.IsDelete = 0 and u.rentid=1853 AND u.UserCode = #{userCode}
        )
    </select>
    
    <select id="findErpRegionByUserquyu" resultType="java.lang.String">
        select OrganName from [HM_wygl_new_1853].[dbo].[Tb_Sys_Organ] WHERE rentid=1853 and OrganCode in
          (
             SELECT OrganId FROM [HM_wygl_new_1853].[dbo].[Tb_Sys_User] WHERE UserCode=#{userCode} and rentid=1853
          )
    </select>

    <select id="findArea" resultType="String">
        SELECT 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 and u.rentid=ur.rentid
        INNER JOIN [HM_wygl_new_1853].[dbo].Tb_Sys_Role r ON r.RoleCode = ur.RoleCode and r.rentid=ur.rentid
        INNER JOIN [HM_wygl_new_1853].[dbo].Tb_Sys_RoleData rd ON rd.RoleCode = r.RoleCode and rd.rentid=r.rentid
        INNER JOIN [HM_wygl_new_1853].[dbo].Tb_HSPR_Community c ON c.CommID = rd.CommID  and c.rentid=rd.rentid and COALESCE(c.IsDelete,0) = 0
        left join [HM_wygl_new_1853].[dbo].[Tb_Sys_Organ] a ON	a.OrganCode = c.OrganCode and a.rentid=c.rentid and a.isdelete = 0
        where u.UserCode = #{userCode} and u.rentid=1853
        group by c.OrganCode
    </select>

    <select id="findProject" resultType="String">
        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 and u.rentid=ur.rentid
        INNER JOIN [HM_wygl_new_1853].[dbo].Tb_Sys_Role r ON r.RoleCode = ur.RoleCode and r.rentid=ur.rentid
        INNER JOIN [HM_wygl_new_1853].[dbo].Tb_Sys_RoleData rd ON rd.RoleCode = r.RoleCode and rd.rentid=r.rentid
        INNER JOIN [HM_wygl_new_1853].[dbo].Tb_HSPR_Community c ON c.CommID = rd.CommID and COALESCE(c.IsDelete,0) = 0 and c.rentid=rd.rentid
        left join [HM_wygl_new_1853].[dbo].[Tb_Sys_Organ] a ON	a.OrganCode = c.OrganCode and a.isdelete = 0 and a.rentid=c.rentid
        where u.UserCode = #{userCode} and u.rentid=1853
    </select>

    <select id="getAreasByLists" resultType="String">
        SELECT distinct
        a.OrganCode
        FROM
       [HM_wygl_new_1853].[dbo].Tb_HSPR_Community c
        INNER join [HM_wygl_new_1853].[dbo].[Tb_Sys_Organ] a ON	a.OrganCode = c.OrganCode
        WHERE
        a.isdelete = 0 and c.rentid=1853 and a.rentid=1853
        and ISNULL(c.IsDelete,0) = 0
        <if test=" areaIds!= null and  areaIds.size()>0">
            AND	c.CommID IN
            <foreach item="item" index="index" collection="areaIds" open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>
    </select>

</mapper>