hello world!!!!!

写下自己的一些心得,写下自己问题的方式,写下程序之路的艰辛,希望能够有朝一日成为大牛。
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

地区排名脚本 一千三百多行代码

Posted on 2015-11-27 09:48  陈力  阅读(260)  评论(0编辑  收藏  举报
CREATE OR REPLACE PACKAGE BODY QMS_RPT_AREA AS
  /******************************************************************************
     NAME:       QMS_RPT_AREA
     PURPOSE:  对网点,省 ,市进行维修数量排名
  
     REVISIONS:
     Ver        Date        Author           Description
     ---------  ----------  ---------------  ------------------------------------
     1.0        2015/8/18      chenli       1. Created this package.
  ******************************************************************************/

  /******************************************************************************
     NAME:       
     PURPOSE:   按网点统计维修率
  ******************************************************************************/
  FUNCTION branch_Slice(ReportId        VARCHAR2,
                        evaluate_id     VARCHAR2,
                        p_slice_id      varchar2,
                        Slice_Date_From DATE,
                        Slice_Date_To   DATE,
                        ORGID           varchar2) RETURN VARCHAR2 IS
    Slice_Date_FromTemp VARCHAR2(200);
    SQLSTR              VARCHAR2(18000);
  
  BEGIN
  
    Slice_Date_FromTemp := to_char(Slice_Date_From, 'yyyy/mm/dd');
  
    if ORGID = '1' then
      SQLSTR := 'insert into qms_rpt_area_rank_detail(ID,Province_Region_ID,Province_Region_Name ,maintain_count,report_type,org_id,maintain_date,evaluate_id,branch_id,branch_name) SELECT
 sys_guid(),  

 SUBSTR( (SELECT UU.REGION_ID FROM UNITS UU WHERE UU.UNIT_ID=G.unit_id),1,3),
 (SELECT RT.REG_NAME FROM REGION_TYPE RT WHERE RT.REGION_ID=SUBSTR( (SELECT UU.REGION_ID FROM UNITS UU WHERE UU.UNIT_ID=G.unit_id),1,3)) AS REGION_NAME, 
  
 G.maintainCount,
 1,
 1,
 to_date(''' || Slice_Date_FromTemp || ''',''yyyy/mm/dd''),
 ''' || evaluate_id || ''',
 G.unit_id,
 (select un.unit_name from units un where un.unit_id=  G.unit_id)
 
 FROM
 
 ( SELECT count(1) as maintainCount, s.unit_id

FROM  vw_rpt_css_service_rec_mdkt s ,units u where
  
    s.unit_id in(select r.unit_id from qms_rpt_area_rank r where r.evaluate_id=''' ||
                evaluate_id || ''')
 and s.unit_id=u.unit_id(+)
{WHERE} 
 group by s.unit_id ) 
  G; ';
    elsif ORGID = '2' then
      SQLSTR := 'insert into qms_rpt_area_rank_detail(ID,Province_Region_ID,Province_Region_Name ,maintain_count,report_type,org_id,maintain_date,evaluate_id,branch_id,branch_name) SELECT
 sys_guid(),  

 SUBSTR( (SELECT UU.REGION_ID FROM UNITS UU WHERE UU.UNIT_ID=G.unit_id),1,3),
 (SELECT RT.REG_NAME FROM REGION_TYPE RT WHERE RT.REGION_ID=SUBSTR( (SELECT UU.REGION_ID FROM UNITS UU WHERE UU.UNIT_ID=G.unit_id),1,3)) AS REGION_NAME, 
  
 G.maintainCount,
 1,
 1,
 to_date(''' || Slice_Date_FromTemp || ''',''yyyy/mm/dd''),
 ''' || evaluate_id || ''',
 G.unit_id,
 (select un.unit_name from units un where un.unit_id=  G.unit_id)
 
 FROM
 
 ( SELECT count(1) as maintainCount, s.unit_id

FROM  css_service_rec s ,units u where 1=1 and
  
    s.unit_id in(select r.unit_id from qms_rpt_area_rank r where r.evaluate_id=''' ||
                evaluate_id || ''')
 and s.unit_id=u.unit_id(+)
{WHERE} 
 group by s.unit_id  ) 
  G; ';
    end if;
  
    return SQLSTR;
  END branch_Slice;
  /******************************************************************************
     NAME:       
     PURPOSE:   按省统计维修率
  ******************************************************************************/
  FUNCTION PROVINCE_SLICE(ReportId        VARCHAR2,
                          p_slice_id      varchar2,
                          evaluate_id     varchar2,
                          Slice_Date_From DATE,
                          Slice_Date_TO   DATE,
                          ORGID           varchar2) RETURN VARCHAR2 IS
    SQLSTR              VARCHAR2(18000);
    Slice_Date_FromTemp VARCHAR2(200);
  BEGIN
  
    Slice_Date_FromTemp := to_char(Slice_Date_From, 'yyyy/mm/dd');
  
    if ORGID = '1' then
    
      SQLSTR := 'insert into qms_rpt_area_rank_detail(ID,Province_Region_ID,Province_Region_Name,maintain_count,report_type,maintain_date,evaluate_id) SELECT
 sys_guid(),  G.provinceId,G.provineName ,G.maintainCount,2,to_date(''' ||
                Slice_Date_FromTemp || ''',''yyyy/mm/dd''),''' ||
                evaluate_id || '''
 FROM( SELECT count(1) as maintainCount ,substr(r.region_id,1,3) as provinceId,
 (select t.reg_name from region_type t where t.region_id=substr(r.region_id,1,3)) as provineName
FROM  vw_rpt_css_service_rec_mdkt  s,region_type r where   substr(s.region_code,1,3)=r.region_id
 and substr(r.region_id,1,3) in( select eva.provinceid from QMS_RPT_AREA_RANK eva where eva.evaluate_id=''' ||
                evaluate_id || ''')
 {WHERE}
 group  by substr(r.region_id,1,3))  G;  ';
    elsif ORGID = '2' then
      SQLSTR := 'insert into qms_rpt_area_rank_detail(ID,Province_Region_ID,Province_Region_Name,maintain_count,report_type,maintain_date,evaluate_id) SELECT
 sys_guid(),  G.provinceId,G.provineName ,G.maintainCount,2,to_date(''' ||
                Slice_Date_FromTemp || ''',''yyyy/mm/dd''),''' ||
                evaluate_id || '''
 FROM( SELECT count(1) as maintainCount ,substr(u.region_id,1,3) as provinceId,
 (select t.reg_name from region_type t where t.region_id=substr(u.region_id,1,3)) as provineName
FROM  css_service_rec  s,region_type r  , units u
  where  
  s.unit_id = u.unit_id
  and substr(u.region_id, 1, 3) = r.region_id  
 and substr(u.region_id,1,3) in( select eva.provinceid from QMS_RPT_AREA_RANK eva where eva.evaluate_id=''' ||
                evaluate_id || ''')
 {WHERE}
 group by substr(u.region_id,1,3))  G;  ';
    end if;
    return SQLSTR;
  END PROVINCE_SLICE;
  /******************************************************************************
     NAME:       
     PURPOSE:   按市统计维修率
  ******************************************************************************/
  FUNCTION CITY_SLICE(ReportId        VARCHAR2,
                      p_slice_id      varchar2,
                      evaluate_id     varchar2,
                      Slice_Date_From DATE,
                      Slice_Date_To   DATE,
                      ORGID           varchar2) RETURN VARCHAR2 IS
  
    SQLSTR              VARCHAR2(18000);
    Slice_Date_FromTemp VARCHAR2(200);
  BEGIN
    Slice_Date_FromTemp := to_char(Slice_Date_From, 'yyyy/mm/dd');
    if ORGID = '1' then
      SQLSTR := '
 insert into qms_rpt_area_rank_detail(ID,Province_Region_ID,Province_Region_Name ,city_region_id,city_region_name,maintain_count,report_type,org_id,maintain_date,evaluate_id) SELECT
 sys_guid(),  
substr(g.cityId,1,3), 
 (select t.reg_name from region_type t where t.region_id=substr(g.cityId,1,3)),
 G.cityId,
 G.cityName ,

 G.maintainCount,
 3,
 1,
  to_date(''' || Slice_Date_FromTemp || ''',''yyyy/mm/dd''),
 ''' || evaluate_id || '''
 FROM 
 
 ( SELECT count(1) as maintainCount ,
 substr(r.region_id,1,5) as cityId,
 (select t.reg_name from region_type t where t.region_id=substr(r.region_id,1,5)) as cityName
 
FROM  vw_rpt_css_service_rec_mdkt  s,region_type r where  substr(s.region_code,1,5)=r.region_id
 and substr(r.region_id,1,5) in (select rnk.cityid from qms_rpt_area_rank rnk where rnk.evaluate_id=''' ||
                evaluate_id || ''')
 {WHERE} 
 group by substr(r.region_id,1,5))  G;';
    elsif ORGID = '2' then
    
      SQLSTR := '
 insert into qms_rpt_area_rank_detail(ID,Province_Region_ID,Province_Region_Name ,city_region_id,city_region_name,maintain_count,report_type,org_id,maintain_date,evaluate_id) SELECT
 sys_guid(),  
substr(g.cityId,1,3), 
 (select t.reg_name from region_type t where t.region_id=substr(g.cityId,1,3)),
 G.cityId,
 G.cityName ,

 G.maintainCount,
 3,
 1,
  to_date(''' || Slice_Date_FromTemp || ''',''yyyy/mm/dd''),
 ''' || evaluate_id || '''
 FROM 
 
 ( SELECT count(1) as maintainCount ,
 substr(u.region_id,1,5) as cityId,
 (select t.reg_name from region_type t where t.region_id=substr(u.region_id,1,5)) as cityName
 
FROM  css_service_rec  s,units u, region_type r  
  where  
  s.unit_id = u.unit_id
 and substr(u.region_id, 1, 5) = r.region_id 
 and substr(u.region_id,1,5) in (select rnk.cityid from qms_rpt_area_rank rnk where rnk.evaluate_id=''' ||
                evaluate_id || ''')
 {WHERE} 
 group by (substr(u.region_id,1,5)))  G;';
    end if;
  
    return SQLSTR;
  END CITY_SLICE;

  /******************************************************************************
     NAME:       
     PURPOSE:   用于获取各个网点的维修率排名
  ******************************************************************************/
  FUNCTION GetBranchRankSQL(PRODUCE_CATEGORY_PRA VARCHAR2,
                            EVALUATE_PRA         VARCHAR2,
                            RANKCOUNT            NUMBER,
                            ORGID                VARCHAR2) RETURN VARCHAR2 IS
    SQLSTR VARCHAR2(8000);
  
  BEGIN
  
    if ORGID = '1' then
      SQLSTR := ' insert into  qms_rpt_area_rank (ID,EVALUATE_ID,PROVINCEID,PROVINCENAME,MAINTAINCOUNT,REPORT_TYPE,PRODUCT_CATEGORY,UNIT_ID,UNIT_NAME,AVERAGE,RATE )  
select  sys_guid(),''' || EVALUATE_PRA ||
                ''' ,  substr(plcaeTbl.region_id,0,3) as ProviceCode,(select  r.reg_name  from REGION_TYPE r  where r.region_id= substr(plcaeTbl.region_id,0,3)) as province  , plcaeTbl.nums,   ''1'',     ''' ||
                PRODUCE_CATEGORY_PRA || ''', plcaeTbl.unit_id ,  plcaeTbl.unit_name,  0,  0    
 from (select 
  u.unit_name,
  u.region_name,
  u.region_id,
  tbl.nums,
  tbl.unit_id 
  from units u  , 
  ( select * from ( select  s.unit_id, count(1)  as nums from  vw_rpt_css_service_rec_mdkt s
                           where 1=1  {WHERE}    group by rollup(s.unit_id)
  order by nums desc) 
    where rownum<=' || RANKCOUNT ||
                '  )  tbl where  tbl.unit_id=u.unit_id(+)  order by tbl.nums desc)  plcaeTbl;
                update qms_rpt_area_rank  r set r.RECODCOUNT=( select (select count(1) from ( 
 select count(1)
    from css_service_rec s
                           where s.sorg_id=''MDKT'' 
   {WHERE}           
     group by (s.unit_id)))   from dual ) where r.evaluate_id=''' ||
                EVALUATE_PRA || ''' and r.UNIT_ID is null; ';
    
    elsif ORGID = '2' then
    
      SQLSTR := 'insert into  qms_rpt_area_rank (ID,EVALUATE_ID,PROVINCEID,PROVINCENAME,MAINTAINCOUNT,REPORT_TYPE,PRODUCT_CATEGORY,UNIT_ID,UNIT_NAME,AVERAGE,RATE )  
select
        sys_guid(),''' || EVALUATE_PRA || '''
        ,
        substr(plcaeTbl.region_id,0,3) as ProviceCode,   
       (select  r.reg_name  from REGION_TYPE r  where r.region_id= substr(plcaeTbl.region_id,0,3)) as province  ,
       plcaeTbl.nums,   ''1'',     ''' ||
                PRODUCE_CATEGORY_PRA ||
                ''', plcaeTbl.unit_id ,  plcaeTbl.unit_name,  0,  0    
 from (select 
  u.unit_name,
  u.region_name,
  u.region_id,
  tbl.nums,
  tbl.unit_id 
  from units u  , ( select * from (
 select  s.unit_id, count(1)  as nums
    from css_service_rec s where 1=1
            {WHERE}  
    group by rollup(s.unit_id)
 
  order by nums desc) where rownum<=' || RANKCOUNT ||
                '  )  tbl where  tbl.unit_id=u.unit_id(+)  order by tbl.nums desc)  plcaeTbl;
                update qms_rpt_area_rank  r set r.RECODCOUNT=( select (select count(1) from ( 
 select count(1)
    from css_service_rec s where 1=1
   {WHERE}           
    group by (s.unit_id)))  from dual ) where r.evaluate_id=''' ||
                EVALUATE_PRA || ''' and r.UNIT_ID is null; ';
    end if;
  
    return SQLSTR;
  END GetBranchRankSQL;

  /******************************************************************************
     NAME:       
     PURPOSE:   用于获取各个省的维修率排名
  ******************************************************************************/
  FUNCTION GetProvinceRankSQL(PRODUCE_CATEGORY_PRA VARCHAR2,
                              EVALUATE_PRA         VARCHAR2,
                              RANKCOUNT            NUMBER,
                              ORGID                VARCHAR2) RETURN VARCHAR2 IS
    SQLSTR VARCHAR2(18000);
  BEGIN
  
    if (ORGID = '1') then
    
      SQLSTR := 'insert into  qms_rpt_area_rank (ID,EVALUATE_ID,PROVINCEID,PROVINCENAME,MAINTAINCOUNT,REPORT_TYPE,PRODUCT_CATEGORY,AVERAGE,RATE )
select  
 sys_guid(),
 ''' || EVALUATE_PRA || ''',
 provinceMaintain.provinceID,

(select R.REG_NAME from region_type  r where r.region_id=  provinceMaintain. provinceID ) as provinceName,
provinceMaintain. matainCount,   
 2,
 ''' || PRODUCE_CATEGORY_PRA || ''',
 0,
 0
    from
  (select * from (
select substr(r.region_id,1,3) as provinceID , count(1) as matainCount
 from vw_rpt_css_service_rec_mdkt s,region_type r where   substr(s.region_code,1,3)=r.region_id 
  {WHERE} 
  group by rollup (substr(r.region_id,1,3)) order by matainCount desc) tbl where rownum<=' ||
                RANKCOUNT ||
                ')  provinceMaintain;
                update qms_rpt_area_rank  r set r.RECODCOUNT=( select (select count(1) from ( 
 select count(1)
    from VW_RPT_CSS_SERVICE_REC_MDKT s  , region_type r where   substr(s.region_code,1,3)=r.region_id 
    {WHERE}
           
    group by substr(r.region_id,1,3)))  from dual ) where r.evaluate_id=''' ||
                EVALUATE_PRA || ''' and r.PROVINCEID is null;';
    
    elsif ORGID = '2' then
    
      SQLSTR := 'insert into  qms_rpt_area_rank (ID,EVALUATE_ID,PROVINCEID,PROVINCENAME,MAINTAINCOUNT,REPORT_TYPE,PRODUCT_CATEGORY,AVERAGE,RATE )
select  
 sys_guid(),
 ''' || EVALUATE_PRA || ''',
 provinceMaintain.provinceID,

(select R.REG_NAME from region_type  r where r.region_id=  provinceMaintain. provinceID ) as provinceName,
provinceMaintain. matainCount,   
 2,
 ''' || PRODUCE_CATEGORY_PRA || ''',
 0,
 0
    from
  (select * from (
select substr(u.region_id,1,3) as provinceID , count(1) as matainCount
 from css_service_rec s,  region_type r , units u
  where  
  s.unit_id = u.unit_id
   and substr(u.region_id, 1, 5) = r.region_id  
  {WHERE} 
  group by   rollup (substr(u.region_id,1,3)) order by matainCount desc) tbl where rownum<=' ||
                RANKCOUNT ||
                ')  provinceMaintain;
                 update qms_rpt_area_rank  r set r.RECODCOUNT=( select (select count(1) from ( 
 select count(1)
    from css_service_rec s  , region_type r , units u where   s.unit_id = u.unit_id
   and substr(u.region_id, 1, 3) = r.region_id
    {WHERE}
           
    group by substr(u.region_id, 1, 3)))  from dual ) where r.evaluate_id=''' ||
                EVALUATE_PRA || ''' and r.PROVINCEID is null;';
    end if;
  
    return SQLSTR;
  END GetProvinceRankSQL;

  /******************************************************************************
     NAME:       
     PURPOSE:   用于获取各个市的维修率排名
  ******************************************************************************/
  FUNCTION GETCityRankSQL(PRODUCE_CATEGORY_PRA VARCHAR2,
                          EVALUATE_PRA         VARCHAR2,
                          RANKCOUNT            NUMBER,
                          ORGID                VARCHAR2) RETURN VARCHAR2 IS
  
    SQLSTR VARCHAR2(18000);
  BEGIN
  
    if ORGID = '1' then
    
      SQLSTR := 'insert into qms_rpt_area_rank(Id,EVALUATE_ID,PROVINCEID,PROVINCENAME,CITYID,CITYNAME,MAINTAINCOUNT,REPORT_TYPE ,PRODUCT_CATEGORY ,AVERAGE,RATE)
select sys_guid(),
''' || EVALUATE_PRA || ''',
substr(cityMaintain.cityID,1,3),
(select r.REG_NAME from region_type r where r.region_id=substr(cityMaintain.cityID,1,3)) as provinceName,
cityMaintain.cityID,
(select R.REG_NAME from region_type r where r.region_id=cityMaintain .cityID) as cityName,cityMaintain.maintainCount,
3,
''' || PRODUCE_CATEGORY_PRA || ''',
0,
0 from(select * from ( select substr(r.region_id,1,5) as cityID,count(1) as maintainCount from vw_rpt_css_service_rec_mdkt s, region_type r
where substr(s.region_code,1,5)=r.region_id 
{WHERE}
group by  rollup (substr(r.region_id, 1, 5)) order by maintainCount desc)tbl where rownum<=' ||
                RANKCOUNT ||
                ') cityMaintain;  
                update qms_rpt_area_rank  r set r.RECODCOUNT=(select (select count(1) from ( 
 select count(1)
    from VW_RPT_CSS_SERVICE_REC_MDKT s  ,  region_type r where  substr(s.region_code,1,5)=r.region_id
   {WHERE}
           
    group by substr(r.region_id, 1, 5))) from dual ) where r.evaluate_id=''' ||
                EVALUATE_PRA || ''' and r.CITYID is null ;';
    elsif ORGID = '2' then
    
      SQLSTR := 'insert into qms_rpt_area_rank(Id,EVALUATE_ID,PROVINCEID,PROVINCENAME,CITYID,CITYNAME,MAINTAINCOUNT,REPORT_TYPE ,PRODUCT_CATEGORY ,AVERAGE,RATE)
select sys_guid(),
''' || EVALUATE_PRA || ''',
substr(cityMaintain.cityID,1,3),
(select r.REG_NAME from region_type r where r.region_id=substr(cityMaintain.cityID,1,3)) as provinceName,
cityMaintain.cityID,
(select R.REG_NAME from region_type r where r.region_id=cityMaintain .cityID) as cityName,cityMaintain.maintainCount,
3,
''' || PRODUCE_CATEGORY_PRA || ''',
0,
0 from(select * from ( select substr(u.region_id,1,5) as cityID,count(1) as maintainCount from css_service_rec s,  region_type r,units u 
where   s.unit_id = u.unit_id
   and substr(u.region_id, 1, 5) = r.region_id   
{WHERE}
group by  rollup  (substr(u.region_id, 1, 5)) order by maintainCount desc)tbl where rownum<=' ||
                RANKCOUNT ||
                ') cityMaintain; 
                update qms_rpt_area_rank  r set r.RECODCOUNT=( select (select count(1) from( 
 select count(1)
    from css_service_rec s ,  region_type r , units u where  s.unit_id = u.unit_id
   and substr(u.region_id, 1, 5) = r.region_id
   {WHERE}
           
    group by substr(u.region_id, 1, 5))) from dual) where r.evaluate_id=''' ||
                EVALUATE_PRA || ''' and r.CITYID is null ; ';
    end if;
    return SQLSTR;
  END GETCityRankSQL;

  FUNCTION GET_CONDITION_RANK(p_evaluateno_id IN VARCHAR2) RETURN CLOB IS
    V_WHERE CLOB;
    CURSOR c IS
      SELECT parameter_key, parameter_value
        FROM qms_calculate_criteria t
       WHERE qms_report_request_log_id = p_evaluateno_id
            
         AND t.parameter_key <> 'MADE_ID';
    c_row               c%rowtype;
    is_exist            number;
    str_slice_date_from varchar2(50);
    str_slice_date_to   varchar2(50);
  
    produceFrom  varchar2(50);
    produceTO    varchar2(50);
    mountFrom    varchar2(50);
    mountTO      varchar2(50);
    moutainFrom  varchar2(50);
    moutainTO    varchar2(50);
    PRODUCT_TYPE varchar2(50);
    ORGID        varchar(50); --1、家用空调,2、厨房电器 
  
  BEGIN
  
    ORGID := qms_rpt_utl.get_criteria_by_key(p_evaluateno_id, 'ORGID');
    open c;
    fetch c
      into c_row;
    while c%found loop
      IF c_row.PARAMETER_KEY = 'FIRST_PRODUCE_DATE_FROM' THEN
        produceFrom := c_row.parameter_value;
        if ORGID = '1' then
          --如果是家用空调     
          V_WHERE := V_WHERE || ' AND s.PRODUCE_DATE >=' || 'to_date(''' ||
                     produceFrom || ''',''yyyy-mm-dd hh24:mi:ss'')';
        elsif ORGID = '2' THEN
          --如果是厨房电器 生产时间是存放在安装时间字段
          V_WHERE := V_WHERE || ' AND S.FAULT_DATE >=' || 'to_date(''' ||
                     produceFrom || ''',''yyyy-mm-dd hh24:mi:ss'')';
        end if;
      ELSIF c_row.PARAMETER_KEY = 'FIRST_PRODUCE_DATE_TO' THEN
        produceTO := c_row.parameter_value;
        if ORGID = '1' then
          --如果是家用空调   
          V_WHERE := V_WHERE || ' AND s.PRODUCE_DATE <=' || 'to_date(''' ||
                     produceTO || ''',''yyyy-mm-dd hh24:mi:ss'')';
        elsif ORGID = '2' THEN
          --如果是厨房电器 生产时间是存放在安装时间字段
          V_WHERE := V_WHERE || ' AND s.FAULT_DATE <=' || 'to_date(''' ||
                     produceTO || ''',''yyyy-mm-dd hh24:mi:ss'')';
        end if;
      ELSIF c_row.PARAMETER_KEY = 'FIRST_MOUNT_DATE_FROM' THEN
        mountFrom := c_row.parameter_value;
        if ORGID = '1' then
          --如果是家用空调  
        
          V_WHERE := V_WHERE || ' AND s.FAULT_DATE>=' || 'to_date(''' ||
                     mountFrom || ''',''yyyy-mm-dd hh24:mi:ss'')';
        elsif ORGID = '2' THEN
          --如果是厨房电器 
          dbms_output.put_line('');
          /*   V_WHERE := V_WHERE || ' AND S.FAULT_DATE>=' || 'to_date(''' ||
          mountFrom || ''',''yyyy-mm-dd hh24:mi:ss'')';*/
        END IF;
      ELSIF c_row.PARAMETER_KEY = 'FIRST_MOUNT_DATE_TO' THEN
        mountTO := c_row.parameter_value;
        if ORGID = '1' then
          --如果是家用空调  
          V_WHERE := V_WHERE || ' AND s.FAULT_DATE<=' || 'to_date(''' ||
                     mountTO || ''',''yyyy-mm-dd hh24:mi:ss'')';
        elsif ORGID = '2' THEN
          --如果是厨房电器 
          dbms_output.put_line('');
          /* V_WHERE := V_WHERE || ' AND S.FAULT_DATE<=' || 'to_date(''' ||
          mountTO || ''',''yyyy-mm-dd hh24:mi:ss'')';*/
        END IF;
      
      ELSIF c_row.PARAMETER_KEY = 'FIRST_MAINT_DATE_FROM' THEN
        moutainFrom := c_row.parameter_value;
        V_WHERE     := V_WHERE || ' AND S.MAINT_DATE>=' || 'to_date(''' ||
                       moutainFrom || ''',''yyyy-mm-dd hh24:mi:ss'')';
      
      ELSIF c_row.PARAMETER_KEY = 'FIRST_MAINT_DATE_TO' THEN
        moutainTO := c_row.parameter_value;
        V_WHERE   := V_WHERE || ' AND S.MAINT_DATE<=' || 'to_date(''' ||
                     moutainTO || ''',''yyyy-mm-dd hh24:mi:ss'')';
      
      ELSIF c_row.PARAMETER_KEY = 'ORGID' THEN
        if c_row.PARAMETER_VALUE = '1' THEN
          --如果是家用空调事业部
        
          --如果是未选择品类    
          IF (qms_rpt_utl.get_criteria_by_key(p_evaluateno_id,
                                              'PRODUCT_MODE_ID') = 'NONE') THEN
          
            --  V_WHERE := V_WHERE || ' AND S.sorg_id=''MDKT''';
            dbms_output.put_line('');
          ELSE
            V_WHERE := V_WHERE ||
                       ' AND S.prod_id in
       (SELECT P.PROD_ID
          FROM qms.product p, qms_product_mode_detail pd
         where p.product_mode_id = pd.mode_id
                     and pd.mode_id =  (select pm.id from qms_product_mode  pm where pm.mode_code= qms_rpt_utl.get_criteria_by_key(''' ||
                       p_evaluateno_id || ''',''PRODUCT_MODE_ID'')))';
          
          END IF;
        elsif c_row.PARAMETER_VALUE = '2' THEN
          --如果是厨房电器事业部
          V_WHERE := V_WHERE ||
                     ' AND S.PROD_TYPE=   qms_rpt_utl.get_criteria_by_key(''' ||
                     p_evaluateno_id || ''',''PRODUCT_MODE_ID'')';
        end if;
      
      end if;
      fetch c
        into c_row;
    end loop;
  
    RETURN V_WHERE;
  
  END GET_CONDITION_RANK;

  /******************************************************************************
     NAME:       
     PURPOSE:   根据条件获取排名
  ******************************************************************************/
  FUNCTION CAL_RANK(Report_Id VARCHAR2, v_date_lt_str VARCHAR2)
    RETURN VARCHAR2 IS
  
    CHART_RANK      number;
    DATE_RANK       number;
    COLUMN_MODEL    varchar2(10);
    PRODUCT_MODE_ID VARCHAR2(20);
    v_EVALUATE_NO   varchar2(32);
    v_sql           varchar2(4000);
    SORG_ID         varchar2(50);
    str             varchar2(50);
    cursor1         INTEGER;
  
  BEGIN
    select EVALUATE_NO
      into v_EVALUATE_NO
      from qms_report_request_log2 t
     where t.id = Report_Id;
    dbms_output.put_line(v_EVALUATE_NO);
  
    CHART_RANK := qms_rpt_utl.get_criteria_by_key(Report_Id, 'CHART_RANK');
    DATE_RANK  := qms_rpt_utl.get_criteria_by_key(Report_Id, 'DATE_RANK');
    SORG_ID    := qms_rpt_utl.get_criteria_by_key(Report_Id, 'ORGID');
  
    IF SORG_ID = 2 THEN
      PRODUCT_MODE_ID := qms_rpt_utl.get_criteria_by_key(Report_Id,
                                                         'PRODUCT_MODE_ID');
    ELSIF SORG_ID = 1 THEN
      PRODUCT_MODE_ID := '';
    END IF;
  
    IF CHART_RANK < DATE_RANK THEN
      CHART_RANK := DATE_RANK;
    END IF;
  
    CHART_RANK   := CHART_RANK + 1; --加一的目的是其它一条记录为受影响为总维修数
    COLUMN_MODEL := qms_rpt_utl.get_criteria_by_key(Report_Id,
                                                    'COLUMN_MODEL');
  
    if COLUMN_MODEL = '1' then
      v_sql := PKG_RPT_UTL.GET_MERGE_CONDITION_SQL(GetBranchRankSQL(PRODUCT_MODE_ID,
                                                                    v_EVALUATE_NO,
                                                                    CHART_RANK,
                                                                    SORG_ID),
                                                   GET_CONDITION_RANK(Report_Id));
    ELSIF COLUMN_MODEL = '2' then
      v_sql := PKG_RPT_UTL.GET_MERGE_CONDITION_SQL(GetProvinceRankSQL(PRODUCT_MODE_ID,
                                                                      v_EVALUATE_NO,
                                                                      CHART_RANK,
                                                                      SORG_ID),
                                                   GET_CONDITION_RANK(Report_Id));
    ELSIF COLUMN_MODEL = '3' then
      v_sql := PKG_RPT_UTL.GET_MERGE_CONDITION_SQL(GETCityRankSQL(PRODUCT_MODE_ID,
                                                                  v_EVALUATE_NO,
                                                                  CHART_RANK,
                                                                  SORG_ID),
                                                   GET_CONDITION_RANK(Report_Id));
    end if;
  /*
    insert into SQLTEXT_TEST (text, Name) values (v_sql, '66666');
    COMMIT;*/
  
    -- qms_rpt_operating_station_pub.log(Report_Id,'qms_rpt_area.cal_rank','cal_rank_executing',v_sql);
    EXECUTE IMMEDIATE 'begin  ' || v_sql || ' end;';
    --   qms_rpt_operating_station_pub.log(Report_Id,'qms_rpt_area.cal_rank','cal_rank_executed',v_sql);
    COMMIT;
  
    /*  cursor1 := dbms_sql.open_cursor;
    dbms_sql.parse(cursor1, v_sql, dbms_sql.v);
    dbms_sql.close_cursor(cursor1);*/
  
    --补充计算 比率 平均值
    RETURN v_sql;
  END CAL_RANK;

  FUNCTION GET_CONDITION(report_ID       IN VARCHAR2,
                         Slice_Date_From DATE,
                         Slice_Date_To   DATE,
                         GroupType       VARCHAR2) RETURN CLOB IS
    V_WHERE CLOB;
    CURSOR c IS
      SELECT parameter_key, parameter_value
        FROM qms_calculate_criteria t
       WHERE qms_report_request_log_id = report_ID;
  
    c_row               c%rowtype;
    is_exist            number;
    str_slice_date_from varchar2(50);
    str_slice_date_to   varchar2(50);
    GROUP_TYPE          varchar2(50); --分组类型
    ORGID               varchar2(50);
  BEGIN
  
    --只求当天的安装数目,在汇总的时候,进行安装数,维修数进行汇总
    str_slice_date_from := to_char(Slice_Date_To, 'yyyy-mm-dd'); --00 :00:00秒开始
    str_slice_date_to   := to_char(Slice_Date_To, 'yyyy-mm-dd hh24:mi:ss'); --23:59:59秒开始
    ORGID               := qms_rpt_utl.get_criteria_by_key(report_ID,
                                                           'ORGID');
    open c;
    fetch c
      into c_row;
    --求明细数据时不需要给出生产及安装时间 2015/9/6
  
    while c%found loop
    
      if GroupType = 'MAINT_DATE' THEN
      
        IF c_row.PARAMETER_KEY = 'FIRST_PRODUCE_DATE_FROM' THEN
        
          if ORGID = '1' then
            --如果是家用空调     
            V_WHERE := V_WHERE || ' AND s.PRODUCE_DATE >=' || 'to_date(''' ||
                       c_row.parameter_value ||
                       ''',''yyyy-mm-dd hh24:mi:ss'')';
          elsif ORGID = '2' THEN
            --如果是厨房电器 生产时间是存放在安装时间字段
            V_WHERE := V_WHERE || ' AND S.FAULT_DATE >=' || 'to_date(''' ||
                       c_row.parameter_value ||
                       ''',''yyyy-mm-dd hh24:mi:ss'')';
          end if;
        ELSIF c_row.PARAMETER_KEY = 'FIRST_PRODUCE_DATE_TO' THEN
        
          if ORGID = '1' then
            --如果是家用空调   
          
            V_WHERE := V_WHERE || ' AND s.PRODUCE_DATE <=' || 'to_date(''' ||
                       c_row.parameter_value ||
                       ''',''yyyy-mm-dd hh24:mi:ss'')';
          elsif ORGID = '2' THEN
            --如果是厨房电器 生产时间是存放在安装时间字段
            V_WHERE := V_WHERE || ' AND S.FAULT_DATE <=' || 'to_date(''' ||
                       c_row.parameter_value ||
                       ''',''yyyy-mm-dd hh24:mi:ss'')';
          end if;
        ELSIF c_row.PARAMETER_KEY = 'FIRST_MOUNT_DATE_FROM' THEN
          if ORGID = '1' then
            --如果是家用空调   
            V_WHERE := V_WHERE || ' AND s.FAULT_DATE>=' || 'to_date(''' ||
                       c_row.parameter_value ||
                       ''',''yyyy-mm-dd hh24:mi:ss'')';
          elsif ORGID = '2' THEN
            V_WHERE := V_WHERE || ' AND S.FAULT_DATE>=' || 'to_date(''' ||
                       c_row.parameter_value ||
                       ''',''yyyy-mm-dd hh24:mi:ss'')';
          END IF;
        ELSIF c_row.PARAMETER_KEY = 'FIRST_MOUNT_DATE_TO' THEN
          if ORGID = '1' then
            --如果是家用空调 
            V_WHERE := V_WHERE || ' AND s.FAULT_DATE<=' || 'to_date(''' ||
                       c_row.parameter_value ||
                       ''',''yyyy-mm-dd hh24:mi:ss'')';
          elsif ORGID = '2' THEN
            V_WHERE := V_WHERE || ' AND S.FAULT_DATE<=' || 'to_date(''' ||
                       c_row.parameter_value ||
                       ''',''yyyy-mm-dd hh24:mi:ss'')';
          END IF;
        
        ELSIF c_row.PARAMETER_KEY = 'FIRST_MAINT_DATE_FROM' THEN
        
          V_WHERE := V_WHERE || ' AND S.MAINT_DATE>=' || 'to_date(''' ||
                     str_slice_date_from || ''',''yyyy-mm-dd hh24:mi:ss'')';
        
        ELSIF c_row.PARAMETER_KEY = 'FIRST_MAINT_DATE_TO' THEN
          V_WHERE := V_WHERE || ' AND S.MAINT_DATE>=' || 'to_date(''' ||
                     str_slice_date_to || ''',''yyyy-mm-dd hh24:mi:ss'')';
        end if;
      
      elsif GroupType = 'FAULT_DATE' THEN
      
        IF c_row.PARAMETER_KEY = 'FIRST_PRODUCE_DATE_FROM' THEN
        
          if ORGID = '1' then
            --如果是家用空调     
            V_WHERE := V_WHERE || ' AND s.PRODUCE_DATE >=' || 'to_date(''' ||
                       c_row.parameter_value ||
                       ''',''yyyy-mm-dd hh24:mi:ss'')';
          elsif ORGID = '2' THEN
            --如果是厨房电器 生产时间是存放在安装时间字段
            V_WHERE := V_WHERE || ' AND S.FAULT_DATE >=' || 'to_date(''' ||
                       c_row.parameter_value ||
                       ''',''yyyy-mm-dd hh24:mi:ss'')';
          end if;
        ELSIF c_row.PARAMETER_KEY = 'FIRST_PRODUCE_DATE_TO' THEN
        
          if ORGID = '1' then
            --如果是家用空调   
            V_WHERE := V_WHERE || ' AND s.PRODUCE_DATE <=' || 'to_date(''' ||
                       c_row.parameter_value ||
                       ''',''yyyy-mm-dd hh24:mi:ss'')';
          elsif ORGID = '2' THEN
            --如果是厨房电器 生产时间是存放在安装时间字段
            V_WHERE := V_WHERE || ' AND S.FAULT_DATE <=' || 'to_date(''' ||
                       c_row.parameter_value ||
                       ''',''yyyy-mm-dd hh24:mi:ss'')';
          end if;
        ELSIF c_row.PARAMETER_KEY = 'FIRST_MOUNT_DATE_FROM' THEN
        
          if ORGID = '1' then
            V_WHERE := V_WHERE || ' AND s.FAULT_DATE>=' || 'to_date(''' ||
                       str_slice_date_from ||
                       ''',''yyyy-mm-dd hh24:mi:ss'')';
          
          elsif ORGID = '2' THEN
            V_WHERE := V_WHERE || ' AND S.FAULT_DATE>=' || 'to_date(''' ||
                       str_slice_date_from ||
                       ''',''yyyy-mm-dd hh24:mi:ss'')';
          END IF;
        
        ELSIF c_row.PARAMETER_KEY = 'FIRST_MOUNT_DATE_TO' THEN
          if ORGID = '1' then
            V_WHERE := V_WHERE || ' AND s.FAULT_DATE<=' || 'to_date(''' ||
                       str_slice_date_to || ''',''yyyy-mm-dd hh24:mi:ss'')';
          elsif ORGID = '2' THEN
            V_WHERE := V_WHERE || ' AND S.FAULT_DATE<=' || 'to_date(''' ||
                       str_slice_date_to || ''',''yyyy-mm-dd hh24:mi:ss'')';
          END IF;
        
        ELSIF c_row.PARAMETER_KEY = 'FIRST_MAINT_DATE_FROM' THEN
        
          V_WHERE := V_WHERE || ' AND S.MAINT_DATE>=' || 'to_date(''' ||
                     c_row.parameter_value ||
                     ''',''yyyy-mm-dd hh24:mi:ss'')';
        
        ELSIF c_row.PARAMETER_KEY = 'FIRST_MAINT_DATE_TO' THEN
        
          V_WHERE := V_WHERE || ' AND S.MAINT_DATE<=' || 'to_date(''' ||
                     c_row.parameter_value ||
                     ''',''yyyy-mm-dd hh24:mi:ss'')';
        end if;
      
      elsif GroupType = 'PRODUCE_DATE' THEN
      
        IF c_row.PARAMETER_KEY = 'FIRST_PRODUCE_DATE_FROM' THEN
        
          if ORGID = '1' then
            --如果是家用空调     
            V_WHERE := V_WHERE || ' AND s.PRODUCE_DATE >=' || 'to_date(''' ||
                       str_slice_date_from ||
                       ''',''yyyy-mm-dd hh24:mi:ss'')';
          elsif ORGID = '2' THEN
            --如果是厨房电器 生产时间是存放在安装时间字段
            V_WHERE := V_WHERE || ' AND S.FAULT_DATE >=' || 'to_date(''' ||
                       str_slice_date_from ||
                       ''',''yyyy-mm-dd hh24:mi:ss'')';
          end if;
        ELSIF c_row.PARAMETER_KEY = 'FIRST_PRODUCE_DATE_TO' THEN
        
          if ORGID = '1' then
            --如果是家用空调   
            V_WHERE := V_WHERE || ' AND s.PRODUCE_DATE <=' || 'to_date(''' ||
                       str_slice_date_to || ''',''yyyy-mm-dd hh24:mi:ss'')';
          elsif ORGID = '2' THEN
            --如果是厨房电器 生产时间是存放在安装时间字段
            V_WHERE := V_WHERE || ' AND S.FAULT_DATE <=' || 'to_date(''' ||
                       str_slice_date_to || ''',''yyyy-mm-dd hh24:mi:ss'')';
          end if;
        ELSIF c_row.PARAMETER_KEY = 'FIRST_MOUNT_DATE_FROM' THEN
          if ORGID = '1' then
            V_WHERE := V_WHERE || ' AND s.FAULT_DATE>=' || 'to_date(''' ||
                       c_row.parameter_value ||
                       ''',''yyyy-mm-dd hh24:mi:ss'')';
          elsif ORGID = '2' THEN
            V_WHERE := V_WHERE || ' AND S.FAULT_DATE>=' || 'to_date(''' ||
                       c_row.parameter_value ||
                       ''',''yyyy-mm-dd hh24:mi:ss'')';
          end if;
        ELSIF c_row.PARAMETER_KEY = 'FIRST_MOUNT_DATE_TO' THEN
          if ORGID = '1' then
            V_WHERE := V_WHERE || ' AND s.FAULT_DATE<=' || 'to_date(''' ||
                       c_row.parameter_value ||
                       ''',''yyyy-mm-dd hh24:mi:ss'')';
          elsif ORGID = '2' THEN
            V_WHERE := V_WHERE || ' AND S.FAULT_DATE<=' || 'to_date(''' ||
                       c_row.parameter_value ||
                       ''',''yyyy-mm-dd hh24:mi:ss'')';
          END IF;
        
        ELSIF c_row.PARAMETER_KEY = 'FIRST_MAINT_DATE_FROM' THEN
        
          V_WHERE := V_WHERE || ' AND S.MAINT_DATE>=' || 'to_date(''' ||
                     c_row.parameter_value ||
                     ''',''yyyy-mm-dd hh24:mi:ss'')';
        
        ELSIF c_row.PARAMETER_KEY = 'FIRST_MAINT_DATE_TO' THEN
        
          V_WHERE := V_WHERE || ' AND S.MAINT_DATE<=' || 'to_date(''' ||
                     c_row.parameter_value ||
                     ''',''yyyy-mm-dd hh24:mi:ss'')';
        end if;
      
      end if;
    
      if c_row.PARAMETER_KEY = 'ORGID' THEN
        if c_row.PARAMETER_VALUE = '1' THEN
          --如果是家用空调事业部
        
          --如果是未选择品类    
          IF (qms_rpt_utl.get_criteria_by_key(report_ID, 'PRODUCT_MODE_ID') =
             'NONE') THEN
            dbms_output.put_line('');
          ELSE
            V_WHERE := V_WHERE ||
                       ' AND S.prod_id in
       (SELECT P.PROD_ID
          FROM qms.product p, qms_product_mode_detail pd
         where p.product_mode_id = pd.mode_id
           and pd.mode_id =  (select pm.id from qms_product_mode  pm where pm.mode_code= qms_rpt_utl.get_criteria_by_key(''' ||
                       report_ID || ''',''PRODUCT_MODE_ID'')))';
          
          END IF;
        elsif c_row.PARAMETER_VALUE = '2' THEN
          --如果是厨房电器事业部
          V_WHERE := V_WHERE ||
                     ' AND S.PROD_TYPE= qms_rpt_utl.get_criteria_by_key(''' ||
                     report_ID || ''',''PRODUCT_MODE_ID'')';
        end if;
      end if;
      fetch c
        into c_row;
    end loop;
  
    RETURN V_WHERE;
  
  END GET_CONDITION;

  PROCEDURE slicing_cal_moretime(p_report_id            VARCHAR2,
                                 p_slicing_time_by_name ARRAY_TYPE,
                                 p_prefix               VARCHAR2 DEFAULT '') IS
    DATE_IS_INCOMPLETE EXCEPTION;
    DATE_IS_Begin      EXCEPTION;
  
    PRAGMA EXCEPTION_INIT(DATE_IS_INCOMPLETE, -21167);
    DATE_IS_NULL EXCEPTION;
    PRAGMA EXCEPTION_INIT(DATE_IS_NULL, -21168);
    v_fault_date         DATE; --起始时间
    v_fault_date_from    DATE;
    v_fault_date_to      DATE;
    v_temp_date_from     DATE;
    v_temp_date_to       DATE;
    v_days               NUMBER;
    v_days2              NUMBER;
    v_times              NUMBER := 0;
    v_guid               VARCHAR2(32);
    v_total_days         NUMBER := 0;
    v_criteria_slice_row qms_calculate_criteria_slice2%ROWTYPE;
    vv_temp_date         VARCHAR2(20);
    x_ret_msg            VARCHAR(4000);
    v_date_name          VARCHAR(80);
    v_slice_interval     NUMBER := g_slice_interval;
    CURSOR cur_c IS
      SELECT *
        FROM qms_report_request_log2
       WHERE id = p_report_id
         AND progress_status = 'S';
  
  BEGIN
    FOR cur IN cur_c LOOP
      BEGIN
        FOR I IN 1 .. p_slicing_time_by_name.COUNT() LOOP
          v_fault_date_from := NULL;
          v_fault_date_to   := NULL;
        
          v_fault_date_from := TO_DATE(qms_rpt_utl.get_criteria_by_key(cur.id,
                                                                       p_prefix || '_' ||
                                                                       p_slicing_time_by_name(I) || '_' ||
                                                                       g_start_date_suffix),
                                       g_date_format);
          v_fault_date_to   := TO_DATE(qms_rpt_utl.get_criteria_by_key(cur.id,
                                                                       p_prefix || '_' ||
                                                                       p_slicing_time_by_name(I) || '_' ||
                                                                       g_end_date_suffix),
                                       g_date_format);
          IF v_fault_date_from IS NULL OR v_fault_date_to IS NULL THEN
            RAISE DATE_IS_INCOMPLETE; --日期不完整抛异常
            EXIT;
          END IF;
          IF v_fault_date_from IS NOT NULL AND v_fault_date_to IS NOT NULL THEN
            v_date_name := p_slicing_time_by_name(I);
            EXIT; --日期完整,往下执行分片
          END IF;
        
        END LOOP;
      
        IF to_char(v_fault_date_to, 'mm/dd') = '01/01' THEN
          RAISE DATE_IS_Begin; --第一年的第一天不做统计。chenli 2015/9/2
        END IF;
      
        IF v_fault_date_from IS NULL OR v_fault_date_to IS NULL THEN
          RAISE DATE_IS_NULL; --必填日期空,抛异常
        END IF;
      
        ---下面执行分片
        qms_rpt_utl.calc_day_interval(p_begin_date   => v_fault_date_from,
                                      p_end_date     => v_fault_date_to,
                                      p_interval     => v_slice_interval,
                                      x_day_interval => v_days,
                                      x_trunc        => v_times);
      
        v_temp_date_from := v_fault_date_from;
        v_fault_date     := v_fault_date_from;
      
        FOR i IN 1 .. v_times LOOP
          SELECT v_temp_date_from INTO v_temp_date_to FROM DUAL;
        
          IF (v_temp_date_to > v_fault_date_to) THEN
            v_temp_date_to := v_fault_date_to;
          END IF;
        
          SELECT SYS_GUID() INTO v_guid FROM DUAL;
        
          SELECT ROUND(TO_NUMBER(v_temp_date_to - v_fault_date))
            INTO v_days2
            FROM DUAL;
        
          --切片按日期分段查询,格式化日期格式-----------------------------------
          vv_temp_date     := TO_CHAR(v_temp_date_from, 'yyyy/MM/DD') ||
                              ' 00:00:00';
          v_temp_date_from := TO_DATE(vv_temp_date, g_date_format);
        
          vv_temp_date   := TO_CHAR(v_temp_date_to, 'yyyy/MM/DD') ||
                            ' 23:59:59';
          v_temp_date_to := TO_DATE(vv_temp_date, g_date_format);
        
          IF v_days2 >= 0 THEN
            v_criteria_slice_row.id                        := v_guid; --主键值
            v_criteria_slice_row.qms_report_request_log_id := cur.id; --主报表编号
            v_criteria_slice_row.slice_date_from           := v_temp_date_from; --开始时间
            v_criteria_slice_row.slice_date_to             := v_temp_date_to; --结束时间
            v_criteria_slice_row.progress_status           := 'I'; --状态
            v_criteria_slice_row.progress_cur_qty          := 0; --
            v_criteria_slice_row.progress_total_qty        := v_days2 + 1; --总数
            v_criteria_slice_row.status                    := '1'; --是否删除
            v_criteria_slice_row.datetime_created          := SYSDATE; --创建时间
            v_criteria_slice_row.SLICE_GROUP               := v_date_name; --切分的字段
          
            SELECT seq_report.NEXTVAL
              INTO v_criteria_slice_row.report_sequence --序号 
              FROM DUAL;
          
            INSERT INTO qms_calculate_criteria_slice2
            VALUES v_criteria_slice_row;
          
            v_total_days := v_total_days +
                            v_criteria_slice_row.progress_total_qty;
          END IF;
        
          v_temp_date_from := v_temp_date_to + 1;
        END LOOP;
        UPDATE qms_report_request_log2
           SET progress_status    = 'R',
               progress_total_qty = v_total_days,
               PROGRESS_DESC      = '计算中'
         WHERE id = cur.id;
      
        COMMIT;
      EXCEPTION
        WHEN DATE_IS_INCOMPLETE THEN
          UPDATE qms_report_request_log2
             SET progress_status = 'E', progress_desc = '日期不完整'
           WHERE id = cur.id;
          COMMIT;
          EXIT;
        WHEN DATE_IS_NULL THEN
          UPDATE qms_report_request_log2
             SET progress_status = 'E', progress_desc = '必填日期不能为空'
           WHERE id = cur.id;
          COMMIT;
          EXIT;
        WHEN DATE_IS_Begin THEN
          UPDATE qms_report_request_log2
             SET progress_status = 'E',
                 progress_desc   = '1月1日当天不做统计'
           WHERE id = cur.id;
          COMMIT;
          EXIT;
        WHEN OTHERS THEN
          x_ret_msg := SQLERRM;
          UPDATE qms_report_request_log2
             SET progress_status = 'E', progress_desc = x_ret_msg
           WHERE id = cur.id;
          COMMIT;
          EXIT;
      END;
    END LOOP;
    COMMIT;
  EXCEPTION
    WHEN OTHERS THEN
      NULL;
  END;

  /******************************************************************************
     NAME:       slicing_cal
     PURPOSE:  切片
  ******************************************************************************/
  PROCEDURE slicing_cal(p_report_id VARCHAR2) IS
  
    v_date_lt_str varchar2(80);
    v_date_lt     ARRAY_TYPE;
    v_sql         varchar2(8000);
    b             varchar2(50);
  BEGIN
  
    IF length(qms_rpt_utl.get_criteria_by_key(p_report_id,
                                              'FIRST_PRODUCE_DATE_FROM')) > 0 THEN
      v_date_lt := ARRAY_TYPE('PRODUCE_DATE'); --使用生产时间做切片 
    
    elsif length(qms_rpt_utl.get_criteria_by_key(p_report_id,
                                                 'FIRST_MAINT_DATE_FROM')) > 0 THEN
      v_date_lt := ARRAY_TYPE('MAINT_DATE'); --使用维修时间做切片 
    
    elsif length(qms_rpt_utl.get_criteria_by_key(p_report_id,
                                                 'FIRST_MOUNT_DATE_FROM')) > 0 THEN
      v_date_lt := ARRAY_TYPE('FAULT_DATE'); --使用安装时间做切片 
    
    end if;
  
    v_sql := CAL_RANK(p_report_id, v_date_lt_str); --求出排名 
  
    -- qms_rpt_operating_station_pub.log(p_report_id,'slicing_cal_moretime','cal_rank_executing',g_current_year_group_type);
    slicing_cal_moretime(p_report_id, v_date_lt, g_current_year_group_type);
    -- qms_rpt_operating_station_pub.log(p_report_id,'slicing_cal_moretime','cal_rank_executing',g_current_year_group_type);
  
  END;

  /******************************************************************************
     NAME:       sliced_data_cal
     PURPOSE:  报表切片后的片段计算
  ******************************************************************************/
  PROCEDURE sliced_data_cal(p_slice_id VARCHAR2) IS
    v_criteria_slice_row qms_calculate_criteria_slice2%ROWTYPE;
    v_sql                VARCHAR2(18000);
    COLUMN_MODEL         VARCHAR2(50);
    v_EVALUATE_NO        varchar2(50);
    sliceType            varchar2(20);
    ORG_ID               varchar2(20);
  BEGIN
    SELECT *
      INTO v_criteria_slice_row
      FROM QMS_CALCULATE_CRITERIA_SLICE2 T
     WHERE T.ID = p_slice_id;
    sliceType := v_criteria_slice_row.SLICE_GROUP;
  
    --branch_Slice(ReportId VARCHAR2,evaluate_id VARCHAR2, p_slice_id varchar2,
    --   Slice_Date_From DATE,Slice_Date_To DATE) RETURN VARCHAR2 IS
  
    select EVALUATE_NO
      into v_EVALUATE_NO
      from qms_report_request_log2 t
     where t.id = v_criteria_slice_row.qms_report_request_log_id;
    dbms_output.put_line(v_EVALUATE_NO);
  
    COLUMN_MODEL := qms_rpt_utl.get_criteria_by_key(v_criteria_slice_row.qms_report_request_log_id,
                                                    'COLUMN_MODEL'); --获取列模式,根据模式计算单位维修率
    ORG_ID       := qms_rpt_utl.get_criteria_by_key(v_criteria_slice_row.qms_report_request_log_id,
                                                    'ORGID');
  
    if COLUMN_MODEL = 1 then
      v_sql := PKG_RPT_UTL.GET_MERGE_CONDITION_SQL(branch_Slice(v_criteria_slice_row.qms_report_request_log_id,
                                                                v_EVALUATE_NO,
                                                                p_slice_id,
                                                                v_criteria_slice_row.slice_date_from,
                                                                v_criteria_slice_row.slice_date_to,
                                                                ORG_ID),
                                                   GET_CONDITION(v_criteria_slice_row.qms_report_request_log_id,
                                                                 v_criteria_slice_row.slice_date_from,
                                                                 v_criteria_slice_row.slice_date_to,
                                                                 sliceType));
    elsif COLUMN_MODEL = 2 then
    
      v_sql := PKG_RPT_UTL.GET_MERGE_CONDITION_SQL(province_Slice(v_criteria_slice_row.qms_report_request_log_id,
                                                                  p_slice_id,
                                                                  v_EVALUATE_NO,
                                                                  v_criteria_slice_row.slice_date_from,
                                                                  v_criteria_slice_row.slice_date_to,
                                                                  ORG_ID),
                                                   GET_CONDITION(v_criteria_slice_row.qms_report_request_log_id,
                                                                 v_criteria_slice_row.slice_date_from,
                                                                 v_criteria_slice_row.slice_date_to,
                                                                 sliceType));
    elsif COLUMN_MODEL = 3 then
    
      v_sql := PKG_RPT_UTL.GET_MERGE_CONDITION_SQL(city_Slice(v_criteria_slice_row.qms_report_request_log_id,
                                                              p_slice_id,
                                                              v_EVALUATE_NO,
                                                              v_criteria_slice_row.slice_date_from,
                                                              v_criteria_slice_row.slice_date_to,
                                                              ORG_ID),
                                                   GET_CONDITION(v_criteria_slice_row.qms_report_request_log_id,
                                                                 v_criteria_slice_row.slice_date_from,
                                                                 v_criteria_slice_row.slice_date_to,
                                                                 sliceType));
    END IF;
  /*
    insert into SQLTEXT_TEST (text, Name) values (v_sql, '666888');
    commit;
  */
   /*  delete QMS_CALCULATE_CRITERIA_SLICE2 t where t.id = p_slice_id;
    commit;*/
  
    EXECUTE IMMEDIATE 'begin ' || v_sql || 'end;';
    COMMIT;
    ---更新状态
    UPDATE qms_calculate_criteria_slice2
       SET progress_status = 'D', progress_desc = '完成.'
     WHERE id = p_slice_id;
  
    COMMIT;
  END;

  /******************************************************************************
     NAME:       total_data_cal
     PURPOSE:  报表切片后的总体计算
  ******************************************************************************/

  PROCEDURE total_data_cal(p_report_id VARCHAR2) IS
  
    v_EVALUATE_NO varchar2(32);
    sliceType     varchar2(20);
    provinceCount number; --省的总个数
    cityCount     number; --市的总个数
    unitsCount    number; --网点的总个数
  
    provinceMianitainTotal number; --省维修总数
    cityMaintainTotal      number; --市维修总数
    unitsMaintainTotal     number; --网点网总数
    str                    varchar(20);
    recordCount            number;
  
  begin
   delete QMS_CALCULATE_CRITERIA_SLICE2 t
     where t.qms_report_request_log_id = p_report_id;
    commit;
    sliceType := qms_rpt_utl.get_criteria_by_key(p_report_id,
                                                 'COLUMN_MODEL'); --获取列模式,根据模式计算单位维修率
    select EVALUATE_NO
      into v_EVALUATE_NO
      from qms_report_request_log2 t
     where t.id = p_report_id;
    dbms_output.put_line(v_EVALUATE_NO);
  
    select count(1)
      into recordCount
      from QMS_RPT_AREA_RANK r
     where r.evaluate_id = v_EVALUATE_NO;
  
    if recordCount = 0 then
      UPDATE qms_report_request_log2 T
         SET progress_status        = 'D',
             progress_desc          = '完成.',
             TOTAL_RECORDS          = 0,
             DATETIME_CALC_FINISHED = SYSDATE
       WHERE id = p_report_id;
      COMMIT;
      return;
    end if;
    IF sliceType = 1 then
    
      select t.recodcount
        into unitsCount
        from QMS_RPT_AREA_RANK t
       where t.unit_name is null
         and t.unit_id is null
         and t.evaluate_id = v_EVALUATE_NO;
    
      select t.maintaincount
        into unitsMaintainTotal
        from QMS_RPT_AREA_RANK t
       where t.unit_name is null
         and t.unit_id is null
         and t.evaluate_id = v_EVALUATE_NO;
      dbms_output.put_line(unitsCount);
      dbms_output.put_line(unitsMaintainTotal);
    
      --更新网点排名平均数及占比
    
      update qms_rpt_area_rank r
         set r.average = decode(unitsMaintainTotal / unitsCount,
                                0,
                                '0.00',
                                trim(to_char(unitsMaintainTotal / unitsCount,
                                             '9999999.99'))),
             r.rate    = decode(r.maintaincount / unitsMaintainTotal,
                                0,
                                '0.00',
                                trim(to_char(r.maintaincount /
                                             unitsMaintainTotal,
                                             '9999999.9999'))) * 100
       where r.evaluate_id = v_EVALUATE_NO
         and r.unit_id is not null;
      --更新网点详细排名平均数及占比
    
      --  str := updateBranch(v_EVALUATE_NO, unitsCount);
    
    elsif sliceType = 2 then
    
      select t.recodcount
        into provinceCount
        from QMS_RPT_AREA_RANK t
       where t.provinceid is null
         and t.provincename is null
         and t.evaluate_id = v_EVALUATE_NO;
    
      select t.maintaincount
        into provinceMianitainTotal
        from QMS_RPT_AREA_RANK t
       where t.provinceid is null
         and t.provincename is null
         and t.evaluate_id = v_EVALUATE_NO;
      dbms_output.put_line(provinceCount);
      dbms_output.put_line(provinceMianitainTotal);
      --更新省排名平均数及占比
    
      update qms_rpt_area_rank r
         set r.average = decode(provinceMianitainTotal / provinceCount,
                                0,
                                '0.00',
                                trim(to_char(provinceMianitainTotal /
                                             provinceCount,
                                             '9999999.99'))),
             r.rate    = decode(r.maintaincount / provinceMianitainTotal,
                                0,
                                '0.00',
                                trim(to_char(r.maintaincount /
                                             provinceMianitainTotal,
                                             '9999999.9999'))) * 100
       where r.evaluate_id = v_EVALUATE_NO
         and r.provinceid is not null;
      --更新详细省排名平均数及占比
    
      -- str := updateProvince(v_EVALUATE_NO, unitsCount);
    
    elsif sliceType = 3 then
    
      select t.recodcount
        into cityCount
        from QMS_RPT_AREA_RANK t
       where t.cityid is null
         and t.cityname is null
         and t.evaluate_id = v_EVALUATE_NO;
    
      select t.maintaincount
        into cityMaintainTotal
        from QMS_RPT_AREA_RANK t
       where t.cityid is null
         and t.cityname is null
         and t.evaluate_id = v_EVALUATE_NO;
    
      dbms_output.put_line(cityCount);
      dbms_output.put_line(cityMaintainTotal);
    
      --更新市排名平均数及占比
      update qms_rpt_area_rank r
         set r.average = decode(cityMaintainTotal / cityCount,
                                0,
                                '0.00',
                                trim(to_char(cityMaintainTotal / cityCount,
                                             '9999999.99'))),
             r.rate    = decode(r.maintaincount / cityMaintainTotal,
                                0,
                                '0.00',
                                trim(to_char(r.maintaincount /
                                             cityMaintainTotal,
                                             '9999999.9999'))) * 100
       where r.evaluate_id = v_EVALUATE_NO
         and r.cityid is not null;
      --更新市详细排名平均数及占比
      --str := updateCity(v_EVALUATE_NO, cityCount);
    end if;
    UPDATE qms_report_request_log2 T
       SET progress_status        = 'D',
           progress_desc          = '完成.',
           TOTAL_RECORDS          = 1,
           DATETIME_CALC_FINISHED = SYSDATE
     WHERE id = p_report_id;
    COMMIT;
  end;

END;


qms_report_request_log2
QMS_CALCULATE_CRITERIA_SLICE2
qms_report_type
RPT_RESULT
一:定义JOB调用 qms_report_type
切片存储过程
切片片运算存储过程
合并结果存储过程 存储的状态为R


二:JOB会自动取R状态的分片结果QMS_CALCULATE_CRITERIA_SLICE2
计划完成后设D状态,

三:合并到自己 RPT_RESULT 根据需求读取展示到报表中去。