<?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.BizFinancialReceiveTicketFeedbackMapper">

    <resultMap type="cn.ezhizao.project.business.receiveTicket.domain.BizFinancialReceiveTicketFeedback"
               id="BizFinancialReceiveTicketFeedbackResult">
        <id column="id" property="id"/>
        <result column="company_id" jdbcType="BIGINT" property="companyId"/>
        <association property="serviceName" select="getServiceName" column="company_id"></association>
    </resultMap>


    <select id="getList" parameterType="BizFinancialReceiveTicketFeedback"
            resultMap="BizFinancialReceiveTicketFeedbackResult">
        SELECT t1.*, t2.name as company_name, t2.tax_type, t3.year, t3.month,t8.account_name as toTenantName,
        t4.account_name as from_company_name FROM biz_financial_receive_ticket_feedback t1 left join
        biz_company t2 on t2.id = t1.company_id
        left join biz_financial_receive_ticket_detail t3 on t3.id = t1.financial_receive_ticket_detail_id
        left join biz_tenant t4 on t4.id = t2.tenant_id
        left join biz_entrust t7 on
         t7.company_id = t3.company_id and t7.task_type_id = 1
        and t7.deleted = 0 and date_format(t7.current_month, '%Y%m') = concat(t3.year, t3.month)
        left join biz_tenant t8 on t8.id = t7.to_tenant_id
        <trim prefix=" WHERE" suffix="" suffixOverrides="AND">
            t1.deleted = 0 AND
            <if test="id != null  ">
                t1.id = #{id} AND
            </if>
            <if test="principal != null and principal != ''">
                (t2.leader_id=#{principal} OR t2.adviser_id=#{principal}) AND
            </if>
            <if test="year != null  ">
                t3.year = #{year} AND
            </if>
            <if test="month != null  ">
                t3.month = #{month} AND
            </if>
            <if test="financialReceiveTicketId != null  ">
                t1.financial_receive_ticket_id = #{financialReceiveTicketId} AND
            </if>
            <if test="currentMonth != null">
                concat(t3.year, '-', t3.month, '-01') = #{currentMonth} AND
            </if>
            <if test="taxType != null and taxType != ''">
                t2.tax_type = #{taxType} AND
            </if>
            <if test="fromCompanyName != null and fromCompanyName != ''">
                (t4.account_name like concat('%', #{fromCompanyName}, '%')) AND
            </if>
            <if test="companyId != null  ">
                t1.company_id = #{companyId} AND
            </if>
            <if test="tenantId != null">
                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(t3.year, t3.month)
                and eb.company_id = t3.company_id and eb.task_type_id = 1
                ) AND
            </if>
            <!--            <if test="tenantId != null  ">-->
            <!--                (t1.tenant_id = #{tenantId}-->
            <!--                or exists (select 1 from entrust where entrust.deleted = 0 and entrust.is_stop = 0 and entrust.company_id = t1.company_id and entrust.task_type_id = 1 and entrust.from_tenant_id = #{tenantId})-->
            <!--                ) AND-->
            <!--            </if>-->
            <!--            exists (select 1 from entrust where entrust.deleted = 0 and entrust.is_stop = 0 and entrust.company_id = t1.company_id and entrust.task_type_id = 1) AND-->

            <if test="companyName != null and companyName != ''">
                t2.name like concat('%', #{companyName}, '%') AND
            </if>
            <if test="financialReceiveTicketDetailId != null  ">
                t1.financial_receive_ticket_detail_id = #{financialReceiveTicketDetailId} AND
            </if>
            <if test="feedbackTime != null  ">
                t1.feedback_time = #{feedbackTime} AND
            </if>
            <if test="needSync != null  ">
                t1.need_sync = #{needSync} AND
            </if>
            <if test="status != null  ">
                t1.status = #{status} AND
            </if>
            <if test="remark != null  and remark != '' ">
                t1.remark = #{remark} AND
            </if>
            <if test="createTime != null  ">
                t1.create_time = #{createTime} AND
            </if>
            <if test="creatorId != null  ">
                t1.creator_id = #{creatorId} AND
            </if>
            <if test="updateTime != null  ">
                t1.update_time = #{updateTime} AND
            </if>
            <if test="updaterId != null  ">
                t1.updater_id = #{updaterId} AND
            </if>
            <if test="deleted != null  ">
                t1.deleted = #{deleted} AND
            </if>
            <if test="version != null  ">
                t1.version = #{version} AND
            </if>
        </trim>
    </select>

    <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>

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