<?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.deduction.mapper.BizDeductionMapper">
    <!-- 基础的返回map 多表复杂的 自行组合 -->
    <resultMap id="BaseResultMap" type="cn.ezhizao.project.business.deduction.domain.BizDeduction">
        <id column="id" jdbcType="BIGINT" property="id"/>
        <result column="id" jdbcType="BIGINT" property="id"/>
        <result column="confirm_id" jdbcType="BIGINT" property="confirmId"/>
        <result column="company_id" jdbcType="BIGINT" property="companyId"/>
        <result column="operation_id" jdbcType="BIGINT" property="operationId"/>
        <association property="companyName" javaType="java.lang.String" select="getCompanyName" column="company_id"/>
        <association property="confirmName" javaType="java.lang.String" select="getConfirmName" column="confirm_id"/>
        <association property="operationName" javaType="java.lang.String" select="getOperationName"
                     column="operation_id"/>
        <collection property="files" ofType="cn.ezhizao.project.system.domain.SysFileStorage" select="getFiles"
                    column="id"/>
    </resultMap>

    <resultMap type="cn.ezhizao.project.system.domain.SysFileStorage" id="FileStorageResult">
        <id column="id" property="id"/>
    </resultMap>

    <select id="getFiles" resultMap="FileStorageResult">
        select * from sys_file_storage
        where deleted = 0
        and master_table_name = 'biz_deduction'
        and master_id = #{id}
    </select>

    <select id="getCompanyName" resultType="java.lang.String">
        SELECT name FROM biz_company
        WHERE deleted = 0 AND id = #{company_id};
    </select>

    <select id="getConfirmName" resultType="java.lang.String">
        SELECT nick_name FROM sys_user
        WHERE del_flag = 0 AND user_id = #{confirm_id};
    </select>

    <select id="getOperationName" resultType="java.lang.String">
        SELECT nick_name FROM sys_user
        WHERE del_flag = 0 AND user_id = #{operation_id};
    </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 C
    </select>

    <sql id="getListSql">
        SELECT t1.*, t2.tax_type,
        t3.account_name as from_company_name, t3.id from_tenant_id FROM biz_deduction t1 left join biz_company t2 on t2.id = t1.company_id
        left join biz_tenant t3 on t3.id = t1.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="companyName != null  ">
                t2.name like concat('%', #{companyName}, '%') AND
            </if>
            <if test="currentMonth != null  ">
                concat(t1.year, '-', t1.month) = #{currentMonth} AND
            </if>
            <if test="taxType != null and taxType != ''">
                t2.tax_type = #{taxType} AND
            </if>
            <if test="fromCompanyName != null and fromCompanyName != ''">
                (t3.account_name like concat('%', #{fromCompanyName}, '%')) AND
            </if>
            <if test="amount != null  ">
                t1.amount = #{amount} AND
            </if>
            <if test="status != null  ">
                t1.status = #{status} AND
            </if>
            <if test="sourceId != null  ">
                t1.source_id = #{sourceId} AND
            </if>
            <if test="sourceTableName != null  and sourceTableName != '' ">
                t1.source_table_name = #{sourceTableName} AND
            </if>
            <if test="sourceName != null  and sourceName != '' ">
                t1.source_name = #{sourceName} AND
            </if>
            <if test="sourceNames != null  and sourceNames.size > 0">
                t1.source_name in
                <foreach collection="sourceNames" index="index" item="item" separator="," open="(" close=")">
                    #{item,jdbcType=BIGINT}
                </foreach>
                AND
            </if>
            <if test="confirmId != null  ">
                t1.confirm_id = #{confirmId} AND
            </if>
            <if test="confirmDate != null  ">
                t1.confirm_date = #{confirmDate} AND
            </if>
            <if test="operationId != null  ">
                t1.operation_id = #{operationId} AND
            </if>
            <if test="operationDate != null  ">
                t1.operation_date = #{operationDate} 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>
            <if test="tenantId != null  ">
                (t1.tenant_id = #{tenantId}
                or (case when t1.source_name = '税款' then exists (select 1 from biz_entrust entrust where entrust.deleted = 0 and date_format(entrust.current_month, '%Y%m') = concat(t1.year, t1.month) and entrust.company_id =
                t1.company_id and entrust.task_type_id = 1 and entrust.to_tenant_id = #{tenantId})
                when t1.source_name = '社保' then exists (select 1 from biz_entrust entrust where entrust.deleted = 0 and date_format(entrust.current_month, '%Y%m') = concat(t1.year, t1.month) and entrust.company_id = t1.company_id
                and entrust.task_type_id = 2 and entrust.to_tenant_id = #{tenantId})
                when t1.source_name = '公积金' then exists (select 1 from biz_entrust entrust where entrust.deleted = 0 and date_format(entrust.current_month, '%Y%m') = concat(t1.year, t1.month) and entrust.company_id =
                t1.company_id and entrust.task_type_id = 3 and entrust.to_tenant_id = #{tenantId})
                else false end
                )
                ) AND
            </if>
        </trim>
    </sql>

</mapper>
