###报表所有sql语句
###完成率
#sql("finish")
select count(0)as count, case  when e.STATELABEL = '结束' then '结束' else '未结束' end AS tag  from tlk_examine e
where e.DOMAINID=#para(DOMAINID) and author_dept_index like #para(ID,"like")
and e.created between to_date(#para(begin),'yyyy-mm-dd hh24:mi:ss') and to_date(#para(end),'yyyy-mm-dd hh24:mi:ss')
and e.STATELABEL is not null

#if(ITEM_TEST_DEPT)
and e.ITEM_TEST_DEPT like #para(ITEM_TEST_DEPT,"like")
#end

#if(ITEM_DEPT)
and e.ITEM_DEPT like #para(ITEM_DEPT,"like")
#end

#if(ITEM_PRODUCT)
and e.ITEM_PRODUCT=#para(ITEM_PRODUCT)
#end

group by  case  when e.STATELABEL = '结束'  then '结束' else '未结束' end
#end

###试验类型统计列表
#sql("finish_list")
select e.*  from tlk_examine e
left outer join t_user on t_user.ID=author
left outer join t_department on t_user.defaultdepartment=t_department.ID
where  t_department.ID in (select  ID from t_department start with ID=#para(ID) connect by prior ID=superior)
and e.created between to_date(#para(begin),'yyyy-mm-dd hh24:mi:ss') and to_date(#para(end),'yyyy-mm-dd hh24:mi:ss')

#if(name&&name=='结束')
and e.STATELABEL = '结束'
#else
and e.STATELABEL != '结束'
#end

#if(ITEM_TEST_DEPT)
and e.ITEM_TEST_DEPT like #para(ITEM_TEST_DEPT,"like")
#end

#if(ITEM_DEPT)
and e.ITEM_DEPT like #para(ITEM_DEPT,"like")
#end

#if(ITEM_PRODUCT)
and e.ITEM_PRODUCT=#para(ITEM_PRODUCT)
#end


#end

###选择部门
#sql("department")
 select ID,NAME,SUPERIOR from t_department start with ID=#para(ID) connect by prior ID=superior
#end

###产品线选择
#sql("prouname")
  select item_dic_prouname from tlk_dic_prou
#end

###各流程耗时
#sql("process")
 select round(sum((cast(PROCESSTIME as date)-cast(ACTIONTIME as date))*86400)/60) interval,FLOWNAME from T_RELATIONHIS e
 left outer join t_user on t_user.ID=auditor
 left outer join t_department on t_user.defaultdepartment=t_department.ID
 where t_department.ID in (select  ID from t_department start with ID=#para(ID) connect by prior ID=superior)
 and e.PROCESSTIME between to_date(#para(begin),'yyyy-mm-dd hh24:mi:ss') and to_date(#para(end),'yyyy-mm-dd hh24:mi:ss')
 group by  FLOWNAME
#end

###试验类型统计
#sql("type")
 select nvl(item_experiment_type,'其他')as item_experiment_type,count(*) as item_invalue from tlk_examine
 where DOMAINID=#para(DOMAINID) and author_dept_index like #para(ID,"like")
 and created between to_date(#para(begin),'yyyy-mm-dd hh24:mi:ss') and to_date(#para(end),'yyyy-mm-dd hh24:mi:ss')
#if(ITEM_TEST_DEPT)
 and ITEM_TEST_DEPT like #para(ITEM_TEST_DEPT,"like")
#end

#if(ITEM_PRODUCT)
 and ITEM_PRODUCT=#para(ITEM_PRODUCT)
#end

 group by item_experiment_type
#end

###试验类型统计列表
#sql("type_list")
 select  t.ID,t.ITEM_WT_NUM,t.ITEM_TITLE,nvl(t.item_experiment_type,'其他')as item_experiment_type from tlk_examine t
 where DOMAINID=#para(DOMAINID) and author_dept_index like #para(ID,"like")
 and created between to_date(#para(begin),'yyyy-mm-dd hh24:mi:ss') and to_date(#para(end),'yyyy-mm-dd hh24:mi:ss')
 #if(name&&name!='其他')
 and item_experiment_type=#para(name)
 #else
 and item_experiment_type IS NULL
 #end

#end

###样机试验统计
#sql("model")
select  #para(DOMAINID) AS DOMAINID,item_name,count(*) as item_invalue,y.item_pro from tlk_wyjcs y
 left join tlk_dic_prou p on y.item_pro=p.item_dic_prouname where  p.item_dept like #para(ID, "like") and
 y.created between to_date(#para(begin),'yyyy-mm-dd hh24:mi:ss') and to_date(#para(end),'yyyy-mm-dd hh24:mi:ss')  and  y.item_upbh is null
 group by y.item_name,y.item_pro
#end

###样机试验统计列表
#sql("model_list")
SELECT y.item_name,y.item_pro,y.item_xl_num,w.id,w.created,w.ITEM_TITLE,w.lastmodified
FROM tlk_wyjcs y
	LEFT JOIN tlk_dic_prou p ON y.item_pro = p.item_dic_prouname
	LEFT JOIN TLK_EXAMINE w ON w.item_wt_num = y.item_value
WHERE
	p.item_dept LIKE #para(ID, "like")
	AND y.created BETWEEN TO_DATE(#para(begin), 'yyyy-mm-dd hh24:mi:ss' )
	AND TO_DATE(#para(end), 'yyyy-mm-dd hh24:mi:ss' )
	AND y.item_upbh IS NULL
	AND y.item_name =#para(name)
#end

###产品线统计
#sql("product")
SELECT
	nvl(y.item_product,'其他')as item_product,
	count( * ) AS item_invalue
FROM
	TLK_EXAMINE y
	LEFT JOIN tlk_dic_prou p ON y.item_product = p.item_dic_prouname
WHERE
	1 = 1
    AND y.DOMAINID=#para(DOMAINID) and y.author_dept_index like #para(ID,"like")
	AND y.created BETWEEN to_date(#para(begin), 'yyyy-mm-dd hh24:mi:ss' )AND to_date(#para(end), 'yyyy-mm-dd hh24:mi:ss' )
    #if(ITEM_TEST_DEPT)
    and y.ITEM_TEST_DEPT like #para(ITEM_TEST_DEPT,"like")
    #end

GROUP BY
	item_product
#end

###产品线统计列表
#sql("product_list")
SELECT
	y.id,y.item_wt_num,y.item_title,y.item_experiment_type,y.item_product,y.created
FROM
	TLK_EXAMINE y
	LEFT JOIN tlk_dic_prou p ON y.item_product = p.item_dic_prouname
WHERE
	1 = 1
     AND y.DOMAINID=#para(DOMAINID) and y.author_dept_index like #para(ID,"like")
	 AND y.created BETWEEN to_date(#para(begin), 'yyyy-mm-dd hh24:mi:ss' )AND to_date(#para(end), 'yyyy-mm-dd hh24:mi:ss' )

    #if(ITEM_TEST_DEPT)
    and y.ITEM_TEST_DEPT like #para(ITEM_TEST_DEPT,"like")
    #end

	 #if(name&&name!='其他')
      AND y.item_product =#para(name)
     #else
      AND y.item_product IS NULL OR y.item_product =#para(name)
     #end

#end

###数据查询与统计
#sql("data")
SELECT
	D.*,
	to_char( D.lastmodified, 'yyyy-mm-dd hh24:mi:ss' ) AS item_lastmodified
FROM
	TLK_STRUCTURED_DATA D
	LEFT JOIN tlk_dic_prou p ON D.item_pro_type = p.item_dic_prouname LEFT outer
	JOIN t_user ON t_user.ID = D.author LEFT outer
	JOIN t_department ON t_user.defaultdepartment = t_department.ID
WHERE
	1 = 1
	AND (
	t_department.ID IN ( SELECT ID FROM t_department START WITH ID =#para(ID) CONNECT BY PRIOR ID = superior )
	OR p.item_dept LIKE #para(ID, "like"))

	#if(item_pro_type)
	and D.item_pro_type=#para(item_pro_type)
	#end

	#if(item_test_name)
	and D.item_test_name LIKE #para(item_test_name, "like")
	#end

	#if(item_pro_num)
	and D.item_pro_num LIKE #para(item_pro_num, "like")
	#end

#end

###试验时间线
#sql("time")
SELECT
	y.ITEM_VERSION,
	y.item_key,
	'变更' AS TYPE
FROM
	tlk_myjcs y
WHERE
	y.item_upbh IS NULL
	AND y.item_name =#para(time_name)
    #if(item_pky)
	AND y.item_pky LIKE #para(item_pky, "like")
	#end
#end

#sql("time_list")
SELECT
	y.item_pky,
	y.item_tlog
FROM
	tlk_myjcs y
WHERE
	y.item_key =#para(item_key)
	AND y.ITEM_TLOG IS NOT NULL
	#if(item_pky)
	AND y.item_pky LIKE #para(item_pky, "like")
	#end
#end

###试验时间线列表
#sql("times")
SELECT
	y.item_name,
	y.item_pro,
	y.item_version,
	y.item_xl_num,
	w.id,
	w.ITEM_WT_NUM,
	w.ITEM_TITLE,
	y.created,
	'委托单试验' AS TYPE
FROM
	tlk_wyjcs y
	LEFT JOIN TLK_EXAMINE w ON w.item_wt_num = y.item_value
WHERE
	y.item_upbh IS NULL
	AND y.item_name =#para(time_name)
#end

###试验时间线列表
#sql("change")
SELECT
	m.item_name AS name,
	y.*
FROM
	( SELECT item_name, item_key FROM tlk_myjcs WHERE item_upbh IS NULL
	 #if(name)
	 AND item_name =#para(name)
	 #end
	) m
	LEFT JOIN tlk_myjcs y ON y.item_key = m.item_key
WHERE
	y.ITEM_TLOG IS NOT NULL
	#if(item_pro)
	AND item_pro =#para(item_pro)
	#end
#end

###样机选择
#sql("prou")
SELECT
 D.item_name,
 D.item_xl_num,
 D.item_version
FROM
 TLK_MYJCS D
 LEFT JOIN tlk_dic_prou p ON D.item_pro = p.item_dic_prouname LEFT outer
 JOIN t_user ON t_user.ID = D.author LEFT outer
 JOIN t_department ON t_user.defaultdepartment = t_department.ID
WHERE
 D.item_upbh IS NULL

 #if(item_pro)
    AND D.item_pro=#para(item_pro)
 #end

 #if(item_name)
    AND D.item_name=#para(item_name)
 #end

 #if(item_xl_num)
    AND D.item_xl_num=#para(item_xl_num)
 #end

  #if(item_version)
    AND D.item_version=#para(item_version)
 #end
ORDER BY D.CREATED DESC
#end

###样机最新状态 获取key值通过key值去查询样机数据
#sql("getYj")
SELECT * FROM
             (SELECT
                   y.item_version,
                   y.item_key
               FROM
                   tlk_myjcs y
               WHERE
                   y.item_upbh IS NULL
                    AND y.item_name =#para(name)
                  #if(ITEM_XL_NUM)
                     AND y.ITEM_XL_NUM=#para(ITEM_XL_NUM)
                  #end
                ORDER BY y.item_version desc) WHERE ROWNUM<2
#end

###委托单
#sql("getWtt")
SELECT
    y.ITEM_NAME,
    y.ITEM_PRO,
    y.ITEM_VERSION,
    w.ID,
    w.ITEM_WT_NUM,
    w.ITEM_TITLE,
    y.created
FROM
    tlk_wyjcs y
        LEFT JOIN TLK_EXAMINE w ON w.item_wt_num = y.item_value
WHERE
    y.item_upbh IS NULL
  AND y.item_name =#para(name)
 #end

###委托单联动
#sql("getWttld")
SELECT
    D.*,
    to_char( D.lastmodified, 'yyyy-mm-dd hh24:mi:ss' ) AS item_lastmodified
FROM
    TLK_STRUCTURED_DATA D
WHERE
        D.item_wt_num=#para(ITEM_WT_NUM) and D.item_pro_num=#para(ITEM_PRO_NUM)
#end


