<?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.BizFinancialReportTaxDetailMapper">
    <!-- 基础的返回map 多表复杂的 自行组合 -->
    <resultMap id="BaseResultMap" type="cn.ezhizao.project.business.reportTaxIsZero.domain.BizFinancialReportTaxDetail" >
        <id column="id" property="id"/>
        <collection property="workOrderId" ofType="java.lang.Long" select="getWorkOrderId" column="financialReportTaxId" />
        <collection property="reportFiles" ofType="cn.ezhizao.project.business.reportTaxIsZero.domain.FinancialReportTaxDetailReport" select="getFiles" column="id" />
    </resultMap>

    <resultMap type="cn.ezhizao.project.business.reportTaxIsZero.domain.BizFinancialReportTaxDetailReport" id="FileStorageResult">
        <id column="id" property="id"/>
    </resultMap>


    <select id="getFiles" resultMap="FileStorageResult">
        select * from biz_financial_report_tax_detail_report
        where deleted = 0
        and financial_report_tax_detail_id = #{id}
    </select>

    <select id="getWorkOrderId" resultType="java.lang.Long">
        select work_order_id from biz_financial_report_tax
        where deleted = 0
          and id = #{financialReportTaxId}
    </select>

    <!--带条件的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.*, t2.name as company_name
        from
        biz_financial_report_tax_detail t1
        left join biz_company t2 on t2.id = t1.company_id
        WHERE
        t1.deleted = 0
        <if test="financialReportTaxId != null">
            AND t1.financial_report_tax_id = #{financialReportTaxId}
        </if>
        <if test="id != null">
            AND t1.id = #{id}
        </if>
        <if test="year != null and year != ''">
            AND t1.year = #{year}
        </if>
        <if test="month != null and month != ''">
            AND t1.month = #{month}
        </if>
        <if test="mainIds != null and mainIds.size > 0">
            AND t1.financial_report_tax_id in
            <foreach collection="mainIds" index="index" item="item" separator=","  open="(" close=")">
                #{item,jdbcType=BIGINT}
            </foreach>
        </if>
    </sql>
</mapper>
