<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.jeeplus.modules.sg.audit.report.mapper.ReportMapper">
	<sql id="newInformationColumns">
		a.id,
		a.project_id,
		a.project_name,
		a.project_type,
		a.project_grade,
		a.project_year,
		a.project_total,
		a.project_start,
		a.project_stop,
		a.department,
		a.project_firm,
		a.subDate,
		a.releaseDate,
		a.endDate,
		a.resultDate,
		a.approvalDate,
		a.second_units,
		a.first_units,
		a.firstFinish,
		a.firstFinishDate,
		a.secondFinish,
		a.secondFinishDate,
		a.finishDate,
		a.overdue,
		a.underCentralized
	</sql>
	<select id="findData" resultType="SgReport" parameterType="SgReport">
		SELECT a.department as name ,
		sum(a.st1) as finishCount,
		SUM(a.st2) as unfinishCount
		 from
	    (SELECT department,
	            if(firstFinish='否' or secondFinish = '否',0,1) as st1,
	            if(firstFinish='否' or secondFinish = '否',1,0) as st2
	            from sj_project_information t1
		        <where>
			        1=1
			         <if test="dataScope != null and dataScope != ''">
				      ${dataScope}
			         </if>
		        </where>
	            ) a
	    GROUP BY department
	</select>

	<select id="findList" parameterType="SgReport" resultType="SgReport">
		SELECT b.department as name,
		sum(b.level1) as level1,
		sum(b.level2) as level2,
		sum(b.level3) as level3,
		sum(b.level4) as level4 from
		(SELECT a.id, a.department,
		if(DATEDIFF(a.da, a.approvalDate)&lt;=7,1,0) as level1,
		if(DATEDIFF(a.da, a.approvalDate)&lt;= 14 and DATEDIFF(a.da, a.approvalDate)&gt;7,1,0) as level2,
		if(DATEDIFF(a.da, a.approvalDate)&lt;= 21 and DATEDIFF(a.da, a.approvalDate)&gt;14,1,0) as level3,
		if(DATEDIFF(a.da, a.approvalDate)&gt;21 or a.approvalDate is null,1,0) as level4,
		a.status as status
		from
		(SELECT id,
		department,
		if(firstFinish='否' or secondFinish = '否',NOW(),if(secondFinish = '是',secondFinishDate,firstFinishDate) )as da ,
		approvalDate ,if(firstFinish='否' or secondFinish = '否','0','1') as status
		from sj_project_information t1
		<where>
			1=1
			<if test="dataScope != null and dataScope != ''">
				${dataScope}
			</if>
		</where>
		) a
		<where>
			<if test="status != null and status != ''">
				a.status = #{status} and
			</if>
			1=1
		</where>
		) b   GROUP BY b.department

	</select>


	<select id="findUnitData" resultType="SgReport" parameterType="SgReport">
		SELECT a.underCentralized as name ,
		sum(a.st1) as finishCount,
		SUM(a.st2) as unfinishCount
		 from
	    (SELECT if(t1.underCentralized is null,'未知部门',t1.underCentralized) as underCentralized,
	            if(t1.firstFinish='否' or t1.secondFinish = '否',0,1) as st1,
	            if(t1.firstFinish='否' or t1.secondFinish = '否',1,0) as st2
	            from sj_project_information t1
		<where>
			1=1
			<if test="dataScope != null and dataScope != ''">
				${dataScope}
			</if>
		</where>
	            ) a
	    GROUP BY a.underCentralized
	</select>

	<select id="findUnitList" parameterType="SgReport" resultType="SgReport">
		SELECT b.underCentralized as name,
		sum(b.level1) as level1,
		sum(b.level2) as level2,
		sum(b.level3) as level3,
		sum(b.level4) as level4 from
		(SELECT a.id, a.underCentralized,
		if(DATEDIFF(a.da, a.approvalDate)&lt;=7,1,0) as level1,
		if(DATEDIFF(a.da, a.approvalDate)&lt;= 14 and DATEDIFF(a.da, a.approvalDate)&gt;7,1,0) as level2,
		if(DATEDIFF(a.da, a.approvalDate)&lt;= 21 and DATEDIFF(a.da, a.approvalDate)&gt;14,1,0) as level3,
		if(DATEDIFF(a.da, a.approvalDate)&gt;21 or a.approvalDate is null,1,0) as level4,
		a.status as status
		from
		(SELECT id,
		if(t1.underCentralized is null,'未知部门',t1.underCentralized) as underCentralized,
		if(t1.firstFinish='否' or t1.secondFinish = '否',NOW(),if(t1.secondFinish = '是',t1.secondFinishDate,t1.firstFinishDate) )as da ,
		t1.approvalDate ,if(t1.firstFinish='否' or t1.secondFinish = '否','0','1') as status
		from sj_project_information t1
		<where>
			1=1
			<if test="dataScope != null and dataScope != ''">
				${dataScope}
			</if>
		</where>
		) a
		<where>
			<if test="status != null and status != ''">
				a.status = #{status} and
			</if>
			1=1
		</where>
		) b   GROUP BY b.underCentralized

	</select>

	<select id="findSecondData" resultType="SgReport" parameterType="SgReport">
		SELECT a.second_units as name,
		sum(a.st1) as finishCount,
		SUM(a.st2) as unfinishCount
		from
		(SELECT if(t1.second_units is null,'未知部门',t1.second_units) as second_units,
		if(t1.firstFinish='否' or t1.secondFinish = '否',0,1) as st1,
		if(t1.firstFinish='否' or t1.secondFinish = '否',1,0) as st2
		from sj_project_information t1
		<where>
			1=1
			<if test="dataScope != null and dataScope != ''">
				${dataScope}
			</if>
		</where>
		) a
		GROUP BY a.second_units
	</select>

	<select id="findSecondList" parameterType="SgReport" resultType="SgReport">
		SELECT b.second_units as name,
		sum(b.level1) as level1,
		sum(b.level2) as level2,
		sum(b.level3) as level3,
		sum(b.level4) as level4 from
		(SELECT a.id, a.second_units,
		if(DATEDIFF(a.da, a.approvalDate)&lt;=7,1,0) as level1,
		if(DATEDIFF(a.da, a.approvalDate)&lt;= 14 and DATEDIFF(a.da, a.approvalDate)&gt;7,1,0) as level2,
		if(DATEDIFF(a.da, a.approvalDate)&lt;= 21 and DATEDIFF(a.da, a.approvalDate)&gt;14,1,0) as level3,
		if(DATEDIFF(a.da, a.approvalDate)&gt;21 or a.approvalDate is null,1,0) as level4,
		a.status as status
		from
		(SELECT id,
		if(t1.second_units is null,'未知部门',t1.second_units) as second_units,
		if(t1.firstFinish='否' or t1.secondFinish = '否',NOW(),if(t1.secondFinish = '是',t1.secondFinishDate,t1.firstFinishDate) )as da ,
		t1.approvalDate ,if(t1.firstFinish='否' or t1.secondFinish = '否','0','1') as status
		from sj_project_information t1
		<where>
			1=1
			<if test="dataScope != null and dataScope != ''">
				${dataScope}
			</if>
		</where>
		) a
		<where>
			<if test="status != null and status != ''">
				a.status = #{status} and
			</if>
			1=1
		</where>
		) b   GROUP BY b.second_units

	</select>


	<select id="findInformationList" resultType="Information">
		SELECT
		<include refid="newInformationColumns"/>,b.trial,b.appendStatus,
		datediff(if(firstFinish='否' or secondFinish = '否',NOW(),if(secondFinish = '是',secondFinishDate,firstFinishDate)),approvalDate) as newdate
		FROM sj_project_information as a
		left JOIN sj_project_append as b on a.project_id = b.project_id
		<where>
			1=1
			<if test="type!=null and type!=''">
				<if test="type==0">
					and a.department like concat('%',#{condition},'%')
				</if>
				<if test="type==1">
					and a.underCentralized like concat('%',#{condition},'%')
				</if>
				<if test="type==2">
					and a.second_units like concat('%',#{condition},'%')
				</if>
			</if>
			<if test="status!=null and status !=''">
				<if test="status==0">
					and a.firstFinish='是' and (a.secondFinish != '否' or a.secondFinish is null)
				</if>
				<if test="status==1">
					and (a.firstFinish='否' or a.secondFinish = '否')
				</if>
			</if>
		</where>
	</select>
</mapper>