<?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.receiveTicket.mapper.BizFinancialReceiveTicketMapper">

    <resultMap type="cn.ezhizao.project.business.receiveTicket.domain.BizFinancialReceiveTicket" id="BizFinancialReceiveTicketResult">
        <id column="id" property="id"/>
    </resultMap>


    <select id="getList" parameterType="BizFinancialReceiveTicket" resultMap="BizFinancialReceiveTicketResult">
        SELECT
        t1.*, t2.name as company_name,
        (select concat(frtd.year, '-', frtd.month) from biz_financial_receive_ticket_detail frtd where frtd.financial_receive_ticket_id = t1.id order by year desc, month desc limit 1) currentDate,
        t3.account_name as from_company_name, t2.tax_type
        from biz_financial_receive_ticket t1
        left join biz_company t2 on t2.id = t1.company_id
        left join biz_tenant t3 on t3.id = t1.tenant_id
        WHERE
        t1.deleted = 0
        <if test="id != null">
            AND t1.id = #{id}
        </if>
        <if test="workOrderId != null">
            AND t1.work_order_id = #{workOrderId}
        </if>
        <if test="keyword != null and keyword != ''">
            AND (t2.name like concat('%', #{keyword}, '%') or t2.code like concat('%', #{keyword}, '%'))
        </if>
        <if test="taxType != null and taxType != ''">
            AND t2.tax_type = #{taxType}
        </if>
        <if test="fromCompanyName != null and fromCompanyName != ''">
            AND (t3.account_name like concat('%', #{fromCompanyName}, '%'))
        </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_receive_ticket_detail fsd where fsd.financial_receive_ticket_id = t1.id and
                fsd.month = #{month} and fsd.year = #{year} and fsd.status = #{status} <include refid="detailConditionSql" />)
            </if>
            <if test="status != null and status == 0">
                AND not exists (select 1 from biz_financial_receive_ticket_detail fsd where fsd.financial_receive_ticket_id = t1.id and
                fsd.month = #{month} and fsd.year = #{year})
            </if>
        </if>
    </select>

    <delete id="physicalDelete">
        DELETE FROM biz_financial_receive_ticket
        <trim prefix=" WHERE" suffix="" suffixOverrides="AND">
            <if test="id != null">
                id = #{id} AND
            </if>
       <!-- 删除条件为其他外键可以在这里加 -->
        </trim>
    </delete>

    <sql id="detailConditionSql">
        <if test="hasIncomeTicket != null and hasIncomeTicket == 1">
            AND fsd.income_ticket > 0
        </if>
        <if test="hasIncomeTicket != null and hasIncomeTicket == 0">
            AND fsd.income_ticket = 0
        </if>
        <if test="hasCostTicket != null and hasIncomeTicket == 1">
            AND fsd.cost_ticket > 0
        </if>
        <if test="hasCostTicket != null and hasIncomeTicket == 0">
            AND fsd.cost_ticket = 0
        </if>
        <if test="feeTicket != null and feeTicket == 1">
            AND fsd.fee_ticket_number > 0
        </if>
        <if test="feeTicket != null and feeTicket == 0">
            AND fsd.fee_ticket_number = 0
        </if>
        <if test="bankCopy != null and bankCopy == 1">
            AND fsd.bank_copy_number > 0
        </if>
        <if test="bankCopy != null and bankCopy == 0">
            AND fsd.bank_copy_number = 0
        </if>
        <if test="bankSlips != null and bankSlips == 1">
            AND fsd.bank_slips_number > 0
        </if>
        <if test="bankSlips != null and bankSlips == 0">
            AND fsd.bank_slips_number = 0
        </if>
        <if test="payroll != null and payroll == 1">
            AND fsd.salary_ticket > 0
        </if>
        <if test="payroll != null and payroll == 0">
            AND fsd.salary_ticket = 0
        </if>
        <if test="hasSsafTicket != null and hasSsafTicket == 1">
            AND fsd.ssaf_ticket > 0
        </if>
        <if test="hasSsafTicket != null and hasSsafTicket == 0">
            AND fsd.ssaf_ticket = 0
        </if>
        <if test="others != null and others == 1">
            AND fsd.other_ticket > 0
        </if>
        <if test="others != null and others == 0">
            AND fsd.other_ticket = 0
        </if>
        <if test="sumNumber != null and sumNumber == 1">
            AND fsd.sum_number > 0
        </if>
        <if test="sumNumber != null and sumNumber == 0">
            AND fsd.sum_number = 0
        </if>
        <if test="saveStatus != null and saveStatus > 0">
            AND fsd.save_status = #{saveStatus}
        </if>
    </sql>
</mapper>
