<?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.BizWaybillOrderMapper">
    
    <resultMap type="com.ruoyi.logistics.domain.BizWaybillOrder" id="BizWaybillOrderResult">
        <result property="waybillId"    column="waybill_id"    />
        <result property="waybillNo"    column="waybill_no"    />
        <result property="externalWaybillNo"    column="external_waybill_no"    />
        <result property="orderType"    column="order_type"    />
        <result property="senderName"    column="sender_name"    />
        <result property="senderPhone"    column="sender_phone"    />
        <result property="senderProvince"    column="sender_province"    />
        <result property="senderCity"    column="sender_city"    />
        <result property="senderCounty"    column="sender_county"    />
        <result property="senderAddress"    column="sender_address"    />
        <result property="receiverName"    column="receiver_name"    />
        <result property="receiverPhone"    column="receiver_phone"    />
        <result property="receiverProvince"    column="receiver_province"    />
        <result property="receiverCity"    column="receiver_city"    />
        <result property="receiverCounty"    column="receiver_county"    />
        <result property="receiverAddress"    column="receiver_address"    />
        <result property="goodsName"    column="goods_name"    />
        <result property="goodsWeight"    column="goods_weight"    />
        <result property="goodsVolume"    column="goods_volume"    />
        <result property="goodsQty"    column="goods_qty"    />
        <result property="orderStatus"    column="order_status"    />
        <result property="pickupType"    column="pickup_type"    />
        <result property="sendStartTime"    column="send_start_time"    />
        <result property="sendEndTime"    column="send_end_time"    />
        <result property="pickupTime"    column="pickup_time"    />
        <result property="deliveryTime"    column="delivery_time"    />
        <result property="signTime"    column="sign_time"    />
        <result property="cancelReason"    column="cancel_reason"    />
        <result property="parentWaybillId"    column="parent_waybill_id"    />
        <result property="invoiceFlag"    column="invoice_flag"    />
        <result property="adjustFlag"    column="adjust_flag"    />
        <result property="remark"    column="remark"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateTime"    column="update_time"    />
        <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="userId"    column="user_id"    />
        <result property="productCode"    column="product_code"    />
        <result property="addedService"    column="added_service"    />
        <result property="nickName"    column="nick_name"    />
        <result property="deptName"    column="dept_name"    />
        <result property="pdfUrl" column="pdf_url" />
        <result property="totalAmount" column="total_amount" />
        <result property="batchNum" column="batch_num" />

    </resultMap>

    <sql id="selectBizWaybillOrderVo">
        select bwo.waybill_id, bwo.waybill_no, bwo.external_waybill_no, bwo.order_type, bwo.sender_name, bwo.sender_phone, bwo.sender_province, bwo.sender_city, bwo.sender_county, bwo.sender_address, bwo.receiver_name,
               bwo.receiver_phone, bwo.receiver_province, bwo.receiver_city, bwo.receiver_county, bwo.receiver_address, bwo.goods_name, bwo.goods_weight, bwo.goods_volume, bwo.goods_qty, bwo.order_status, bwo.pickup_type,
               bwo.send_start_time, bwo.send_end_time, bwo.pickup_time, bwo.delivery_time, bwo.sign_time, bwo.cancel_reason, bwo.parent_waybill_id, bwo.invoice_flag, bwo.adjust_flag, bwo.remark, bwo.create_time, bwo.update_time,
               bwo.del_flag, bwo.create_by, bwo.update_by, bwo.dept_id, bwo.user_id, bwo.product_code, bwo.added_service, u.nick_name, d.dept_name, bwo.pdf_url,
                sum(bwcd.amount) as total_amount
        from biz_waybill_order bwo
                 left join sys_user u on bwo.user_id = u.user_id
                 left join sys_dept d on bwo.dept_id = d.dept_id
                 left join biz_waybill_cost_details bwcd on bwo.waybill_id = bwcd.waybill_id
    </sql>

    <select id="selectBizWaybillOrderList" parameterType="com.ruoyi.logistics.domain.BizWaybillOrder" resultMap="BizWaybillOrderResult">
        <include refid="selectBizWaybillOrderVo"/>
        <where>  
            <if test="waybillNo != null  and waybillNo != ''"> and waybill_no = #{waybillNo}</if>
            <if test="externalWaybillNo != null  and externalWaybillNo != ''"> and bwo.external_waybill_no = #{externalWaybillNo}</if>
            <if test="orderType != null "> and order_type = #{orderType}</if>
            <if test="senderName != null  and senderName != ''"> and sender_name like concat('%', #{senderName}, '%')</if>
            <if test="senderPhone != null  and senderPhone != ''"> and sender_phone = #{senderPhone}</if>
            <if test="senderProvince != null  and senderProvince != ''"> and sender_province = #{senderProvince}</if>
            <if test="senderCity != null  and senderCity != ''"> and sender_city = #{senderCity}</if>
            <if test="senderCounty != null  and senderCounty != ''"> and sender_county = #{senderCounty}</if>
            <if test="senderAddress != null  and senderAddress != ''"> and sender_address = #{senderAddress}</if>
            <if test="receiverName != null  and receiverName != ''"> and receiver_name like concat('%', #{receiverName}, '%')</if>
            <if test="receiverPhone != null  and receiverPhone != ''"> and receiver_phone = #{receiverPhone}</if>
            <if test="receiverProvince != null  and receiverProvince != ''"> and receiver_province = #{receiverProvince}</if>
            <if test="receiverCity != null  and receiverCity != ''"> and receiver_city = #{receiverCity}</if>
            <if test="receiverCounty != null  and receiverCounty != ''"> and receiver_county = #{receiverCounty}</if>
            <if test="receiverAddress != null  and receiverAddress != ''"> and receiver_address = #{receiverAddress}</if>
            <if test="goodsName != null  and goodsName != ''"> and goods_name like concat('%', #{goodsName}, '%')</if>
            <if test="goodsWeight != null "> and goods_weight = #{goodsWeight}</if>
            <if test="goodsVolume != null "> and goods_volume = #{goodsVolume}</if>
            <if test="goodsQty != null "> and goods_qty = #{goodsQty}</if>
            <if test="orderStatus != null "> and order_status = #{orderStatus}</if>
            <if test="pickupType != null "> and pickup_type = #{pickupType}</if>
            <if test="sendStartTime != null "> and send_start_time = #{sendStartTime}</if>
            <if test="sendEndTime != null "> and send_end_time = #{sendEndTime}</if>
            <if test="pickupTime != null "> and pickup_time = #{pickupTime}</if>
            <if test="deliveryTime != null "> and delivery_time = #{deliveryTime}</if>
            <if test="signTime != null "> and sign_time = #{signTime}</if>
            <if test="cancelReason != null  and cancelReason != ''"> and cancel_reason = #{cancelReason}</if>
            <if test="parentWaybillId != null "> and parent_waybill_id = #{parentWaybillId}</if>
            <if test="invoiceFlag != null  and invoiceFlag != ''"> and invoice_flag = #{invoiceFlag}</if>
            <if test="adjustFlag != null  and adjustFlag != ''"> and adjust_flag = #{adjustFlag}</if>
            <if test="searchKeyword != null and searchKeyword != ''"> and (external_waybill_no like concat('%', #{searchKeyword}, '%') or
                sender_name like concat('%', #{searchKeyword}, '%') or
                sender_phone like concat('%', #{searchKeyword}, '%') or
                receiver_name like concat('%', #{searchKeyword}, '%') or
                receiver_phone like concat('%', #{searchKeyword}, '%'))</if>
            <if test="nickName != null and nickName != ''"> and u.nick_name like concat('%', #{nickName}, '%')</if>
            <if test="deptName != null and deptName != ''"> and d.dept_name like concat('%', #{deptName}, '%')</if>
            <if test="params.beginTime != null and params.beginTime != ''"><!-- 开始时间检索 -->
                and date_format(bwo.create_time,'%Y%m%d') &gt;= date_format(#{params.beginTime},'%Y%m%d')
            </if>
            <if test="params.endTime != null and params.endTime != ''"><!-- 结束时间检索 -->
                and date_format(bwo.create_time,'%Y%m%d') &lt;= date_format(#{params.endTime},'%Y%m%d')
            </if>
            <!-- 数据范围过滤 -->
            ${params.dataScope}
        </where>
        group by bwo.waybill_id
        order by create_time desc
    </select>
    
    <select id="selectBizWaybillOrderByWaybillId" parameterType="Long" resultMap="BizWaybillOrderResult">
        <include refid="selectBizWaybillOrderVo"/>
        where bwo.waybill_id = #{waybillId}
    </select>

    <insert id="insertBizWaybillOrder" parameterType="com.ruoyi.logistics.domain.BizWaybillOrder" useGeneratedKeys="true" keyProperty="waybillId">
        insert into biz_waybill_order
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="waybillNo != null">waybill_no,</if>
            <if test="externalWaybillNo != null">external_waybill_no,</if>
            <if test="orderType != null">order_type,</if>
            <if test="senderName != null">sender_name,</if>
            <if test="senderPhone != null">sender_phone,</if>
            <if test="senderProvince != null">sender_province,</if>
            <if test="senderCity != null">sender_city,</if>
            <if test="senderCounty != null">sender_county,</if>
            <if test="senderAddress != null">sender_address,</if>
            <if test="receiverName != null">receiver_name,</if>
            <if test="receiverPhone != null">receiver_phone,</if>
            <if test="receiverProvince != null">receiver_province,</if>
            <if test="receiverCity != null">receiver_city,</if>
            <if test="receiverCounty != null">receiver_county,</if>
            <if test="receiverAddress != null">receiver_address,</if>
            <if test="goodsName != null">goods_name,</if>
            <if test="goodsWeight != null">goods_weight,</if>
            <if test="goodsVolume != null">goods_volume,</if>
            <if test="goodsQty != null">goods_qty,</if>
            <if test="orderStatus != null">order_status,</if>
            <if test="pickupType != null">pickup_type,</if>
            <if test="sendStartTime != null">send_start_time,</if>
            <if test="sendEndTime != null">send_end_time,</if>
            <if test="pickupTime != null">pickup_time,</if>
            <if test="deliveryTime != null">delivery_time,</if>
            <if test="signTime != null">sign_time,</if>
            <if test="cancelReason != null">cancel_reason,</if>
            <if test="parentWaybillId != null">parent_waybill_id,</if>
            <if test="invoiceFlag != null">invoice_flag,</if>
            <if test="adjustFlag != null">adjust_flag,</if>
            <if test="remark != null">remark,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateTime != null">update_time,</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="productCode != null">product_code,</if>
            <if test="addedService != null">added_service,</if>
            <if test="batchNum != null">batch_num,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="waybillNo != null">#{waybillNo},</if>
            <if test="externalWaybillNo != null">#{externalWaybillNo},</if>
            <if test="orderType != null">#{orderType},</if>
            <if test="senderName != null">#{senderName},</if>
            <if test="senderPhone != null">#{senderPhone},</if>
            <if test="senderProvince != null">#{senderProvince},</if>
            <if test="senderCity != null">#{senderCity},</if>
            <if test="senderCounty != null">#{senderCounty},</if>
            <if test="senderAddress != null">#{senderAddress},</if>
            <if test="receiverName != null">#{receiverName},</if>
            <if test="receiverPhone != null">#{receiverPhone},</if>
            <if test="receiverProvince != null">#{receiverProvince},</if>
            <if test="receiverCity != null">#{receiverCity},</if>
            <if test="receiverCounty != null">#{receiverCounty},</if>
            <if test="receiverAddress != null">#{receiverAddress},</if>
            <if test="goodsName != null">#{goodsName},</if>
            <if test="goodsWeight != null">#{goodsWeight},</if>
            <if test="goodsVolume != null">#{goodsVolume},</if>
            <if test="goodsQty != null">#{goodsQty},</if>
            <if test="orderStatus != null">#{orderStatus},</if>
            <if test="pickupType != null">#{pickupType},</if>
            <if test="sendStartTime != null">#{sendStartTime},</if>
            <if test="sendEndTime != null">#{sendEndTime},</if>
            <if test="pickupTime != null">#{pickupTime},</if>
            <if test="deliveryTime != null">#{deliveryTime},</if>
            <if test="signTime != null">#{signTime},</if>
            <if test="cancelReason != null">#{cancelReason},</if>
            <if test="parentWaybillId != null">#{parentWaybillId},</if>
            <if test="invoiceFlag != null">#{invoiceFlag},</if>
            <if test="adjustFlag != null">#{adjustFlag},</if>
            <if test="remark != null">#{remark},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateTime != null">#{updateTime},</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="productCode != null">#{productCode},</if>
            <if test="addedService != null">#{addedService},</if>
            <if test="batchNum != null">#{batchNum},</if>
         </trim>
    </insert>

    <update id="updateBizWaybillOrder" parameterType="com.ruoyi.logistics.domain.BizWaybillOrder">
        update biz_waybill_order
        <trim prefix="SET" suffixOverrides=",">
            <if test="waybillNo != null">waybill_no = #{waybillNo},</if>
            <if test="externalWaybillNo != null">external_waybill_no = #{externalWaybillNo},</if>
            <if test="orderType != null">order_type = #{orderType},</if>
            <if test="senderName != null">sender_name = #{senderName},</if>
            <if test="senderPhone != null">sender_phone = #{senderPhone},</if>
            <if test="senderProvince != null">sender_province = #{senderProvince},</if>
            <if test="senderCity != null">sender_city = #{senderCity},</if>
            <if test="senderCounty != null">sender_county = #{senderCounty},</if>
            <if test="senderAddress != null">sender_address = #{senderAddress},</if>
            <if test="receiverName != null">receiver_name = #{receiverName},</if>
            <if test="receiverPhone != null">receiver_phone = #{receiverPhone},</if>
            <if test="receiverProvince != null">receiver_province = #{receiverProvince},</if>
            <if test="receiverCity != null">receiver_city = #{receiverCity},</if>
            <if test="receiverCounty != null">receiver_county = #{receiverCounty},</if>
            <if test="receiverAddress != null">receiver_address = #{receiverAddress},</if>
            <if test="goodsName != null">goods_name = #{goodsName},</if>
            <if test="goodsWeight != null">goods_weight = #{goodsWeight},</if>
            <if test="goodsVolume != null">goods_volume = #{goodsVolume},</if>
            <if test="goodsQty != null">goods_qty = #{goodsQty},</if>
            <if test="orderStatus != null">order_status = #{orderStatus},</if>
            <if test="pickupType != null">pickup_type = #{pickupType},</if>
            <if test="sendStartTime != null">send_start_time = #{sendStartTime},</if>
            <if test="sendEndTime != null">send_end_time = #{sendEndTime},</if>
            <if test="pickupTime != null">pickup_time = #{pickupTime},</if>
            <if test="deliveryTime != null">delivery_time = #{deliveryTime},</if>
            <if test="signTime != null">sign_time = #{signTime},</if>
            <if test="cancelReason != null">cancel_reason = #{cancelReason},</if>
            <if test="parentWaybillId != null">parent_waybill_id = #{parentWaybillId},</if>
            <if test="invoiceFlag != null">invoice_flag = #{invoiceFlag},</if>
            <if test="adjustFlag != null">adjust_flag = #{adjustFlag},</if>
            <if test="remark != null">remark = #{remark},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateTime != null">update_time = #{updateTime},</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="productCode != null">product_code = #{productCode},</if>
            <if test="addedService != null">added_service = #{addedService},</if>
            <if test="pdfUrl != null">pdf_url = #{pdfUrl},</if>
        </trim>
        where waybill_id = #{waybillId}
    </update>

    <delete id="deleteBizWaybillOrderByWaybillId" parameterType="Long">
        delete from biz_waybill_order where waybill_id = #{waybillId}
    </delete>

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

    <select id="selectBizWaybillOrderByQuery" parameterType="com.ruoyi.logistics.domain.BizWaybillOrder" resultMap="BizWaybillOrderResult">
        <include refid="selectBizWaybillOrderVo"/>
        <where>
            <if test="waybillNo != null  and waybillNo != ''"> and waybill_no = #{waybillNo}</if>
            <if test="externalWaybillNo != null  and externalWaybillNo != ''"> and external_waybill_no = #{externalWaybillNo}</if>
            <if test="orderType != null "> and order_type = #{orderType}</if>
            <if test="senderName != null  and senderName != ''"> and sender_name like concat('%', #{senderName}, '%')</if>
            <if test="senderPhone != null  and senderPhone != ''"> and sender_phone = #{senderPhone}</if>
            <if test="senderProvince != null  and senderProvince != ''"> and sender_province = #{senderProvince}</if>
            <if test="senderCity != null  and senderCity != ''"> and sender_city = #{senderCity}</if>
            <if test="senderCounty != null  and senderCounty != ''"> and sender_county = #{senderCounty}</if>
            <if test="senderAddress != null  and senderAddress != ''"> and sender_address = #{senderAddress}</if>
            <if test="receiverName != null  and receiverName != ''"> and receiver_name like concat('%', #{receiverName}, '%')</if>
            <if test="receiverPhone != null  and receiverPhone != ''"> and receiver_phone = #{receiverPhone}</if>
            <if test="receiverProvince != null  and receiverProvince != ''"> and receiver_province = #{receiverProvince}</if>
            <if test="receiverCity != null  and receiverCity != ''"> and receiver_city = #{receiverCity}</if>
            <if test="receiverCounty != null  and receiverCounty != ''"> and receiver_county = #{receiverCounty}</if>
            <if test="receiverAddress != null  and receiverAddress != ''"> and receiver_address = #{receiverAddress}</if>
            <if test="goodsName != null  and goodsName != ''"> and goods_name like concat('%', #{goodsName}, '%')</if>
            <if test="goodsWeight != null "> and goods_weight = #{goodsWeight}</if>
            <if test="goodsVolume != null "> and goods_volume = #{goodsVolume}</if>
            <if test="goodsQty != null "> and goods_qty = #{goodsQty}</if>
            <if test="orderStatus != null "> and order_status = #{orderStatus}</if>
            <if test="pickupType != null "> and pickup_type = #{pickupType}</if>
            <if test="sendStartTime != null "> and send_start_time = #{sendStartTime}</if>
            <if test="sendEndTime != null "> and send_end_time = #{sendEndTime}</if>
            <if test="pickupTime != null "> and pickup_time = #{pickupTime}</if>
            <if test="deliveryTime != null "> and delivery_time = #{deliveryTime}</if>
            <if test="signTime != null "> and sign_time = #{signTime}</if>
            <if test="cancelReason != null  and cancelReason != ''"> and cancel_reason = #{cancelReason}</if>
            <if test="parentWaybillId != null "> and parent_waybill_id = #{parentWaybillId}</if>
            <if test="invoiceFlag != null  and invoiceFlag != ''"> and invoice_flag = #{invoiceFlag}</if>
            <if test="adjustFlag != null  and adjustFlag != ''"> and adjust_flag = #{adjustFlag}</if>
            <if test="deptId != null "> and dept_id = #{deptId}</if>
            <if test="userId != null "> and user_id = #{userId}</if>
        </where>
    </select>



    <update id="updateBizWaybillOrderState" parameterType="com.ruoyi.logistics.domain.BizWaybillOrder">
        update biz_waybill_order
        <trim prefix="SET" suffixOverrides=",">
            <if test="waybillNo != null">waybill_no = #{waybillNo},</if>
            <if test="orderType != null">order_type = #{orderType},</if>
            <if test="senderName != null">sender_name = #{senderName},</if>
            <if test="senderPhone != null">sender_phone = #{senderPhone},</if>
            <if test="senderProvince != null">sender_province = #{senderProvince},</if>
            <if test="senderCity != null">sender_city = #{senderCity},</if>
            <if test="senderCounty != null">sender_county = #{senderCounty},</if>
            <if test="senderAddress != null">sender_address = #{senderAddress},</if>
            <if test="receiverName != null">receiver_name = #{receiverName},</if>
            <if test="receiverPhone != null">receiver_phone = #{receiverPhone},</if>
            <if test="receiverProvince != null">receiver_province = #{receiverProvince},</if>
            <if test="receiverCity != null">receiver_city = #{receiverCity},</if>
            <if test="receiverCounty != null">receiver_county = #{receiverCounty},</if>
            <if test="receiverAddress != null">receiver_address = #{receiverAddress},</if>
            <if test="goodsName != null">goods_name = #{goodsName},</if>
            <if test="goodsWeight != null">goods_weight = #{goodsWeight},</if>
            <if test="goodsVolume != null">goods_volume = #{goodsVolume},</if>
            <if test="goodsQty != null">goods_qty = #{goodsQty},</if>
            <if test="orderStatus != null">order_status = #{orderStatus},</if>
            <if test="pickupType != null">pickup_type = #{pickupType},</if>
            <if test="sendStartTime != null">send_start_time = #{sendStartTime},</if>
            <if test="pickupTime != null">pickup_time = #{pickupTime},</if>
            <if test="deliveryTime != null">delivery_time = #{deliveryTime},</if>
            <if test="signTime != null">sign_time = #{signTime},</if>
            <if test="cancelReason != null">cancel_reason = #{cancelReason},</if>
            <if test="parentWaybillId != null">parent_waybill_id = #{parentWaybillId},</if>
            <if test="invoiceFlag != null">invoice_flag = #{invoiceFlag},</if>
            <if test="adjustFlag != null">adjust_flag = #{adjustFlag},</if>
            <if test="remark != null">remark = #{remark},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateTime != null">update_time = #{updateTime},</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="productCode != null">product_code = #{productCode},</if>
            <if test="addedService != null">added_service = #{addedService},</if>
        </trim>
        where external_waybill_no = #{externalWaybillNo}
    </update>

    <select id="selectTotalOrdersByOrderType" parameterType="BizWaybillOrder" resultType="com.ruoyi.logistics.domain.dto.OrderStatisticsDto">
        SELECT
            COALESCE(SUM(CASE WHEN bwo.order_type = 1 THEN 1 ELSE 0 END), 0) AS jdOrderCount,
            COALESCE(SUM(CASE WHEN bwo.order_type = 2 THEN 1 ELSE 0 END), 0) AS sfOrderCount,
            COALESCE(COUNT(1), 0) AS totalOrderCount
        FROM biz_waybill_order bwo
        left join sys_user u on bwo.user_id = u.user_id
        left join sys_dept d on bwo.dept_id = d.dept_id
        <where>
            bwo.del_flag = '0'
            AND bwo.order_status != 6  <!-- 排除已取消的订单 -->
            <!-- 数据范围过滤 -->
            ${params.dataScope}
        </where>
    </select>

    <select id="selectLast7DaysOrderStatisticsByDateRange" resultType="com.ruoyi.logistics.domain.dto.DailyOrderStatisticsDto" parameterType="map">
        SELECT
            DATE(bwo.create_time) AS statisticsDate,
            COALESCE(SUM(CASE WHEN bwo.order_type = 1 THEN 1 ELSE 0 END), 0) AS jdOrderCount,
            COALESCE(SUM(CASE WHEN bwo.order_type = 2 THEN 1 ELSE 0 END), 0) AS sfOrderCount,
            COALESCE(COUNT(1), 0) AS totalOrderCount
        FROM biz_waybill_order bwo
        left join sys_user u on bwo.user_id = u.user_id
        left join sys_dept d on bwo.dept_id = d.dept_id
        <where>
            bwo.del_flag = '0'
            AND bwo.order_status != 6  <!-- 排除已取消的订单 -->
            AND DATE(bwo.create_time) BETWEEN #{startDate} AND #{endDate}
            <!-- 数据范围过滤 -->
            ${params.dataScope}
        </where>
        GROUP BY DATE(bwo.create_time)
        ORDER BY DATE(bwo.create_time) ASC
    </select>

    <update id="canceleBizWaybillOrder" parameterType="com.ruoyi.logistics.domain.BizWaybillOrder">
        update biz_waybill_order
        <trim prefix="SET" suffixOverrides=",">
            <if test="waybillNo != null">waybill_no = #{waybillNo},</if>
            <if test="externalWaybillNo != null">external_waybill_no = #{externalWaybillNo},</if>
            <if test="orderType != null">order_type = #{orderType},</if>
            <if test="senderName != null">sender_name = #{senderName},</if>
            <if test="senderPhone != null">sender_phone = #{senderPhone},</if>
            <if test="senderProvince != null">sender_province = #{senderProvince},</if>
            <if test="senderCity != null">sender_city = #{senderCity},</if>
            <if test="senderCounty != null">sender_county = #{senderCounty},</if>
            <if test="senderAddress != null">sender_address = #{senderAddress},</if>
            <if test="receiverName != null">receiver_name = #{receiverName},</if>
            <if test="receiverPhone != null">receiver_phone = #{receiverPhone},</if>
            <if test="receiverProvince != null">receiver_province = #{receiverProvince},</if>
            <if test="receiverCity != null">receiver_city = #{receiverCity},</if>
            <if test="receiverCounty != null">receiver_county = #{receiverCounty},</if>
            <if test="receiverAddress != null">receiver_address = #{receiverAddress},</if>
            <if test="goodsName != null">goods_name = #{goodsName},</if>
            <if test="goodsWeight != null">goods_weight = #{goodsWeight},</if>
            <if test="goodsVolume != null">goods_volume = #{goodsVolume},</if>
            <if test="goodsQty != null">goods_qty = #{goodsQty},</if>
            <if test="orderStatus != null">order_status = #{orderStatus},</if>
            <if test="pickupType != null">pickup_type = #{pickupType},</if>
            <if test="sendStartTime != null">send_start_time = #{sendStartTime},</if>
            <if test="sendEndTime != null">send_end_time = #{sendEndTime},</if>
            <if test="pickupTime != null">pickup_time = #{pickupTime},</if>
            <if test="deliveryTime != null">delivery_time = #{deliveryTime},</if>
            <if test="signTime != null">sign_time = #{signTime},</if>
            <if test="cancelReason != null">cancel_reason = #{cancelReason},</if>
            <if test="parentWaybillId != null">parent_waybill_id = #{parentWaybillId},</if>
            <if test="invoiceFlag != null">invoice_flag = #{invoiceFlag},</if>
            <if test="adjustFlag != null">adjust_flag = #{adjustFlag},</if>
            <if test="remark != null">remark = #{remark},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateTime != null">update_time = #{updateTime},</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="productCode != null">product_code = #{productCode},</if>
            <if test="addedService != null">added_service = #{addedService},</if>
        </trim>
        where waybill_id = #{waybillId}
    </update>






    <update id="updateRateByMonthAndDept" parameterType="java.util.Map">
        UPDATE biz_waybill_cost_details
        SET
            rate_amount = ROUND(amount * #{rate} / 10, 2),
            rate = #{rate}

        WHERE
            external_waybill_no in (
              select   external_waybill_no from   biz_waybill_order where order_type= #{companyType}  and product_code= #{productCode}  and order_status=5
               AND DATE_FORMAT( sign_time, '%Y.%m' )= #{monthCode} and dept_id= #{deptId}
                )
           and  fee_item_name='快递运费'

    </update>
</mapper>