<?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.workreimbursementcount.dao.WorkInvoiceMonthDao">
	<select id="findList" resultType="WorkInvoiceMonth" >
		SELECT
		0 "id",
		date_format(i.date, '%Y-%m') AS "officeName",
		"0" As "type",
		count(*) AS "invoiceCount",
		sum(i.tax_money) as "invoiceMoneys",
		sum(i.income_money)  AS "incomeMoneys"
		FROM work_invoice_detail i
		WHERE
		i.date BETWEEN concat(#{date}, '-01-01')
		AND concat(#{date}, '-12-31')
		AND i.del_flag = '0'
		AND i.state IN ('正常', '作废')
		and i.office_id in (select o.id from sys_office o where o.parent_ids LIKE concat('%' #{companyId}, '%'))
		group by date_format(i.date, '%Y-%m')
		UNION ALL
		select 0 "id",
		"合计" as "officeName" ,
		"0" As "type",
		sum(su.invoiceCount)  AS "invoiceCount",
		sum(su.invoiceMoneys) AS "invoiceMoneys",
		sum(su.incomeMoneys)AS "incomeMoneys"
		from ( SELECT
		0 "id",
		date_format(i.date, '%Y-%m') AS "officeName",
		"0" As "type",
		count(*) AS "invoiceCount",
		sum(i.tax_money) as "invoiceMoneys",
		sum(i.income_money)  AS "incomeMoneys"
		FROM work_invoice_detail i
		WHERE
		i.date BETWEEN concat(#{date}, '-01-01')
		AND concat(#{date}, '-12-31')
		AND i.del_flag = '0'
		AND i.state IN ('正常', '作废')
		and i.office_id in (select o.id from sys_office o where o.parent_ids LIKE concat('%' #{companyId}, '%'))
		group by date_format(i.date, '%Y-%m')
		) su
	</select>

	<select id="findOfficeList" resultType="WorkInvoiceMonth" >
		SELECT
		0 "id",
		date_format(i.date, '%Y-%m') AS "officeName",
		count(*) AS "invoiceCount",
		sum(i.tax_money) as "invoiceMoneys",
		sum(i.income_money)  AS "incomeMoneys"
		FROM work_invoice_detail i
		WHERE
		i.date BETWEEN concat(#{date}, '-01-01')
		AND concat(#{date}, '-12-31')
		AND i.del_flag = '0'
		AND i.state IN ('正常', '作废')
		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.date, '%Y-%m')
		UNION ALL
		select 0 "id",
		"合计" as "officeName" ,
		sum(su.invoiceCount)  AS "invoiceCount",
		sum(su.invoiceMoneys) AS "invoiceMoneys",
		sum(su.incomeMoneys)AS "incomeMoneys"
		from (
		SELECT
		0 "id",
		date_format(i.date, '%Y-%m') AS "officeName",
		count(*) AS "invoiceCount",
		sum(i.tax_money) as "invoiceMoneys",
		sum(i.income_money)  AS "incomeMoneys"
		FROM work_invoice_detail i
		WHERE
		i.date BETWEEN concat(#{date}, '-01-01')
		AND concat(#{date}, '-12-31')
		AND i.del_flag = '0'
		AND i.state IN ('正常', '作废')
		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.date, '%Y-%m')
		) su
	</select>




	<select id="findByCompanyList" resultType="WorkInvoiceMonth" >
		SELECT
		a.id AS "id",
		t3.datestr AS "officeName",
		a.type As "type",
		ifnull(count(DISTINCT t3.id),0) AS "invoiceCount",
		ifnull( sum(t3.tax_money),0) AS "invoiceMoneys",
		ifnull( sum(t3.income_money),0) AS "incomeMoneys"
		FROM sys_office a
		LEFT JOIN sys_office t2 ON (t2.parent_ids LIKE concat('%', a.id, '%') AND t2.type = '2')
		LEFT JOIN (select i.* ,date_format(i.date, '%Y-%m') datestr
		from work_invoice_detail i
		where i.date
		BETWEEN concat(#{date},'-01-01') and concat(#{date},'-12-31')
		and i.del_flag = '0' and i.state in ('正常','作废')) 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 t3.datestr
		union all
		select 0 "id",
		"合计" as "officeName" ,
		"0" As "type",
		sum(su.invoiceCount)  AS "invoiceCount",
		sum(su.invoiceMoneys) AS "invoiceMoneys",
		sum(su.incomeMoneys)AS "incomeMoneys"
		from (SELECT
		a.id AS "id",
		t3.datestr AS "officeName",
		a.type As "type",
		ifnull(count(DISTINCT t3.id),0) AS "invoiceCount",
		ifnull( sum(t3.tax_money),0) AS "invoiceMoneys",
		ifnull( sum(t3.income_money),0) AS "incomeMoneys"
		FROM sys_office a
		LEFT JOIN sys_office t2 ON (t2.parent_ids LIKE concat('%', a.id, '%') AND t2.type = '2')
		LEFT JOIN (select i.* ,date_format(i.date, '%Y-%m') datestr
		from work_invoice_detail i
		where i.date
		BETWEEN concat(#{date},'-01-01') and concat(#{date},'-12-31')
		and i.del_flag = '0' and i.state in ('正常','作废')) 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 t3.datestr) su
	</select>


</mapper>