<?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.RdDistrictSettledMapper">
    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="cn.com.countrygarden.bi.main.model.districtReport.RdDistrictSettled">
                                    <id column="id" property="id" />
                                                <result column="district_code" property="districtCode" />
                                                <result column="project_name" property="projectName" />
                                                <result column="emplacement_project_date" property="emplacementProjectDate" />
                                                <result column="project_place" property="projectPlace" />
                                                <result column="emplacement_project_area" property="emplacementProjectArea" />
                                                <result column="descs" property="descs" />
                                                <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.RdDistrictSettledDTO">
    SELECT DISTINCT rs.id AS id,rd.region_name AS regionName,rs.district_code as districtCode,rd.district_name AS districtName,rs.project_name AS projectName,
    rs.emplacement_project_date AS emplacementProjectDateString,rs.project_place AS projectPlace,rs.emplacement_project_area AS emplacementProjectArea,
    rs.descs AS descs,rs.create_by AS createBy,rs.create_name as createName,rs.create_time AS createTime FROM rd_district_settled rs
    LEFT JOIN rd_district_info rd ON rd.district_code=rs.district_code
    LEFT JOIN rd_district_project_mp rm ON rm.district_code=rd.district_code
    LEFT JOIN rd_district_principal rdp ON rd.district_code=rdp.district_code
    <where>
        <if test="organCodeString!=null and organCodeString.size()!=0">
            and rd.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 rs.project_name 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="rdDistrictSettledDTO.createBy!=null and rdDistrictSettledDTO.createBy!='' ">
            and rs.create_by=#{rdDistrictSettledDTO.createBy}
        </if>
       <if test="rdDistrictSettledDTO.regionCode!=null and rdDistrictSettledDTO.regionCode!=''">
           and rd.region_code=#{rdDistrictSettledDTO.regionCode}
       </if>
        <if test="rdDistrictSettledDTO.districtCode!=null and rdDistrictSettledDTO.districtCode!=''">
            and rs.district_code=#{rdDistrictSettledDTO.districtCode}
        </if>
        <if test="rdDistrictSettledDTO.projectName!=null and rdDistrictSettledDTO.projectName!=''">
            and rs.project_name like concat('%',#{rdDistrictSettledDTO.projectName},'%')
        </if>
        <if test="rdDistrictSettledDTO.projectdateleft!=null and rdDistrictSettledDTO.projectdateleft!='' and rdDistrictSettledDTO.projectdateright!=null and rdDistrictSettledDTO.projectdateright!=''">
           and rs.emplacement_project_date BETWEEN #{rdDistrictSettledDTO.projectdateleft} AND #{rdDistrictSettledDTO.projectdateright}
        </if>
        and rs.ENABLE=1
    </where>
        ORDER BY rs.project_name,rs.district_code,rs.create_time
</select>

    <select id="queryById" resultType="cn.com.countrygarden.bi.main.dto.districtReport.RdDistrictSettledDTO">
          SELECT rs.id AS id,rd.region_code as regionCode,rs.district_code as districtCode,rd.region_name AS regionName,rd.district_name AS districtName,rs.project_name AS projectName,
          rs.emplacement_project_date AS emplacementProjectDate,rs.project_place AS projectPlace,rs.emplacement_project_area AS emplacementProjectArea,
         rs.descs AS descs,rs.create_by AS createBy,rs.create_time AS createTime FROM rd_district_settled rs
         LEFT JOIN rd_district_info rd ON rd.district_code=rs.district_code
         <where>
             rs.id=#{id}
         </where>
    </select>

    <select id="settledDTOAddone" resultType="cn.com.countrygarden.bi.main.dto.districtReport.RdDistrictSettledDTO">
        SELECT rs.id AS id,rd.region_code as regionCode,rs.district_code as districtCode,rd.region_name AS regionName,rd.district_name AS districtName,rs.project_name AS projectName,
          rs.emplacement_project_date AS emplacementProjectDate,rs.project_place AS projectPlace,rs.emplacement_project_area AS emplacementProjectArea,
         rs.descs AS descs,rs.create_by AS createBy,rs.create_time AS createTime FROM rd_district_settled rs
         LEFT JOIN rd_district_info rd ON rd.district_code=rs.district_code
         <where>
         <if test="regionCode!=null and regionCode!=''">
             and rd.region_code=#{regionCode}
         </if>
         <if test="districtCode!=null and districtCode!=''">
              and rs.district_code=#{districtCode}
         </if>
             <if test="projectName!=null and projectName!=''">
                 and rs.project_name=#{projectName}
             </if>
             <if test="createdBy!=null and createdBy!=''">
                 and rs.create_by=#{createdBy}
             </if>
             and rs.ENABLE=1
         </where>
    </select>

    <update id="deleteupdate">
            UPDATE rd_district_settled SET ENABLE = 0,update_time=NOW(),update_by=#{updateBy},update_name=#{updateName}
            where id=#{id}
    </update>

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

    <select id="evaluationrdDistrictSettled" resultType="cn.com.countrygarden.bi.main.dto.districtReport.RdDistrictSettledVO">
               SELECT DISTINCT rd.region_name AS regionName,rs.district_code as districtCode,rd.district_name AS districtName,rs.project_name AS projectName,
    rs.emplacement_project_date AS emplacementProjectDate,rs.project_place AS projectPlace,rs.emplacement_project_area AS emplacementProjectArea,
    rs.descs AS descs,rs.create_time FROM rd_district_settled rs
    LEFT JOIN rd_district_info rd ON rd.district_code=rs.district_code
    LEFT JOIN rd_district_principal rdp ON rd.district_code=rdp.district_code
    <where>
        <if test="organCodeString!=null and organCodeString.size()!=0">
        and rd.region_code in
        <foreach collection="organCodeString" item="organCodeString" index="index" open="(" close=")" separator=",">
            #{organCodeString}
        </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="rdDistrictSettledDTO.createBy!=null and rdDistrictSettledDTO.createBy!='' ">
            and rs.create_by=#{rdDistrictSettledDTO.createBy}
        </if>
        <if test="rdDistrictSettledDTO.regionCode!=null and rdDistrictSettledDTO.regionCode!=''">
            and rd.region_code=#{rdDistrictSettledDTO.regionCode}
        </if>
        <if test="rdDistrictSettledDTO.districtCode!=null and rdDistrictSettledDTO.districtCode!=''">
            and rs.district_code=#{rdDistrictSettledDTO.districtCode}
        </if>
        <if test="rdDistrictSettledDTO.projectName!=null and rdDistrictSettledDTO.projectName!=''">
            and rs.project_name like concat('%',#{rdDistrictSettledDTO.projectName},'%')
        </if>
        <if test="rdDistrictSettledDTO.projectdateleft!=null and rdDistrictSettledDTO.projectdateleft!='' and rdDistrictSettledDTO.projectdateright!=null and rdDistrictSettledDTO.projectdateright!=''">
            and rs.emplacement_project_date BETWEEN #{rdDistrictSettledDTO.projectdateleft} AND #{rdDistrictSettledDTO.projectdateright}
        </if>
        and rs.ENABLE=1
    </where>
        ORDER BY rs.project_name,rs.district_code,rs.create_time
    </select>

    <update id="update">
          UPDATE rd_district_settled
          <set>
              <if test="districtCode!=null and districtCode!=''">district_code = #{districtCode},</if>
              <if test="projectName!=null and projectName!=''">project_name = #{projectName},</if>
              <if test="emplacementProjectDate!=null">emplacement_project_date = #{emplacementProjectDate},</if>
              <if test="projectPlace!=null and projectPlace!=''">project_place = #{projectPlace},</if>
              <if test="emplacementProjectArea!=null and emplacementProjectArea!=''">emplacement_project_area = #{emplacementProjectArea},</if>
              <if test="descs!=null">descs = #{descs},</if>
              <if test="enable!=null">ENABLE = #{enable},</if>
              <if test="updateBy!=null and updateBy!=''">update_by = #{updateBy},</if>
              <if test="updateTime!=null">update_time = #{updateTime},</if>
          </set>
	        WHERE id = #{id};
    </update>

    <select id="districtSettledone" resultType="cn.com.countrygarden.bi.main.dto.districtReport.RdDistrictSettledDTO">
        SELECT rs.id AS id,rd.region_code as regionCode,rs.district_code as districtCode,rd.region_name AS regionName,rd.district_name AS districtName,rs.project_name AS projectName,
        rs.emplacement_project_date AS emplacementProjectDate,rs.project_place AS projectPlace,rs.emplacement_project_area AS emplacementProjectArea,
        rs.descs AS descs,rs.create_by AS createBy,rs.create_time AS createTime FROM rd_district_settled rs
        LEFT JOIN rd_district_info rd ON rd.district_code=rs.district_code
        <where>
            <if test="districtCode!=null and districtCode!=''">
                 and rs.district_code=#{districtCode}
            </if>
            <if test="projectName!=null and projectName!=''">
                and rs.project_name=#{projectName}
            </if>
            and rs.ENABLE=1
        </where>

    </select>
    <select id="districtProjectName" resultType="int">
          SELECT COUNT(DISTINCT project_name) FROM rd_district_settled
          <where>
                 <if test="projectName!=null and projectName!=''">
                    and project_name=#{projectName}
                 </if>
              and ENABLE=1
          </where>
    </select>

</mapper>