<?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="cn.ezhizao.project.business.ticketReport.mapper.BizFinancialTicketReportDetailMapper">
    <!-- 基础的返回map 多表复杂的 自行组合 -->
    <resultMap id="BaseResultMap" type="cn.ezhizao.project.business.ticketReport.domain.BizFinancialTicketReportDetail" />

    <resultMap id="BaseMainResultMap" type="cn.ezhizao.project.business.keepAccounts.domain.BizFinancialKeepAccount" />

    <!--带条件的List查询-->
    <select id="getList" resultMap="BaseResultMap">
        <include refid="getListSql" />
    </select>


    <!--带条件的List查询 数据总条数-->
    <select id="getTotal" resultType="java.lang.Integer">
        SELECT COUNT(1) FROM (<include refid="getListSql" />) AS t
    </select>

    <sql id="getListSql">
        SELECT
        t1.id, t2.name as company_name, t3.*
        from
        biz_financial_keep_account t1
        left join biz_company t2 on t2.id = t1.company_id
        inner join (
        select 1 type, year, month, company_id, utf.is_finished, null sum_number, null status, null employee_count, null employee_salary from biz_archive_receive_ticket_finished utf left join biz_company fas on utf.company_id = fas.id where utf.deleted = 0
        union all
        select 2 type, year, month, company_id, null, null, fsd.status, (select count(1) from biz_financial_salary_detail_employee fsde where fsde.financial_salary_detail_id = fsd.id and fsde.deleted = 0) employee_count, (select sum(actually_salary) from biz_financial_salary_detail_employee fsde where fsde.financial_salary_detail_id = fsd.id and fsde.deleted = 0) as employee_salary  from financial_salary_detail fsd where fsd.deleted = 0
        union all
        select 0 type, year, month, company_id, null, frtd.sum_number amount, frtd.`status`, null employee_count, null employee_salary from biz_financial_receive_ticket_detail frtd where frtd.deleted = 0) t3 on t3.company_id = t2.id
        WHERE
        t1.deleted = 0
        <if test="companyName != null and companyName != ''">
            AND t2.name like concat('%', #{companyName}, '%')
        </if>
        <if test="year != null and year != ''">
            AND t3.year = #{year}
        </if>
        <if test="ids != null and ids.size > 0">
            AND t1.id in
            <foreach collection="ids" index="index" item="item" separator=","  open="(" close=")">
                #{item,jdbcType=BIGINT}
            </foreach>
        </if>
    </sql>

    <select id="getMainList" resultMap="BaseMainResultMap">
        <include refid="getMainListSql" />
        ORDER BY id desc
    </select>

    <select id="getMainTotal" resultType="java.lang.Integer">
        SELECT COUNT(1) FROM (<include refid="getMainListSql" />) AS t
    </select>

    <sql id="getMainListSql">
        SELECT
        t1.*, t2.name as company_name,
        t3.account_name as from_company_name
        from biz_financial_keep_account t1
        left join biz_company t2 on t2.id = t1.company_id
        left join biz_tenant t3 on t3.id = t1.tenant_id
        <!--        left join fxy_financial_checkout t3 on t3.account_sets_id = t1.account_sets_id-->
        WHERE
        t1.deleted = 0
        <if test="id != null">
            AND t1.id = #{id}
        </if>
        <if test="taxType != null and taxType != ''">
            AND t2.tax_type = #{taxType}
        </if>
    </sql>
</mapper>
