<?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.reportTaxIsZero.mapper.BizFinancialReportTaxMapper">
    <!-- 基础的返回map 多表复杂的 自行组合 -->
    <resultMap id="BaseResultMap" type="cn.ezhizao.project.business.reportTaxIsZero.domain.BizFinancialReportTax">
        <result column="company_id" jdbcType="BIGINT" property="companyId"/>
        <association property="currentEmployeeName" javaType="java.lang.String" select="getCurrentEmployeeName"
                     column="current_employee_id"/>
        <association property="serviceName" select="getServiceName" column="company_id"></association>
    </resultMap>

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


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

    <sql id="getListSql">
        SELECT
        t1.*, t2.name as company_name, t2.social_credit_code, t2.tax_type, t2.is_zero, t2.customer_label_id, t4.lable as customerLabelName,
        (select concat(frtd.year, '-', frtd.month) from biz_financial_report_tax_detail frtd where
        frtd.financial_report_tax_id = t1.id order by year desc, month desc limit 1) currentDate,
        t3.account_name as from_company_name,t6.nick_name as verifierName,t8.account_name as toTenantName,
        t5.verifier_time
        from biz_financial_report_tax 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 biz_lable t4 on t4.id = t2.customer_label_id
        left join biz_financial_report_tax_detail t5 on t5.financial_report_tax_id = t1.id
        and t5.deleted = 0 and t5.month = #{month} and t5.year = #{year} and t5.financial_report_tax_id = t1.id
        left join sys_user t6 on t6.user_id = t5.verifier_id
        left join biz_entrust t9 on t9.work_order_id = t1.work_order_id and t9.deleted = 0 and  date_format(t9.current_month, '%Y%m') = concat(#{year}, #{month})
        left join biz_tenant t8 on t8.id = t9.to_tenant_id
        <!--        left join fxy_financial_checkout t3 on t3.account_sets_id = t1.account_sets_id-->
        WHERE
        t1.deleted = 0
        AND t2.is_zero = 1
        <if test="id != null">
            AND t1.id = #{id}
        </if>
        <if test="taxType != null and taxType != ''">
            AND t2.tax_type = #{taxType}
        </if>
        <if test="customerLabelId != null">
            AND t2.customer_label_id = #{customerLabelId}
        </if>
        <if test="fromCompanyName != null and fromCompanyName != ''">
            AND (t3.account_name like concat('%', #{fromCompanyName}, '%'))
        </if>
        <if test="companyName != null and companyName != ''">
            AND (t2.name like concat('%', #{companyName}, '%'))
        </if>
        <if test="socialCreditCode != null and socialCreditCode!=''">
            AND t2.social_credit_code = #{socialCreditCode}
        </if>
        <if test="verifierName != null">
            AND  t6.nick_name like concat('%', #{verifierName}, '%')
        </if>
        <if test="dateFilter != null and dateFilter.size()>0 ">
            AND  (
            DATE_FORMAT(t5.verifier_time,'%Y-%m-%d') &gt;= #{dateFilter[0]}
            AND
            DATE_FORMAT(t5.verifier_time,'%Y-%m-%d') &lt;= #{dateFilter[1]}
            )
        </if>
        <if test="fromCompanyName != null and fromCompanyName != ''">
            AND t3.account_name like concat('%', #{fromCompanyName}, '%')
        </if>
        <if test="principal != null and principal != ''">
            AND (t2.leader_id=#{principal} OR t2.adviser_id=#{principal})
        </if>
        <if test="month != null and year != null">
            <if test="tenantId != null">
                AND exists (select 1 from biz_entrust eb
                where eb.deleted = 0
                and (eb.to_tenant_id = #{tenantId} or eb.from_tenant_id = #{tenantId})
                and date_format(eb.current_month, '%Y%m') = concat(#{year}, #{month})
                and eb.work_order_id = t1.work_order_id
                )
            </if>
            <if test="status != null and status != 0">
                AND exists (select 1 from biz_financial_report_tax_detail fsd where fsd.financial_report_tax_id = t1.id
                and
                fsd.month = #{month} and fsd.year = #{year} and fsd.status = #{status}
                )
            </if>
            <!--   <include refid="detailConditionSql"/>     -->

            <if test="status != null and status == 0">
                AND not exists (select 1 from biz_financial_report_tax_detail fsd where fsd.financial_report_tax_id =
                t1.id and
                fsd.month = #{month} and fsd.year = #{year})
            </if>
        </if>
    </sql>

    <select id="getServiceName" resultType="String">
        SELECT s.nick_name FROM sys_user s
        WHERE s.user_id =(SELECT leader_id  FROM biz_company WHERE id=#{company_id})
    </select>

    <!-- 查询当前维护人员 -->
    <select id="getCurrentEmployeeName" resultType="java.lang.String">
        SELECT nick_name
        FROM sys_user
        WHERE user_id = #{current_employee_id};
    </select>


    <sql id="detailConditionSql">
        <if test="valueAddedTax != null and valueAddedTax == 1 ">
            AND t4.value_added_tax > 0
        </if>
        <if test="valueAddedTax != null and valueAddedTax == 0 ">
            AND t4.value_added_tax = 0
        </if>
        <if test="cityConstructTax != null and cityConstructTax == 1 ">
            AND t4.city_construct_tax > 0
        </if>
        <if test="cityConstructTax != null and cityConstructTax == 0 ">
            AND t4.city_construct_tax = 0
        </if>
        <if test="additionsTaxOfEducation != null and additionsTaxOfEducation == 1 ">
            AND t4.additions_tax_of_education > 0
        </if>
        <if test="additionsTaxOfEducation != null and additionsTaxOfEducation == 0 ">
            AND t4.additions_tax_of_education = 0
        </if>
        <if test="ladditionsTaxOfEducation != null and ladditionsTaxOfEducation == 1 ">
            AND t4.l_additions_tax_of_education > 0
        </if>
        <if test="ladditionsTaxOfEducation != null and ladditionsTaxOfEducation == 0 ">
            AND t4.l_additions_tax_of_education = 0
        </if>
        <if test="individualIncomeTax != null and individualIncomeTax == 1 ">
            AND t4.individual_income_tax > 0
        </if>
        <if test="individualIncomeTax != null and individualIncomeTax == 0 ">
            AND t4.individual_income_tax = 0
        </if>
        <if test="businessIncomeTax != null and businessIncomeTax == 1 ">
            AND t4.business_income_tax > 0
        </if>
        <if test="businessIncomeTax != null and businessIncomeTax == 0 ">
            AND t4.business_income_tax = 0
        </if>
        <if test="stampTax != null and stampTax == 1 ">
            AND t4.stamp_tax > 0
        </if>
        <if test="stampTax != null and stampTax == 0 ">
            AND t4.stamp_tax = 0
        </if>
        <if test="culturalConstructTax != null and culturalConstructTax == 1 ">
            AND t4.cultural_construct_tax > 0
        </if>
        <if test="culturalConstructTax != null and culturalConstructTax == 0 ">
            AND t4.cultural_construct_tax = 0
        </if>
        <if test="excise != null and excise == 1 ">
            AND t4.excise > 0
        </if>
        <if test="excise != null and excise == 0 ">
            AND t4.excise = 0
        </if>
        <if test="sumTax != null and sumTax == 1 ">
            AND t4.sum_tax > 0
        </if>
        <if test="sumTax != null and sumTax == 0 ">
            AND t4.sum_tax = 0
        </if>
    </sql>
</mapper>
