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

    <resultMap type="FTmsaccbills" id="FTmsaccbillsResult">
        <result property="id"    column="id"    />
        <result property="accYear"    column="acc_year"    />
        <result property="accMonth"    column="acc_month"    />
        <result property="actId"    column="act_id"    />
        <result property="fFromDate"    column="f_from_date"    />
        <result property="amt"    column="amt"    />
        <result property="fToDate"    column="f_to_date"    />
        <result property="refNo"    column="ref_no"    />
        <result property="postDate"    column="post_date"    />
        <result property="fAmtcr"    column="f_amtcr"    />
        <result property="fAmtdr"    column="f_amtdr"    />
        <result property="delFlag"    column="del_flag"    />
        <result property="createBy"    column="create_by"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateBy"    column="update_by"    />
        <result property="updateTime"    column="update_time"    />
        <result property="fCtrlcorpid"    column="f_ctrlcorpid"    />
        <result property="tMblno"    column="t_mblno"    />
        <result property="fBilltype"    column="f_billtype"    />
        <result property="fAccbilldate"    column="f_accbilldate"    />
        <result property="fRefNo"    column="f_ref_no"    />
        <result property="fBillno"    column="f_billno"    />
        <result property="fDeptid"    column="f_deptid"    />
        <result property="chargingMethod"    column="charging_method"    />
        <result property="fBillstatus"    column="f_billstatus"    />
        <result property="invoiceNo"    column="invoice_no"    />
        <result property="bank"    column="bank"    />
        <result property="waterBillNo"    column="water_bill_no"    />
        <result property="fCorpid"    column="f_corpid"    />
        <result property="fSystemType"    column="f_system_type"    />
        <result property="fRemarks"    column="f_remarks"    />
    </resultMap>

    <sql id="selectFTmsaccbillsVo">
        select id, acc_year, acc_month, act_id, f_from_date, amt, f_to_date, ref_no, post_date, f_amtcr, f_amtdr, del_flag, create_by, create_time, update_by, update_time, f_ctrlcorpid, t_mblno, f_billtype, f_accbilldate, f_ref_no, f_billno, f_deptid, charging_method, f_billstatus, invoice_no, bank, water_bill_no, f_corpid, f_system_type, f_remarks from F_TMSACCBILLS
    </sql>

    <select id="selectFTmsaccbillsList" parameterType="FTmsaccbills" resultMap="FTmsaccbillsResult">
        <include refid="selectFTmsaccbillsVo"/>
        <where>
            <if test="accYear != null "> and acc_year = #{accYear}</if>
            <if test="accMonth != null "> and acc_month = #{accMonth}</if>
            <if test="actId != null "> and act_id = #{actId}</if>
            <if test="fFromDate != null "> and f_from_date = #{fFromDate}</if>
            <if test="amt != null "> and amt = #{amt}</if>
            <if test="fToDate != null "> and f_to_date = #{fToDate}</if>
            <if test="refNo != null  and refNo != ''"> and ref_no = #{refNo}</if>
            <if test="postDate != null "> and post_date = #{postDate}</if>
            <if test="fAmtcr != null "> and f_amtcr = #{fAmtcr}</if>
            <if test="fAmtdr != null "> and f_amtdr = #{fAmtdr}</if>
            <if test="fCtrlcorpid != null  and fCtrlcorpid != ''"> and f_ctrlcorpid = #{fCtrlcorpid}</if>
            <if test="tMblno != null  and tMblno != ''"> and t_mblno = #{tMblno}</if>
            <if test="fBilltype != null  and fBilltype != ''"> and f_billtype = #{fBilltype}</if>
            <if test="fRefNo != null  and fRefNo != ''"> and f_ref_no = #{fRefNo}</if>
            <if test="fBillno != null  and fBillno != ''"> and f_billno = #{fBillno}</if>
            <if test="fDeptid != null "> and f_deptid = #{fDeptid}</if>
            <if test="chargingMethod != null  and chargingMethod != ''"> and charging_method = #{chargingMethod}</if>
            <if test="fBillstatus != null "> and f_billstatus = #{fBillstatus}</if>
            <if test="invoiceNo != null  and invoiceNo != ''"> and invoice_no = #{invoiceNo}</if>
            <if test="bank != null  and bank != ''"> and bank = #{bank}</if>
            <if test="waterBillNo != null  and waterBillNo != ''"> and water_bill_no = #{waterBillNo}</if>
            <if test="fCorpid != null "> and f_corpid = #{fCorpid}</if>
            <if test="fSystemType != null  and fSystemType != ''"> and f_system_type = #{fSystemType}</if>
            <if test="fRemarks != null  and fRemarks != ''"> and f_remarks = #{fRemarks}</if>
            <if test='timeInterval != null and timeInterval[0] != null and timeInterval[0]!= ""'>
                and f_accbilldate &gt;= #{timeInterval[0]}
            </if>
            <if test='timeInterval != null and timeInterval[1] != null and timeInterval[1]!= ""'>
                and f_accbilldate &lt;= #{timeInterval[1]}
            </if>
        </where>
    </select>
    
    <select id="selectFTmsaccbillsById" parameterType="Long" resultMap="FTmsaccbillsResult">
        <include refid="selectFTmsaccbillsVo"/>
        where id = #{id}
    </select>

    <insert id="insertFTmsaccbills" parameterType="FTmsaccbills" useGeneratedKeys="true" keyProperty="id">
        insert into F_TMSACCBILLS
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="accYear != null">acc_year,</if>
            <if test="accMonth != null">acc_month,</if>
            <if test="actId != null">act_id,</if>
            <if test="fFromDate != null">f_from_date,</if>
            <if test="amt != null">amt,</if>
            <if test="fToDate != null">f_to_date,</if>
            <if test="refNo != null">ref_no,</if>
            <if test="postDate != null">post_date,</if>
            <if test="fAmtcr != null">f_amtcr,</if>
            <if test="fAmtdr != null">f_amtdr,</if>
            <if test="delFlag != null">del_flag,</if>
            <if test="createBy != null">create_by,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="fCtrlcorpid != null">f_ctrlcorpid,</if>
            <if test="tMblno != null">t_mblno,</if>
            <if test="fBilltype != null and fBilltype != ''">f_billtype,</if>
            <if test="fAccbilldate != null">f_accbilldate,</if>
            <if test="fRefNo != null">f_ref_no,</if>
            <if test="fBillno != null">f_billno,</if>
            <if test="fDeptid != null">f_deptid,</if>
            <if test="chargingMethod != null">charging_method,</if>
            <if test="fBillstatus != null">f_billstatus,</if>
            <if test="invoiceNo != null">invoice_no,</if>
            <if test="bank != null">bank,</if>
            <if test="waterBillNo != null">water_bill_no,</if>
            <if test="fCorpid != null">f_corpid,</if>
            <if test="fSystemType != null">f_system_type,</if>
            <if test="fRemarks != null">f_remarks,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="accYear != null">#{accYear},</if>
            <if test="accMonth != null">#{accMonth},</if>
            <if test="actId != null">#{actId},</if>
            <if test="fFromDate != null">#{fFromDate},</if>
            <if test="amt != null">#{amt},</if>
            <if test="fToDate != null">#{fToDate},</if>
            <if test="refNo != null">#{refNo},</if>
            <if test="postDate != null">#{postDate},</if>
            <if test="fAmtcr != null">#{fAmtcr},</if>
            <if test="fAmtdr != null">#{fAmtdr},</if>
            <if test="delFlag != null">#{delFlag},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="fCtrlcorpid != null">#{fCtrlcorpid},</if>
            <if test="tMblno != null">#{tMblno},</if>
            <if test="fBilltype != null and fBilltype != ''">#{fBilltype},</if>
            <if test="fAccbilldate != null">#{fAccbilldate},</if>
            <if test="fRefNo != null">#{fRefNo},</if>
            <if test="fBillno != null">#{fBillno},</if>
            <if test="fDeptid != null">#{fDeptid},</if>
            <if test="chargingMethod != null">#{chargingMethod},</if>
            <if test="fBillstatus != null">#{fBillstatus},</if>
            <if test="invoiceNo != null">#{invoiceNo},</if>
            <if test="bank != null">#{bank},</if>
            <if test="waterBillNo != null">#{waterBillNo},</if>
            <if test="fCorpid != null">#{fCorpid},</if>
            <if test="fSystemType != null">#{fSystemType},</if>
            <if test="fRemarks != null">#{fRemarks},</if>
        </trim>
    </insert>

    <update id="updateFTmsaccbills" parameterType="FTmsaccbills">
        update F_TMSACCBILLS
        <trim prefix="SET" suffixOverrides=",">
            <if test="accYear != null">acc_year = #{accYear},</if>
            <if test="accMonth != null">acc_month = #{accMonth},</if>
            <if test="actId != null">act_id = #{actId},</if>
            <if test="fFromDate != null">f_from_date = #{fFromDate},</if>
            <if test="amt != null">amt = #{amt},</if>
            <if test="fToDate != null">f_to_date = #{fToDate},</if>
            <if test="refNo != null">ref_no = #{refNo},</if>
            <if test="postDate != null">post_date = #{postDate},</if>
            <if test="fAmtcr != null">f_amtcr = #{fAmtcr},</if>
            <if test="fAmtdr != null">f_amtdr = #{fAmtdr},</if>
            <if test="delFlag != null">del_flag = #{delFlag},</if>
            <if test="createBy != null">create_by = #{createBy},</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="fCtrlcorpid != null">f_ctrlcorpid = #{fCtrlcorpid},</if>
            <if test="tMblno != null">t_mblno = #{tMblno},</if>
            <if test="fBilltype != null and fBilltype != ''">f_billtype = #{fBilltype},</if>
            <if test="fAccbilldate != null">f_accbilldate = #{fAccbilldate},</if>
            <if test="fRefNo != null">f_ref_no = #{fRefNo},</if>
            <if test="fBillno != null">f_billno = #{fBillno},</if>
            <if test="fDeptid != null">f_deptid = #{fDeptid},</if>
            <if test="chargingMethod != null">charging_method = #{chargingMethod},</if>
            <if test="fBillstatus != null">f_billstatus = #{fBillstatus},</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="fCorpid != null">f_corpid = #{fCorpid},</if>
            <if test="fSystemType != null">f_system_type = #{fSystemType},</if>
            <if test="fRemarks != null">f_remarks = #{fRemarks},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteFTmsaccbillsById" parameterType="Long">
        delete from F_TMSACCBILLS where id = #{id}
    </delete>

    <delete id="deleteFTmsaccbillsByIds" parameterType="String">
        delete from F_TMSACCBILLS where id in 
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>

    <select id="fTmsaccbillsListAccamount" resultType="java.util.Map">
        SELECT
            t.id fSrcpid,
            c.f_name fFeesName,
            car.mblno fMblno,
            car.order_no orderNo,
            car.carreg_no carregNo,
            '对账' billType,
            date_format( car.accept_date, '%Y-%m-%d' ) AS acceptDate,
            t.load_addr loadAddr,
            t.md_load_addr mdLoadAddr,
            w.f_id AS fSrcid,
            w.f_dc AS fSrcdc,
            w.f_qty fQty,
            cntr.dict_label cntrId,
            car.driver_name driverName,
            f.f_name fFeeName,
            w.f_feeid fFeeid,
            IFNULL(w.f_amount, 0) - IFNULL(w.f_accamount, 0) fAmount,
            w.f_amount fAmt
        FROM
            F_TMSORDERBILLS t
            LEFT JOIN F_TMSORDERBILLSCARS car ON car.org_id = t.id
            LEFT JOIN F_TMSORDERBILLSFEES w ON car.id = w.f_pid
            LEFT JOIN t_corps c ON w.f_corpid = c.f_id
            LEFT JOIN sys_dict_data cntr ON cntr.dict_value = car.cntr_id
            AND cntr.dict_type = 'data_cntrId'
            LEFT JOIN t_fees f ON w.f_feeid = f.f_id
        <where>
            cntr.status = '0'
            <if test="fCorpid != null  and fCorpid != ''">and t.corp_id = #{ fCorpid} </if>
            <if test="fToCorpid != null  and fToCorpid != ''">and w.f_corpid = #{ fToCorpid} </if>
            <if test="fMblno != null  and fMblno != ''">and car.mblno  like concat('%', #{ fMblno}, '%') </if>
            <if test="fStatementNo != null and fStatementNo != ''">and w.f_statement_no  like concat('%', #{ fStatementNo}, '%') </if>
            <if test='fReconciliation != null and fReconciliation != "" and fReconciliation == "1" '>
                and w.f_accamount_date IS NOT NULL
            </if>
            <if test=' fReconciliation != null and  fReconciliation != "" and  fReconciliation == "0" '>
                and w.f_accamount_date IS  NULL
            </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=" fFeeid != null  and   fFeeid != '' ">
                and  w.f_feeid in
                <foreach collection=" fFeeid" item="id" index="index" open="(" close=")" separator=",">
                    #{id}
                </foreach>
            </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=' timeInterval != null and  timeInterval[0] != null and  timeInterval[0]!= ""'>
                and t.f_bsdate &gt;= #{ timeInterval[0]}
            </if>
            <if test=' timeInterval != null and  timeInterval[1] != null and  timeInterval[1]!= ""'>
                and t.f_bsdate &lt;= #{ timeInterval[1]}
            </if>
            <if test=' timeReconci != null and  timeReconci[0] != null and  timeReconci[0]!= ""'>
                and w.f_accamount_date &gt;= #{ timeReconci[0]}
            </if>
            <if test=' timeReconci != null and  timeReconci[1] != null and  timeReconci[1]!= ""'>
                and w.f_accamount_date &lt;= #{ timeReconci[1]}
            </if>
            <if test=' acceptDateList != null and  acceptDateList[0] != null and  acceptDateList[0]!= ""'>
                and car.accept_date &gt;= #{ acceptDateList[0]}
            </if>
            <if test=' acceptDateList != null and  acceptDateList[1] != null and  acceptDateList[1]!= ""'>
                and car.accept_date &lt;= #{ acceptDateList[1]}
            </if>
            <if test=" fBusinessType != null  and  fBusinessType != '' ">
                and  w.f_business_type in
                <foreach collection=" fBusinessType" item="id" open="(" close=")" separator=",">
                    #{id}
                </foreach>
            </if>
            and  ifnull(w.f_amount, 0) - ifnull(w.f_accamount, 0) != 0
            GROUP BY  w.f_id
        </where>
    </select>

    <select id="fTmsaccbillsList" resultType="java.util.Map">
        SELECT
            w.f_id AS fSrcid,
            t.id AS fSrcpid,
            t.corp_id AS fCorpid,
            c1.f_name AS fName,
            c.f_id AS fId,
            c.f_name AS fFeesName,
            car.mblno AS fMblno,
            w.f_product_name AS fProductName,
            w.create_time AS fBsdate,
            w.act_id AS fBilltype,
            dict.dict_label AS billType,
            w.f_feeid AS fFeeid,
            w.src_bill_no AS srcBillNo,
            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
        FROM
            F_TMSORDERBILLSFEES w
            LEFT JOIN F_TMSORDERBILLSCARS car ON car.id = w.f_pid
            LEFT JOIN t_fees f ON w.f_feeid = f.f_id
            LEFT JOIN t_corps AS c ON w.f_corpid = c.f_id
            LEFT JOIN F_TMSORDERBILLS AS t ON w.org_id = t.id
            LEFT JOIN t_corps AS c1 ON t.corp_id = c1.f_id
            LEFT JOIN sys_dict_data dict ON dict.dict_value = t.bill_type
        <where>
            dict.status = '0'
            AND dict.dict_type = 'data_billType'
            <if test=" fCorpid != null  and  fCorpid != ''">and t.corp_id = #{ fCorpid} </if>
            <if test=" fToCorpid != null  and  fToCorpid != ''">and w.f_corpid = #{ fToCorpid} </if>
            <if test=" fMblno != null  and  fMblno != ''">and car.mblno  like concat('%', #{ fMblno}, '%') </if>
            <if test=" fStatementNo != null and  fStatementNo != ''">and w.f_statement_no  like concat('%', #{ fStatementNo}, '%') </if>
            <if test=' fReconciliation != null and  fReconciliation != "" and  fReconciliation == "1" '>
                and w.f_accamount_date IS NOT NULL
            </if>
            <if test=' fReconciliation != null and  fReconciliation != "" and  fReconciliation == "0" '>
                and w.f_accamount_date IS  NULL
            </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=" fFeeid != null  and   fFeeid != '' ">
                and  w.f_feeid in
                <foreach collection=" fFeeid" item="id" index="index" open="(" close=")" separator=",">
                    #{id}
                </foreach>
            </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=' timeInterval != null and  timeInterval[0] != null and  timeInterval[0]!= ""'>
                and t.f_bsdate &gt;= #{ timeInterval[0]}
            </if>
            <if test=' timeInterval != null and  timeInterval[1] != null and  timeInterval[1]!= ""'>
                and t.f_bsdate &lt;= #{ timeInterval[1]}
            </if>
            <if test=' timeReconci != null and  timeReconci[0] != null and  timeReconci[0]!= ""'>
                and w.f_stlamount_date &gt;= #{ timeReconci[0]}
            </if>
            <if test=' timeReconci != null and  timeReconci[1] != null and  timeReconci[1]!= ""'>
                and w.f_stlamount_date &lt;= #{ timeReconci[1]}
            </if>
            and  ifnull(w.f_amount, 0) - ifnull(w.f_stlamount, 0) != 0
            GROUP BY  w.f_id
        </where>
    </select>

    <select id="selectFTmsaccbillsMapList" parameterType="FTmsaccbills" resultType="Map">
        SELECT
            f.id fId,
            f.f_billno fBillno,
            f.f_corpid AS fCorpid,
            c.f_name AS fCorpidName,
            f.f_amtcr AS amt,
            '对账金额',
            f.create_by createBy,
            f.f_accbilldate fAccbilldate,
            f.create_time createTime,
            CASE
            f.f_billstatus
            WHEN '1' THEN
            '保存'
            WHEN '2' THEN
            '暂存'
            WHEN '3' THEN
            '审批驳回'
            WHEN '4' THEN
            '提交审核'
            WHEN '5' THEN
            '审核中'
            WHEN '6' THEN
            '审核完成'
            END AS fBillstatus,
            f.f_remarks fRemarks
        FROM
            F_TMSACCBILLS f
            LEFT JOIN t_corps AS c ON f.f_corpid = c.f_id
        <where>
            <if test="actId != null "> and f.act_id = #{actId}</if>
            <if test="fBilltype != null  and fBilltype != ''"> and f.f_billtype = #{fBilltype}</if>
            <if test="fCorpid != null "> and f.f_corpid = #{fCorpid}</if>
            <if test="fFromDate != null "> and f.f_from_date = #{fFromDate}</if>
            <if test="fToDate != null "> and f.f_to_date = #{fToDate}</if>
            <if test="amt != null "> and f.amt = #{amt}</if>
            <if test="refNo != null  and refNo != ''"> and f.ref_no = #{refNo}</if>
            <if test="postDate != null "> and f.post_date = #{postDate}</if>
            <if test="fBillstatus != null "> and f.f_billstatus = #{fBillstatus}</if>
            <if test="fRemarks != null  and fRemarks != ''"> and f.f_remarks = #{fRemarks}</if>
            <if test="firstMoney != null and firstMoney != 0"> and f.f_amtcr &gt;= #{firstMoney}</if>
            <if test="lastMoney != null and lastMoney != 0"> and f.f_amtcr &lt;= #{lastMoney}</if>
            <if test="fBillno != null  and fBillno != ''">and f.f_billno  like concat('%', #{ fBillno}, '%') </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>
        </where>
    </select>

</mapper>