<?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.extension.ExtensionErpBindingMapper">
    <resultMap id="ErpResultMap" type="cn.com.countrygarden.bi.main.dto.extension.ExtensionErpDTO">
        <result column="CommID" property="id"/>
        <result column="PropertyArea" property="propertyArea"/>
        <result column="AreaNumber" property="areaNumber"/>
        <result column="PropertyProject" property="propertyProject"/>
        <result column="CommKind" property="commKind"/>
        <result column="ProjectNumber" property="projectNumber"/>
        <result column="ProjectType" property="projectType"/>
        <result column="Province"  property="province" />
    </resultMap>

    <resultMap id="bindingMap" type="cn.com.countrygarden.bi.main.dto.extension.ExtensionReportDTO" >
        <id column="CommID" property="erpId"  />
        <result column="PropertyArea" property="propertyArea"  />
        <result column="AreaNumber" property="areaNumber"  />
        <result column="PropertyProject" property="propertyProject"  />
        <result column="CommKind" property="commKind"  />
        <result column="ProjectNumber" property="projectNumber"  />
        <result column="ProjectType" property="projectType"  />
        <result column="Province" property="province"  />
        <result column="City" property="city"  />
        <result column="Borough" property="borough"  />
        <result column="Region" property="region"  />
        <result column="PropertyName" property="propertyName"  />
        <result column="PropertyForm" property="propertyForm"  />
        <result column="ParentCompany" property="parentCompany"  />
        <result column="NCSystemAccount" property="ncSystemAccount"  />
        <result column="NCSystemNumber" property="ncSystemNumber"  />
        <result column="NCPersonNumber" property="ncPersonNumber"  />
        <result column="FundType" property="fundType"  />
        <result column="ConstUnitName" property="developmentSubjectNames"  />
        <result column="DevelopmentSubject" property="developmentSubject"  />
        <result column="DevelopmentState" property="developmentState"  />
        <result column="TakeoverState" property="takeoverState"  />
        <result column="ActualEnterTime" property="actualEnterTime"  />
        <result column="StateProperty" property="stateProperty"  />
        <result column="ConventionalCharges" property="conventionalCharges"  />
        <result column="ConventionalService" property="conventionalService"  />
        <result column="SalesFieldCharges" property="salesFieldCharges"  />
        <result column="SalesFieldService" property="salesFieldService"  />
        <result column="ActualOpen" property="actualOpen"  />
        <result column="PlannedOpen" property="plannedOpen"  />
        <result column="ContractProperty" property="contractPropertyDate"  />
        <result column="ActualProperty" property="actualProperty"  />
        <result column="EndCaseDate" property="endCaseDate"  />
        <result column="EndEstateManagementDate" property="endEstateManagementDate"  />
        <result column="ManageTime" property="manageTime"  />
        <collection property="developmentSubjectNameList" ofType="java.lang.String" >
            <result column="ConstUnitName" />
        </collection>
    </resultMap>

    <!-- 查询 全部 ERP 数据 -->
    <select id="selectAllByDTO" resultMap="ErpResultMap" >
        SELECT
        hc.CommID,
        hb.PropertyArea,
        hb.AreaNumber,
        hb.PropertyProject,
        hc.CommKind,
        hb.ProjectNumber,
        hb.ProjectType,
        hc.Province
        FROM
        [HM_wygl_new_1853].[dbo].[Tb_HSPR_Community] hc
        LEFT JOIN [HM_wygl_new_1853].[dbo].[Tb_HSPR_BasicInfo] hb ON hc.CommID = hb.CommID and hb.isDelete = 0 and hb.rentid=1853
        LEFT JOIN
        [HM_wygl_new_1853].[dbo].[Tb_Sys_Organ] so ON so.OrganCode = hc.OrganCode and so.rentid=1853
        <where>
            hc.IsDelete = 0 and hb.DevelopmentSubject = '外拓'
            <if test="dto.propertyArea != null and dto.propertyArea != ''"> AND hb.PropertyArea in
                <foreach collection="dto.propertyArea.split(',')" item="str" open="(" separator="," close=")" >
                    '${str}'
                </foreach>
            </if>
            <if test="dto.propertyProject != null and dto.propertyProject != ''"> AND hb.PropertyProject LIKE CONCAT('%',#{dto.propertyProject},'%') </if>
            <if test="dto.developmentState != null and dto.developmentState != ''"> AND hb.DevelopmentState = #{dto.developmentState} </if>
            <if test="dto.conventionalCharges != null and dto.conventionalCharges != ''"> AND hb.ConventionalCharges = #{dto.conventionalCharges} </if>
            <if test="dto.conventionalService != null and dto.conventionalService != ''"> AND hb.ConventionalService = #{dto.conventionalService} </if>
            <if test="dto.propertyForm != null and dto.propertyForm != ''"> AND hb.PropertyForm = #{dto.propertyForm} </if>
            <if test="dto.projectType != null and dto.projectType != ''"> AND hb.ProjectType = #{dto.projectType} </if>
            <if test="organName != null and organName != ''" >
                AND so.OrganName = #{organName}
            </if>
            and hc.rentid=1853
        </where>
        ORDER BY hb.PropertyArea DESC
    </select>

    <!-- 导出已录入 ERP 数据 -->
    <select id="selectExportByDto" resultMap="ErpResultMap" >
        SELECT
        hc.CommID,
        hb.PropertyArea,
        hb.AreaNumber,
        hb.PropertyProject,
        hc.CommKind,
        hb.ProjectNumber,
        hb.ProjectType,
        hc.Province
        FROM
        [HM_wygl_new_1853].[dbo].[Tb_HSPR_Community] hc
        LEFT JOIN [HM_wygl_new_1853].[dbo].[Tb_HSPR_BasicInfo] hb ON hc.CommID = hb.CommID and hb.isDelete = 0
        LEFT JOIN
        [HM_wygl_new_1853].[dbo].[Tb_Sys_Organ] so ON so.OrganCode = hc.OrganCode
        <where>
            hc.IsDelete = 0 and hb.DevelopmentSubject = '外拓'
            <if test="dto.propertyArea != null and dto.propertyArea != ''"> AND hb.PropertyArea in
                <foreach collection="dto.propertyArea.split(',')" item="str" open="(" separator="," close=")" >
                    '${str}'
                </foreach>
            </if>
            <if test="dto.propertyProject != null and dto.propertyProject != ''"> AND hb.PropertyProject LIKE CONCAT('%',#{dto.propertyProject},'%') </if>
            <if test="dto.developmentState != null and dto.developmentState != ''"> AND hb.DevelopmentState = #{dto.developmentState} </if>
            <if test="dto.conventionalCharges != null and dto.conventionalCharges != ''"> AND hb.ConventionalCharges = #{dto.conventionalCharges} </if>
            <if test="dto.conventionalService != null and dto.conventionalService != ''"> AND hb.ConventionalService = #{dto.conventionalService} </if>
            <if test="dto.propertyForm != null and dto.propertyForm != ''"> AND hb.PropertyForm = #{dto.propertyForm} </if>
            <if test="dto.projectType != null and dto.projectType != ''"> AND hb.ProjectType = #{dto.projectType} </if>
            <if test="organName != null and organName != ''" >
                AND so.OrganName = #{organName}
            </if>
        </where>
        ORDER BY hb.PropertyArea DESC
    </select>

    <!-- 查询 未绑定的 ERP 数据 -->
    <select id="selectErpUnbinding"  resultMap="ErpResultMap" >
        SELECT
        hc.CommID,
        hb.PropertyArea,
        hb.AreaNumber,
        hb.PropertyProject,
        hc.CommKind,
        hb.ProjectNumber,
        hb.ProjectType,
        hc.Province
        FROM
        [HM_wygl_new_1853].[dbo].[Tb_HSPR_Community] hc
        LEFT JOIN [HM_wygl_new_1853].[dbo].[Tb_HSPR_BasicInfo] hb ON hc.CommID = hb.CommID and hb.isDelete = 0 and hb.rentid=1853
        LEFT JOIN
        [HM_wygl_new_1853].[dbo].[Tb_Sys_Organ] so ON so.OrganCode = hc.OrganCode and so.rentid=1853
        <where>
            hc.IsDelete = 0 and hb.DevelopmentSubject = '外拓'
            <if test="ids != null and ids.size > 0">
                AND hc.CommID  NOT IN
                <foreach collection="ids" open="(" separator="," close=")" item="id" >
                    #{id}
                </foreach>
            </if>
            <if test="dto.propertyArea != null and dto.propertyArea != ''"> AND hb.PropertyArea in
                <foreach collection="dto.propertyArea.split(',')" item="str" open="(" separator="," close=")" >
                    '${str}'
                </foreach>
            </if>
            <if test="dto.propertyProject != null and dto.propertyProject != ''"> AND hb.PropertyProject LIKE CONCAT('%',#{dto.propertyProject},'%') </if>
            <if test="dto.developmentState != null and dto.developmentState != ''"> AND hb.DevelopmentState = #{dto.developmentState} </if>
            <if test="dto.conventionalCharges != null and dto.conventionalCharges != ''"> AND hb.ConventionalCharges = #{dto.conventionalCharges} </if>
            <if test="dto.conventionalService != null and dto.conventionalService != ''"> AND hb.ConventionalService = #{dto.conventionalService} </if>
            <if test="dto.propertyForm != null and dto.propertyForm != ''"> AND hb.PropertyForm = #{dto.propertyForm} </if>
            <if test="dto.projectType != null and dto.projectType != ''"> AND hb.ProjectType = #{dto.projectType} </if>
            <if test="organName != null and organName != ''" >
                AND so.OrganName = #{organName}
            </if>
            and hc.rentid=1853
        </where>
        ORDER BY hb.PropertyArea DESC
    </select>

    <!-- 导出 未绑定的 ERP 数据 -->
    <select id="selectUnbindingExportByDto"  resultMap="ErpResultMap" >
        SELECT
        hc.CommID,
        hb.PropertyArea,
        hb.AreaNumber,
        hb.PropertyProject,
        hc.CommKind,
        hb.ProjectNumber,
        hb.ProjectType,
        hc.Province
        FROM
        [HM_wygl_new_1853].[dbo].[Tb_HSPR_Community] hc
        LEFT JOIN [HM_wygl_new_1853].[dbo].[Tb_HSPR_BasicInfo] hb ON hc.CommID = hb.CommID and hb.isDelete = 0
        LEFT JOIN
        [HM_wygl_new_1853].[dbo].[Tb_Sys_Organ] so ON so.OrganCode = hc.OrganCode
        <where>
            hc.IsDelete = 0 and hb.DevelopmentSubject = '外拓'
            <if test="ids != null and ids.size > 0">
                AND hc.CommID  NOT IN
                <foreach collection="ids" open="(" separator="," close=")" item="id" >
                    #{id}
                </foreach>
            </if>
            <if test="dto.propertyArea != null and dto.propertyArea != ''"> AND hb.PropertyArea in
                <foreach collection="dto.propertyArea.split(',')" item="str" open="(" separator="," close=")" >
                    '${str}'
                </foreach>
            </if>
            <if test="dto.propertyProject != null and dto.propertyProject != ''"> AND hb.PropertyProject LIKE CONCAT('%',#{dto.propertyProject},'%') </if>
            <if test="dto.developmentState != null and dto.developmentState != ''"> AND hb.DevelopmentState = #{dto.developmentState} </if>
            <if test="dto.conventionalCharges != null and dto.conventionalCharges != ''"> AND hb.ConventionalCharges = #{dto.conventionalCharges} </if>
            <if test="dto.conventionalService != null and dto.conventionalService != ''"> AND hb.ConventionalService = #{dto.conventionalService} </if>
            <if test="dto.propertyForm != null and dto.propertyForm != ''"> AND hb.PropertyForm = #{dto.propertyForm} </if>
            <if test="dto.projectType != null and dto.projectType != ''"> AND hb.ProjectType = #{dto.projectType} </if>
            <if test="organName != null and organName != ''" >
                AND so.OrganName = #{organName}
            </if>
        </where>
        ORDER BY hb.PropertyArea DESC
    </select>

    <select id="selectById" resultMap="bindingMap">
        SELECT
            hc.CommID                   ,
            hb.PropertyArea             ,
            hb.AreaNumber               ,
            hb.PropertyProject          ,
            hc.CommKind				 ,
            hb.ProjectNumber			 ,
            hb.ProjectType				 ,
            hc.Province				 ,
            hc.City					 ,
            hc.Borough					 ,
            hb.Region					 ,
            hb.PropertyName			 ,
            hb.PropertyForm             ,
            hb.ParentCompany			 ,
            hb.NCSystemAccount			 ,
            hb.NCSystemNumber			 ,
            hb.NCPersonNumber			 ,
            hb.FundType				 ,
            hb.DevelopmentSubject		 ,
            hbc.ConstUnitName            ,
            hb.DevelopmentState		 ,
            hb.TakeoverState			 ,
            hb.ActualEnterTime			 ,
            hb.StateProperty			 ,
            hb.ConventionalCharges      ,
            hb.ConventionalService      ,
            hb.SalesFieldCharges		 ,
            hb.SalesFieldService		 ,
            hb.ActualOpen				 ,
            hb.PlannedOpen				 ,
            hb.ContractProperty		 ,
            hb.ActualProperty			 ,
            hb.EndCaseDate				 ,
            hb.EndEstateManagementDate      ,
            hc.ManageTime
        FROM
            [HM_wygl_new_1853].[dbo].[Tb_HSPR_Community] hc
            LEFT JOIN
            [HM_wygl_new_1853].[dbo].[Tb_HSPR_BasicInfo] hb ON hc.CommID = hb.CommID and hb.isDelete = 0
            LEFT JOIN
            [HM_wygl_new_1853].[dbo].[Tb_HSPR_BasicInfoCom] hbc ON hc.CommID = hbc.CommID and hbc.isDelete = 0
        WHERE  hc.IsDelete = 0 AND hc.CommID = #{id} and hb.DevelopmentSubject = '外拓'
    </select>

    <select id="selectAllCount" resultType="java.lang.Long">
        SELECT COUNT(0) FROM [HM_wygl_new_1853].[dbo].[Tb_HSPR_Community] hc
        LEFT JOIN
        [HM_wygl_new_1853].[dbo].[Tb_HSPR_BasicInfo] hb ON hc.CommID = hb.CommID and hb.isDelete = 0 and hc.rentid=hb.rentid
        LEFT JOIN
        [HM_wygl_new_1853].[dbo].[Tb_Sys_Organ] so ON so.OrganCode = hc.OrganCode AND hc.rentid=so.rentid
        WHERE  hc.IsDelete = 0 and hb.DevelopmentSubject = '外拓' and hc.rentid=1853
        <if test="organName != null and organName != ''" >
            AND so.OrganName = #{organName}
        </if>
    </select>

    <select id="selectUnbindingCount" resultType="java.lang.Long" >
        SELECT COUNT(0) FROM [HM_wygl_new_1853].[dbo].[Tb_HSPR_Community] hc
        LEFT JOIN
        [HM_wygl_new_1853].[dbo].[Tb_HSPR_BasicInfo] hb ON hc.CommID = hb.CommID and hb.isDelete = 0 and hc.rentid=hb.rentid
        LEFT JOIN
        [HM_wygl_new_1853].[dbo].[Tb_Sys_Organ] so ON so.OrganCode = hc.OrganCode AND hc.rentid=so.rentid
        WHERE  hc.IsDelete = 0 and hb.DevelopmentSubject = '外拓' AND hc.rentid=1853
        <if test="ids != null and ids.size > 0">
            AND hc.CommID  NOT IN
            <foreach collection="ids" open="(" separator="," close=")" item="id" >
                #{id}
            </foreach>
        </if>
        <if test="organName != null and organName != ''" >
            AND so.OrganName = #{organName}
        </if>
    </select>


</mapper>