-- ======================================
-- 报表上新
-- ======================================

SELECT
    op1.entry_id AS "报表id",
    op2.displayname AS "报表名称",
    TO_CHAR(op1.create_time,'YYYY-MM-DD')  AS "上新时间"
FROM
    sdi_fine.input_fr_report_list op1
        LEFT JOIN sdi_fine.fine_authority_object op2 ON op1.entry_id = op2.id
WHERE
    report_status = '0'
--	AND STRING_TO_ARRAY(op1.role_list,',') && STRING_TO_ARRAY('${fine_role}',',')
  AND op1.create_time >= CURRENT_TIMESTAMP - INTERVAL '7 days'
ORDER BY
    op1.create_time DESC


-- ======================================
-- 收藏目录表
-- ======================================

-- 收藏目录表
SELECT
    IF(LENGTH(LEFT(op3.path,POSITION('/' IN op3.path) - 1)) > 0,LEFT(op3.path,POSITION('/' IN op3.path) - 1),"其他") AS `所属根目录名称`,
        op3.path AS `报表全路径`,
        op1.id,
    op1.entryId AS `报表ID`,
        op1.userId AS `用户ID`,
        op2.realName AS `姓名`,
        op2.userName AS `用户名`,
        op3.displayName AS `报表名称`

FROM
    fine_favorite_entry op1
        JOIN fine_user op2 ON op1.userId = op2.id
        JOIN fine_authority_object op3 ON op1.entryId = op3.id
WHERE
    op2.userName = '${fine_username}'
  -- 展示终端类型:此处需要再讨论一下PC及移动端定义
  AND op3.deviceType = 1
  -- 平台类型:此处只展示链接/FR报表/BI报表
  AND op3.expandType IN (5,102,201)
--	AND IFNULL(LEFT(op3.path,POSITION('/' IN op3.path) - 1),"其他") = '${sDefaultFile}'


-- ======================================
-- 最近浏览
-- ======================================

-- 近10天近30个目录
SELECT
    op1.tname AS `报表全路径`,
        op2.id AS `报表ID`,
        SUBSTRING_INDEX(op2.displayName, '/', -1) AS `模版名称`,
	-- FROM_UNIXTIME(MAX(op1.time) / 1000,'%Y-%m-%d %H:%i:%s') AS `访问时间`
        FROM_UNIXTIME(MAX(op1.time) / 1000,'%Y-%m-%d') AS `访问时间`
FROM
    logdb.fine_record_execute op1
        LEFT JOIN finedb.fine_authority_object op2 ON op1.tname = op2.path
WHERE
    op1.username = (SELECT CONCAT(realName,'(',userName,')') AS username FROM finedb.fine_user WHERE userName = '${fine_username}')
  AND op1.time >= UNIX_TIMESTAMP(DATE_SUB(CURDATE(),INTERVAL 10 DAY)) * 1000
GROUP BY
    op2.displayName,
    op2.id
ORDER BY
    op1.time DESC
LIMIT 30



-- ======================================
-- 合同量
-- ======================================

select
'当期' AS time_type
,sum(ct_amt)/10000 AS amt
from dm.v_ag_sal_contract a
-- 权限控制
where exists (
    select
    1
    from
    dm.dm_bi_user_permisson
    where
    bi_user = '${fine_username}'
  and per_model IN ('all','sal')
  and (
    is_all = 1
   OR per_code = a.bu_code  -- 事业部
   OR per_code = a.region_code  -- 大区
   OR per_code = a.office_code  -- 办事处
--     OR per_code = a.city_unit_code -- 城市单元
    )
    )
${IF(len(sStart_date) > 0,"AND ct_year ='"+  FORMAT(sStart_date,'yyyy') +"'","")}
-- 年
--  ${IF(sPeriod=1,"AND ct_year ='"+  FORMAT(sStart_date,'yyyy') +"'","")}
-- 季
-- ${IF(sPeriod=2,"AND to_char(signed_date,'yyyy-MM-dd')  >='"+ sStart_date +"'","")}
-- ${IF(sPeriod=2,"AND to_char(signed_date,'yyyy-MM-dd')  <='"+ sEnd_date +"'","")}
-- 月
-- ${IF(sPeriod=3,"AND to_char(signed_date,'yyyy-MM-dd')  >='"+ sStart_date +"'","")}
--- ${IF(sPeriod=3,"AND to_char(signed_date,'yyyy-MM-dd')  <='"+ sEnd_date +"'","")}
-- 日
-- ${IF(sPeriod=4,"AND to_char(signed_date,'yyyy-MM-dd')  >='"+ sStart_date +"'","")}
-- ${IF(sPeriod=4,"AND to_char(signed_date,'yyyy-MM-dd')  <='"+ sEnd_date +"'","")}
UNION ALL
select
'同期' AS time_type
,sum(ct_amt)/10000 AS amt
from dm.v_ag_sal_contract a
-- 权限控制
where exists (
          select
          1
    from
    dm.dm_bi_user_permisson
    where
    bi_user = '${fine_username}'
  and per_model IN ('all','sal')
  and (
    is_all = 1
   OR per_code = a.bu_code  -- 事业部
   OR per_code = a.region_code  -- 大区
   OR per_code = a.office_code  -- 办事处
   OR per_code = a.city_unit_code -- 城市单元
    )
    )


${IF(len(sStart_date) > 0,"AND ct_year = '" + left(yeardelta(sStart_date,-1),4) + "'","")}
-- 去年
-- ${IF(sPeriod=1,"AND ct_year = '" + left(yeardelta(sStart_date,-1),4) + "'","")}
-- 去年同季
-- ${IF(sPeriod=2,"AND to_char(signed_date,'yyyy-MM-dd')  >='"+ yeardelta(sStart_date,-1) +"'","")}
-- ${IF(sPeriod=2,"AND to_char(signed_date,'yyyy-MM-dd')  <='"+ yeardelta(sEnd_date,-1) +"'","")}
-- 去年同月
-- ${IF(sPeriod=3,"AND to_char(signed_date,'yyyy-MM-dd')  >='"+ yeardelta(sStart_date,-1) +"'","")}
-- ${IF(sPeriod=3,"AND to_char(signed_date,'yyyy-MM-dd')  <='"+ yeardelta(sEnd_date,-1) +"'","")}
-- 去年同日
-- ${IF(sPeriod=4,"AND to_char(signed_date,'yyyy-MM-dd')  >='"+ yeardelta(sStart_date,-1) +"'","")}
-- ${IF(sPeriod=4,"AND to_char(signed_date,'yyyy-MM-dd')  <='"+ yeardelta(sEnd_date,-1) +"'","")}

UNION ALL
select
    '当期回款' time_type
     ,sum(amt_bu) AS amt
from dm.v_sal_indicator_bu a
where (indi_type='回款' OR indi_type='电商订单')
-- 权限控制
  AND exists (
    select
    1
    from
    dm.dm_bi_user_permisson
    where
    bi_user = '${fine_username}'
  and per_model IN ('all','sal')
  and (
    is_all = 1
   OR per_code = a.bu_code  -- 事业部
--   OR per_code = a.region_code  -- 大区
--  OR per_code = a.office_code  -- 办事处
--   OR per_code = a.city_unit_code -- 城市单元
    )
    )

    ${IF(len(sStart_date) > 0,"AND to_char(stat_date,'yyyy') ='"+  FORMAT(sStart_date,'yyyy') +"'","")}
-- 年
 --   ${IF(sPeriod=1,"AND to_char(stat_date,'yyyy') ='"+  FORMAT(sStart_date,'yyyy') +"'","")}
-- 季
 --   ${IF(sPeriod=2,"AND to_char(stat_date,'yyyy-MM-dd')  >='"+ sStart_date +"'","")}
--    ${IF(sPeriod=2,"AND to_char(stat_date,'yyyy-MM-dd')  <='"+ sEnd_date +"'","")}
-- 月
--    ${IF(sPeriod=3,"AND to_char(stat_date,'yyyy-MM-dd')  >='"+ sStart_date +"'","")}
--    ${IF(sPeriod=3,"AND to_char(stat_date,'yyyy-MM-dd')  <='"+ sEnd_date +"'","")}
-- 日
--    ${IF(sPeriod=4,"AND to_char(stat_date,'yyyy-MM-dd')  >='"+ sStart_date +"'","")}
--    ${IF(sPeriod=4,"AND to_char(stat_date,'yyyy-MM-dd')  <='"+ sEnd_date +"'","")}



-- ======================================
-- 回款
-- ======================================

select
 '当期' time_type
,sum(amt_bu) AS amt
from dm.v_sal_indicator_bu a
where (indi_type='回款' OR indi_type='电商订单')
-- 权限控制
AND exists (
          select
          1
    from
    dm.dm_bi_user_permisson
    where
    bi_user = '${fine_username}'
  and per_model IN ('all','sal')
  and (
    is_all = 1
   OR per_code = a.bu_code  -- 事业部
--   OR per_code = a.region_code  -- 大区
--  OR per_code = a.office_code  -- 办事处
--   OR per_code = a.city_unit_code -- 城市单元
    )
    )
-- 年
${IF(sPeriod=1,"AND to_char(stat_date,'yyyy') ='"+  FORMAT(sStart_date,'yyyy') +"'","")}
-- 季
${IF(sPeriod=2,"AND CONCAT(to_char(stat_date,'yyyy'),EXTRACT(QUARTER FROM stat_date))  ='"+  CONCATENATE(left(sStart_date,4),roundup(month(sStart_date)/3)) +"'","")}
-- ${IF(sPeriod=2,"AND to_char(stat_date,'yyyy-MM-dd')  <='"+ sEnd_date +"'","")}
-- 月
${IF(sPeriod=3,"AND to_char(stat_date,'yyyy-MM')  ='"+ FORMAT(sStart_date,'yyyy-MM') +"'","")}
-- ${IF(sPeriod=3,"AND to_char(stat_date,'yyyy-MM-dd')  <='"+ sEnd_date +"'","")}
-- 日
${IF(sPeriod=4,"AND to_char(stat_date,'yyyy-MM-dd')  ='"+ sStart_date +"'","")}
-- ${IF(sPeriod=4,"AND to_char(stat_date,'yyyy-MM-dd')  <='"+ sEnd_date +"'","")}

UNION ALL
select
'同期' time_type
,sum(amt_bu) AS amt
from dm.v_sal_indicator_bu a
where (indi_type='回款' OR indi_type='电商订单')
  AND exists (
    select
    1
    from
    dm.dm_bi_user_permisson
    where
    bi_user = '${fine_username}'
  and per_model IN ('all','sal')
  and (
    is_all = 1
   OR per_code = a.bu_code  -- 事业部
--   OR per_code = a.region_code  -- 大区
--  OR per_code = a.office_code  -- 办事处
--   OR per_code = a.city_unit_code -- 城市单元
    )
    )

-- 去年
${IF(sPeriod=1,"AND to_char(stat_date,'yyyy')  = '" + left(yeardelta(sStart_date,-1),4) + "'","")}
-- 去年同季
${IF(sPeriod=2,"AND CONCAT(to_char(stat_date,'yyyy'),EXTRACT(QUARTER FROM stat_date))  ='"+ CONCATENATE(left(YEARDELTA(sStart_date,-1),4),roundup(month(YEARDELTA(sStart_date,-1))/3))  +"'","")}

-- ${IF(sPeriod=2,"AND to_char(stat_date,'yyyy-MM-dd')  <='"+ yeardelta(sEnd_date,-1) +"'","")}
-- 去年同月
${IF(sPeriod=3,"AND to_char(stat_date,'yyyy-MM') ='"+ FORMAT(yeardelta(sStart_date,-1),'yyyy-MM') +"'","")}
-- ${IF(sPeriod=3,"AND to_char(stat_date,'yyyy-MM-dd')  <='"+ yeardelta(sEnd_date,-1) +"'","")}
-- 去年同日
${IF(sPeriod=4,"AND to_char(stat_date,'yyyy-MM-dd')  ='"+ yeardelta(sStart_date,-1) +"'","")}
-- ${IF(sPeriod=4,"AND to_char(stat_date,'yyyy-MM-dd')  <='"+ yeardelta(sEnd_date,-1) +"'","")}
UNION
select
'目标' time_type,
sum(amt_bu) AS amt
from dm.v_sal_indicator_bu a
where   ${switch(sPeriod
              ,1,"indi_type='年度目标_事业部_回款'"
              ,2,"indi_type='季度目标_事业部_回款'"
              ,3,"indi_type='月度目标_事业部_回款'"
              ,4,"indi_type='不存在'")}
AND exists (
    select
    1
    from
    dm.dm_bi_user_permisson
    where
    bi_user = '${fine_username}'
  and per_model IN ('all','sal')
  and (
    is_all = 1
   OR per_code = a.bu_code  -- 事业部
--   OR per_code = a.region_code  -- 大区
--  OR per_code = a.office_code  -- 办事处
--   OR per_code = a.city_unit_code -- 城市单元
    )
    )
-- 年
${IF(sPeriod=1,"AND to_char(stat_date,'yyyy') ='"+  FORMAT(sStart_date,'yyyy') +"'","")}
-- 季
${IF(sPeriod=2,"AND CONCAT(to_char(stat_date,'yyyy'),EXTRACT(QUARTER FROM stat_date))  ='"+  CONCATENATE(left(sStart_date,4),roundup(month(sStart_date)/3)) +"'","")}
-- ${IF(sPeriod=2,"AND to_char(stat_date,'yyyy-MM-dd')  <='"+ sEnd_date +"'","")}
-- 月
${IF(sPeriod=3,"AND to_char(stat_date,'yyyy-MM')  ='"+ FORMAT(sStart_date,'yyyy-MM') +"'","")}
-- ${IF(sPeriod=3,"AND to_char(stat_date,'yyyy-MM-dd')  <='"+ sEnd_date +"'","")}
-- 日
${IF(sPeriod=4,"AND to_char(stat_date,'yyyy-MM-dd')  ='"+ sStart_date +"'","")}

-- ${IF(sPeriod=4,"AND to_char(stat_date,'yyyy-MM-dd')  <='"+ sEnd_date +"'","")}

-- ======================================
-- 经销商出货
-- ======================================

select
'当期' time_type
,sum(amt_bu) AS amt
from dm.v_sal_indicator_bu a
where (indi_type='出货' OR indi_type='电商订单')
-- 权限控制
  AND exists (
    select
    1
    from
    dm.dm_bi_user_permisson
    where
    bi_user = '${fine_username}'
  and per_model IN ('all','sal')
  and (
    is_all = 1
   OR per_code = a.bu_code  -- 事业部
--   OR per_code = a.region_code  -- 大区
--  OR per_code = a.office_code  -- 办事处
--   OR per_code = a.city_unit_code -- 城市单元
    )
    )
-- 年
    ${IF(sPeriod=1,"AND to_char(stat_date,'yyyy') ='"+  FORMAT(sStart_date,'yyyy') +"'","")}
-- 季
    ${IF(sPeriod=2,"AND CONCAT(to_char(stat_date,'yyyy'),EXTRACT(QUARTER FROM stat_date))  ='"+  CONCATENATE(left(sStart_date,4),roundup(month(sStart_date)/3)) +"'","")}
--    ${IF(sPeriod=2,"AND to_char(stat_date,'yyyy-MM-dd')  <='"+ sEnd_date +"'","")}
-- 月
    ${IF(sPeriod=3,"AND to_char(stat_date,'yyyy-MM')  ='"+ FORMAT(sStart_date,'yyyy-MM') +"'","")}
--   ${IF(sPeriod=3,"AND to_char(stat_date,'yyyy-MM-dd')  <='"+ sEnd_date +"'","")}
-- 日
    ${IF(sPeriod=4,"AND to_char(stat_date,'yyyy-MM-dd')  ='"+ sStart_date +"'","")}
 --   ${IF(sPeriod=4,"AND to_char(stat_date,'yyyy-MM-dd')  <='"+ sEnd_date +"'","")}

UNION ALL
select
'同期' time_type
,sum(amt_bu) AS amt
from dm.v_sal_indicator_bu a
where (indi_type='出货' OR indi_type='电商订单')
  AND exists (
    select
    1
    from
    dm.dm_bi_user_permisson
    where
    bi_user = '${fine_username}'
  and per_model IN ('all','sal')
  and (
    is_all = 1
   OR per_code = a.bu_code  -- 事业部
--   OR per_code = a.region_code  -- 大区
--  OR per_code = a.office_code  -- 办事处
--   OR per_code = a.city_unit_code -- 城市单元
    )
    )

-- 去年
    ${IF(sPeriod=1,"AND to_char(stat_date,'yyyy')  = '" + left(yeardelta(sStart_date,-1),4) + "'","")}
-- 去年同季
    ${IF(sPeriod=2,"AND CONCAT(to_char(stat_date,'yyyy'),EXTRACT(QUARTER FROM stat_date))  ='"+ CONCATENATE(left(YEARDELTA(sStart_date,-1),4),roundup(month(YEARDELTA(sStart_date,-1))/3))  +"'","")}
--    ${IF(sPeriod=2,"AND to_char(stat_date,'yyyy-MM-dd')  <='"+ yeardelta(sEnd_date,-1) +"'","")}
-- 去年同月
    ${IF(sPeriod=3,"AND to_char(stat_date,'yyyy-MM') ='"+ FORMAT(yeardelta(sStart_date,-1),'yyyy-MM') +"'","")}
--    ${IF(sPeriod=3,"AND to_char(stat_date,'yyyy-MM-dd')  <='"+ yeardelta(sEnd_date,-1) +"'","")}
-- 去年同日
    ${IF(sPeriod=4,"AND to_char(stat_date,'yyyy-MM-dd')  ='"+ yeardelta(sStart_date,-1) +"'","")}
--    ${IF(sPeriod=4,"AND to_char(stat_date,'yyyy-MM-dd')  <='"+ yeardelta(sEnd_date,-1) +"'","")}


UNION
select
'目标' time_type
,sum(amt_bu) AS amt
from dm.v_sal_indicator_bu a
where   ${switch(sPeriod
              ,1,"indi_type='年度目标_事业部_出货'"
              ,2,"indi_type='季度目标_事业部_出货'"
              ,3,"indi_type='月度目标_事业部_出货'"
              ,4,"indi_type='不存在'")}

  AND exists (
    select
    1
    from
    dm.dm_bi_user_permisson
    where
    bi_user = '${fine_username}'
  and per_model IN ('all','sal')
  and (
    is_all = 1
   OR per_code = a.bu_code  -- 事业部
--   OR per_code = a.region_code  -- 大区
--  OR per_code = a.office_code  -- 办事处
--   OR per_code = a.city_unit_code -- 城市单元
    )
    )
-- 年
    ${IF(sPeriod=1,"AND to_char(stat_date,'yyyy') ='"+  FORMAT(sStart_date,'yyyy') +"'","")}
-- 季
    ${IF(sPeriod=2,"AND CONCAT(to_char(stat_date,'yyyy'),EXTRACT(QUARTER FROM stat_date))  ='"+  CONCATENATE(left(sStart_date,4),roundup(month(sStart_date)/3)) +"'","")}
--    ${IF(sPeriod=2,"AND to_char(stat_date,'yyyy-MM-dd')  <='"+ sEnd_date +"'","")}
-- 月
    ${IF(sPeriod=3,"AND to_char(stat_date,'yyyy-MM')  ='"+ FORMAT(sStart_date,'yyyy-MM') +"'","")}
--    ${IF(sPeriod=3,"AND to_char(stat_date,'yyyy-MM-dd')  <='"+ sEnd_date +"'","")}
-- 日
    ${IF(sPeriod=4,"AND to_char(stat_date,'yyyy-MM-dd')  ='"+ sStart_date +"'","")}
  --  ${IF(sPeriod=4,"AND to_char(stat_date,'yyyy-MM-dd')  <='"+ sEnd_date +"'","")}


-- ======================================
-- 发货量
-- ======================================

select
'当期' time_type
,sum(amt_bu) AS amt
from dm.v_sal_indicator_bu a
where (indi_type='发货' OR indi_type='费用发货')
-- 权限控制
  AND exists (
    select
    1
    from
    dm.dm_bi_user_permisson
    where
    bi_user = '${fine_username}'
  and per_model IN ('all','sal')
  and (
    is_all = 1
   OR per_code = a.bu_code  -- 事业部
--   OR per_code = a.region_code  -- 大区
--  OR per_code = a.office_code  -- 办事处
--   OR per_code = a.city_unit_code -- 城市单元
    )
    )
-- 年
    ${IF(sPeriod=1,"AND to_char(stat_date,'yyyy') ='"+  FORMAT(sStart_date,'yyyy') +"'","")}
-- 季
    ${IF(sPeriod=2,"AND CONCAT(to_char(stat_date,'yyyy'),EXTRACT(QUARTER FROM stat_date))  ='"+  CONCATENATE(left(sStart_date,4),roundup(month(sStart_date)/3)) +"'","")}
--    ${IF(sPeriod=2,"AND to_char(stat_date,'yyyy-MM-dd')  <='"+ sEnd_date +"'","")}
-- 月
    ${IF(sPeriod=3,"AND to_char(stat_date,'yyyy-MM')  ='"+ FORMAT(sStart_date,'yyyy-MM') +"'","")}
--    ${IF(sPeriod=3,"AND to_char(stat_date,'yyyy-MM-dd')  <='"+ sEnd_date +"'","")}
-- 日
    ${IF(sPeriod=4,"AND to_char(stat_date,'yyyy-MM-dd')  ='"+ sStart_date +"'","")}
--    ${IF(sPeriod=4,"AND to_char(stat_date,'yyyy-MM-dd')  <='"+ sEnd_date +"'","")}
UNION ALL
select
'同期' time_type
,sum(amt_bu) AS amt
from dm.v_sal_indicator_bu a
where (indi_type='发货' OR indi_type='费用发货')
  AND exists (
    select
    1
    from
    dm.dm_bi_user_permisson
    where
    bi_user = '${fine_username}'
  and per_model IN ('all','sal')
  and (
    is_all = 1
   OR per_code = a.bu_code  -- 事业部
--   OR per_code = a.region_code  -- 大区
--  OR per_code = a.office_code  -- 办事处
--   OR per_code = a.city_unit_code -- 城市单元
    )
    )

-- 去年
    ${IF(sPeriod=1,"AND to_char(stat_date,'yyyy')  = '" + left(yeardelta(sStart_date,-1),4) + "'","")}
-- 去年同季
    ${IF(sPeriod=2,"AND CONCAT(to_char(stat_date,'yyyy'),EXTRACT(QUARTER FROM stat_date))  ='"+ CONCATENATE(left(YEARDELTA(sStart_date,-1),4),roundup(month(YEARDELTA(sStart_date,-1))/3))  +"'","")}
 --   ${IF(sPeriod=2,"AND to_char(stat_date,'yyyy-MM-dd')  <='"+ yeardelta(sEnd_date,-1) +"'","")}
-- 去年同月
    ${IF(sPeriod=3,"AND to_char(stat_date,'yyyy-MM') ='"+ FORMAT(yeardelta(sStart_date,-1),'yyyy-MM') +"'","")}
--    ${IF(sPeriod=3,"AND to_char(stat_date,'yyyy-MM-dd')  <='"+ yeardelta(sEnd_date,-1) +"'","")}
-- 去年同日
    ${IF(sPeriod=4,"AND to_char(stat_date,'yyyy-MM-dd')  ='"+ yeardelta(sStart_date,-1) +"'","")}
--    ${IF(sPeriod=4,"AND to_char(stat_date,'yyyy-MM-dd')  <='"+ yeardelta(sEnd_date,-1) +"'","")}


UNION
select
'目标' time_type
,sum(amt_bu) AS amt
from dm.v_sal_indicator_bu a
where   ${switch(sPeriod
              ,1,"indi_type IN('年度目标_事业部_费用发货','年度目标_事业部_销售发货')"
              ,2,"indi_type IN('季度目标_事业部_费用发货','季度目标_事业部_销售发货')"
              ,3,"indi_type IN('月度目标_事业部_费用发货','月度目标_事业部_销售发货')"
              ,4,"indi_type IN('不存在')")}
  AND exists (
    select
    1
    from
    dm.dm_bi_user_permisson
    where
    bi_user = '${fine_username}'
  and per_model IN ('all','sal')
  and (
    is_all = 1
   OR per_code = a.bu_code  -- 事业部
--   OR per_code = a.region_code  -- 大区
--  OR per_code = a.office_code  -- 办事处
--   OR per_code = a.city_unit_code -- 城市单元
    )
    )
-- 年
    ${IF(sPeriod=1,"AND to_char(stat_date,'yyyy') ='"+  FORMAT(sStart_date,'yyyy') +"'","")}
-- 季
    ${IF(sPeriod=2,"AND CONCAT(to_char(stat_date,'yyyy'),EXTRACT(QUARTER FROM stat_date))  ='"+  CONCATENATE(left(sStart_date,4),roundup(month(sStart_date)/3)) +"'","")}
--    ${IF(sPeriod=2,"AND to_char(stat_date,'yyyy-MM-dd')  <='"+ sEnd_date +"'","")}
-- 月
    ${IF(sPeriod=3,"AND to_char(stat_date,'yyyy-MM')  ='"+ FORMAT(sStart_date,'yyyy-MM') +"'","")}
--    ${IF(sPeriod=3,"AND to_char(stat_date,'yyyy-MM-dd')  <='"+ sEnd_date +"'","")}
-- 日
    ${IF(sPeriod=4,"AND to_char(stat_date,'yyyy-MM-dd')  ='"+ sStart_date +"'","")}
--    ${IF(sPeriod=4,"AND to_char(stat_date,'yyyy-MM-dd')  <='"+ sEnd_date +"'","")}


-- ======================================
-- 财务资金状况
-- ======================================



-- 资金计划与执行
select
'当期' AS time_type
,'收入' AS fee_type -- 收支方向 收入/支出
,SUM(CASE WHEN trade_direction = '收入' THEN amt ELSE 0 END) / 10000 AS amt
from dm.dm_fim_fud_indicator a
where corp_code not in ('0101','0103','0102')
AND trade_direction = '收入'
AND indi_type = '执行'
AND plan_item_name NOT IN ('内部调拨收入','取得贷款')
AND owner_bank_account <> '22195101040012008'
-- 权限控制
AND exists (
    select
        1
    from
        dm.dm_bi_user_permisson
    where
        bi_user = '${fine_username}'
      and per_model IN ('all','sal')
      and (
        is_all = 1
            OR per_code = a.corp_code  -- 事业部
--   OR per_code = a.region_code  -- 大区
--  OR per_code = a.office_code  -- 办事处
--   OR per_code = a.city_unit_code -- 城市单元
        )
)
-- 年
    ${IF(sPeriod_fin=1,"AND to_char(stat_date,'yyyy') ='"+  FORMAT(sStart_date_fin,'yyyy') +"'","")}
-- 月
    ${IF(sPeriod_fin=2,"AND to_char(stat_date,'yyyy-MM') = '"+ FORMAT(sStart_date_fin,'yyyy-MM') +"'","")}
-- 日
    ${IF(sPeriod_fin=3,"AND to_char(stat_date,'yyyy-MM-dd')  >= '"+ sStart_date_fin +"'","")}
    ${IF(sPeriod_fin=4,"AND to_char(stat_date,'yyyy-MM-dd')  <= '"+ sStart_date_fin  +"'","")}
UNION ALL
select
'当期' AS time_type
,'支出' AS fee_type -- 收支方向 收入/支出
,SUM(CASE WHEN trade_direction = '支出' THEN amt ELSE 0 END) / 10000 AS amt
from dm.dm_fim_fud_indicator a
where corp_code not in ('0101','0103','0102')
  AND trade_direction = '支出'
  AND indi_type = '执行'
  AND plan_item_name NOT IN ('内部调拨支出','偿还贷款')
  AND owner_bank_account <> '22195101040012008'
-- 权限控制
  AND exists (
    select
        1
    from
        dm.dm_bi_user_permisson
    where
        bi_user = '${fine_username}'
      and per_model IN ('all','sal')
      and (
        is_all = 1
            OR per_code = a.corp_code  -- 事业部
--   OR per_code = a.region_code  -- 大区
--  OR per_code = a.office_code  -- 办事处
--   OR per_code = a.city_unit_code -- 城市单元
        )
)

-- 年
    ${IF(sPeriod_fin=1,"AND to_char(stat_date,'yyyy') ='"+  FORMAT(sStart_date_fin,'yyyy') +"'","")}
-- 月
    ${IF(sPeriod_fin=2,"AND to_char(stat_date,'yyyy-MM')  ='"+ FORMAT(sStart_date_fin,'yyyy-MM') +"'","")}
-- 日
    ${IF(sPeriod_fin=3,"AND to_char(stat_date,'yyyy-MM-dd')  >='"+ sStart_date_fin +"'","")}
    ${IF(sPeriod_fin=4,"AND to_char(stat_date,'yyyy-MM-dd')  <='"+ sStart_date_fin  +"'","")}
UNION ALL
-- 月初资金余额要取上月的月初的，年初要取上年末12月份的
select
'当期' AS time_type
,'余额' AS fee_type
,sum(amt) / 10000 AS AMT
from dm.dm_fim_fud_balance a
where corp_code not in ('0101','0103','0102')
AND owner_bank_account NOT IN ('20351059900100000512291'
    ,'22195101040012008'
    ,'信用证保证金'
    ,'22195162750000012'
    ,'22195162650000017'
    ,'22195162350000014'
    ,'22195162050000015'
    ,'951018033000447470')
-- 权限控制
  AND exists (
    select
        1
    from
        dm.dm_bi_user_permisson
    where
        bi_user = '${fine_username}'
      and per_model IN ('all','sal')
      and (
        is_all = 1
            OR per_code = a.corp_code  -- 事业部
--   OR per_code = a.region_code  -- 大区
--  OR per_code = a.office_code  -- 办事处
--   OR per_code = a.city_unit_code -- 城市单元
        )
)
-- 年
    ${IF(sPeriod_fin=1,"AND to_char(stat_date,'yyyy') ='"+  FORMAT(sStart_date_fin,'yyyy') +"'","")}
-- 月
    ${IF(sPeriod_fin=2,"AND to_char(stat_date,'yyyy-MM')  ='"+ FORMAT(MONTHDELTA(sStart_date_fin,-1),'yyyy-MM') +"'","")}
-- 日
    ${IF(sPeriod_fin=3,"AND to_char(stat_date,'yyyy-MM-dd')  >='"+ sStart_date_fin +"'","")}
    ${IF(sPeriod_fin=4,"AND to_char(stat_date,'yyyy-MM-dd')  <='"+ sStart_date_fin  +"'","")}

UNION ALL
-- 资金计划与执行
select
    '上期' AS time_type
     ,'收入' AS fee_type -- 收支方向 收入/支出
     ,SUM(CASE WHEN trade_direction = '收入' THEN amt ELSE 0 END) / 10000 AS amt
from dm.dm_fim_fud_indicator a
where corp_code not in ('0101','0103','0102')
  AND trade_direction = '收入'
  AND indi_type = '执行'
  AND plan_item_name NOT IN ('内部调拨收入','取得贷款')
  AND owner_bank_account <> '22195101040012008'
-- 权限控制
  AND exists (
    select
    1
    from
        dm.dm_bi_user_permisson
    where
        bi_user = '${fine_username}'
      and per_model IN ('all','sal')
      and (
        is_all = 1
            OR per_code = a.corp_code  -- 事业部
--   OR per_code = a.region_code  -- 大区
--  OR per_code = a.office_code  -- 办事处
--   OR per_code = a.city_unit_code -- 城市单元
        )
)
-- 上年
    ${IF(sPeriod_fin=1,"AND to_char(stat_date,'yyyy') ='"+  FORMAT(YEARDELTA(sStart_date_fin,-1),'yyyy') +"'","")}
-- 上月
    ${IF(sPeriod_fin=2,"AND to_char(stat_date,'yyyy-MM') = '"+ FORMAT(MONTHDELTA(sStart_date_fin,-1),'yyyy-MM') +"'","")}
-- 昨日
    ${IF(sPeriod_fin=3,"AND to_char(stat_date,'yyyy-MM-dd')  >= '"+ DATEDELTA(sStart_date_fin,-1) +"'","")}
    ${IF(sPeriod_fin=4,"AND to_char(stat_date,'yyyy-MM-dd')  <= '"+ DATEDELTA(sStart_date_fin,-1)  +"'","")}
UNION ALL
select
    '上期' AS time_type
     ,'支出' AS fee_type -- 收支方向 收入/支出
     ,SUM(CASE WHEN trade_direction = '支出' THEN amt ELSE 0 END) / 10000 AS amt
from dm.dm_fim_fud_indicator a
where corp_code not in ('0101','0103','0102')
  AND trade_direction = '支出'
  AND indi_type = '执行'
  AND plan_item_name NOT IN ('内部调拨支出','偿还贷款')
  AND owner_bank_account <> '22195101040012008'
-- 权限控制
  AND exists (
    select
    1
    from
        dm.dm_bi_user_permisson
    where
        bi_user = '${fine_username}'
      and per_model IN ('all','sal')
      and (
        is_all = 1
            OR per_code = a.corp_code  -- 事业部
--   OR per_code = a.region_code  -- 大区
--  OR per_code = a.office_code  -- 办事处
--   OR per_code = a.city_unit_code -- 城市单元
        )
)

-- 上年
    ${IF(sPeriod_fin=1,"AND to_char(stat_date,'yyyy') ='"+  FORMAT(YEARDELTA(sStart_date_fin,-1),'yyyy') +"'","")}
-- 上月
    ${IF(sPeriod_fin=2,"AND to_char(stat_date,'yyyy-MM') = '"+ FORMAT(MONTHDELTA(sStart_date_fin,-1),'yyyy-MM') +"'","")}
-- 昨日
    ${IF(sPeriod_fin=3,"AND to_char(stat_date,'yyyy-MM-dd')  >= '"+ DATEDELTA(sStart_date_fin,-1) +"'","")}
    ${IF(sPeriod_fin=4,"AND to_char(stat_date,'yyyy-MM-dd')  <= '"+ DATEDELTA(sStart_date_fin,-1)  +"'","")}
UNION ALL
-- 月初资金余额要取上月的月初的，年初要取上年末12月份的
select
    '上期' AS time_type
     ,'余额' AS fee_type
     ,sum(amt) / 10000 AS AMT
from dm.dm_fim_fud_balance a
where corp_code not in ('0101','0103','0102')
  AND owner_bank_account NOT IN ('20351059900100000512291'
    ,'22195101040012008'
    ,'信用证保证金'
    ,'22195162750000012'
    ,'22195162650000017'
    ,'22195162350000014'
    ,'22195162050000015'
    ,'951018033000447470')
-- 权限控制
  AND exists (
    select
    1
    from
        dm.dm_bi_user_permisson
    where
        bi_user = '${fine_username}'
      and per_model IN ('all','sal')
      and (
        is_all = 1
            OR per_code = a.corp_code  -- 事业部
--   OR per_code = a.region_code  -- 大区
--  OR per_code = a.office_code  -- 办事处
--   OR per_code = a.city_unit_code -- 城市单元
        )
)
-- 上年
    ${IF(sPeriod_fin=1,"AND to_char(stat_date,'yyyy') ='"+  FORMAT(YEARDELTA(sStart_date_fin,-1),'yyyy') +"'","")}
-- 上月
    ${IF(sPeriod_fin=2,"AND to_char(stat_date,'yyyy-MM')  ='"+ FORMAT(MONTHDELTA(sStart_date_fin,-2),'yyyy-MM') +"'","")}
-- 昨日
    ${IF(sPeriod_fin=3,"AND to_char(stat_date,'yyyy-MM-dd')  >= '"+ DATEDELTA(sStart_date_fin,-1) +"'","")}
    ${IF(sPeriod_fin=4,"AND to_char(stat_date,'yyyy-MM-dd')  <= '"+ DATEDELTA(sStart_date_fin,-1)  +"'","")}



-- ======================================
-- 财务资金月度
-- ======================================

SELECT
t.stat_date AS stat_date
,to_char(t.stat_date,'MM') as month
,CASE WHEN t.fee_type = '收入' THEN amt ELSE 0 END AS revenue_fee -- 收入
,CASE WHEN t.fee_type = '支出' THEN amt ELSE 0 END AS expend_fee  -- 支出
,CASE WHEN t.fee_type = '余额' THEN amt ELSE 0 END AS balance_fee -- 余额
FROM (SELECT stat_date
           , '收入'                                                              AS fee_type -- 收支方向 收入/支出
           , SUM(CASE WHEN trade_direction = '收入' THEN amt ELSE 0 END) / 10000 AS amt
      FROM dm.dm_fim_fud_indicator a
      WHERE corp_code NOT IN ('0101', '0103', '0102')
        AND trade_direction = '收入'
        AND indi_type = '执行'
        AND plan_item_name NOT IN ('内部调拨收入', '取得贷款')
        AND owner_bank_account <> '22195101040012008'
-- 权限控制
        AND exists(select 1
    FROM
    dm.dm_bi_user_permisson
    WHERE
    bi_user = '${fine_username}'
  AND per_model IN ('all','sal')
  AND (
    is_all = 1
   OR per_code = a.corp_code  -- 事业部
--   OR per_code = a.region_code  -- 大区
--  OR per_code = a.office_code  -- 办事处
--   OR per_code = a.city_unit_code -- 城市单元
    )
    )
          ${IF(len(sStart_date_fin) > 0,"AND to_char(stat_date,'yyyy') ='"+  FORMAT(sStart_date_fin,'yyyy') +"'","")}
      -- 年
      --   ${IF(sPeriod_fin=1,"AND to_char(stat_date,'yyyy') ='"+  FORMAT(sStart_date_fin,'yyyy') +"'","")}
-- 月
--  ${IF(sPeriod_fin=2,"AND to_char(stat_date,'yyyy-MM') = '"+ FORMAT(sStart_date_fin,'yyyy-MM') +"'","")}
-- 日
--   ${IF(sPeriod_fin=3,"AND to_char(stat_date,'yyyy-MM-dd')  >= '"+ sStart_date_fin +"'","")}
--    ${IF(sPeriod_fin=4,"AND to_char(stat_date,'yyyy-MM-dd')  <= '"+ sStart_date_fin  +"'","")}
      GROUP BY stat_date
      UNION ALL
      SELECT stat_date
           , '支出'                                                              AS fee_type -- 收支方向 收入/支出
           , SUM(CASE WHEN trade_direction = '支出' THEN amt ELSE 0 END) / 10000 AS amt
      FROM dm.dm_fim_fud_indicator a
      WHERE corp_code NOT IN ('0101', '0103', '0102')
        AND trade_direction = '支出'
        AND indi_type = '执行'
        AND plan_item_name NOT IN ('内部调拨支出', '偿还贷款')
        AND owner_bank_account <> '22195101040012008'
-- 权限控制
        AND exists(select 1
    FROM
    dm.dm_bi_user_permisson
    WHERE
    bi_user = '${fine_username}'
  AND per_model IN ('all','sal')
  AND (
    is_all = 1
   OR per_code = a.corp_code  -- 事业部
--   OR per_code = a.region_code  -- 大区
--  OR per_code = a.office_code  -- 办事处
--   OR per_code = a.city_unit_code -- 城市单元
    )
    )
          ${IF(len(sStart_date_fin) > 0,"AND to_char(stat_date,'yyyy') ='"+  FORMAT(sStart_date_fin,'yyyy') +"'","")}
      -- 年
      --   ${IF(sPeriod_fin=1,"AND to_char(stat_date,'yyyy') ='"+  FORMAT(sStart_date_fin,'yyyy') +"'","")}
-- 月
--    ${IF(sPeriod_fin=2,"AND to_char(stat_date,'yyyy-MM')  ='"+ FORMAT(sStart_date_fin,'yyyy-MM') +"'","")}
-- 日
--  ${IF(sPeriod_fin=3,"AND to_char(stat_date,'yyyy-MM-dd')  >='"+ sStart_date_fin +"'","")}
--    ${IF(sPeriod_fin=4,"AND to_char(stat_date,'yyyy-MM-dd')  <='"+ sStart_date_fin  +"'","")}
      GROUP BY stat_date
      UNION ALL
-- 月初资金余额要取上月的月初的，年初要取上年末12月份的

      SELECT
       A.stat_date        AS stat_date
       , '余额'           AS fee_type
       ,SUM(B.amt) / 10000 AS AMT
      FROM
      (
        SELECT
            stat_date
            ,stat_date - INTERVAL '1 month'  AS stat_date1
           , SUM(amt) / 10000 AS AMT
      FROM dm.dm_fim_fud_balance a
      WHERE corp_code NOT IN ('0101', '0103', '0102')
        AND owner_bank_account NOT IN
            ('20351059900100000512291', '22195101040012008', '信用证保证金', '22195162750000012', '22195162650000017',
             '22195162350000014', '22195162050000015', '951018033000447470')
-- 权限控制
        AND EXISTS (SELECT 1
                    FROM dm.dm_bi_user_permisson
                    WHERE bi_user = '${fine_username}'
                      AND per_model IN ('all', 'sal')
                      AND (
                        is_all = 1
                            OR per_code = a.corp_code -- 事业部
--   OR per_code = a.region_code  -- 大区
--  OR per_code = a.office_code  -- 办事处
--   OR per_code = a.city_unit_code -- 城市单元
                        ))
      GROUP BY stat_date - INTERVAL '1 month',stat_date
    )A
    LEFT JOIN
    (
        SELECT stat_date        AS stat_date

             , SUM(amt) / 10000 AS AMT
        FROM dm.dm_fim_fud_balance a
        WHERE corp_code NOT IN ('0101', '0103', '0102')
          AND owner_bank_account NOT IN
              ('20351059900100000512291', '22195101040012008', '信用证保证金', '22195162750000012', '22195162650000017',
               '22195162350000014', '22195162050000015', '951018033000447470')
-- 权限控制
          AND EXISTS (SELECT 1
                      FROM dm.dm_bi_user_permisson
                      WHERE bi_user = '${fine_username}'
                        AND per_model IN ('all', 'sal')
                        AND (
                          is_all = 1
                              OR per_code = a.corp_code -- 事业部
                          ))
        GROUP BY stat_date
    )B
    ON A.stat_date1 = B.stat_date
    ${IF(len(sStart_date_fin) > 0,"AND to_char(a.stat_date,'yyyy') ='"+  FORMAT(sStart_date_fin,'yyyy') +"'","")}


        -- 年
        --   ${IF(sPeriod_fin=1,"AND to_char(stat_date,'yyyy') ='"+  FORMAT(sStart_date_fin,'yyyy') +"'","")}
-- 月
--   ${IF(sPeriod_fin=2,"AND to_char(stat_date,'yyyy-MM')  ='"+ FORMAT(MONTHDELTA(sStart_date_fin,-1),'yyyy-MM') +"'","")}
-- 日
--   ${IF(sPeriod_fin=3,"AND to_char(stat_date,'yyyy-MM-dd')  >='"+ sStart_date_fin +"'","")}
--   ${IF(sPeriod_fin=4,"AND to_char(stat_date,'yyyy-MM-dd')  <='"+ sStart_date_fin  +"'","")}
        group by  A.stat_date
    )t
ORDER BY  to_char(t.stat_date,'MM')