<?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.finance.mapper.TFeeMapper">

    <resultMap type="TFee" id="TFeeResult">
        <result property="fId"    column="f_id"    />
        <result property="fBillno"    column="f_billno"    />
        <result property="fCtrlcorpid"    column="f_ctrlcorpid"    />
        <result property="fActId"    column="f_act_id"    />
        <result property="fCorpid"    column="f_corpid"    />
        <result property="tMblno"    column="t_mblno"    />
        <result property="fTransActId"    column="f_trans_act_id"    />
        <result property="fAmtdr"    column="f_amtdr"    />
        <result property="fAmtcr"    column="f_amtcr"    />
        <result property="fBilltype"    column="f_billtype"    />
        <result property="fBillstatus"    column="f_billstatus"    />
        <result property="fRemarks"    column="f_remarks"    />
        <result property="fFromDate"    column="f_from_date"    />
        <result property="fToDate"    column="f_to_date"    />
        <result property="fAccbilldate"    column="f_accbilldate"    />
        <result property="fRefNo"    column="f_ref_no"    />
        <result property="delFlag"    column="del_flag"    />
        <result property="createBy"    column="create_by"    />
        <result property="fDeptid"    column="f_deptid"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateBy"    column="update_by"    />
        <result property="updateTime"    column="update_time"    />
        <result property="chargingMethod"    column="charging_method"    />
        <result property="invoiceNo"    column="invoice_no"    />
        <result property="bank"    column="bank"    />
        <result property="waterBillNo"    column="water_bill_no"    />
        <result property="fSystemType"    column="f_system_type"    />
    </resultMap>

    <sql id="selectTFeeVo">
        select f_id, f_billno, f_ctrlcorpid, f_act_id, f_corpid, t_mblno, f_trans_act_id, f_amtdr, f_amtcr, f_billtype, f_billstatus, f_remarks, f_from_date, f_to_date, f_accbilldate, f_ref_no, del_flag, create_by, f_deptid, create_time, update_by, update_time, charging_method, invoice_no, bank, water_bill_no, f_system_type from t_fee
     </sql>

    <select id="selectTFeeList" parameterType="TFee" resultMap="TFeeResult">
        select
            ware.*
        from
            t_fee ware
            left join sys_user u on ware.create_by = u.user_name
            left join sys_dept d on ware.f_deptid = d.dept_id
        <where>
            <if test="fBillno != null  and fBillno != ''">and ware.f_billno = #{fBillno}</if>
            <if test="fCtrlcorpid != null ">and ware.f_ctrlcorpid = #{fCtrlcorpid}</if>
            <if test="fActId != null ">and ware.f_act_id = #{fActId}</if>
            <if test="fCorpid != null ">and ware.f_corpid = #{fCorpid}</if>
            <if test="fTransActId != null ">and ware.f_trans_act_id = #{fTransActId}</if>
            <if test="tMblno != null  and tMblno != ''">and t_mblno = #{tMblno}</if>
            <if test="fAmtdr != null ">and ware.f_amtdr = #{fAmtdr}</if>
            <if test="fAmtcr != null ">and ware.f_amtcr = #{fAmtcr}</if>
            <if test="fBilltype != null  and fBilltype != ''">and ware.f_billtype = #{fBilltype}</if>
            <if test="fBillstatus != null  and fBillstatus != ''">and ware.f_billstatus = #{fBillstatus}</if>
            <if test="fRemarks != null  and fRemarks != ''">and ware.f_remarks = #{fRemarks}</if>
            <if test="fFromDate != null ">and ware.f_from_date = #{fFromDate}</if>
            <if test="fAccbilldate != null ">and ware.f_accbilldate = #{fAccbilldate}</if>
            <if test="fToDate != null ">and ware.f_to_date = #{fToDate}</if>
            <if test="fRefNo != null  and fRefNo != ''">and ware.f_ref_no = #{fRefNo}</if>
            <if test="fDeptid != null ">and ware.f_deptid = #{fDeptid}</if>
            <if test='timeInterval != null and timeInterval[0] != null and timeInterval[0]!= ""'>
                and ware.f_accbilldate &gt;= #{timeInterval[0]}
            </if>
            <if test='timeInterval != null and timeInterval[1] != null and timeInterval[1]!= ""'>
                and ware.f_accbilldate &lt;= #{timeInterval[1]}
            </if>
            <if test="chargingMethod != null ">and ware.charging_method = #{chargingMethod}</if>
            <if test="invoiceNo != null ">and ware.invoice_no = #{invoiceNo}</if>
            <if test="bank != null ">and ware.bank = #{bank}</if>
            <if test="waterBillNo != null ">and ware.water_bill_no = #{waterBillNo}</if>
        </where>
        ${params.dataScope}
    </select>

    <select id="selectTFeeList1" parameterType="TFee" resultType="Map">
        SELECT
            f.f_id AS fId,
            f.f_billno AS fBillno,
            f.f_ctrlcorpid AS fCtrlcorpid,
            f.f_corpid AS fCorpid,
            c.f_name AS fCorpidName,
            f.f_accbilldate AS fAccbilldate,
            f.t_mblno AS tMblno,
            f.f_amtdr AS fAmtdr,
            f.f_amtcr AS fAmtcr,
            f.f_billtype AS fBilltype,
            f.f_system_type AS fSystemType,
            CASE
            WHEN  f.f_billstatus = '1' THEN '保存'
            WHEN  f.f_billstatus = '2' THEN '暂存'
            WHEN  f.f_billstatus = '3' THEN '审批驳回'
            WHEN  f.f_billstatus = '4' THEN '提交审核'
            WHEN  f.f_billstatus = '5' THEN '审核中'
            WHEN  f.f_billstatus = '6' THEN '审核完成'
            END
            AS fBillstatus,
            f.f_remarks AS fRemarks,
            f.create_by AS createBy
        FROM
            t_fee AS f
            LEFT JOIN t_corps AS c ON f.f_corpid = c.f_id
            left join sys_user u on f.create_by = u.user_name
            left join sys_dept d on f.f_deptid = d.dept_id
        <where>
            <if test="fBillno != null  and fBillno != ''">and f.f_billno = #{fBillno}</if>
            <if test="fCtrlcorpid != null ">and f.f_ctrlcorpid = #{fCtrlcorpid}</if>
            <if test="fCorpid != null ">and f.f_corpid = #{fCorpid}</if>
            <if test="tMblno != null  and tMblno != ''">and f.t_mblno = #{tMblno}</if>
            <if test="fAmtdr != null ">and f.f_amtdr = #{fAmtdr}</if>
            <if test="fAmtcr != null ">and f.f_amtcr = #{fAmtcr}</if>
            <if test="fBilltype != null  and fBilltype != ''">and f.f_billtype = #{fBilltype}</if>
            <if test="fBillstatus != null  and fBillstatus != ''">and f.f_billstatus = #{fBillstatus}</if>
            <if test="fRemarks != null  and fRemarks != ''">and f.f_remarks = #{fRemarks}</if>
            <if test="fAccbilldate != null ">and f.f_accbilldate = #{fAccbilldate}</if>
            <if test="fDeptid != null ">and f.f_deptid = #{fDeptid}</if>
            <if test='timeInterval != null and timeInterval[0] != null and timeInterval[0]!= ""'>
                and f.f_accbilldate &gt;= #{timeInterval[0]}
            </if>
            <if test='timeInterval != null and timeInterval[1] != null and timeInterval[1]!= ""'>
                and f.f_accbilldate &lt;= #{timeInterval[1]}
            </if>
            <if test="chargingMethod != null ">and f.charging_method = #{chargingMethod}</if>
            <if test="invoiceNo != null ">and f.invoice_no = #{invoiceNo}</if>
            <if test="bank != null ">and f.bank = #{bank}</if>
            <if test="waterBillNo != null ">and f.water_bill_no = #{waterBillNo}</if>
        </where>
        ${params.dataScope}
    </select>

    <select id="selectTFeeById" parameterType="Long" resultMap="TFeeResult">
        <include refid="selectTFeeVo"/>
        where f_id = #{fId}
    </select>

    <insert id="insertTFee" parameterType="TFee" useGeneratedKeys="true" keyProperty="fId">
        insert into t_fee
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="fId != null">f_id,</if>
            <if test="fBillno != null">f_billno,</if>
            <if test="fCtrlcorpid != null">f_ctrlcorpid,</if>
            <if test="fActId != null">f_act_id,</if>
            <if test="fCorpid != null">f_corpid,</if>
            <if test="tMblno != null">t_mblno,</if>
            <if test="fTransActId != null">f_trans_act_id,</if>
            <if test="fAmtdr != null">f_amtdr,</if>
            <if test="fAmtcr != null">f_amtcr,</if>
            <if test="fBilltype != null and fBilltype != ''">f_billtype,</if>
            <if test="fBillstatus != null and fBillstatus != ''">f_billstatus,</if>
            <if test="fRemarks != null">f_remarks,</if>
            <if test="fFromDate != null">f_from_date,</if>
            <if test="fToDate != null">f_to_date,</if>
            <if test="fAccbilldate != null">f_accbilldate,</if>
            <if test="fRefNo != null">f_ref_no,</if>
            <if test="delFlag != null">del_flag,</if>
            <if test="createBy != null">create_by,</if>
            <if test="fDeptid != null">f_deptid,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="chargingMethod != null">charging_method,</if>
            <if test="invoiceNo != null">invoice_no,</if>
            <if test="bank != null">bank,</if>
            <if test="waterBillNo != null">water_bill_no,</if>
            <if test="fSystemType != null">f_system_type,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="fId != null">#{fId},</if>
            <if test="fBillno != null">#{fBillno},</if>
            <if test="fCtrlcorpid != null">#{fCtrlcorpid},</if>
            <if test="fActId != null">#{fActId},</if>
            <if test="fCorpid != null">#{fCorpid},</if>
            <if test="tMblno != null">#{tMblno},</if>
            <if test="fTransActId != null">#{fTransActId},</if>
            <if test="fAmtdr != null">#{fAmtdr},</if>
            <if test="fAmtcr != null">#{fAmtcr},</if>
            <if test="fBilltype != null and fBilltype != ''">#{fBilltype},</if>
            <if test="fBillstatus != null and fBillstatus != ''">#{fBillstatus},</if>
            <if test="fRemarks != null">#{fRemarks},</if>
            <if test="fFromDate != null">#{fFromDate},</if>
            <if test="fToDate != null">#{fToDate},</if>
            <if test="fAccbilldate != null">#{fAccbilldate},</if>
            <if test="fRefNo != null">#{fRefNo},</if>
            <if test="delFlag != null">#{delFlag},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="fDeptid != null">#{fDeptid},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="chargingMethod != null">#{chargingMethod},</if>
            <if test="invoiceNo != null">#{invoiceNo},</if>
            <if test="bank != null">#{bank},</if>
            <if test="waterBillNo != null">#{waterBillNo},</if>
            <if test="fSystemType != null">#{fSystemType},</if>
        </trim>
    </insert>

    <update id="updateTFee" parameterType="TFee">
        update t_fee
        <trim prefix="SET" suffixOverrides=",">
            <if test="fBillno != null">f_billno = #{fBillno},</if>
            <if test="fCtrlcorpid != null">f_ctrlcorpid = #{fCtrlcorpid},</if>
            <if test="fActId != null">f_act_id = #{fActId},</if>
            <if test="fCorpid != null">f_corpid = #{fCorpid},</if>
            <if test="tMblno != null">t_mblno = #{tMblno},</if>
            <if test="fTransActId != null">f_trans_act_id = #{fTransActId},</if>
            <if test="fAmtdr != null">f_amtdr = #{fAmtdr},</if>
            <if test="fAmtcr != null">f_amtcr = #{fAmtcr},</if>
            <if test="fBilltype != null and fBilltype != ''">f_billtype = #{fBilltype},</if>
            <if test="fBillstatus != null and fBillstatus != ''">f_billstatus = #{fBillstatus},</if>
            <if test="fRemarks != null">f_remarks = #{fRemarks},</if>
            <if test="fFromDate != null">f_from_date = #{fFromDate},</if>
            <if test="fToDate != null">f_to_date = #{fToDate},</if>
            <if test="fAccbilldate != null">f_accbilldate = #{fAccbilldate},</if>
            <if test="fRefNo != null">f_ref_no = #{fRefNo},</if>
            <if test="delFlag != null">del_flag = #{delFlag},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="fDeptid != null">f_deptid = #{fDeptid},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="chargingMethod != null">charging_method = #{chargingMethod},</if>
            <if test="invoiceNo != null">invoice_no = #{invoiceNo},</if>
            <if test="bank != null">bank = #{bank},</if>
            <if test="waterBillNo != null">water_bill_no = #{waterBillNo},</if>
            <if test="fSystemType != null">f_system_type = #{fSystemType},</if>
        </trim>
        where f_id = #{fId}
    </update>

    <delete id="deleteTFeeById" parameterType="Long">
        delete from t_fee where f_id = #{fId}
    </delete>

    <delete id="deleteTFeeByIds" parameterType="String">
        delete
            f.*,
            fd.*
        from
             t_fee  f
            LEFT JOIN t_fee_do fd ON fd.f_pid = f.f_id
        where f.f_id in
        <foreach item="fId" collection="array" open="(" separator="," close=")">
            #{fId}
        </foreach>
    </delete>

    <select id="warehouseBillsFeesList" resultType="java.util.Map">
        SELECT
            w.f_id AS fSrcid,
            t.f_id AS fSrcpid,
            t.f_corpid AS fCorpid,
            c.f_name AS fName,
            w.fId AS fId,
            w.fName AS fFeesName,
            t.f_bscorpno AS fBscorpno,
            w.f_mblno AS fMblno,
            w.f_product_name AS fProductName,
            w.f_bsdate AS fBsdate,
            t.f_billtype AS fBilltype,
            t.f_review_date AS fReviewDate,
            w.f_feeid AS fFeeid,
            w.src_bill_no AS srcBillNo,
            w.f_mblno AS fMblno,
            w.f_product_name AS fProductName,
            w.f_marks AS fMarks,
            w.f_chargedate AS fChargedate,
            w.f_billing_days AS fBillingDays,
            w.f_billing_deadline AS fBillingDeadline,
            w.f_inventory_days AS fInventoryDays,
            w.f_originalbilldate AS fOriginalbilldate,
            f.f_name AS fFeeName,
            w.f_dc AS fSrcdc,
            ifnull( w.f_amount, 0 ) - ifnull( w.f_stlamount, 0 ) AS fAmtdr,
            ifnull( w.f_amount, 0 ) - ifnull( w.f_stlamount, 0 ) AS fAmt,
            dictIn.dict_label AS fBusinessType
        FROM
            t_warehousebills t
            LEFT JOIN t_corps c ON c.f_id = t.f_corpid
            LEFT JOIN (
            SELECT
                f.f_id AS f_id,
                f.f_pid AS f_pid,
                f.f_lineno AS f_lineno,
                f.f_corpid,
                f.f_feeid,
                f.f_feeUnitid,
                f.f_qty,
                f.f_unitprice,
                f.f_amount,
                f.f_currency,
                f.f_exrate,
                f.f_taxrate,
                f.f_dc,
                f.f_billstatus,
                f.f_statement_no,
                f.f_accamount,
                f.f_accamount_date,
                f.f_stlamount_no,
                f.f_stlamount,
                f.f_stlamount_date,
                f.f_invnos,
                f.f_invamount,
                f.f_askamount,
                f.f_chargedate,
                f.f_status,
                f.remark,
                f.f_mblno,
                f.f_product_name,
                f.src_bill_no,
                f.f_billing_days,
                f.f_inventory_days,
                f.f_marks,
                f.f_billing_deadline,
                f.f_originalbilldate,
                f.f_billtype,
                f.f_business_type,
                f.f_bsdate,
                c.f_id AS fId,
                c.f_name AS fName
            FROM
                t_warehousebillsfees AS f
                LEFT JOIN t_corps AS c ON f.f_corpid = c.f_id
            ) w ON w.f_pid = t.f_id
            LEFT JOIN t_fees f ON w.f_feeid = f.f_id
            LEFT JOIN sys_dict_data dictIn ON dictIn.dict_value = w.f_business_type
        <where>
            <if test="map.tWareHouseFees.fCorpid != null  and map.tWareHouseFees.fCorpid != ''">and t.f_corpid = #{map.tWareHouseFees.fCorpid} </if>
            <if test="map.tWareHouseFees.fToCorpid != null  and map.tWareHouseFees.fToCorpid != ''">and w.f_corpid = #{map.tWareHouseFees.fToCorpid} </if>
            <if test="map.tWareHouseFees.fMblno != null  and map.tWareHouseFees.fMblno != ''">and w.f_mblno  like concat('%', #{map.tWareHouseFees.fMblno}, '%') </if>
            <if test="map.tWareHouseFees.fStatementNo != null and map.tWareHouseFees.fStatementNo != ''">and w.f_statement_no  like concat('%', #{map.tWareHouseFees.fStatementNo}, '%') </if>

            <if test='map.tWareHouseFees.fReconciliation != null and map.tWareHouseFees.fReconciliation != "" and map.tWareHouseFees.fReconciliation == "1" '>
                and w.f_accamount_date IS NOT NULL
            </if>
            <if test='map.tWareHouseFees.fReconciliation != null and map.tWareHouseFees.fReconciliation != "" and map.tWareHouseFees.fReconciliation == "0" '>
                and w.f_accamount_date IS  NULL
            </if>

            <if test='map.tWareHouseFees.fDc != null and map.tWareHouseFees.fDc != "" and map.tWareHouseFees.fDc == "D" '>
                    and w.f_dc = #{map.tWareHouseFees.fDc}
            </if>
            <if test='map.tWareHouseFees.fDc != null and map.tWareHouseFees.fDc != "" and map.tWareHouseFees.fDc == "C" '>
                    and w.f_dc = #{map.tWareHouseFees.fDc}
            </if>
            <if test="map.tWareHouseFees.fFeeid != null  and  map.tWareHouseFees.fFeeid != '' ">
                and  w.f_feeid in
                <foreach collection="map.tWareHouseFees.fFeeid" item="id" index="index" open="(" close=")" separator=",">
                    #{id}
                </foreach>
            </if>
            <if test='map.tWareHouseFees.timeExamine != null and map.tWareHouseFees.timeExamine[0] != null and map.tWareHouseFees.timeExamine[0]!= ""'>
                and t.f_review_date &gt;= #{map.tWareHouseFees.timeExamine[0]}
            </if>
            <if test='map.tWareHouseFees.timeExamine != null and map.tWareHouseFees.timeExamine[1] != null and map.tWareHouseFees.timeExamine[1]!= ""'>
                and t.f_review_date &lt;= #{map.tWareHouseFees.timeExamine[1]}
            </if>
            <if test='map.tWareHouseFees.timeInterval != null and map.tWareHouseFees.timeInterval[0] != null and map.tWareHouseFees.timeInterval[0]!= ""'>
                and t.f_bsdate &gt;= #{map.tWareHouseFees.timeInterval[0]}
            </if>
            <if test='map.tWareHouseFees.timeInterval != null and map.tWareHouseFees.timeInterval[1] != null and map.tWareHouseFees.timeInterval[1]!= ""'>
                and t.f_bsdate &lt;= #{map.tWareHouseFees.timeInterval[1]}
            </if>
            <if test='map.tWareHouseFees.timeReconci != null and map.tWareHouseFees.timeReconci[0] != null and map.tWareHouseFees.timeReconci[0]!= ""'>
                and w.f_accamount_date &gt;= #{map.tWareHouseFees.timeReconci[0]}
            </if>
            <if test='map.tWareHouseFees.timeReconci != null and map.tWareHouseFees.timeReconci[1] != null and map.tWareHouseFees.timeReconci[1]!= ""'>
                and w.f_accamount_date &lt;= #{map.tWareHouseFees.timeReconci[1]}
            </if>
            <if test="map.tWareHouseFees.fBilltype != null  and map.tWareHouseFees.fBilltype == 'SJRK'">
                and w.f_billtype = #{map.tWareHouseFees.fBilltype}
                AND dictIn.dict_type = 'st_in_type'
                AND dictIn.status = '0'
            </if>
            <if test="map.tWareHouseFees.fBilltype != null  and map.tWareHouseFees.fBilltype == 'SJCK'">
                and w.f_billtype = #{map.tWareHouseFees.fBilltype}
                AND dictIn.dict_type = 'st_out_type'
                AND dictIn.status = '0'
            </if>
            <if test="map.tWareHouseFees.fBilltype != null  and map.tWareHouseFees.fBilltype == 'HQZY'">
                and w.f_billtype = #{map.tWareHouseFees.fBilltype}
                AND dictIn.dict_type = 'st_trans_type'
                AND dictIn.status = '0'
            </if>
            <if test="map.tWareHouseFees.fBilltype == null ">
                AND dictIn.dict_type in ('st_in_type','st_out_type','st_trans_type')
                AND dictIn.status = '0'
            </if>
            <if test="map.tWareHouseFees.fBusinessType != null  and map.tWareHouseFees.fBusinessType != '' ">
                and  w.f_business_type in
                <foreach collection="map.tWareHouseFees.fBusinessType" item="id" open="(" close=")" separator=",">
                    #{id}
                </foreach>
            </if>
            and  t.f_review_date IS NOT NULL
            and  ifnull(w.f_amount, 0) - ifnull(w.f_stlamount, 0) > 0
            GROUP BY  w.f_id
        </where>
    </select>

    <select id="warehouseBillsFeesListAccamount" resultType="java.util.Map">
        SELECT
            w.f_id AS fSrcid,
            t.f_id AS fSrcpid,
            t.f_corpid AS fCorpid,
            c.f_name AS fName,
            w.fName AS fFeesName,
            t.f_bscorpno AS fBscorpno,
            w.f_mblno AS fMblno,
            w.f_product_name AS fProductName,
            w.f_bsdate AS fBsdate,
            t.f_billtype AS fBilltype,
            t.f_review_date AS fReviewDate,
            w.f_marks AS fMarks,
            w.f_feeid AS fFeeid,
            w.f_billtype AS fBilltype,
            w.f_chargedate AS fChargedate,
            w.f_originalbilldate AS fOriginalbilldate,
            w.f_billing_deadline AS fBillingDeadline,
            w.f_billing_days AS fBillingDays,
            w.f_inventory_days AS fInventoryDays,
            f.f_name AS fFeeName,
            w.f_dc AS fSrcdc,
            ifnull( w.f_amount, 0 ) - ifnull( w.f_accamount, 0 ) AS fAmtdr,
            ifnull( w.f_amount, 0 ) - ifnull( w.f_accamount, 0 ) AS fAmt,
            dict.dict_label AS fFeeunitid,
            w.f_qty AS fQty ,
            w.f_unitprice AS  fUnitprice ,
            dictIn.dict_label AS fBusinessType
        FROM
            t_warehousebills t
            LEFT JOIN t_corps c ON c.f_id = t.f_corpid
            LEFT JOIN (
                SELECT
                    f.f_id AS f_id,
                    f.f_pid AS f_pid,
                    f.f_lineno AS f_lineno,
                    f.f_corpid,
                    f.f_feeid,
                    f.f_feeUnitid,
                    f.f_qty,
                    f.f_unitprice,
                    f.f_amount,
                    f.f_currency,
                    f.f_exrate,
                    f.f_taxrate,
                    f.f_dc,
                    f.f_billstatus,
                    f.f_statement_no,
                    f.f_accamount,
                    f.f_accamount_date,
                    f.f_stlamount_no,
                    f.f_stlamount,
                    f.f_stlamount_date,
                    f.f_invnos,
                    f.f_invamount,
                    f.f_askamount,
                    f.f_status,
                    f.remark,
                    f.f_inventory_days,
                    f.f_billing_days,
                    f.f_billing_deadline,
                    f.f_originalbilldate,
                    f.f_mblno,
                    f.f_product_name,
                    f.f_chargedate,
                    f.src_bill_no,
                    f.f_billtype,
                    f.f_marks,
                    f.f_business_type,
                    f.f_bsdate,
                    c.f_id AS fId,
                    c.f_name AS fName
                FROM
                    t_warehousebillsfees AS f
                    LEFT JOIN t_corps AS c ON f.f_corpid = c.f_id
                ) w ON w.f_pid = t.f_id
            LEFT JOIN t_fees f ON w.f_feeid = f.f_id
            LEFT JOIN sys_dict_data dict ON dict.dict_value = w.f_feeUnitid
            LEFT JOIN sys_dict_data dictIn ON dictIn.dict_value = w.f_business_type
        <where>
            dict.status = '0'
            AND dict.dict_type = 'data_unitfees'
            <if test="map.tWareHouseFees.fCorpid != null  and map.tWareHouseFees.fCorpid != ''">and t.f_corpid = #{map.tWareHouseFees.fCorpid} </if>
            <if test="map.tWareHouseFees.fToCorpid != null  and map.tWareHouseFees.fToCorpid != ''">and w.f_corpid = #{map.tWareHouseFees.fToCorpid} </if>
            <if test="map.tWareHouseFees.fMblno != null  and map.tWareHouseFees.fMblno != ''">and w.f_mblno  like concat('%', #{map.tWareHouseFees.fMblno}, '%') </if>
            <if test="map.tWareHouseFees.fStatementNo != null and map.tWareHouseFees.fStatementNo != ''">and w.f_statement_no  like concat('%', #{map.tWareHouseFees.fStatementNo}, '%') </if>

            <if test='map.tWareHouseFees.fReconciliation != null and map.tWareHouseFees.fReconciliation != "" and map.tWareHouseFees.fReconciliation == "1" '>
                and w.f_accamount_date IS NOT NULL
            </if>
            <if test='map.tWareHouseFees.fReconciliation != null and map.tWareHouseFees.fReconciliation != "" and map.tWareHouseFees.fReconciliation == "0" '>
                and w.f_accamount_date IS  NULL
            </if>

            <if test='map.tWareHouseFees.fDc != null and map.tWareHouseFees.fDc != "" and map.tWareHouseFees.fDc == "D" '>
                and w.f_dc = #{map.tWareHouseFees.fDc}
            </if>
            <if test='map.tWareHouseFees.fDc != null and map.tWareHouseFees.fDc != "" and map.tWareHouseFees.fDc == "C" '>
                and w.f_dc = #{map.tWareHouseFees.fDc}
            </if>
            <if test="map.tWareHouseFees.fFeeid != null  and  map.tWareHouseFees.fFeeid != '' ">
                and  w.f_feeid in
                <foreach collection="map.tWareHouseFees.fFeeid" item="id" index="index" open="(" close=")" separator=",">
                    #{id}
                </foreach>
            </if>
            <if test='map.tWareHouseFees.timeExamine != null and map.tWareHouseFees.timeExamine[0] != null and map.tWareHouseFees.timeExamine[0]!= ""'>
                and t.f_review_date &gt;= #{map.tWareHouseFees.timeExamine[0]}
            </if>
            <if test='map.tWareHouseFees.timeExamine != null and map.tWareHouseFees.timeExamine[1] != null and map.tWareHouseFees.timeExamine[1]!= ""'>
                and t.f_review_date &lt;= #{map.tWareHouseFees.timeExamine[1]}
            </if>
            <if test='map.tWareHouseFees.timeInterval != null and map.tWareHouseFees.timeInterval[0] != null and map.tWareHouseFees.timeInterval[0]!= ""'>
                and t.f_bsdate &gt;= #{map.tWareHouseFees.timeInterval[0]}
            </if>
            <if test='map.tWareHouseFees.timeInterval != null and map.tWareHouseFees.timeInterval[1] != null and map.tWareHouseFees.timeInterval[1]!= ""'>
                and t.f_bsdate &lt;= #{map.tWareHouseFees.timeInterval[1]}
            </if>
            <if test='map.tWareHouseFees.timeReconci != null and map.tWareHouseFees.timeReconci[0] != null and map.tWareHouseFees.timeReconci[0]!= ""'>
                and w.f_accamount_date &gt;= #{map.tWareHouseFees.timeReconci[0]}
            </if>
            <if test='map.tWareHouseFees.timeReconci != null and map.tWareHouseFees.timeReconci[1] != null and map.tWareHouseFees.timeReconci[1]!= ""'>
                and w.f_accamount_date &lt;= #{map.tWareHouseFees.timeReconci[1]}
            </if>
            <if test="map.tWareHouseFees.fBilltype != null  and map.tWareHouseFees.fBilltype == 'SJRK'">
                and w.f_billtype = #{map.tWareHouseFees.fBilltype}
                AND dictIn.dict_type = 'st_in_type'
                AND dictIn.status = '0'
             </if>
            <if test="map.tWareHouseFees.fBilltype != null  and map.tWareHouseFees.fBilltype == 'SJCK'">
                and w.f_billtype = #{map.tWareHouseFees.fBilltype}
                AND dictIn.dict_type = 'st_out_type'
                AND dictIn.status = '0'
            </if>
            <if test="map.tWareHouseFees.fBilltype != null  and map.tWareHouseFees.fBilltype == 'HQZY'">
                and w.f_billtype = #{map.tWareHouseFees.fBilltype}
                AND dictIn.dict_type = 'st_trans_type'
                AND dictIn.status = '0'
            </if>
            <if test="map.tWareHouseFees.fBilltype == null ">
                AND dictIn.dict_type in ('st_in_type','st_out_type','st_trans_type')
                AND dictIn.status = '0'
            </if>
            <if test="map.tWareHouseFees.fBusinessType != null  and map.tWareHouseFees.fBusinessType != '' ">
                and  w.f_business_type in
                <foreach collection="map.tWareHouseFees.fBusinessType" item="id" open="(" close=")" separator=",">
                    #{id}
                </foreach>
            </if>
            and  t.f_review_date IS NOT NULL
            and  ifnull(w.f_amount, 0) - ifnull(w.f_accamount, 0) > 0
            GROUP BY  w.f_id
        </where>
    </select>

    <select id="warehouseBillsFeesList1" resultType="java.util.Map">
        SELECT
            w.f_id AS fSrcid,
            t.f_id AS fSrcpid,
            t.f_corpid AS fCorpid,
            c.f_name AS fName,
            w.f_corpid AS fCorpids,
            c1.f_name AS fFeesName,
            w.f_mblno AS fMblno,
            w.f_product_name AS fProductName,
            w.f_marks AS fMarks,
            t.f_billtype AS fBilltype,
            t.f_review_date AS fReviewDate,
            w.f_dc AS fSrcdc,
            SUM(w.f_amount) AS fAmount,
            SUM(w.f_stlamount) AS fStlamount,
            ifnull(SUM( w.f_amount ) , 0 ) - ifnull( SUM( w.f_stlamount ) , 0 ) AS nnfinished
        FROM
            t_warehousebills t
                left join sys_user u on t.create_by = u.user_name
                left join sys_dept d on t.f_bsdeptid = d.dept_id
                LEFT JOIN t_corps c ON c.f_id = t.f_corpid
                LEFT JOIN t_warehousebillsfees w ON w.f_pid = t.f_id
                LEFT JOIN t_corps  c1 ON w.f_corpid = c1.f_id
                LEFT JOIN t_fees f ON w.f_feeid = f.f_id
        <where>
            <if test="fCorpid != null  and fCorpid != ''">and t.f_corpid = #{fCorpid} </if>
            <if test="fToCorpid != null  and fToCorpid != ''">and w.f_corpid = #{fToCorpid} </if>
            <if test="fMblno != null  and fMblno != ''">and w.f_mblno  like concat('%', #{fMblno}, '%') </if>
            <if test="fMarks != null and fMarks != ''">and w.f_marks  like concat('%', #{fMarks}, '%') </if>
            <if test="fProductName != null and fProductName != ''">and w.f_product_name  like concat('%', #{fProductName}, '%') </if>
            <if test='fBillstatus != null and fBillstatus != "" and fBillstatus == "0" '>
                and w.f_billstatus != 6
            </if>
            <if test='fBillstatus != null and fBillstatus != "" and fBillstatus == "1" '>
                and w.f_billstatus = 6
            </if>
            <if test='fDc != null and fDc != "" and fDc == "D" '>
                and w.f_dc = #{fDc}
            </if>
            <if test='fDc != null and fDc != "" and fDc == "C" '>
                and w.f_dc = #{fDc}
            </if>
            <if test='timeExamine != null and timeExamine[0] != null and timeExamine[0]!= ""'>
                and t.f_review_date &gt;= #{timeExamine[0]}
            </if>
            <if test='timeExamine != null and timeExamine[1] != null and timeExamine[1]!= ""'>
                and t.f_review_date &lt;= #{timeExamine[1]}
            </if>
            <if test='fReconciliation!= null and fReconciliation != "" and fReconciliation == "0" '>
                and ifnull(w.f_amount, 0) - ifnull(w.f_stlamount, 0) != 0
            </if>
            and  t.f_review_date IS NOT NULL
            ${params.dataScope}
            GROUP BY
            w.f_pid,
            w.f_dc,
            w.f_corpid
        </where>
    </select>

    <select id="selectTWarehousebillsItemsList"
            resultType="com.ruoyi.warehouseBusiness.excel.TWareHouseExcelItem">
        SELECT
            ( @ii := @ii + 1 ) AS fId,
            wi.f_mblno AS fMblno,
            wi.f_bsdate AS fBsdate,
            f.f_name AS fFeeid,
            CASE
                WHEN w.f_srcdc = 'D' THEN
                    '收'
                WHEN w.f_srcdc = 'C' THEN
                    '付'
                END AS fSrcdc,
            w.f_amtdr AS fAmtdr,
            dict.dict_label AS fFeeunitid,
            wi.f_qty AS fBillingQty,
            wi.f_unitprice AS fUnitprice,
            w.f_amt AS fAmt,
            wi.f_business_type AS fBusinessType,
            CASE
                wi.f_billtype
                WHEN 'SJRK' THEN
                    '入库'
                WHEN 'SJCK' THEN
                    '出库'
                WHEN 'HQZY' THEN
                    '货转'
                WHEN 'CKDB' THEN
                    '调拨'
                WHEN 'HWTG' THEN
                    '货物通关'
                WHEN 'KCZZ' THEN
                    '库存总账'
                END AS fBilltype,
            wi.f_product_name AS fGoodsid,
            wi.f_marks AS fMarks,
            wi.f_chargedate AS fChargedate,
            wi.f_billing_deadline AS fBillingDeadline,
            wi.f_inventory_days AS fInventoryDays,
            wi.f_billing_days AS fBillingDays,
            w.f_remarks AS fRemarks
        FROM
            t_fee_do w
                LEFT JOIN t_warehousebillsfees wi ON w.f_srcid = wi.f_id
                LEFT JOIN t_fees  f ON wi.f_feeid = f.f_id
                LEFT JOIN sys_dict_data dict ON dict.dict_value = wi.f_feeUnitid
           ,(
            SELECT
                @ii := 0
        ) AS ii
        WHERE
            dict.status = '0'
            AND dict.dict_type = 'data_unitfees'
            AND w.f_pid = #{fId}
    </select>

    <select id="warehouseBillsFeesProfitList" resultType="java.util.Map">
        SELECT
        t.f_id AS fSrcpid,
        c.f_name AS fName,
        c1.f_name AS fFeesName,
        w.f_mblno AS fMblno,
        w.f_product_name AS fProductName,
        w.f_marks AS fMarks,
        t.f_billtype AS fBilltype,
        w.f_review_date AS fReviewDate,
        sum( CASE f_dc WHEN 'D' THEN f_amount ELSE 0 END ) fAmount,
        sum( CASE f_dc WHEN 'C' THEN f_amount ELSE 0 END ) fStlamount,
        sum( CASE f_dc WHEN 'D' THEN f_amount ELSE 0 END ) - sum( CASE f_dc WHEN 'C' THEN f_amount ELSE 0 END ) nnfinished
        FROM
        t_warehousebillsfees w
        LEFT JOIN t_warehousebills t ON t.f_id = w.f_pid
        left join sys_user u on t.create_by = u.user_name
        left join sys_dept d on t.f_bsdeptid = d.dept_id
        LEFT JOIN t_corps c ON c.f_id = t.f_corpid
        LEFT JOIN t_corps c1 ON c1.f_id = w.f_corpid
        <where>
            <if test="fCorpid != null  and fCorpid != ''">and t.f_corpid = #{fCorpid} </if>
            <if test="fToCorpid != null  and fToCorpid != ''">and w.f_corpid = #{fToCorpid} </if>
            <if test="fMblno != null  and fMblno != ''">and w.f_mblno  like concat('%', #{fMblno}, '%') </if>
            <if test="fMarks != null and fMarks != ''">and w.f_marks  like concat('%', #{fMarks}, '%') </if>
            <if test="fProductName != null and fProductName != ''">and  w.f_product_name  like concat('%', #{fProductName}, '%') </if>
            <if test='fBillstatus != null and fBillstatus != "" and fBillstatus == "0" '>
                and w.f_billstatus != 6
            </if>
            <if test='fBillstatus != null and fBillstatus != "" and fBillstatus == "1" '>
                and w.f_billstatus = 6
            </if>
            <if test='timeExamine != null and timeExamine[0] != null and timeExamine[0]!= ""'>
                and w.f_review_date &gt;= #{timeExamine[0]}
            </if>
            <if test='timeExamine != null and timeExamine[1] != null and timeExamine[1]!= ""'>
                and w.f_review_date &lt;= #{timeExamine[1]}
            </if>
            <if test='fReconciliation!= null and fReconciliation != "" and fReconciliation == "0" '>
                and ifnull(w.f_amount, 0) - ifnull(w.f_stlamount, 0) != 0
            </if>
            <if test="fBusinessType != null  and fBusinessType != '' ">
                and  w.f_business_type in
                <foreach collection="fBusinessType" item="id" open="(" close=")" separator=",">
                    #{id}
                </foreach>
            </if>
            <if test="fFeeid != null  and fFeeid != '' ">
                and  w.f_feeid in
                <foreach collection="fFeeid" item="id" open="(" close=")" separator=",">
                    #{id}
                </foreach>
            </if>
            and  w.f_review_date IS NOT NULL
            ${params.dataScope}
            GROUP BY
            w.f_mblno
        </where>
    </select>
    <select id="selectFeeList" resultType="java.util.Map">
        select
        ware.create_by  AS createBy,
        ware.f_billno AS fBillno,
        ware.f_ctrlcorpid AS fCtrlcorpid,
        ware.create_time AS createTime,
        ware.f_accbilldate AS fAccbilldate,
        ware.t_mblno AS tMblno,
        dict.dict_label AS chargingMethod,
        ware.invoice_no AS invoiceNo,
        ware.bank AS bank,
        ware.water_bill_no AS waterBillNo,
        ware.f_amtdr AS fAmtdr,
        ware.f_amtcr AS fAmtcr,
        ware.f_remarks AS fRemarks,
        CASE
        WHEN  ware.f_billstatus = '1' THEN '保存'
        WHEN  ware.f_billstatus = '2' THEN '暂存'
        WHEN  ware.f_billstatus = '3' THEN '审批驳回'
        WHEN  ware.f_billstatus = '4' THEN '提交审核'
        WHEN  ware.f_billstatus = '5' THEN '审核中'
        WHEN  ware.f_billstatus = '6' THEN '审核完成'
        END
        AS fBillstatus
        from
        t_fee ware
        left join sys_user u on ware.create_by = u.user_name
        left join sys_dept d on ware.f_deptid = d.dept_id
        LEFT JOIN sys_dict_data dict ON dict.dict_value = ware.charging_method
        <where>
            dict.status = '0'
            AND dict.dict_type = 'data_settlement_type'
            <if test="fBillno != null  and fBillno != ''">and ware.f_billno = #{fBillno}</if>
            <if test="fCtrlcorpid != null ">and ware.f_ctrlcorpid = #{fCtrlcorpid}</if>
            <if test="fActId != null ">and ware.f_act_id = #{fActId}</if>
            <if test="fCorpid != null ">and ware.f_corpid = #{fCorpid}</if>
            <if test="fTransActId != null ">and ware.f_trans_act_id = #{fTransActId}</if>
            <if test="tMblno != null  and tMblno != ''">and ware.t_mblno = #{tMblno}</if>
            <if test="fAmtdr != null ">and ware.f_amtdr = #{fAmtdr}</if>
            <if test="fAmtcr != null ">and ware.f_amtcr = #{fAmtcr}</if>
            <if test="fBilltype != null  and fBilltype != ''">and ware.f_billtype = #{fBilltype}</if>
            <if test="fBillstatus != null  and fBillstatus != ''">and ware.f_billstatus = #{fBillstatus}</if>
            <if test="fRemarks != null  and fRemarks != ''">and ware.f_remarks = #{fRemarks}</if>
            <if test="fFromDate != null ">and ware.f_from_date = #{fFromDate}</if>
            <if test="fAccbilldate != null ">and ware.f_accbilldate = #{fAccbilldate}</if>
            <if test="fToDate != null ">and ware.f_to_date = #{fToDate}</if>
            <if test="fRefNo != null  and fRefNo != ''">and ware.f_ref_no = #{fRefNo}</if>
            <if test="fDeptid != null ">and ware.f_deptid = #{fDeptid}</if>
            <if test='timeInterval != null and timeInterval[0] != null and timeInterval[0]!= ""'>
                and ware.f_accbilldate &gt;= #{timeInterval[0]}
            </if>
            <if test='timeInterval != null and timeInterval[1] != null and timeInterval[1]!= ""'>
                and ware.f_accbilldate &lt;= #{timeInterval[1]}
            </if>
            <if test="chargingMethod != null ">and ware.charging_method = #{chargingMethod}</if>
            <if test="invoiceNo != null ">and ware.invoice_no = #{invoiceNo}</if>
            <if test="bank != null ">and ware.bank = #{bank}</if>
            <if test="waterBillNo != null ">and ware.water_bill_no = #{waterBillNo}</if>
        </where>
        ${params.dataScope}
    </select>
    <select id="selectDetailedList" resultType="com.ruoyi.finance.excel.Detailed">
        SELECT
            ( @ii := @ii + 1 ) AS fId,
            wi.f_mblno AS fMblno,
            wi.f_bsdate AS fBsdate,
            t.f_bscorpno AS fBscorpno,
            f.f_name AS fFeeid,
            CASE
                WHEN w.f_srcdc = 'D' THEN
                    '收'
                WHEN w.f_srcdc = 'C' THEN
                    '付'
                END AS fSrcdc,
            w.f_amtdr AS fAmtdr,
            dict.dict_label AS fFeeunitid,
            wi.f_qty AS fBillingQty,
            wi.f_unitprice AS fUnitprice,
            w.f_amt AS fAmt,
            wi.f_business_type AS fBusinessType,
            CASE
                wi.f_billtype
                WHEN 'SJRK' THEN
                    '入库'
                WHEN 'SJCK' THEN
                    '出库'
                WHEN 'HQZY' THEN
                    '货转'
                WHEN 'CKDB' THEN
                    '调拨'
                WHEN 'HWTG' THEN
                    '货物通关'
                WHEN 'KCZZ' THEN
                    '库存总账'
                END AS fBilltype,
            wi.f_product_name AS fGoodsid,
            wi.f_marks AS fMarks,
            w.f_remarks AS fRemarks
        FROM
            t_fee_do w
                LEFT JOIN t_warehousebillsfees wi ON w.f_srcid = wi.f_id
                LEFT JOIN t_warehousebills t ON wi.f_pid = t.f_id
                LEFT JOIN t_fees  f ON wi.f_feeid = f.f_id
                LEFT JOIN sys_dict_data dict ON dict.dict_value = wi.f_feeUnitid
           ,(
            SELECT
                @ii := 0
        ) AS ii
        WHERE
            dict.status = '0'
          AND dict.dict_type = 'data_unitfees'
          AND w.f_pid = #{fId}
    </select>
    <select id="selectDetailedList1" resultType="java.util.Map">
        SELECT
            t.f_id AS fSrcpid,
            t.f_corpid AS fCorpid,
            c.f_name AS fName,
            c1.f_name AS fFeesName,
            w.f_mblno AS fMblno,
            w.f_product_name AS fProductName,
            w.f_marks AS fMarks,
            w.f_dc AS fSrcdc,
            w.f_amount  AS fAmount,
            w.f_stlamount  AS fStlamount,
            ifnull( w.f_amount , 0 ) - ifnull( w.f_stlamount , 0 ) AS nnfinished
        FROM
            t_warehousebills t
                LEFT JOIN sys_user u ON t.create_by = u.user_name
                LEFT JOIN sys_dept d ON t.f_bsdeptid = d.dept_id
                LEFT JOIN t_corps c ON c.f_id = t.f_corpid
                LEFT JOIN t_warehousebillsfees w ON w.f_pid = t.f_id
                LEFT JOIN t_corps c1 ON w.f_corpid = c1.f_id
                LEFT JOIN t_fees f ON w.f_feeid = f.f_id
        WHERE
            w.f_dc = #{fSrcdc}
          AND t.f_review_date IS NOT NULL
          AND w.f_corpid = #{fCorpIds}
          AND t.f_corpid = #{fCorpId}
          AND w.f_mblno = #{fMblno}
    </select>
</mapper>