<?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.ruoyi.info.collection.mapper.CcdiPurchaseTransactionMapper">

    <!-- 采购交易信息ResultMap -->
    <resultMap type="com.ruoyi.info.collection.domain.vo.CcdiPurchaseTransactionVO" id="CcdiPurchaseTransactionVOResult">
        <id property="purchaseId" column="purchase_id"/>
        <result property="purchaseCategory" column="purchase_category"/>
        <result property="projectName" column="project_name"/>
        <result property="subjectName" column="subject_name"/>
        <result property="subjectDesc" column="subject_desc"/>
        <result property="purchaseQty" column="purchase_qty"/>
        <result property="budgetAmount" column="budget_amount"/>
        <result property="bidAmount" column="bid_amount"/>
        <result property="actualAmount" column="actual_amount"/>
        <result property="contractAmount" column="contract_amount"/>
        <result property="settlementAmount" column="settlement_amount"/>
        <result property="purchaseMethod" column="purchase_method"/>
        <result property="supplierName" column="supplier_name"/>
        <result property="contactPerson" column="contact_person"/>
        <result property="contactPhone" column="contact_phone"/>
        <result property="supplierUscc" column="supplier_uscc"/>
        <result property="supplierBankAccount" column="supplier_bank_account"/>
        <result property="supplierCount" column="supplier_count"/>
        <result property="applyDate" column="apply_date"/>
        <result property="planApproveDate" column="plan_approve_date"/>
        <result property="announceDate" column="announce_date"/>
        <result property="bidOpenDate" column="bid_open_date"/>
        <result property="contractSignDate" column="contract_sign_date"/>
        <result property="expectedDeliveryDate" column="expected_delivery_date"/>
        <result property="actualDeliveryDate" column="actual_delivery_date"/>
        <result property="acceptanceDate" column="acceptance_date"/>
        <result property="settlementDate" column="settlement_date"/>
        <result property="applicantId" column="applicant_id"/>
        <result property="applicantName" column="applicant_name"/>
        <result property="applyDepartment" column="apply_department"/>
        <result property="purchaseLeaderId" column="purchase_leader_id"/>
        <result property="purchaseLeaderName" column="purchase_leader_name"/>
        <result property="purchaseDepartment" column="purchase_department"/>
        <result property="createTime" column="create_time"/>
        <result property="updateTime" column="update_time"/>
        <result property="createdBy" column="created_by"/>
        <result property="updatedBy" column="updated_by"/>
    </resultMap>

    <!-- 分页查询采购交易列表 -->
    <select id="selectTransactionPage" resultMap="CcdiPurchaseTransactionVOResult">
        SELECT
            t.purchase_id, t.purchase_category, t.project_name, t.subject_name, t.subject_desc,
            t.purchase_qty, t.budget_amount, t.bid_amount, t.actual_amount, t.contract_amount, t.settlement_amount,
            t.purchase_method, t.supplier_name, t.contact_person, t.contact_phone, t.supplier_uscc, t.supplier_bank_account,
            IFNULL(supplier_stats.supplier_count, 0) AS supplier_count,
            t.apply_date, t.plan_approve_date, t.announce_date, t.bid_open_date, t.contract_sign_date,
            t.expected_delivery_date, t.actual_delivery_date, t.acceptance_date, t.settlement_date,
            t.applicant_id, t.applicant_name, t.apply_department, t.purchase_leader_id, t.purchase_leader_name, t.purchase_department,
            t.created_by, t.create_time, t.updated_by, t.update_time
        FROM ccdi_purchase_transaction t
        LEFT JOIN (
            SELECT purchase_id, COUNT(*) AS supplier_count
            FROM ccdi_purchase_transaction_supplier
            GROUP BY purchase_id
        ) supplier_stats ON supplier_stats.purchase_id = t.purchase_id
        <where>
            <if test="query.projectName != null and query.projectName != ''">
                AND t.project_name LIKE CONCAT('%', #{query.projectName}, '%')
            </if>
            <if test="query.subjectName != null and query.subjectName != ''">
                AND t.subject_name LIKE CONCAT('%', #{query.subjectName}, '%')
            </if>
            <if test="query.applicantName != null and query.applicantName != ''">
                AND t.applicant_name LIKE CONCAT('%', #{query.applicantName}, '%')
            </if>
            <if test="query.applicantId != null and query.applicantId != ''">
                AND t.applicant_id = #{query.applicantId}
            </if>
            <if test="query.applyDateStart != null">
                AND t.apply_date &gt;= #{query.applyDateStart}
            </if>
            <if test="query.applyDateEnd != null">
                AND t.apply_date &lt;= #{query.applyDateEnd}
            </if>
        </where>
        ORDER BY t.create_time DESC
    </select>

    <!-- 查询采购交易详情 -->
    <select id="selectTransactionById" resultMap="CcdiPurchaseTransactionVOResult">
        SELECT
            t.purchase_id, t.purchase_category, t.project_name, t.subject_name, t.subject_desc,
            t.purchase_qty, t.budget_amount, t.bid_amount, t.actual_amount, t.contract_amount, t.settlement_amount,
            t.purchase_method, t.supplier_name, t.contact_person, t.contact_phone, t.supplier_uscc, t.supplier_bank_account,
            IFNULL(supplier_stats.supplier_count, 0) AS supplier_count,
            t.apply_date, t.plan_approve_date, t.announce_date, t.bid_open_date, t.contract_sign_date,
            t.expected_delivery_date, t.actual_delivery_date, t.acceptance_date, t.settlement_date,
            t.applicant_id, t.applicant_name, t.apply_department, t.purchase_leader_id, t.purchase_leader_name, t.purchase_department,
            t.created_by, t.create_time, t.updated_by, t.update_time
        FROM ccdi_purchase_transaction t
        LEFT JOIN (
            SELECT purchase_id, COUNT(*) AS supplier_count
            FROM ccdi_purchase_transaction_supplier
            GROUP BY purchase_id
        ) supplier_stats ON supplier_stats.purchase_id = t.purchase_id
        WHERE t.purchase_id = #{purchaseId}
    </select>

    <!-- 批量插入采购交易数据 -->
    <insert id="insertBatch">
        INSERT INTO ccdi_purchase_transaction
        (purchase_id, purchase_category, project_name, subject_name, subject_desc,
         purchase_qty, budget_amount, bid_amount, actual_amount, contract_amount, settlement_amount,
         purchase_method, supplier_name, contact_person, contact_phone, supplier_uscc, supplier_bank_account,
         apply_date, plan_approve_date, announce_date, bid_open_date, contract_sign_date,
         expected_delivery_date, actual_delivery_date, acceptance_date, settlement_date,
         applicant_id, applicant_name, apply_department, purchase_leader_id, purchase_leader_name, purchase_department,
         created_by, create_time, updated_by, update_time)
        VALUES
        <foreach collection="list" item="item" separator=",">
            (#{item.purchaseId}, #{item.purchaseCategory}, #{item.projectName}, #{item.subjectName}, #{item.subjectDesc},
            #{item.purchaseQty}, #{item.budgetAmount}, #{item.bidAmount}, #{item.actualAmount}, #{item.contractAmount}, #{item.settlementAmount},
            #{item.purchaseMethod}, #{item.supplierName}, #{item.contactPerson}, #{item.contactPhone}, #{item.supplierUscc}, #{item.supplierBankAccount},
            #{item.applyDate}, #{item.planApproveDate}, #{item.announceDate}, #{item.bidOpenDate}, #{item.contractSignDate},
            #{item.expectedDeliveryDate}, #{item.actualDeliveryDate}, #{item.acceptanceDate}, #{item.settlementDate},
            #{item.applicantId}, #{item.applicantName}, #{item.applyDepartment}, #{item.purchaseLeaderId}, #{item.purchaseLeaderName}, #{item.purchaseDepartment},
            #{item.createdBy}, NOW(), #{item.updatedBy}, NOW())
        </foreach>
    </insert>

    <!-- 批量更新采购交易数据（先删除再插入） -->
    <update id="insertOrUpdateBatch">
        <foreach collection="list" item="item" separator=";">
            DELETE FROM ccdi_purchase_transaction WHERE purchase_id = #{item.purchaseId};
            INSERT INTO ccdi_purchase_transaction
            (purchase_id, purchase_category, project_name, subject_name, subject_desc,
             purchase_qty, budget_amount, bid_amount, actual_amount, contract_amount, settlement_amount,
             purchase_method, supplier_name, contact_person, contact_phone, supplier_uscc, supplier_bank_account,
             apply_date, plan_approve_date, announce_date, bid_open_date, contract_sign_date,
             expected_delivery_date, actual_delivery_date, acceptance_date, settlement_date,
             applicant_id, applicant_name, apply_department, purchase_leader_id, purchase_leader_name, purchase_department,
             created_by, create_time, updated_by, update_time)
            VALUES
            (#{item.purchaseId}, #{item.purchaseCategory}, #{item.projectName}, #{item.subjectName}, #{item.subjectDesc},
            #{item.purchaseQty}, #{item.budgetAmount}, #{item.bidAmount}, #{item.actualAmount}, #{item.contractAmount}, #{item.settlementAmount},
            #{item.purchaseMethod}, #{item.supplierName}, #{item.contactPerson}, #{item.contactPhone}, #{item.supplierUscc}, #{item.supplierBankAccount},
            #{item.applyDate}, #{item.planApproveDate}, #{item.announceDate}, #{item.bidOpenDate}, #{item.contractSignDate},
            #{item.expectedDeliveryDate}, #{item.actualDeliveryDate}, #{item.acceptanceDate}, #{item.settlementDate},
            #{item.applicantId}, #{item.applicantName}, #{item.applyDepartment}, #{item.purchaseLeaderId}, #{item.purchaseLeaderName}, #{item.purchaseDepartment},
            #{item.createdBy}, NOW(), #{item.updatedBy}, NOW())
        </foreach>
    </update>

    <delete id="deleteSuppliersByPurchaseIds">
        DELETE FROM ccdi_purchase_transaction_supplier
        WHERE purchase_id IN
        <foreach collection="purchaseIds" item="purchaseId" open="(" separator="," close=")">
            #{purchaseId}
        </foreach>
    </delete>

</mapper>
