<?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.projectcontentinfo.dao.ProjectReportDataMonthDao">


	<select id="findList" resultType="ProjectReportDataMonth" >
		  select
		     0 "id",
         	count(i.report_date) as "reportCount",
		    date_format(i.report_date, '%Y-%m') AS "name"
		 from project_report_data i
        	WHERE i.report_date
        	BETWEEN concat(#{date},'-01-01') and concat(#{date},'-12-31') AND i.status = '5'
		and i.office_id in (select o.id from sys_office o where o.parent_ids LIKE concat('%' #{companyId}, '%'))
		group by date_format(i.report_date, '%Y-%m')
	</select>
	<select id="findByCompanyList" resultType="ProjectReportDataMonth" >
		SELECT
		a.id,
		count(t3.report_date) AS "reportCount",
		t3.datestr AS "name"
		FROM
		sys_office a
		LEFT JOIN sys_office t2 ON (
		t2.parent_ids LIKE concat('%', a.id, '%')
		AND t2.type = '2'
		)
		LEFT JOIN (
		SELECT
		id,
		date_format(report_date, '%Y-%m') AS "datestr",
		report_date,
		office_id
		FROM
		project_report_data
		WHERE
		report_date BETWEEN concat(#{date}, '-01-01')
		AND concat(#{date}, '-12-31')
		AND STATUS = '5'
		) t3 ON t3.office_id = t2.id
		WHERE
		a.type IN ('1', '3')
		and (a.id != #{companyId} AND a.parent_ids LIKE concat('%' ,#{companyId}, '%'))
		or (t2.branch_office IS NULL AND (a.parent_ids like concat('%',#{companyId},'%') or a.id = #{companyId}))
		GROUP BY
		datestr;
	</select>
	<select id="findByOfficeList" resultType="ProjectReportDataMonth" >
		  select
		     0 "id",
         	count(i.report_date) as "reportCount",
		    date_format(i.report_date, '%Y-%m') AS "name"
		 from project_report_data i
        	WHERE i.report_date
        	BETWEEN concat(#{date},'-01-01') and concat(#{date},'-12-31') AND i.status = '5'
		and i.office_id in (select o.id from sys_office o where o.parent_ids LIKE concat('%' #{companyId}, '%'))
		<if test="officeId != null and officeId != ''">
			AND i.office_id = #{officeId}
		</if>
		group by date_format(i.report_date, '%Y-%m')
	</select>

</mapper>