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

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 1=1
${IF(sPeriod=1,"AND ct_year ='"+  FORMAT(TODAY(),'yyyy') +"'","")}   -- 年
${IF(sPeriod=2,"AND CONCAT(to_char(signed_date,'yyyy'),EXTRACT(QUARTER FROM signed_date))  ='"+ CONCATENATE(left(today(),4),roundup(month(today())/3)) +"'","")} -- 季
${IF(sPeriod=3,"AND to_char(signed_date,'yyyy-MM') ='"+  FORMAT(TODAY(),'yyyy-MM') +"'","")}   -- 月
${IF(sPeriod=4,"AND to_char(signed_date,'yyyy-MM-dd') =  to_char(CURRENT_DATE - INTERVAL '1 day','yyyy-MM-dd') ","")} -- 日
UNION ALL
select
'同期' AS time_type
,sum(ct_amt)/10000 AS amt
from dm.v_ag_sal_contract a
WHERE 1=1
${IF(sPeriod=1,"AND ct_year =  EXTRACT(YEAR FROM CURRENT_DATE - INTERVAL '1 year')  ","")}   -- 去年
${IF(sPeriod=2,"AND CONCAT(to_char(signed_date,'yyyy'),EXTRACT(QUARTER FROM signed_date)) = CONCAT(EXTRACT(YEAR FROM CURRENT_DATE - INTERVAL '1 year'),EXTRACT(QUARTER FROM CURRENT_DATE)) ","")} -- 去年同季
${IF(sPeriod=3,"AND to_char(signed_date,'yyyy-MM') =  to_char(CURRENT_DATE - INTERVAL '12 month','yyyy-MM') ","")}   -- 去年同月
${IF(sPeriod=4,"AND to_char(signed_date,'yyyy-MM-dd') = to_char((CURRENT_DATE - INTERVAL '1 day') - INTERVAL '1 year','yyyy-MM-dd')  ","")} -- 当前时间的前天


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

select
 '当期' time_type
,sum(amt_bu) AS amt
from dm.v_sal_indicator_bu
where (indi_type='回款' OR indi_type='电商订单')
${IF(sPeriod=1,"AND to_char(stat_date,'yyyy') ='"+  FORMAT(TODAY(),'yyyy') +"'","")}   -- 年
${IF(sPeriod=2,"AND CONCAT(to_char(stat_date,'yyyy'),EXTRACT(QUARTER FROM stat_date))  ='"+ CONCATENATE(left(today(),4),roundup(month(today())/3)) +"'","")} -- 季
${IF(sPeriod=3,"AND to_char(stat_date,'yyyy-MM') ='"+  FORMAT(TODAY(),'yyyy-MM') +"'","")}   -- 月
${IF(sPeriod=4,"AND to_char(stat_date,'yyyy-MM-dd') =  to_char(CURRENT_DATE - INTERVAL '1 day','yyyy-MM-dd') ","")} -- 日
UNION ALL
select
'同期' time_type
,sum(amt_bu) AS amt
from dm.v_sal_indicator_bu
where (indi_type='回款' OR indi_type='电商订单')
${IF(sPeriod=1,"AND to_char(stat_date,'yyyy')  =  EXTRACT(YEAR FROM CURRENT_DATE - INTERVAL '1 year')  ","")}   -- 去年
${IF(sPeriod=2,"AND CONCAT(to_char(stat_date,'yyyy'),EXTRACT(QUARTER FROM stat_date)) = CONCAT(EXTRACT(YEAR FROM CURRENT_DATE - INTERVAL '1 year'),EXTRACT(QUARTER FROM CURRENT_DATE)) ","")} -- 去年同季
${IF(sPeriod=3,"AND to_char(stat_date,'yyyy-MM') =  to_char(CURRENT_DATE - INTERVAL '12 month','yyyy-MM') ","")}   -- 去年同月
${IF(sPeriod=4,"AND to_char(stat_date,'yyyy-MM-dd') = to_char((CURRENT_DATE - INTERVAL '1 day') - INTERVAL '1 year','yyyy-MM-dd')  ","")} -- 当前时间的前天

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

            ${IF(sPeriod=1,"AND to_char(stat_date,'yyyy') ='"+  FORMAT(TODAY(),'yyyy') +"'","")}   -- 年
    ${IF(sPeriod=2,"AND CONCAT(to_char(stat_date,'yyyy'),EXTRACT(QUARTER FROM stat_date))  ='"+ CONCATENATE(left(today(),4),roundup(month(today())/3)) +"'","")} -- 季
        ${IF(sPeriod=3,"AND to_char(stat_date,'yyyy-MM') ='"+  FORMAT(TODAY(),'yyyy-MM') +"'","")}   -- 月
        ${IF(sPeriod=4,"AND to_char(stat_date,'yyyy-MM-dd') =  to_char(CURRENT_DATE - INTERVAL '1 day','yyyy-MM-dd') ","")} -- 日


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

select
'当期' time_type
,sum(amt_bu) AS amt
from dm.v_sal_indicator_bu
where (indi_type='出货' OR indi_type='电商订单')
${IF(sPeriod=1,"AND to_char(stat_date,'yyyy') ='"+  FORMAT(TODAY(),'yyyy') +"'","")}   -- 年
${IF(sPeriod=2,"AND CONCAT(to_char(stat_date,'yyyy'),EXTRACT(QUARTER FROM stat_date))  ='"+ CONCATENATE(left(today(),4),roundup(month(today())/3)) +"'","")} -- 季
${IF(sPeriod=3,"AND to_char(stat_date,'yyyy-MM') ='"+  FORMAT(TODAY(),'yyyy-MM') +"'","")}   -- 月
${IF(sPeriod=4,"AND to_char(stat_date,'yyyy-MM-dd') =  to_char(CURRENT_DATE - INTERVAL '1 day','yyyy-MM-dd') ","")} -- 日
UNION ALL
select
'同期' time_type
,sum(amt_bu) AS amt
from dm.v_sal_indicator_bu
where (indi_type='出货' OR indi_type='电商订单')
${IF(sPeriod=1,"AND to_char(stat_date,'yyyy')  =  EXTRACT(YEAR FROM CURRENT_DATE - INTERVAL '1 year')  ","")}   -- 去年
${IF(sPeriod=2,"AND CONCAT(to_char(stat_date,'yyyy'),EXTRACT(QUARTER FROM stat_date)) = CONCAT(EXTRACT(YEAR FROM CURRENT_DATE - INTERVAL '1 year'),EXTRACT(QUARTER FROM CURRENT_DATE)) ","")} -- 去年同季
${IF(sPeriod=3,"AND to_char(stat_date,'yyyy-MM') =  to_char(CURRENT_DATE - INTERVAL '12 month','yyyy-MM') ","")}   -- 去年同月
${IF(sPeriod=4,"AND to_char(stat_date,'yyyy-MM-dd') = to_char((CURRENT_DATE - INTERVAL '1 day') - INTERVAL '1 year','yyyy-MM-dd')  ","")} -- 当前时间的前天
UNION
select
'目标' time_type
,sum(amt_bu) AS amt
from dm.v_sal_indicator_bu
where   ${switch(sPeriod
              ,1,"indi_type='年度目标_事业部_出货'"
              ,2,"indi_type='季度目标_事业部_出货'"
              ,3,"indi_type='月度目标_事业部_出货'"
              ,4,"indi_type='不存在'")}

${IF(sPeriod=1,"AND to_char(stat_date,'yyyy') ='"+  FORMAT(TODAY(),'yyyy') +"'","")}   -- 年
${IF(sPeriod=2,"AND CONCAT(to_char(stat_date,'yyyy'),EXTRACT(QUARTER FROM stat_date))  ='"+ CONCATENATE(left(today(),4),roundup(month(today())/3)) +"'","")} -- 季
${IF(sPeriod=3,"AND to_char(stat_date,'yyyy-MM') ='"+  FORMAT(TODAY(),'yyyy-MM') +"'","")}   -- 月
${IF(sPeriod=4,"AND to_char(stat_date,'yyyy-MM-dd') =  to_char(CURRENT_DATE - INTERVAL '1 day','yyyy-MM-dd') ","")} -- 日


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

select
'当期' time_type
,sum(amt_bu) AS amt
from dm.v_sal_indicator_bu
where (indi_type='发货' OR indi_type='费用发货')
${IF(sPeriod=1,"AND to_char(stat_date,'yyyy') ='"+  FORMAT(TODAY(),'yyyy') +"'","")}   -- 年
${IF(sPeriod=2,"AND CONCAT(to_char(stat_date,'yyyy'),EXTRACT(QUARTER FROM stat_date))  ='"+ CONCATENATE(left(today(),4),roundup(month(today())/3)) +"'","")} -- 季
${IF(sPeriod=3,"AND to_char(stat_date,'yyyy-MM') ='"+  FORMAT(TODAY(),'yyyy-MM') +"'","")}   -- 月
${IF(sPeriod=4,"AND to_char(stat_date,'yyyy-MM-dd') =  to_char(CURRENT_DATE - INTERVAL '1 day','yyyy-MM-dd') ","")} -- 日
UNION ALL
select
'同期' time_type
,sum(amt_bu) AS amt
from dm.v_sal_indicator_bu
where (indi_type='发货' OR indi_type='费用发货')
${IF(sPeriod=1,"AND to_char(stat_date,'yyyy')  =  EXTRACT(YEAR FROM CURRENT_DATE - INTERVAL '1 year')  ","")}   -- 去年
${IF(sPeriod=2,"AND CONCAT(to_char(stat_date,'yyyy'),EXTRACT(QUARTER FROM stat_date)) = CONCAT(EXTRACT(YEAR FROM CURRENT_DATE - INTERVAL '1 year'),EXTRACT(QUARTER FROM CURRENT_DATE)) ","")} -- 去年同季
${IF(sPeriod=3,"AND to_char(stat_date,'yyyy-MM') =  to_char(CURRENT_DATE - INTERVAL '12 month','yyyy-MM') ","")}   -- 去年同月
${IF(sPeriod=4,"AND to_char(stat_date,'yyyy-MM-dd') = to_char((CURRENT_DATE - INTERVAL '1 day') - INTERVAL '1 year','yyyy-MM-dd')  ","")} -- 当前时间的前天
UNION
select
'目标' time_type
,sum(amt_bu) AS amt
from dm.v_sal_indicator_bu
where   ${switch(sPeriod
              ,1,"indi_type IN('年度目标_事业部_费用发货','年度目标_事业部_销售发货')"
              ,2,"indi_type IN('季度目标_事业部_费用发货','季度目标_事业部_销售发货')"
              ,3,"indi_type IN('月度目标_事业部_费用发货','月度目标_事业部_销售发货')"
              ,4,"indi_type IN('不存在')")}
${IF(sPeriod=1,"AND to_char(stat_date,'yyyy') ='"+  FORMAT(TODAY(),'yyyy') +"'","")}   -- 年
${IF(sPeriod=2,"AND CONCAT(to_char(stat_date,'yyyy'),EXTRACT(QUARTER FROM stat_date))  ='"+ CONCATENATE(left(today(),4),roundup(month(today())/3)) +"'","")} -- 季
${IF(sPeriod=3,"AND to_char(stat_date,'yyyy-MM') ='"+  FORMAT(TODAY(),'yyyy-MM') +"'","")}   -- 月
${IF(sPeriod=4,"AND to_char(stat_date,'yyyy-MM-dd') =  to_char(CURRENT_DATE - INTERVAL '1 day','yyyy-MM-dd') ","")} -- 日
