<?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.districtReport.RdNewProjectMapper">
    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="cn.com.countrygarden.bi.main.model.districtReport.RdNewProject">
                                    <id column="id" property="id" />
                                                <result column="district_code" property="districtCode" />
                                                <result column="project_code" property="projectCode" />
                                                <result column="project_name" property="projectName" />
                                                <result column="plan_place_area" property="planPlaceArea" />
                                                <result column="total_build_area" property="totalBuildArea" />
                                                <result column="building_type" property="buildingType" />
                                                <result column="plani_total_households" property="planiTotalHouseholds" />
                                                <result column="property_price" property="propertyPrice" />
                                                <result column="exhibition_start_date" property="exhibitionStartDate" />
                                                <result column="exhibition_area" property="exhibitionArea" />
                                                <result column="comprehensive_area" property="comprehensiveArea" />
                                                <result column="demonstration_area" property="demonstrationArea" />
                                                <result column="start_date" property="startDate" />
                                                <result column="end_date" property="endDate" />
                                                <result column="sync_date" property="syncDate" />
                                                <result column="sync_usercode" property="syncUsercode" />
                                                <result column="ENABLE" property="enable" />
                                                <result column="create_time" property="createTime" />
                                                <result column="create_by" property="createBy" />
                                                <result column="create_name" property="createName" />
                                                <result column="update_time" property="updateTime" />
                                                <result column="update_by" property="updateBy" />
                                                <result column="update_name" property="updateName" />
                        </resultMap>

    <select id="queryPageList" resultType="cn.com.countrygarden.bi.main.dto.districtReport.RdNewProjectDTO">
            SELECT DISTINCT p.id as id,p.district_code as districtCode,d.district_name as districtName,d.region_code as regionCode,d.region_name as regionName,p.project_code as projectCode,
            p.project_name as projectName,p.plan_place_area as planPlaceArea,
            p.total_build_area as totalBuildArea,p.building_type as buildingType,p.plani_total_households as planiTotalHouseholds,p.property_price as propertyPrice,
            p.exhibition_start_date as exhibitionStartDateString,p.exhibition_area as exhibitionArea,p.comprehensive_area as comprehensiveArea
            ,p.demonstration_area as demonstrationArea,p.start_date as startDateString,p.end_date as endDateString,p.sync_date as syncDate,p.sync_usercode as syncUsercode,
            p.create_by as createBy,p.create_name as createName,p.create_time as createTime FROM rd_new_project p
            LEFT JOIN rd_district_info d ON d.district_code=p.district_code
            LEFT JOIN rd_district_principal rdp ON d.district_code=rdp.district_code
            <where>
                <if test="organCodeString!=null and organCodeString.size()!=0">
                    and d.region_code in
                    <foreach collection="organCodeString" item="organCodeString" index="index" open="(" close=")" separator=",">
                        #{organCodeString}
                    </foreach>
                </if>
                <if test="commIdString!=null and commIdString.size()!=0">
                    and p.project_code in
                    <foreach collection="commIdString" item="commIdString" index="index" open="(" close=")" separator=",">
                        #{commIdString}
                    </foreach>
                </if>
                <if test="principaString!=null and principaString.size()!=0">
                    and rdp.principal_code in
                    <foreach collection="principaString" item="principaString" index="index" open="(" close=")" separator=",">
                        #{principaString}
                    </foreach>
                    AND rdp.ENABLE=1
                </if>
                <if test="rdNewProjectDTO.regionCode!=null and rdNewProjectDTO.regionCode!=''">
                    and d.region_code=#{rdNewProjectDTO.regionCode}
                </if>
                <if test="rdNewProjectDTO.districtCode!=null and rdNewProjectDTO.districtCode!=''">
                    and p.district_code=#{rdNewProjectDTO.districtCode}
                </if>
                <if test="rdNewProjectDTO.projectCode!=null and rdNewProjectDTO.projectCode!=''">
                    and p.project_code=#{rdNewProjectDTO.projectCode}
                </if>
                <if test="rdNewProjectDTO.buildingType!=null and rdNewProjectDTO.buildingType!=''">
                    and FIND_IN_SET(#{rdNewProjectDTO.buildingType},p.building_type)!=0
                </if>
                <if test="rdNewProjectDTO.createBy!=null and rdNewProjectDTO.createBy!=''">
                    and p.create_by=#{rdNewProjectDTO.createBy}
                </if>
                and p.ENABLE=1
            </where>
        ORDER BY p.project_code,p.district_code,p.create_time
    </select>

    <select id="queryById" resultType="cn.com.countrygarden.bi.main.dto.districtReport.RdNewProjectDTO">
           SELECT DISTINCT p.id as id,p.district_code as districtCode,d.district_name as districtName,d.region_code as regionCode,d.region_name as regionName,p.project_code as projectCode,
            p.project_name as projectName,p.plan_place_area as planPlaceArea,
            p.total_build_area as totalBuildArea,p.building_type as buildingType,p.plani_total_households as planiTotalHouseholds,p.property_price as propertyPrice,
            p.exhibition_start_date as exhibitionStartDate,p.exhibition_area as exhibitionArea,p.comprehensive_area as comprehensiveArea
            ,p.demonstration_area as demonstrationArea,p.start_date as startDate,p.end_date as endDate,p.sync_date as syncDate,p.sync_usercode as syncUsercode,
            p.create_by as createBy,p.create_time as createTime FROM rd_new_project p
            LEFT JOIN rd_district_info d ON d.district_code=p.district_code
        <where>
              <if test="id!=null">
                   and p.id=#{id}
              </if>
            and p.ENABLE=1
        </where>
    </select>

    <select id="rdNewProjectAddOne" resultType="cn.com.countrygarden.bi.main.dto.districtReport.RdNewProjectDTO">
        SELECT DISTINCT p.id as id,p.district_code as districtCode,d.district_name as districtName,d.region_code as regionCode,d.region_name as regionName,p.project_code as projectCode,
        p.project_name as projectName,p.plan_place_area as planPlaceArea,
        p.total_build_area as totalBuildArea,p.building_type as buildingType,p.plani_total_households as planiTotalHouseholds,p.property_price as propertyPrice,
        p.exhibition_start_date as exhibitionStartDate,p.exhibition_area as exhibitionArea,p.comprehensive_area as comprehensiveArea
        ,p.demonstration_area as demonstrationArea,p.start_date as startDate,p.end_date as endDate,p.sync_date as syncDate,p.sync_usercode as syncUsercode,
        p.create_by as createBy,p.create_time as createTime FROM rd_new_project p
        LEFT JOIN rd_district_info d ON d.district_code=p.district_code
        <where>
            <if test="districtCode!=null and districtCode!=''">
                and p.district_code=#{districtCode}
            </if>
            <if test="projectCode!=null and projectCode!=''">
                and p.project_code=#{projectCode}
            </if>
            <if test="createdBy!=null and createdBy!=''">
                and p.create_by=#{createdBy}
            </if>
            and p.ENABLE=1
        </where>
    </select>

    <select id="RdNewProjectAddone" resultType="int">
             SELECT COUNT(DISTINCT p.project_code) FROM rd_new_project p
            LEFT JOIN rd_district_info d ON d.district_code=p.district_code
        <where>
            <!--<if test="districtCode!=null and districtCode!=''">-->
                <!--and p.district_code=#{districtCode}-->
            <!--</if>-->
            <if test="projectCode!=null and projectCode!=''">
                and p.project_code=#{projectCode}
            </if>

            <!--<if test="exhibitionArea!=null">-->
                <!--and p.exhibition_area=#{exhibitionArea}-->
            <!--</if>-->
            <!--<if test="comprehensiveArea!=null">-->
                <!--and p.comprehensive_area=#{comprehensiveArea}-->
            <!--</if>-->
            <!--<if test="demonstrationArea!=null">-->
                <!--and p.demonstration_area=#{demonstrationArea}-->
            <!--</if>-->

            and p.ENABLE=1
        </where>
    </select>

    <update id="edit">
          UPDATE rd_new_project
          <set>
              <if test="districtCode!=null and districtCode!=''">district_code = #{districtCode},</if>
              <if test="projectCode!=null and projectCode!=''">project_code = #{projectCode},</if>
              <if test="projectName!=null and projectName!=''">project_name = #{projectName},</if>
              <if test="planPlaceArea!=null">plan_place_area = #{planPlaceArea},</if>
              <if test="totalBuildArea!=null">total_build_area = #{totalBuildArea},</if>
              <if test="buildingType!=null and buildingType!=''">building_type = #{buildingType},</if>
              <if test="planiTotalHouseholds!=null">plani_total_households = #{planiTotalHouseholds},</if>
              <if test="propertyPrice!=null and propertyPrice!=''">property_price = #{propertyPrice},</if>
              <if test="exhibitionStartDate!=null">exhibition_start_date = #{exhibitionStartDate},</if>
              <if test="exhibitionArea!=null">exhibition_area = #{exhibitionArea},</if>
              <if test="comprehensiveArea!=null">comprehensive_area = #{comprehensiveArea},</if>
              <if test="demonstrationArea!=null">demonstration_area = #{demonstrationArea},</if>
              <if test="startDate!=null">start_date = #{startDate},</if>
              <if test="endDate!=null">end_date = #{endDate},</if>
              <if test="syncDate!=null">sync_date = #{syncDate},</if>
              <if test="syncUsercode!=null and syncUsercode!=''">sync_usercode = #{syncUsercode},</if>
              <if test="enable!=null">ENABLE = #{enable},</if>
              <if test="updateTime!=null">update_time = #{updateTime},</if>
              <if test="updateBy!=null and updateBy!=''">update_by = #{updateBy},</if>
          </set>
	        WHERE
                 id = #{id};
    </update>

    <update id="deleteUpdate">
          UPDATE rd_new_project SET ENABLE = 0,update_time=NOW(),update_by=#{updateBy},update_name=#{updateName}
          WHERE id = #{id};
    </update>

    <update id="delpingliangtag">
        UPDATE rd_new_project set ENABLE=0,update_time=NOW(),update_by=#{updateBy},update_name=#{updateName}
        <where>
            <if test="rids!=null and rids.size()!=0">
                id in
                <foreach collection="rids" item="rids" index="index" open="(" close=")" separator=",">
                    #{rids}
                </foreach>

            </if>
        </where>
    </update>

    <select id="evaluationRdNewProject" resultType="cn.com.countrygarden.bi.main.dto.districtReport.RdNewProjectVO">
        SELECT DISTINCT p.id as id,p.district_code as districtCode,d.district_name as districtName,d.region_code as regionCode,d.region_name as regionName,p.project_code as projectCode,
        p.project_name as projectName,p.plan_place_area as planPlaceArea,
        p.total_build_area as totalBuildArea,p.building_type as buildingType,p.plani_total_households as planiTotalHouseholds,p.property_price as propertyPrice,
        p.exhibition_start_date as exhibitionStartDate,p.exhibition_area as exhibitionArea,p.comprehensive_area as comprehensiveArea
        ,p.demonstration_area as demonstrationArea,p.start_date as startDate,p.end_date as endDate,p.sync_date as syncDate,p.sync_usercode as syncUsercode,
        p.create_by as createBy,p.create_time as createTime FROM rd_new_project p
        LEFT JOIN rd_district_info d ON d.district_code=p.district_code
        LEFT JOIN rd_district_principal rdp ON d.district_code=rdp.district_code
        <where>
            <if test="organCodeString!=null and organCodeString.size()!=0">
                and d.region_code in
                <foreach collection="organCodeString" item="organCodeString" index="index" open="(" close=")" separator=",">
                    #{organCodeString}
                </foreach>
            </if>
            <if test="commIdString!=null and commIdString.size()!=0">
                and p.project_code in
                <foreach collection="commIdString" item="commIdString" index="index" open="(" close=")" separator=",">
                    #{commIdString}
                </foreach>
            </if>
            <if test="principaString!=null and principaString.size()!=0">
                and rdp.principal_code in
                <foreach collection="principaString" item="principaString" index="index" open="(" close=")" separator=",">
                    #{principaString}
                </foreach>
                AND rdp.ENABLE=1
            </if>
            <if test="rdNewProjectDTO.regionCode!=null and rdNewProjectDTO.regionCode!=''">
                and d.region_code=#{rdNewProjectDTO.regionCode}
            </if>
            <if test="rdNewProjectDTO.districtCode!=null and rdNewProjectDTO.districtCode!=''">
                and p.district_code=#{rdNewProjectDTO.districtCode}
            </if>
            <if test="rdNewProjectDTO.projectCode!=null and rdNewProjectDTO.projectCode!=''">
                and p.project_code=#{rdNewProjectDTO.projectCode}
            </if>
            <if test="rdNewProjectDTO.buildingType!=null and rdNewProjectDTO.buildingType!=''">
                and FIND_IN_SET(#{rdNewProjectDTO.buildingType},p.building_type)!=0
            </if>
            and p.ENABLE=1
        </where>
        ORDER BY p.project_code,p.district_code,p.create_time
    </select>

</mapper>