<?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.logistics.mapper.RptFinancialMonthSummaryMapper">
    
    <resultMap type="com.ruoyi.logistics.domain.RptFinancialMonthSummary" id="RptFinancialMonthSummaryResult">
        <result property="summaryId"    column="summary_id"    />
        <result property="summaryMonth"    column="summary_month"    />
        <result property="amount"    column="amount"    />
        <result property="payableAmount"    column="payable_amount"    />
        <result property="purchaseAmount"    column="purchase_amount"    />
        <result property="transactionCount"    column="transaction_count"    />
        <result property="status"    column="status"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateTime"    column="update_time"    />
        <result property="remark"    column="remark"    />
        <result property="delFlag"    column="del_flag"    />
        <result property="createBy"    column="create_by"    />
        <result property="updateBy"    column="update_by"    />
        <result property="deptId"    column="dept_id"    />
        <result property="deptName"    column="dept_name"    />
        <result property="userId"    column="user_id"    />
        <result property="bwDjbh"    column="bw_djbh"    />
        <result property="bwDzfpPdfUrl"    column="bw_dzfp_pdf_url"    />
        <result property="bwDzfpOfdUrl"    column="bw_dzfp_ofd_url"    />
        <result property="bwDzfpXmlUrl"    column="bw_dzfp_xml_url"    />
        <result property="kprq"    column="kprq"    />
    </resultMap>

    <sql id="selectRptFinancialMonthSummaryVo">
        select summary_id, summary_month, payable_amount, transaction_count, status, create_time, update_time, remark, del_flag, create_by, update_by, dept_id, user_id, bw_djbh, bw_dzfp_pdf_url, bw_dzfp_ofd_url, bw_dzfp_xml_url from rpt_financial_month_summary
    </sql>

    <select id="selectRptFinancialMonthSummaryList" parameterType="com.ruoyi.logistics.domain.RptFinancialMonthSummary" resultMap="RptFinancialMonthSummaryResult">
        select a.summary_id, a.summary_month, a.payable_amount, a.transaction_count, a.status, a.create_time,
        a.update_time, a.remark, a.del_flag, a.create_by, a.update_by, a.dept_id, a.user_id, a.bw_djbh,
        a.bw_dzfp_pdf_url, a.bw_dzfp_ofd_url, a.bw_dzfp_xml_url,d.dept_name from rpt_financial_month_summary a
               left join  sys_dept d on  a.dept_id=d.dept_id
        <where>  
            <if test="summaryMonth != null  and summaryMonth != ''"> and a.summary_month >= #{summaryMonth}</if>
            <if test="summaryMonthEnd != null  and summaryMonthEnd != ''"> and a.summary_month <![CDATA[ <= ]]> #{summaryMonthEnd}</if>

            <if test="payableAmount != null  and payableAmount != ''"> and a.payable_amount = #{payableAmount}</if>
            <if test="transactionCount != null  and transactionCount != ''"> a.and transaction_count = #{transactionCount}</if>
            <if test="status != null  and status != ''"> and a.status = #{status}</if>
            <if test="deptId != null  and deptId != ''"> and a.dept_id = #{deptId}</if>
            <if test="deptName != null "> and d.dept_name  like concat('%', #{deptName}, '%')</if>
            <if test="bwDjbh != null  and bwDjbh != ''"> and a.bw_djbh = #{bwDjbh}</if>
            <if test="bwDzfpPdfUrl != null  and bwDzfpPdfUrl != ''"> and a.bw_dzfp_pdf_url = #{bwDzfpPdfUrl}</if>
            <if test="bwDzfpOfdUrl != null  and bwDzfpOfdUrl != ''"> and a.bw_dzfp_ofd_url = #{bwDzfpOfdUrl}</if>
            <if test="bwDzfpXmlUrl != null  and bwDzfpXmlUrl != ''"> and a.bw_dzfp_xml_url = #{bwDzfpXmlUrl}</if>

            <!-- 数据范围过滤 -->
            ${params.dataScope}

        </where>


    </select>


    <select id="selectRptFinancialMonthSummaryListToCheck" parameterType="com.ruoyi.logistics.domain.RptFinancialMonthSummary" resultMap="RptFinancialMonthSummaryResult">
        select a.summary_id, a.summary_month, a.payable_amount, a.transaction_count, a.status, a.create_time,
        a.update_time, a.remark, a.del_flag, a.create_by, a.update_by, a.dept_id, a.user_id, a.bw_djbh,
        a.bw_dzfp_pdf_url, a.bw_dzfp_ofd_url, a.bw_dzfp_xml_url,d.dept_name from rpt_financial_month_summary a
        left join  sys_dept d on  a.dept_id=d.dept_id
        <where>
        <if test="summaryMonth != null  and summaryMonth != ''"> and a.summary_month = #{summaryMonth}</if>
        <if test="status != null  and status != ''"> and a.status = #{status}</if>
        <if test="deptId != null  and deptId != ''"> and a.dept_id = #{deptId}</if>
        </where>
    </select>




    
    <select id="selectRptFinancialMonthSummaryBySummaryId" parameterType="Long" resultMap="RptFinancialMonthSummaryResult">
        <include refid="selectRptFinancialMonthSummaryVo"/>
        where summary_id = #{summaryId}
    </select>

    <insert id="insertRptFinancialMonthSummary" parameterType="com.ruoyi.logistics.domain.RptFinancialMonthSummary" useGeneratedKeys="true" keyProperty="summaryId">
        insert into rpt_financial_month_summary
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="summaryMonth != null">summary_month,</if>
            <if test="payableAmount != null">payable_amount,</if>
            <if test="transactionCount != null">transaction_count,</if>
            <if test="status != null">status,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="remark != null">remark,</if>
            <if test="delFlag != null">del_flag,</if>
            <if test="createBy != null">create_by,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="deptId != null">dept_id,</if>
            <if test="userId != null">user_id,</if>
            <if test="bwDjbh != null">bw_djbh,</if>
            <if test="bwDzfpPdfUrl != null">bw_dzfp_pdf_url,</if>
            <if test="bwDzfpOfdUrl != null">bw_dzfp_ofd_url,</if>
            <if test="bwDzfpXmlUrl != null">bw_dzfp_xml_url,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="summaryMonth != null">#{summaryMonth},</if>
            <if test="payableAmount != null">#{payableAmount},</if>
            <if test="transactionCount != null">#{transactionCount},</if>
            <if test="status != null">#{status},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="remark != null">#{remark},</if>
            <if test="delFlag != null">#{delFlag},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="deptId != null">#{deptId},</if>
            <if test="userId != null">#{userId},</if>
            <if test="bwDjbh != null">#{bwDjbh},</if>
            <if test="bwDzfpPdfUrl != null">#{bwDzfpPdfUrl},</if>
            <if test="bwDzfpOfdUrl != null">#{bwDzfpOfdUrl},</if>
            <if test="bwDzfpXmlUrl != null">#{bwDzfpXmlUrl},</if>
         </trim>
    </insert>

    <update id="updateRptFinancialMonthSummary" parameterType="com.ruoyi.logistics.domain.RptFinancialMonthSummary">
        update rpt_financial_month_summary
        <trim prefix="SET" suffixOverrides=",">
            <if test="summaryMonth != null">summary_month = #{summaryMonth},</if>
            <if test="payableAmount != null">payable_amount = #{payableAmount},</if>
            <if test="transactionCount != null">transaction_count = #{transactionCount},</if>
            <if test="status != null">status = #{status},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="remark != null">remark = #{remark},</if>
            <if test="delFlag != null">del_flag = #{delFlag},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="deptId != null">dept_id = #{deptId},</if>
            <if test="userId != null">user_id = #{userId},</if>
            <if test="bwDjbh != null">bw_djbh = #{bwDjbh},</if>
            <if test="bwDzfpPdfUrl != null">bw_dzfp_pdf_url = #{bwDzfpPdfUrl},</if>
            <if test="bwDzfpOfdUrl != null">bw_dzfp_ofd_url = #{bwDzfpOfdUrl},</if>
            <if test="bwDzfpXmlUrl != null">bw_dzfp_xml_url = #{bwDzfpXmlUrl},</if>
            <if test="kprq != null">kprq = #{kprq},</if>
        </trim>
        where summary_id = #{summaryId}
    </update>

    <update id="updateRptFinancialMonthSummaryByInvoice" parameterType="com.ruoyi.logistics.domain.RptFinancialMonthSummary">
        update rpt_financial_month_summary
        <trim prefix="SET" suffixOverrides=",">
            <if test="summaryMonth != null">summary_month = #{summaryMonth},</if>
            <if test="payableAmount != null">payable_amount = #{payableAmount},</if>
            <if test="transactionCount != null">transaction_count = #{transactionCount},</if>
            <if test="status != null">status = #{status},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="remark != null">remark = #{remark},</if>
            <if test="delFlag != null">del_flag = #{delFlag},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="deptId != null">dept_id = #{deptId},</if>
            <if test="userId != null">user_id = #{userId},</if>
            <if test="bwDjbh != null">bw_djbh = #{bwDjbh},</if>
            <if test="bwDzfpPdfUrl != null">bw_dzfp_pdf_url = #{bwDzfpPdfUrl},</if>
            <if test="bwDzfpOfdUrl != null">bw_dzfp_ofd_url = #{bwDzfpOfdUrl},</if>
            <if test="bwDzfpXmlUrl != null">bw_dzfp_xml_url = #{bwDzfpXmlUrl},</if>
        </trim>
        where bw_djbh =  #{bwDjbh}
    </update>

    <delete id="deleteRptFinancialMonthSummaryBySummaryId" parameterType="Long">
        delete from rpt_financial_month_summary where summary_id = #{summaryId}
    </delete>

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




    <select id="selectMonthSummaryList"  parameterType="java.util.Map" resultType="java.util.HashMap"  >
        SELECT
           #{monthCode} summary_month,
            a.dept_id,
            count( DISTINCT a.external_waybill_no ) transaction_count,
            sum(
                    ifnull(adjust_amount, rate_amount  )) payable_amount,
        sum(amount)  amount,
        sum(purchase_amount) purchase_amount
        FROM
            ( SELECT * FROM biz_waybill_order WHERE order_status = 5 AND DATE_FORMAT( sign_time, '%Y.%m' )= #{monthCode}
        <if test="deptId != null and deptId !='' "> and  dept_id = #{deptId}</if>
                                              ) a
                INNER JOIN biz_waybill_cost_details b ON a.external_waybill_no = b.external_waybill_no
        WHERE
            1 = 1
        GROUP BY
            a.dept_id
    </select>




    <select id="selectMonthSummaryGroupProductList"  parameterType="java.util.Map" resultType="java.util.HashMap"  >
        SELECT
        #{monthCode} summary_month,
        a.dept_id,
        a.order_type,
        a.product_code,
        count( DISTINCT a.external_waybill_no ) transaction_count,
        sum(amount) payable_amount
        FROM
        ( SELECT * FROM biz_waybill_order WHERE order_status = 5 AND DATE_FORMAT( sign_time, '%Y.%m' )= #{monthCode}
        <if test="deptId != null and deptId != '' " > and  dept_id = #{deptId}</if>
        ) a
        INNER JOIN biz_waybill_cost_details b ON a.external_waybill_no = b.external_waybill_no
        WHERE
        b.fee_item_name='快递运费'
        <if test="waybillNo != null"> and  a.external_waybill_no = #{waybillNo}</if>
        GROUP BY
        a.dept_id,a.order_type,a.product_code
    </select>



    <insert id="insertBachRptFinancialMonthSummary" parameterType="java.util.List"  >
        INSERT INTO rpt_financial_month_summary (summary_month,amount, payable_amount,purchase_amount ,transaction_count,dept_id,status,create_time) VALUES
        <foreach collection="list" item="item" separator=",">
            (#{item.summary_month}, #{item.amount},#{item.payable_amount},#{item.purchase_amount}, #{item.transaction_count}, #{item.dept_id},'1',now())
        </foreach>
        ON DUPLICATE KEY UPDATE
        amount = VALUES(amount),
        payable_amount = VALUES(payable_amount),
        purchase_amount = VALUES(purchase_amount),
        transaction_count = VALUES(transaction_count),
        dept_id = VALUES(dept_id),
        update_time = NOW()
    </insert>
</mapper>