<?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.unis.financialSupervision.modular.priceApproval.mapper.PriceApprovalMapper">
    <select id="pageList" parameterType="com.baomidou.mybatisplus.core.conditions.Wrapper" resultType="com.unis.financialSupervision.modular.priceApproval.entity.PriceApproval">
        SELECT
            pa.*,
            pa.org_id orgId,
            ui.dwmc,
            oi.org_name orgName,
            bc.contract_number contractNumber,
            IFNULL(SUM(ci.settle_amount), 0) settleAmount,
            IFNULL(
                ci.updatedate,
                ci.createdate
            ) settleDate
        FROM
            depot_qh.business_fs_storage_price_approval pa
        LEFT JOIN org_info oi ON pa.org_id = oi.org_id
        LEFT JOIN province_all.biz_unit_info ui ON oi.unit_info_id = ui.id
        LEFT JOIN business_contract bc ON bc.id = pa.contract_id
        LEFT JOIN business_fs_in_confirm ic ON pa.org_id = ic.org_id
        AND pa.contract_id = ic.contract_id
        LEFT JOIN business_fs_in_confirm_contract_item ci ON ic.id = ci.fs_receipt_confirm_id
        ${ew.customSqlSegment}
    </select>
    <select id="getContractInfo" parameterType="integer" resultType="com.unis.financialSupervision.modular.priceApproval.param.BusinessContractParam">
        SELECT
            swd.id,
            swd.grain_kind grainKind,
            be.enumname grainKindName,
            swd.in_count inCount,
            swd.in_price inPrice,
            swd.in_detail_total_price inDetailTotalPrice
        FROM
            business_store_ware_detail swd
        LEFT JOIN basic_enum be ON be.enumId = swd.grain_kind
        WHERE
            swd.zid = #{id}
    </select>
    <select id="getQualityInfoBySaveJoin" parameterType="integer" resultType="com.unis.financialSupervision.modular.priceApprovalInspection.entity.PriceApprovalInspection">
        SELECT
            ai.id,
            ai.fs_storage_price_approval_id fsStoragePriceApprovalId,
            ai.qc_quality_inspection_id qcQualityInspectionId,
            qi.basic_storehouse_id basicStorehouseId,
            bs.storehouse_name storehouseName,
            qi.warehouse_id warehouseId,
            bw.warehouse_name warehouseName,
            SUM(CASE WHEN qii.inspection_item_id = 7619 THEN qii.inspection_value ELSE 0 END) zz,
            SUM(CASE WHEN qii.inspection_item_id = 9528239 THEN qii.inspection_value ELSE 0 END) sf,
            CASE WHEN qi.check_result = 0 THEN '不合格' WHEN qi.check_result = 1 THEN '合格' ELSE '' END jg,
            qi.produce_area produceArea,
            qi.bgcjsj
        FROM
            business_qc_quality_inspection qi
        LEFT JOIN depot_qh.business_fs_storage_price_approval_inspection ai ON qi.id = ai.qc_quality_inspection_id
        LEFT JOIN basic_storehouse bs ON qi.basic_storehouse_id = bs.storehouse_id
        LEFT JOIN basic_warehouse bw ON qi.warehouse_id = bw.warehouse_id
        LEFT JOIN business_qc_quality_inspection_item qii ON qi.id = qii.qc_quality_inspection_id
        WHERE
            qi.jylb = '01' AND ai.fs_storage_price_approval_id = #{id}
    </select>
    <select id="threeConsistences" parameterType="integer" resultType="com.unis.financialSupervision.modular.priceApprovalThreeConsistence.entity.PriceApprovalThreeConsistence">
        SELECT
            tc.id,
            tc.basic_storehouse_id,
            tc.`month`,
            bs.storehouse_name basicStorehouseName,
            tc.equal_quantity_supplement provinceEqualQuantitySupplement,
            tc.custody_account_quantity provinceCustodyAccountQuantity,
            tc.custody_account_quantity provinceCustodyAccountQuantity,
            tc.statistical_account_quantity provinceStatisticalAccountQuantity,
            tc.accounting_quantity provinceAccountingQuantity,
            tc.accounting_unit_price provinceAccountingUnitPrice,
            tc.accounting_amount provinceAccountingAmount
        FROM
            business_fs_storage_price_approval_three_consistence tc
        LEFT JOIN basic_storehouse bs ON tc.basic_storehouse_id = bs.storehouse_id
        WHERE
            tc.fs_storage_price_approval_id = #{id}
    </select>
    <select id="getApprovalSettleList" parameterType="integer" resultType="com.unis.financialSupervision.modular.priceApprovalSettle.entity.PriceApprovalSettle">
        SELECT
            pas.id,
            ic.receipt_confirmation_time receiptConfirmationTime,
            ic.total_settle_quantity provinceTotalSettleQuantity,
            ic.total_settle_amount provinceTotalSettleAmount,
            ic.payment_time provincePaymentTime,
            ic.voucher_number provinceVoucherNumber,
            ic.payer_unit_name provincePayerUnitName,
            ic.paid_grain_money provincePaidGrainMoney,
            ic.unpaid_grain_money provinceUnpaidGrainMoney,
            ic.invoice_unit provinceInvoiceUnit,
            ic.invoice_quantity provinceInvoiceQuantity,
            ic.invoice_amount provinceInvoiceAmount,
            ic.wcm_cost_main_unit provinceWcmCostMainUnit,
            ic.wcm_cost_name provinceWcmCostName,
            ic.wcm_accounting_voucher_number provinceWcmAccountingVoucherNumber,
            ic.wcm_invoice_amount provinceWcmInvoiceAmount,
            ic.wcm_paid_cost provinceWcmPaidCost,
            ic.wcm_non_payment_cost provinceWcmNonPaymentCost
        FROM
            business_fs_in_confirm ic
        LEFT JOIN business_fs_storage_price_approval_settle pas ON ic.id = pas.fs_receipt_confirm_id
        WHERE
            pas.fs_storage_price_approval_id = #{id}
    </select>
    <select id="getApprovalThreeRKMX" parameterType="integer" resultType="com.unis.financialSupervision.modular.priceApproval.entity.DataCPZ">
        SELECT
            rmdj.htbh,
            cpz.sjchID,
            bs.storehouse_name storehouseName,
            bw.warehouse_name warehouseName,
            cpz.qcph,
            cpz.mzzl,
            cpz.pz,
            cpz.bzwkl,
            cpz.cpzsj
        FROM
            data_crkyw_cpz_default cpz
        LEFT JOIN data_crkyw_rmdj_default rmdj ON cpz.BizNo = rmdj.BizNo
        LEFT JOIN basic_storehouse bs ON bs.storehouse_id = cpz.sjchID
        LEFT JOIN basic_warehouse bw ON bw.warehouse_id = cpz.sjhwID
        WHERE cpz.DeptID = #{orgId}
    </select>
    <select id="queryEntityById" parameterType="integer" resultType="com.unis.financialSupervision.modular.priceApproval.entity.PriceApproval">
        SELECT
            pa.*, oi.org_name orgName,
            bc.contract_number contractNumber,
            bc.attachment,
            bc.signing_time signingTime
        FROM
            business_fs_storage_price_approval pa
        LEFT JOIN org_info oi ON pa.org_id = oi.org_id
        LEFT JOIN business_contract bc ON pa.contract_id = bc.id
        WHERE
            pa.id = #{id}
    </select>
</mapper>