<?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.recycleBin.mapper.BizRecycleBinMapper">

    <resultMap type="cn.ezhizao.project.business.recycleBin.domain.BizRecycleBin" id="BizRecycleBinResult">
        <id column="id" property="id"/>
    </resultMap>


    <select id="getList" parameterType="BizRecycleBin" resultMap="BizRecycleBinResult">
        SELECT input.id as id,input.id as sourceId,input.contract_no as archiveCode,com.name as sourceValue FROM biz_archive_input input
        left join biz_company com on com.id =input.company_id
        <trim prefix=" WHERE" suffix="" suffixOverrides="AND">
            input.deleted = 1
        </trim>
        order by input.create_time desc
    </select>

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

    <select id="getById" parameterType="BizRecycleBin" resultMap="BizRecycleBinResult">
        SELECT *
        FROM biz_recycle_bin
        WHERE id = #{id}
    </select>
    <!--    &#45;&#45;         UPDATE ${table} t-->
    <!--    &#45;&#45;         SET t.deleted=1-->
    <!--    &#45;&#45;         WHERE t.company_id = ${id}-->
    <!--  软删除客户关联子表数据  -->
    <update id="deleteData">
        <foreach collection="tabs" item="item" index="index" separator=";">
            UPDATE ${item}
            SET deleted=1
            WHERE company_id=#{id}
        </foreach>
    </update>

    <!--  软删除客户关联子表数据  -->
    <update id="deleteArchiveData">
        <foreach collection="tabs" item="item" index="index" separator=";">
            UPDATE ${item}
            SET deleted=1
            WHERE contract_id=#{id}
        </foreach>
    </update>

    <!--  还原客户关联子表数据  -->
    <update id="restoreData">
        <foreach collection="tabs" item="item" index="index" separator=";">
            UPDATE ${item}
            SET deleted=0
            WHERE company_id=#{id}
        </foreach>
    </update>

    <!--  还原订单关联子表数据  -->
    <update id="restoreArchiveData">
        <foreach collection="tabs" item="item" index="index" separator=";">
            UPDATE ${item}
            SET deleted=0
            WHERE contract_id=#{id}
        </foreach>
    </update>

    <!--  回收站数据还原  -->
    <update id="restore">
        UPDATE biz_recycle_bin
        SET is_return=1
        WHERE id = #{id}
           OR from_id = #{id}
    </update>

    <!--  回收站数据删除  -->
    <update id="remove">
        UPDATE biz_recycle_bin
        SET is_clean=1,
            deleted=1
        WHERE id = #{id}
           OR from_id = #{id}
    </update>

    <!--  还原用户表数据  -->
    <update id="restoreCompany">
        UPDATE biz_company
        SET deleted=0
        WHERE id = #{id}
    </update>

    <!--  还原订单表数据  -->
    <update id="restoreArchive">
        UPDATE biz_archive_input
        SET deleted=0
        WHERE id = #{id}
    </update>

    <select id="getChildById" resultType="Long">
        SELECT t.id
        FROM ${table} t
        WHERE t.company_id = ${id}
    </select>

    <select id="getArchiveById" resultType="Long">
        SELECT t.id
        FROM ${tab} t
        WHERE t.contract_id = ${id}
    </select>

    <select id="getWorkOrderDitailById" resultType="Long">
        SELECT t.id
        FROM ${tab} t
        WHERE t.work_order_id = ${id}
    </select>

    <select id="getChildIdsByParentId" resultMap="BizRecycleBinResult">
        <foreach collection="tabs" item="item" index="index" separator="union">
            SELECT t.id as source_id, #{item.tab} as source_table_name
            FROM ${item.tab} t
            WHERE t.company_id = #{item.id}
        </foreach>
    </select>
</mapper>
