<?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="com.jeeplus.centrecareful.reportArchieve.mapper.ZsReportArchieveMapper">
    <sql id="base_column_list">
            a.id,
            a.create_by_id,
            a.create_time,
            a.update_by_id,
            a.update_time,
            a.archieve_no,
            a.office_name,
            a.business_name,
            a.business_type,
            a.project,
            a.report_no,
            a.report_no_type,
            a.reporting_period,
            a.filling_manager,
            a.project_leader,
            a.project_master,
            a.signature_cpa,
            a.signature_parter,
            a.project_level,
            a.report_date,
            a.stamp_date,
            a.archieve_date,
            a.file_box_number,
            a.box_number,
            a.book_numbers,
            a.remarks,
            a.original_contract,
            a.total_assets,
            a.archieve_resever,
            a.storage_place,
            a.status,
            a.special_place,
            a.transfer_location,
            date_format(a.transfer_time, '%Y-%m-%d %H:%i:%s') as transferTime
    </sql>
    <select id="findList" resultType="com.jeeplus.centrecareful.reportArchieve.domain.ZsReportArchieve">
        select
            <include refid="base_column_list"></include>,
            su.name as archieveReseverName
        from zs_report_archieve a
        left join sys_user su on a.archieve_resever = su.id and su.del_flag = '0'
        left join sys_office so on a.office_name = so.name and so.del_flag = '0'
        ${ew.customSqlSegment}
        ORDER BY a.report_no asc, a.update_time DESC
    </select>

    <select id="exportList" resultType="com.jeeplus.centrecareful.reportArchieve.service.dto.ZsReportArchieveExport">
        select
        <include refid="base_column_list"></include>,
        su.name as archieveReseverName
        from zs_report_archieve a
        left join sys_user su on a.archieve_resever = su.id and su.del_flag = '0'
        left join sys_office so on a.office_name = so.name and so.del_flag = '0'
        ${ew.customSqlSegment}
        ORDER BY a.report_no asc
    </select>

    <select id="getOfficeNameList" resultType="com.jeeplus.centrecareful.reportArchieve.domain.ZsReportArchieve">
        SELECT office_name from zs_report_archieve where del_flag = '0' GROUP BY office_name
    </select>

    <select id="getOfficeDTOByName" resultType="com.jeeplus.sys.service.dto.OfficeDTO">
        select * from sys_office where name = #{name} and del_flag = '0'
    </select>

    <select id="findById" resultType="com.jeeplus.centrecareful.reportArchieve.domain.ZsReportArchieve">
        select
        <include refid="base_column_list"></include>,
        su.name as archieveReseverName
        from zs_report_archieve a
        left join sys_user su on a.archieve_resever = su.id and su.del_flag = '0'
        left join sys_office so on a.office_name = so.name and so.del_flag = '0'
        where a.id = #{id} and a.del_flag = '0'
    </select>

    <select id="findByReportNoType" resultType="com.jeeplus.centrecareful.reportArchieve.domain.ZsReportArchieve">
        select
        <include refid="base_column_list"></include>,
        su.name as updateName
        from zs_report_archieve a
        left join sys_user su on a.update_by_id = su.id and su.del_flag = '0'
        where a.report_no_type = #{reportNoType} and a.del_flag = '0'
        order by
            CAST(SUBSTRING_INDEX(a.file_box_number, '-', 1) AS UNSIGNED) DESC,
            CAST(SUBSTRING_INDEX(a.file_box_number, '-', -1) AS UNSIGNED) DESC
        limit 1
    </select>

    <select id="findByReportNoTypeAndPrefixYear" resultType="com.jeeplus.centrecareful.reportArchieve.domain.ZsReportArchieve">
        select
        <include refid="base_column_list"></include>,
        su.name as updateName
        from zs_report_archieve a
        left join sys_user su on a.update_by_id = su.id and su.del_flag = '0'
        where a.report_no_type = #{reportNoType} and a.del_flag = '0'
        and a.report_no like concat('%', #{prefixReportNo}, '%')
        order by
            CAST(SUBSTRING_INDEX(a.file_box_number, '-', 1) AS UNSIGNED) DESC,
            CAST(SUBSTRING_INDEX(a.file_box_number, '-', -1) AS UNSIGNED) DESC
        limit 1
    </select>

    <select id="findByReportNo" resultType="com.jeeplus.centrecareful.reportArchieve.domain.ZsReportArchieve">
        select
        <include refid="base_column_list"></include>,
        su.name as updateName
        from zs_report_archieve a
        left join sys_user su on a.update_by_id = su.id and su.del_flag = '0'
        where a.report_no = #{reportNo} and a.del_flag = '0' order by a.file_box_number DESC limit 1
    </select>

    <select id="findByFileBoxNumber" resultType="com.jeeplus.centrecareful.reportArchieve.domain.ZsReportArchieve">
        select
        <include refid="base_column_list"></include>,
        su.name as updateName
        from zs_report_archieve a
        left join sys_user su on a.update_by_id = su.id and su.del_flag = '0'
        where a.report_no_type = #{reportNoType} and a.file_box_number = #{fileBoxNumber} and a.del_flag = '0'
    </select>

    <insert id="insertBatch">
        INSERT INTO zs_report_archieve (
        id,
        create_by_id,
        create_time,
        update_by_id,
        update_time,
        archieve_no,
        office_name,
        business_name,
        business_type,
        project,
        report_no,
        report_no_type,
        reporting_period,
        filling_manager,
        project_leader,
        project_master,
        signature_cpa,
        signature_parter,
        project_level,
        report_date,
        stamp_date,
        archieve_date,
        file_box_number,
        box_number,
        book_numbers,
        remarks,
        original_contract,
        total_assets,
        archieve_resever,
        storage_place,
        status,
        tenant_id,
        del_flag,
        special_place
        )
        VALUES
        <foreach collection="batchList" item="item" separator=",">
            (#{item.id},
            #{item.createById},
            #{item.createTime},
            #{item.updateById},
            #{item.updateTime},
            #{item.archieveNo},
            #{item.officeName},
            #{item.businessName},
            #{item.businessType},
            #{item.project},
            #{item.reportNo},
            #{item.reportNoType},
            #{item.reportingPeriod},
            #{item.fillingManager},
            #{item.projectLeader},
            #{item.projectMaster},
            #{item.signatureCpa},
            #{item.signatureParter},
            #{item.projectLevel},
            #{item.reportDate},
            #{item.stampDate},
            #{item.archieveDate},
            #{item.fileBoxNumber},
            #{item.boxNumber},
            #{item.bookNumbers},
            #{item.remarks},
            #{item.originalContract},
            #{item.totalAssets},
            #{item.archieveResever},
            #{item.storagePlace},
            #{item.status},
            #{item.tenantId},
            #{item.delFlag},
            #{item.specialPlace})
        </foreach>
    </insert>

    <update id="updatePlaceByYear">
        UPDATE zs_report_archieve a
        SET
            a.transfer_location = #{transferLocation},
            a.transfer_time = NOW()
        WHERE
            (a.report_no LIKE CONCAT('%(', #{reportNo}, ')%')
                OR a.report_no LIKE CONCAT('%[', #{reportNo}, ']%'))
          AND a.status = '1'
          AND a.del_flag = '0';
    </update>
</mapper>