<?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.wcx.WcxRegionProjectMapper">
    <resultMap id="BaseRegionProjectDtoMap" type="cn.com.countrygarden.bi.main.dto.wcx.WcxRegionProjectDto">
        <result property="regionName" column="region_name"/>
        <result property="projectName" column="project_name"/>
        <result property="userId" column="user_id"/>
        <result property="regionPrizeInnNumber" column="regionPrizeInnNumber"/>
        <result property="projectPrizeInnNumber" column="projectPrizeInnNumber"/>
        <result property="regionProjectId" column="region_project_id"/>
    </resultMap>

    <select id="selectOne" resultType="cn.com.countrygarden.bi.main.dto.wcx.WcxRegionProjectDto">
        SELECT
            r.region_name regionName,
            r.project_name projectName,
            u.user_id userId
        FROM biapp.wcx_user u
        JOIN (
            SELECT PK_ORG,ID
            FROM performance_app.bd_psndoc
            WHERE ID = #{employeeCardId}
            ORDER BY TS DESC, BID DESC
            LIMIT 1
        ) e ON u.employee_card_id = e.ID
        JOIN wcx_region_project_rel r ON r.property_project_name = e.PK_ORG
        WHERE u.employee_card_id = #{employeeCardId}
    </select>

    <select id="selectAll" resultMap="BaseRegionProjectDtoMap">
        SELECT
            region_project_id,
            region_name,
            project_name
        FROM wcx_region_project_rel rp
        WHERE rp.project_name is NOT NULL AND rp.project_name != ''
    </select>

    <update id="updateRegionNameByProjectName">
        update wcx_region_project_rel set region_name = #{regionName} where project_name = #{projectName};
    </update>

    <select id="selectBestPassNumberRegion" resultMap="BaseRegionProjectDtoMap">
        SELECT tp.inn_region_name region_name, tp.regionPrizeInnNumber
        FROM (SELECT item.inn_region_name, count(item.inn_region_name) regionPrizeInnNumber
        FROM wcx_prize p
        JOIN wcx_prize_item item ON p.prize_id = item.prize_id AND p.prize_type in ('1', '2')
        WHERE p.prize_open_date <![CDATA[>=]]> #{startDate}
        AND p.prize_open_date <![CDATA[<=]]> #{endDate}
        AND p.status = '1' AND item.status = '1'
        GROUP BY item.inn_region_name) tp
        WHERE tp.regionPrizeInnNumber = (
            SELECT count(item.inn_region_name) regionPrizeInnNumber
            FROM wcx_prize p
            JOIN wcx_prize_item item ON p.prize_id = item.prize_id AND p.prize_type in ('1', '2')
            WHERE p.prize_open_date <![CDATA[>=]]> #{startDate}
            AND p.prize_open_date <![CDATA[<=]]> #{endDate}
            AND p.status = '1' AND item.status = '1'
            GROUP BY item.inn_region_name
            ORDER BY regionPrizeInnNumber DESC
            limit 1
        ) AND tp.inn_region_name is not null
    </select>

    <select id="selectBestPassNumberProject" resultMap="BaseRegionProjectDtoMap">
        SELECT tp.inn_project_name project_name, tp.projectPrizeInnNumber
        FROM (SELECT item.inn_project_name, count(item.inn_project_name) projectPrizeInnNumber
            FROM wcx_prize p
            JOIN wcx_prize_item item ON p.prize_id = item.prize_id AND p.prize_type in ('1', '2')
            WHERE p.prize_open_date <![CDATA[>=]]> #{startDate}
            AND p.prize_open_date <![CDATA[<=]]> #{endDate}
            AND p.status = '1' AND item.status = '1'
            GROUP BY item.inn_project_name) tp
        WHERE tp.projectPrizeInnNumber = (SELECT count(item.inn_project_name) projectPrizeInnNumber
            FROM wcx_prize p
            JOIN wcx_prize_item item ON p.prize_id = item.prize_id AND p.prize_type in ('1', '2')
            WHERE p.prize_open_date <![CDATA[>=]]> #{startDate}
            AND p.prize_open_date <![CDATA[<=]]> #{endDate}
            AND p.status = '1' AND item.status = '1'
            GROUP BY item.inn_project_name ORDER BY projectPrizeInnNumber DESC limit 1) AND
            tp.projectPrizeInnNumber is not null
    </select>

    <update id="updateRegionNameByRegionProjectId">
        UPDATE wcx_region_project_rel SET region_name = #{regionName} WHERE region_project_id = #{regionProjectId}
    </update>

    <select id="findAllNCRegion" resultMap="BaseRegionProjectDtoMap">
        SELECT MAX(Four_tissue) region_name FROM performance_app.bd_type WHERE Four_tissue is NOT null AND Four_tissue != '' GROUP BY Four_tissue
    </select>
  <select id="findAllRoleOrganCode" resultMap="BaseRegionProjectDtoMap">
        SELECT MAX(Four_tissue) region_name FROM performance_app.bd_type WHERE Four_tissue is NOT null AND Four_tissue != '' 
        <if test="regionName!='all'">
          and Four_tissue  like concat('%',#{regionName},'%')
		</if>            
        GROUP BY Four_tissue
    </select>


    <select id="findNCProject" resultMap="BaseRegionProjectDtoMap">
        SELECT MAX(pk_org) project_name FROM performance_app.bd_type WHERE Four_tissue = #{regionName} AND pk_org is NOT null AND pk_org != '' GROUP BY pk_org
    </select>

    <select id="getErpProjectName" resultType="string">
        SELECT rp.project_name FROM wcx_region_project_rel rp
        WHERE rp.property_project_name = #{ncProjectName}
    </select>

    <!--<select id="getErpRegionName" resultType="string">
        SELECT rp.region_name FROM wcx_region_project_rel rp
        WHERE rp.property_project_name = #{erpProjectName}
    </select>-->
</mapper>