<?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.order.mapper.BizArchiveInputMapper">

    <resultMap type="cn.ezhizao.project.business.order.domain.BizArchiveInput" id="BizArchiveInputResult">
        <id column="id" property="id"/>
        <result column="source_id" property="sourceId"/>
        <result column="referrer_data_source" property="referrerDataSource"/>
        <association property="sourceName"
                     column="{ source_id = source_id, referrer_data_source = referrer_data_source }"
                     select="getSourceName"/>
        <association property="alterNumber" column="{ id = id, from_id = from_id }" select="getAlterNumber"/>
    </resultMap>
    <select id="getSourceName" resultType="java.lang.String">
        <if test="referrer_data_source == null">
            select '' as name
        </if>
        <if test="referrer_data_source == 'company'">
            select name from biz_company where biz_company.id = #{source_id}
        </if>
        <if test="referrer_data_source == 'employee'">
            select nick_name from sys_user where sys_user.user_id = #{source_id}
        </if>
        <if test="referrer_data_source == 'channel'">
            select name from biz_channel where biz_channel.id = #{source_id}
        </if>
        <if test="referrer_data_source == ''">
            select '' as name
        </if>
    </select>

    <select id="getAlterNumber" resultType="java.lang.Integer">
        select count(1) from biz_archive_input t where t.deleted = 0
        <if test="from_id == 0">
            and t.from_id = #{id}
        </if>
        <if test="from_id != 0">
            and t.from_id = #{from_id} or t.id = #{from_id}
        </if>
    </select>


    <select id="getList" parameterType="BizArchiveInput" resultMap="BizArchiveInputResult">
        SELECT
        t1.*,
        t2.name as company_name,
        t2.social_credit_code as social_credit_code,
        t3.nick_name as signer_name,
        t4.title as source_category_name,
        t4.referrer_data_source
        FROM biz_archive_input t1
        left join biz_company t2 on t2.id = t1.company_id
        left join sys_user t3 on t3.user_id = t1.signer_id
        left join biz_source t4 on t4.id = t1.source_category_id
        <trim prefix=" WHERE" suffix="" suffixOverrides="AND">
            t1.deleted = 0
            <if test="fromId != null">AND (t1.from_id = #{fromId} or t1.id = #{fromId})</if>
            <if test="id != null  and id != ''">AND t1.id = #{id}</if>
            <if test="companyName != null  and companyName != ''">AND t2.name like concat("%", #{companyName}, "%")</if>
            <if test="archiveTypeId != null">and archive_type_id = #{archiveTypeId}</if>
            <if test="companyId != null  and companyId != ''">AND t1.company_id = #{companyId}</if>
            <if test="archiveCode != null  and archiveCode != ''">AND t1.archive_code = #{archiveCode}</if>
            <if test="startMonth != null ">AND t1.start_month = #{startMonth}</if>
            <if test="endMonth != null ">AND t1.end_month = #{endMonth}</if>
            <if test="proofUrl != null  and proofUrl != ''">AND t1.proof_url = #{proofUrl}</if>
            <if test="contractNo != null  and contractNo != ''">AND t1.contract_no = #{contractNo}</if>
            <if test="formDate != null ">AND t1.form_date = #{formDate}</if>
            <if test="amount != null ">AND t1.amount = #{amount}</if>
            <if test="verifyStatus != null ">AND t1.verify_status = #{verifyStatus}</if>
            <if test="contractType != null ">AND t1.contract_type = #{contractType}</if>
            <if test="serviceType != null ">AND t1.service_type = #{serviceType}</if>
            <if test="outputDate != null ">AND t1.output_date = #{outputDate}</if>
            <if test="monthNum != null ">AND t1.month_num = #{monthNum}</if>
            <if test="companyName != null and companyName != '' ">AND t2.name like concat('%', #{companyName}, '%')</if>
            <if test="verifyRemark != null  and verifyRemark != ''">AND t1.verify_remark = #{verifyRemark}</if>
            <if test="tenantId != null">AND t1.tenant_id = #{tenantId}</if>
            <if test="isKeepAccount != null">AND <if test="isKeepAccount == 0">not</if> exists (select 1 from
                biz_archive_input_detail t5 where t5.deleted = 0 and t5.contract_id = t1.id and t5.task_type_id = 1)
            </if>
            <if test="isSocialSecurity != null">AND <if test="isSocialSecurity == 0">not</if> exists (select 1 from
                biz_archive_input_detail t5 where t5.deleted = 0 and t5.contract_id = t1.id and t5.task_type_id = 2)
            </if>
            <if test="isHousingFund != null">AND <if test="isHousingFund == 0">not</if> exists (select 1 from
                biz_archive_input_detail t5 where t5.deleted = 0 and t5.contract_id = t1.id and t5.task_type_id = 3)
            </if>
            <if test="isReturnTax != null">AND <if test="isReturnTax == 0">not</if> exists (select 1 from
                biz_archive_input_detail t5 where t5.deleted = 0 and t5.contract_id = t1.id and t5.task_type_id = 4)
            </if>
            <if test="isRegister != null">AND <if test="isRegister == 0">not</if> exists (select 1 from
                biz_archive_input_detail t5 where t5.deleted = 0 and t5.contract_id = t1.id and t5.task_type_id = 5)
            </if>
            <if test="isAlter != null">AND <if test="isAlter == 0">not</if> exists (select 1 from
                biz_archive_input_detail t5 where t5.deleted = 0 and t5.contract_id = t1.id and t5.task_type_id = 6)
            </if>
            <if test="isLogout != null">AND <if test="isLogout == 0">not</if> exists (select 1 from
                biz_archive_input_detail t5 where t5.deleted = 0 and t5.contract_id = t1.id and t5.task_type_id = 7)
            </if>
            <if test="isQualified != null">AND <if test="isQualified == 0">not</if> exists (select 1 from
                biz_archive_input_detail t5 where t5.deleted = 0 and t5.contract_id = t1.id and t5.task_type_id = 8)
            </if>
            <if test="isOther != null">AND <if test="isOther == 0">not</if> exists (select 1 from
                biz_archive_input_detail t5 where t5.deleted = 0 and t5.contract_id = t1.id and t5.task_type_id = 9)
            </if>
        </trim>
    </select>

    <update id="removeByIds">
        UPDATE biz_archive_input
        SET deleted=1
        WHERE id in
        <foreach collection="ids" item="id" index="index" open="(" separator="," close=")">
            #{id}
        </foreach>
    </update>

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