<?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.salary.mapper.BizFinancialSalaryMapper">

    <resultMap type="cn.ezhizao.project.business.salary.domain.BizFinancialSalary" id="BizFinancialSalaryResult">
        <id column="id" property="id"/>
        <result column="company_id" jdbcType="BIGINT" property="companyId"/>
        <association property="serviceName" select="getServiceName" column="company_id"></association>
        <association property="socialCreditCode" select="getSocialCreditCode" column="company_id"></association>
    </resultMap>

    <resultMap type="cn.ezhizao.project.business.salary.domain.BizFinancialSalary" id="BizFinancialSalaryResult2">
        <id column="id" property="id"/>
        <result column="company_id" jdbcType="BIGINT" property="companyId"/>
<!--        <association property="socialCreditCode" select="getSocialCreditCode" column="company_id"></association>-->
    </resultMap>
    <select id="getList" parameterType="BizFinancialSalary" resultMap="BizFinancialSalaryResult">
        SELECT t1.*, t2.name as company_name,t2.tax_type, t3.account_name as from_company_name,
        t2.customer_label_id, t4.lable as customerLabelName,
        (select concat(bwor.year, '年', bwor.month, '月') from biz_work_order_record bwor where bwor.work_order_id =
        t1.work_order_id order by year desc, month desc limit 1) currentDate,
        t5.verifier_id,
        t6.nick_name as verifierName,
        t5.verifier_time,t8.account_name as toTenantName
        FROM biz_financial_salary 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_salary_detail t5 on t5.financial_salary_id = t1.id
                                 and t5.deleted = 0 and t5.month = #{month} and t5.year = #{year} and t5.financial_salary_id = t1.id
        left join sys_user t6 on t6.user_id = t5.verifier_id
        left join biz_entrust t7 on t7.work_order_id = t1.work_order_id and t7.deleted = 0 and  date_format(t7.current_month, '%Y%m') = concat(#{year}, #{month})
        left join biz_tenant t8 on t8.id = t7.to_tenant_id



        <trim prefix=" WHERE" suffix="" suffixOverrides="AND">
            t1.deleted = 0 and t1.is_stop = 0 and t2.deleted = 0

            <if test="isStop != null ">AND is_stop = #{isStop}</if>
            <if test="companyName != null ">AND t2.name like concat('%', #{companyName}, '%')</if>
            <if test="taxType != null and taxType != ''">
                AND t2.tax_type = #{taxType}
            </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.update_time,'%Y-%m-%d') &gt;= #{dateFilter[0]}
                AND
                DATE_FORMAT(t5.update_time,'%Y-%m-%d') &lt;= #{dateFilter[1]}
                )
            </if>
            <if test="customerLabelId != null">
                AND t2.customer_label_id = #{customerLabelId}
            </if>
            <if test="isZero != null">
                AND t2.is_zero = #{isZero}
            </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_salary_detail fsd where fsd.deleted = 0 and
                    fsd.financial_salary_id = t1.id and
                    fsd.month = #{month} and fsd.year = #{year} and fsd.status = #{status})
                </if>
                <if test="status != null and status == 0">
                    AND not exists (select 1 from biz_financial_salary_detail fsd where fsd.financial_salary_id = t1.id and
                    fsd.month = #{month} and fsd.year = #{year})
                </if>
                <if test="hasIndividualIncomeTax != null">
                    AND exists (select 1 from biz_financial_salary_detail fsd where fsd.deleted = 0 and
                    fsd.financial_salary_id = t1.id and
                    has_individual_income_tax=#{hasIndividualIncomeTax} and fsd.month = #{month} and fsd.year = #{year})
                </if>
            </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>

    <select id="getSocialCreditCode" resultType="String">
        SELECT c.social_credit_code
        FROM biz_company c
        WHERE c.id = #{company_id}
    </select>
    <select id="getList2" parameterType="BizFinancialSalary" resultMap="BizFinancialSalaryResult2">
        SELECT t1.*, t2.name as company_name,t2.tax_type, t3.account_name as from_company_name,
        t2.customer_label_id, t4.lable as customerLabelName,
        (select concat(bwor.year, '年', bwor.month, '月') from biz_work_order_record bwor where bwor.work_order_id =
        t1.work_order_id order by year desc, month desc limit 1) currentDate,
        t5.verifier_id,
        t6.nick_name as verifierName,
        t5.verifier_time,t8.account_name as toTenantName
        FROM biz_financial_salary 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_salary_detail t5 on t5.financial_salary_id = t1.id
        and t5.deleted = 0 and t5.month = #{month} and t5.year = #{year} and t5.financial_salary_id = t1.id
        left join sys_user t6 on t6.user_id = t5.verifier_id
        left join biz_entrust t7 on t7.work_order_id = t1.work_order_id and t7.deleted = 0 and  date_format(t7.current_month, '%Y%m') = concat(#{year}, #{month})
        left join biz_tenant t8 on t8.id = t7.to_tenant_id
        <trim prefix=" WHERE" suffix="" suffixOverrides="AND">
            t1.deleted = 0 and t1.is_stop = 0

            <if test="isStop != null ">AND is_stop = #{isStop}</if>
            <if test="companyName != null ">AND t2.name like concat('%', #{companyName}, '%')</if>
            <if test="taxType != null and taxType != ''">
                AND t2.tax_type = #{taxType}
            </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.update_time,'%Y-%m-%d') &gt;= #{dateFilter[0]}
                AND
                DATE_FORMAT(t5.update_time,'%Y-%m-%d') &lt;= #{dateFilter[1]}
                )
            </if>
            <if test="customerLabelId != null">
                AND t2.customer_label_id = #{customerLabelId}
            </if>
            <if test="isZero != null">
                AND t2.is_zero = #{isZero}
            </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_salary_detail fsd where fsd.deleted = 0 and
                    fsd.financial_salary_id = t1.id and
                    fsd.month = #{month} and fsd.year = #{year} and fsd.status = #{status})
                </if>
                <if test="status != null and status == 0">
                    AND not exists (select 1 from biz_financial_salary_detail fsd where fsd.financial_salary_id = t1.id and
                    fsd.month = #{month} and fsd.year = #{year})
                </if>
                <if test="hasIndividualIncomeTax != null">
                    AND exists (select 1 from biz_financial_salary_detail fsd where fsd.deleted = 0 and
                    fsd.financial_salary_id = t1.id and
                    has_individual_income_tax=#{hasIndividualIncomeTax} and fsd.month = #{month} and fsd.year = #{year})
                </if>
            </if>


        </trim>
    </select>

    <delete id="physicalDelete">
        DELETE FROM biz_financial_salary
        <trim prefix=" WHERE" suffix="" suffixOverrides="AND">
            <if test="id != null">
                id = #{id} AND
            </if>
            <!-- 删除条件为其他外键可以在这里加 -->
        </trim>
    </delete>
    <delete id="physicalDeleteIds">
        DELETE FROM biz_financial_salary_detail_employee
        <trim prefix=" WHERE" suffix="" suffixOverrides="AND">
            <if test="ids != null and ids.size() > 0">
                id in
                <foreach collection="ids" item="id" open="(" separator="," close=")">
                    #{id}
                </foreach>
            </if>
        </trim>
    </delete>
    <select id="getSalary" resultType="cn.ezhizao.project.business.salary.domain.BizFinancialSalary">
        SELECT *
        FROM biz_financial_salary salary
            left join biz_company company on company.id = salary.company_id
        <where>
            salary.deleted = 0
            <if test="companyName != null ">AND company.name like concat('%', #{companyName}, '%')</if>
            <if test="tenantId != null ">AND salary.tenant_id = #{tenantId}</if>
            <if test="isZero != null ">AND company.is_zero = #{isZero}</if>
        </where>
    </select>
</mapper>
