<?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.WcxWeeklyStatisticsRegionProjectMapper">
    <resultMap id="BaseMap" type="cn.com.countrygarden.bi.main.model.wcx.WcxWeeklyStatisticsRegionProject">
        <id column="id" property="id"/>
        <result column="org_code" property="orgCode"/>
        <result column="org_name" property="orgName"/>
        <result column="comm_code" property="commCode"/>
        <result column="comm_name" property="commName"/>
        <result column="week" property="week"/>
        <result column="innovation_num" property="innovationNum"/>
        <result column="innovation_num_chain" property="innovationNumChain"/>
        <result column="user_num" property="userNum"/>
        <result column="user_num_chain" property="userNumChain"/>
        <result column="new_user_num" property="newUserNum"/>
        <result column="new_user_chain" property="newUserChain"/>
        <result column="date_range" property="dateRange"/>
        <result column="wekindex" property="wekindex"/>
    </resultMap>

    <select id="innovationNumZhouTongjiRegionProject" resultType="cn.com.countrygarden.bi.main.model.wcx.WcxWeeklyStatisticsRegionProject">
          SELECT t.timein AS `week`,t.orgName as orgName,t.commName as commName,t.cou AS innovationNum,FORMAT(CASE WHEN ISNULL((t.couben-t.coush)/t.coush*100) THEN 0 ELSE (t.couben-t.coush)/t.coush*100 END,2) AS innovationNumChain
FROM
(SELECT r1.timein,r1.cou,r1.orgName,r1.commName,
CASE WHEN ISNULL(r1.cou) THEN 0 ELSE r1.cou END AS couben,CASE WHEN ISNULL(r2.cou) THEN 0 ELSE r2.cou END AS coush
FROM(SELECT (@rownum := @rownum + 1) AS rownum,
CONCAT(SUBSTRING(DATE_FORMAT(tab.create_date ,'%Y-%u'),1,4),'年第',SUBSTRING(DATE_FORMAT(tab.create_date ,'%Y-%u'),6,7),'周') AS `timein`
,COUNT(tab.innovation_id) AS cou,tab.region_name AS orgName
,tab.project_name AS commName
FROM wcx_innovation tab,(SELECT @rownum := 0) r WHERE 1 = 1
AND tab.audit_status=1
GROUP BY tab.region_name,tab.project_name,timein) r1
LEFT JOIN (SELECT (@INDEX := @INDEX + 1) AS rownum,
CONCAT(SUBSTRING(DATE_FORMAT(tab.create_date ,'%Y-%u'),1,4),'年第',SUBSTRING(DATE_FORMAT(tab.create_date ,'%Y-%u'),6,7),'周') AS `timein`
,COUNT(tab.innovation_id) AS cou,tab.region_name AS orgName
,tab.project_name AS commName
FROM wcx_innovation tab,(SELECT @INDEX := 0) r WHERE 1 = 1
AND tab.audit_status=1
GROUP BY tab.region_name,tab.project_name,timein) r2 ON  r1.rownum = r2.rownum+1
   ) t
    </select>

    <update id="updateWeekRegionProject">
         UPDATE `wcx_weekly_statistics_region_project`
SET
  `org_code` = #{orgCode},
  `org_name` = #{orgName},
  `comm_code` = #{commCode},
  `comm_name` = #{commName},
  `innovation_num` = #{innovationNum},
  `innovation_num_chain` = #{innovationNumChain}
WHERE `week` = #{week};
    </update>

    <select id="innovationNumYueTongjiRegionProject" resultType="cn.com.countrygarden.bi.main.model.wcx.WcxWeeklyStatisticsRegionProject">
        SELECT t.timein AS `week`,t.orgName,t.commName,t.cou AS innovationNum,FORMAT(CASE WHEN ISNULL((t.couben-t.coush)/t.coush*100) THEN 0 ELSE (t.couben-t.coush)/t.coush*100 END,2) AS innovationNumChain
FROM
(SELECT r1.timein,r1.cou,r1.orgName,r1.commName,
CASE WHEN ISNULL(r1.cou) THEN 0 ELSE r1.cou END AS couben,CASE WHEN ISNULL(r2.cou) THEN 0 ELSE r2.cou END AS coush
FROM(SELECT (@rownum := @rownum + 1) AS rownum,
CONCAT(SUBSTRING(DATE_FORMAT(tab.create_date ,'%Y-%m'),1,4),'年第',SUBSTRING(DATE_FORMAT(tab.create_date ,'%Y-%m'),6,7),'月') AS `timein`
,COUNT(tab.innovation_id) AS cou,tab.region_name AS orgName
,tab.project_name AS commName
FROM wcx_innovation tab,(SELECT @rownum := 0) r WHERE 1 = 1
AND tab.audit_status=1
GROUP BY tab.region_name,tab.project_name,timein) r1
LEFT JOIN (SELECT (@INDEX := @INDEX + 1) AS rownum,
CONCAT(SUBSTRING(DATE_FORMAT(tab.create_date ,'%Y-%m'),1,4),'年第',SUBSTRING(DATE_FORMAT(tab.create_date ,'%Y-%m'),6,7),'月') AS `timein`
,COUNT(tab.innovation_id) AS cou,tab.region_name AS orgName
,tab.project_name AS commName
FROM wcx_innovation tab,(SELECT @INDEX := 0) r WHERE 1 = 1
AND tab.audit_status=1
GROUP BY tab.region_name,tab.project_name,timein) r2 ON  r1.rownum = r2.rownum+1
   ) t
    </select>



    <select id="innovationNumRiTongjiRegionProject" resultType="cn.com.countrygarden.bi.main.model.wcx.WcxWeeklyStatisticsRegionProject">
          SELECT t.timein AS `week`,t.orgName,t.commName,t.cou AS innovationNum,FORMAT(CASE WHEN ISNULL((t.couben-t.coush)/t.coush*100) THEN 0 ELSE (t.couben-t.coush)/t.coush*100 END,2) AS innovationNumChain
FROM
(SELECT r1.timein,r1.cou,r1.orgName,r1.commName,
CASE WHEN ISNULL(r1.cou) THEN 0 ELSE r1.cou END AS couben,CASE WHEN ISNULL(r2.cou) THEN 0 ELSE r2.cou END AS coush
FROM(SELECT (@rownum := @rownum + 1) AS rownum,
DATE_FORMAT(tab.create_date ,'%Y') AS `timein`
,COUNT(tab.innovation_id) AS cou,tab.region_name AS orgName
,tab.project_name AS commName
FROM wcx_innovation tab,(SELECT @rownum := 0) r WHERE 1 = 1
AND tab.audit_status=1
GROUP BY tab.region_name,tab.project_name,timein) r1
LEFT JOIN (SELECT (@INDEX := @INDEX + 1) AS rownum,
DATE_FORMAT(tab.create_date ,'%Y') AS `timein`
,COUNT(tab.innovation_id) AS cou,tab.region_name AS orgName
,tab.project_name AS commName
FROM wcx_innovation tab,(SELECT @INDEX := 0) r WHERE 1 = 1
AND tab.audit_status=1
GROUP BY tab.region_name,tab.project_name,timein) r2 ON  r1.rownum = r2.rownum+1
   ) t

    </select>

    <select id="selectstatisquyuxiangmu" resultMap="BaseMap">
         SELECT
        DISTINCT
  `org_code`,
  `org_name`,
  `comm_code`,
  `comm_name`,
  `week`,
  `innovation_num`,
  `innovation_num_chain`,
  `user_num`,
  `user_num_chain`,
  `new_user_num`,
  `new_user_chain`,
  `date_range`,
  `wekindex`
FROM `wcx_weekly_statistics_region_project`
WHERE wekindex=#{wekindex} AND org_name=#{orgName} AND comm_name=#{commName}
        <if test="weekList != null and weekList.size()>0">
            and `week` in
            <foreach collection="weekList" index="index" open="(" close=")" separator="," item="item">
                #{item}
            </foreach>
        </if>
       <!-- <if test="pageSize != '-1'">
            limit #{pageOffset}, #{pageSize}
        </if>-->
    </select>

    <select id="selectstatisquyuxiangmuCount" resultType="long">
                 SELECT
  count(DISTINCT `week`)
FROM `wcx_weekly_statistics_region_project`
WHERE wekindex=#{wekindex} AND `week`=#{week} AND org_name=#{orgName} AND comm_name=#{commName}
    </select>

    <select id="selectstatisquyuxiangmuVO" resultType="cn.com.countrygarden.bi.main.dto.wcx.WcxWeeklyStatisticsVO">
        SELECT DISTINCT
        `date_range` as `week`,`innovation_num` as innovationNum,`innovation_num_chain` as innovationNumChain,`user_num` as userNum,
        `user_num_chain` as userNumChain,`new_user_num` as newUserNum,`new_user_chain` as newUserChain
        FROM `wcx_weekly_statistics_region_project`
        WHERE wekindex=#{wekindex} AND org_name=#{orgName} AND comm_name=#{commName}
        <if test="weekList != null and weekList.size()>0">
            and `week` in
            <foreach collection="weekList" index="index" open="(" close=")" separator="," item="item">
                #{item}
            </foreach>
        </if>
    </select>

</mapper>
