<?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.WorkInvoiceOfficeDao">
	<select id="findOfficeList" resultType="WorkInvoiceOffice" >
		select
		  a.id AS "id",
		  a.top_company AS "officeName",
		ifnull(count(DISTINCT t2.id),0) AS "invoiceCount",
		ifnull(sum( t2.tax_money),0) AS "invoiceMoneys",
		ifnull(sum( t2.income_money),0) AS "incomeMoneys"
		from sys_office a
		left join (select i.* from work_invoice_detail i where i.del_flag = '0' and i.state in ('正常','作废')
		<if test="date != '' and date != null">
			AND YEAR(i.date) = #{date}
		</if>
		)  t2 on t2.office_id = a.id
		where a.del_flag = '0'
		and a.type ='2'
		and a.parent_id=#{companyId}
		<if test="officeId != null and officeId != ''">
			AND a.id = #{officeId}
		</if>
		group by a.id
		order by a.create_date ASC
	</select>

	<select id="findOfficeListSum" resultType="WorkInvoiceOffice" >
		select
		a.id AS "id",
		ifnull(count(DISTINCT t2.id),0) AS "invoiceCount",
		ifnull(sum( t2.tax_money),0) AS "invoiceMoneys",
		ifnull(sum( t2.income_money),0) AS "incomeMoneys"
		from sys_office a
		left join (select i.* from work_invoice_detail i where i.del_flag = '0' and i.state in ('正常','作废')
		<if test="date != '' and date != null">
			AND YEAR(i.date) = #{date}
		</if>
		)  t2 on t2.office_id = a.id
		where a.del_flag = '0' and a.type ='2'
		and a.parent_ids like concat('%', #{companyId},'%')
	</select>


</mapper>