<?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.workcontractinfo.dao.WorkContractDataOfficeDao">
	<select id="findList" resultType="WorkContractDataOffice" >
		select
		  a.id AS "id",
		  a.top_company AS "name",
		   count(DISTINCT t3.id) AS "contractCount",
		  count(DISTINCT t4.id) AS "projectCount",
		  count(DISTINCT t5.id) AS "reportCount",
		ifnull(sum(t7.tax_money),0) AS "invoiceMoney",
		ifnull(sum(t7.income_money),0) AS "incomeMoney",
		ifnull(sum(t8.money),0) AS "reimbursementMoney"
		from sys_office a
		left join work_contract_info t3 on t3.office_id = a.id
		<if test="beginDate!=null and beginDate !=''">
			AND t3.contract_date &gt;= #{beginDate}
		</if>
		<if test="endDate != null and endDate != ''">
			AND t3.contract_date &lt;= #{endDate}
		</if>
		left join (select r.* from project_records r where r.del_flag = '0' and r.status  in ('5','8') ) t4 on t4.contract_id = t3.id
		left join (select d.* from project_report_data d where d.del_flag = '0' and d.status  = '5' ) t5 on t5.project_id = t4.id
		left join (select e.* from work_invoice e where e.del_flag = '0' ) t6 on t6.project_id = t4.id
		left join (select i.* from work_invoice_detail i where i.del_flag = '0' and i.state in ('正常','作废')) t7 on t7.invoice_id = t6.id
		left join (select m.* from work_reimbursement m where m.del_flag = '0' and m.status  = '5') t8 on t8.project_id = t4.id
		where a.type = '2'
		and a.parent_id=#{companyId}
		group by a.id
		order by a.create_date ASC;
	</select>

	<select id="findOfficeListSum" resultType="WorkContractDataOffice" >
		select
		a.id AS "id",
		a.name AS "name",
		count(DISTINCT t3.id) AS "contractCount",
		count(DISTINCT t4.id) AS "projectCount",
		count(DISTINCT t5.id) AS "reportCount",
		ifnull(sum(t7.tax_money),0) AS "invoiceMoney",
		ifnull(sum(t7.income_money),0) AS "incomeMoney",
		ifnull(sum(t8.money),0) AS "reimbursementMoney"
		from sys_office a
		left join work_contract_info t3 on t3.office_id = a.id
		<if test="beginDate!=null and beginDate !=''">
			AND t3.contract_date &gt;= #{beginDate}
		</if>
		<if test="endDate != null and endDate != ''">
			AND t3.contract_date &lt;= #{endDate}
		</if>
		left join (select r.* from project_records r where r.del_flag = '0' and r.status  = '5' ) t4 on t4.contract_id = t3.id
		left join (select d.* from project_report_data d where d.del_flag = '0' and d.status  = '5' ) t5 on t5.project_id = t4.id
		left join (select e.* from work_invoice e where e.del_flag = '0' ) t6 on t6.project_id = t4.id
		left join (select i.* from work_invoice_detail i where i.del_flag = '0' and i.state in ('正常','作废')) t7 on t7.invoice_id = t6.id
		left join (select m.* from work_reimbursement m where m.del_flag = '0' and m.status  = '5') t8 on t8.project_id = t4.id
		where a.type = '2'
		and a.parent_ids like concat('%', #{companyId},'%')
	</select>


</mapper>