| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244 |
- <?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.system.mapper.flash.FlashDeliveryOrderMapper">
- <resultMap id="FlashDeliveryOrderResult" type="FlashDeliveryOrder">
- <id property="id" column="id"/>
- <result property="orderNo" column="order_no"/>
- <result property="clientRequestId" column="client_request_id"/>
- <result property="userId" column="user_id"/>
- <result property="receiverUserId" column="receiver_user_id"/>
- <result property="riderId" column="rider_id"/>
- <result property="serviceType" column="service_type"/>
- <result property="deliveryType" column="delivery_type"/>
- <result property="vehicleType" column="vehicle_type"/>
- <result property="status" column="status"/>
- <!-- canAdminCancel 是服务层按站点计算的只读字段,不映射/写入数据库。 -->
- <result property="isDisplay" column="is_display"/>
- <result property="packageType" column="package_type"/>
- <result property="quantity" column="quantity"/>
- <result property="weightRange" column="weight_range"/>
- <result property="specification" column="specification"/>
- <result property="deliveryMode" column="delivery_mode"/>
- <result property="scheduledPickupStartAt" column="scheduled_pickup_start_at"/>
- <result property="scheduledPickupEndAt" column="scheduled_pickup_end_at"/>
- <result property="pinRequired" column="pin_required"/>
- <result property="deliveryPinCode" column="delivery_pin_code"/>
- <result property="pickupName" column="pickup_name"/>
- <result property="pickupPhone" column="pickup_phone"/>
- <result property="pickupAddress" column="pickup_address"/>
- <result property="pickupAddressDetail" column="pickup_address_detail"/>
- <result property="pickupCity" column="pickup_city"/>
- <result property="pickupArea" column="pickup_area"/>
- <result property="pickupHandoffMethod" column="pickup_handoff_method"/>
- <result property="pickupLongitude" column="pickup_longitude"/>
- <result property="pickupLatitude" column="pickup_latitude"/>
- <result property="deliveryName" column="delivery_name"/>
- <result property="deliveryPhone" column="delivery_phone"/>
- <result property="deliveryAddress" column="delivery_address"/>
- <result property="deliveryAddressDetail" column="delivery_address_detail"/>
- <result property="deliveryCity" column="delivery_city"/>
- <result property="deliveryArea" column="delivery_area"/>
- <result property="deliveryHandoffMethod" column="delivery_handoff_method"/>
- <result property="deliveryLongitude" column="delivery_longitude"/>
- <result property="deliveryLatitude" column="delivery_latitude"/>
- <result property="distanceMeters" column="distance_meters"/>
- <result property="distanceSource" column="distance_source"/>
- <result property="estimatedDurationSeconds" column="estimated_duration_seconds"/>
- <result property="amount" column="amount"/>
- <result property="currency" column="currency"/>
- <result property="payType" column="pay_type"/>
- <result property="paymentStatus" column="payment_status"/>
- <result property="pricingId" column="pricing_id"/>
- <result property="pricingVersion" column="pricing_version"/>
- <result property="pricingStartTime" column="pricing_start_time"/>
- <result property="pricingEndTime" column="pricing_end_time"/>
- <result property="startingDistance" column="starting_distance"/>
- <result property="startingFare" column="starting_fare"/>
- <result property="distance" column="distance"/>
- <result property="freight" column="freight"/>
- <result property="distanceFee" column="distance_fee"/>
- <result property="baseDeliveryFee" column="base_delivery_fee"/>
- <result property="urgentRate" column="urgent_rate"/>
- <result property="minimumUrgentFee" column="minimum_urgent_fee"/>
- <result property="urgentFee" column="urgent_fee"/>
- <result property="tipAmount" column="tip_amount"/>
- <result property="userNote" column="user_note"/>
- <result property="version" column="version"/>
- <result property="acceptedAt" column="accepted_at"/>
- <result property="pickedUpAt" column="picked_up_at"/>
- <result property="deliveredAt" column="delivered_at"/>
- <result property="completedAt" column="completed_at"/>
- <result property="cancelledAt" column="cancelled_at"/>
- <result property="riderStars" column="rider_stars"/>
- <result property="cancelReason" column="cancel_reason"/>
- <result property="cancelOperatorType" column="cancel_operator_type"/>
- <result property="cancelOperatorId" column="cancel_operator_id"/>
- <result property="createTime" column="create_time"/>
- <result property="updateTime" column="update_time"/>
- <result property="pickupStopCount" column="pickup_stop_count"/>
- <result property="pickedUpStopCount" column="picked_up_stop_count"/>
- <result property="currentTargetStopId" column="current_target_stop_id"/>
- <result property="problemStopId" column="problem_stop_id"/>
- <result property="handlingResult" column="handling_result"/>
- <result property="pickedGoodsDisposition" column="picked_goods_disposition"/>
- </resultMap>
- <select id="selectByUserRequestId" resultMap="FlashDeliveryOrderResult">
- select * from flash_delivery_order where user_id = #{userId} and client_request_id = #{clientRequestId} limit 1
- </select>
- <!-- 抢单必须在单条 SQL 中同时校验状态和骑手为空,避免并发查询后重复绑定。 -->
- <update id="accept">
- update flash_delivery_order
- set rider_id=#{riderId}, status='ACCEPTED', accepted_at=#{now}, update_time=#{now}, version = version + 1
- where id=#{id} and status = 'WAITING_ACCEPTANCE' and rider_id is null and is_display = true
- and (delivery_mode = 'NOW' or (scheduled_pickup_start_at <= #{now}
- and scheduled_pickup_end_at > #{now}))
- </update>
- <!-- 编辑与抢单竞争时,由同一订单行的原子条件更新决定谁先成功。 -->
- <update id="updateWaitingAddress">
- update flash_delivery_order
- set pickup_stop_count = #{order.pickupStopCount}, picked_up_stop_count = 0, current_target_stop_id = null,
- package_type = #{order.packageType}, quantity = #{order.quantity},
- weight_range = #{order.weightRange}, specification = #{order.specification},
- pickup_name = #{order.pickupName}, pickup_phone = #{order.pickupPhone},
- pickup_address = #{order.pickupAddress}, pickup_address_detail = #{order.pickupAddressDetail},
- pickup_city = #{order.pickupCity}, pickup_area = #{order.pickupArea},
- pickup_handoff_method = #{order.pickupHandoffMethod},
- pickup_longitude = #{order.pickupLongitude}, pickup_latitude = #{order.pickupLatitude},
- delivery_name = #{order.deliveryName}, delivery_phone = #{order.deliveryPhone},
- delivery_address = #{order.deliveryAddress}, delivery_address_detail = #{order.deliveryAddressDetail},
- delivery_city = #{order.deliveryCity}, delivery_area = #{order.deliveryArea},
- delivery_handoff_method = #{order.deliveryHandoffMethod},
- delivery_longitude = #{order.deliveryLongitude}, delivery_latitude = #{order.deliveryLatitude},
- receiver_user_id = #{order.receiverUserId},
- distance_meters = #{order.distanceMeters}, distance_source = #{order.distanceSource},
- estimated_duration_seconds = #{order.estimatedDurationSeconds},
- pricing_id = #{order.pricingId}, pricing_version = #{order.pricingVersion},
- pricing_start_time = #{order.pricingStartTime}, pricing_end_time = #{order.pricingEndTime},
- starting_distance = #{order.startingDistance}, starting_fare = #{order.startingFare},
- distance = #{order.distance}, freight = #{order.freight},
- urgent_rate = #{order.urgentRate}, minimum_urgent_fee = #{order.minimumUrgentFee},
- distance_fee = #{order.distanceFee}, base_delivery_fee = #{order.baseDeliveryFee},
- urgent_fee = #{order.urgentFee}, amount = #{order.amount},
- update_time = #{order.updateTime}, version = version + 1
- where id = #{order.id} and user_id = #{order.userId}
- and status = 'WAITING_ACCEPTANCE' and rider_id is null and version = #{expectedVersion}
- </update>
- <update id="updateWaitingTip">
- update flash_delivery_order
- set tip_amount = #{order.tipAmount}, amount = #{order.amount},
- update_time = #{order.updateTime}, version = version + 1
- where id = #{order.id} and user_id = #{order.userId}
- and status = 'WAITING_ACCEPTANCE' and rider_id is null and version = #{expectedVersion}
- </update>
- <!-- 骑手履约同时校验订单归属和预期状态,只有中单骑手能按顺序推进。 -->
- <update id="transitionByRider">
- update flash_delivery_order
- set status=#{nextStatus},
- <choose><when test="timeColumn == 'picked_up_at'">picked_up_at=#{now},</when><otherwise>delivered_at=#{now},</otherwise></choose>
- update_time=#{now}, version = version + 1
- where id=#{id} and rider_id=#{riderId} and status=#{expectedStatus}
- </update>
- <!-- 用户确认收货必须同时满足订单归属和已送达状态。 -->
- <update id="transitionByParticipant">
- update flash_delivery_order
- set status=#{nextStatus}, completed_at=#{now}, update_time=#{now}, version = version + 1
- where id=#{id} and (user_id=#{userId} or receiver_user_id = #{userId}) and status=#{expectedStatus}
- </update>
- <!-- 评分条件更新:仅寄件人的完成态订单、已接骑手且从未评价才成功,并发重复提交返回 0。 -->
- <update id="rateRider">
- update flash_delivery_order
- set rider_stars=#{riderStars}, update_time=#{now}
- where id=#{id} and user_id=#{userId} and status='COMPLETED'
- and rider_id is not null and rider_stars is null
- </update>
- <!-- 骑手闪送平均分只统计闪送订单内的评价,与外卖评分体系互不影响。 -->
- <select id="selectRiderAverageStars" resultType="java.lang.Double">
- select round(avg(rider_stars), 1) from flash_delivery_order
- where rider_id = #{riderId} and rider_stars is not null
- </select>
- <!-- 平台/系统完成同样使用预期状态条件,防止与用户确认并发重复成功。 -->
- <update id="transitionByStatus">
- update flash_delivery_order
- set status=#{nextStatus}, completed_at=#{now}, update_time=#{now}, version = version + 1
- where id=#{id} and status=#{expectedStatus}
- </update>
- <!-- 取消仅取件前;服务行锁和站点检查之外再加条件,防止旧入口绕过部分取件事实。 -->
- <update id="cancel">
- update flash_delivery_order
- set status='CANCELLED', cancelled_at=#{now}, cancel_reason=#{reason},
- cancel_operator_type=#{operatorType}, cancel_operator_id=#{operatorId},
- update_time=#{now}, version = version + 1
- where id=#{id} and status=#{expectedStatus}
- and status in ('WAITING_ACCEPTANCE','ACCEPTED') and picked_up_at is null
- and coalesce(picked_up_stop_count,0)=0
- and not exists (select 1 from flash_delivery_order_stop s where s.order_id=#{id}
- and s.stop_type='PICKUP' and (s.status='PICKED_UP' or s.picked_up_at is not null))
- </update>
- <!-- 定时任务只扫描达到 24 小时阈值且仍处于已送达状态的订单。 -->
- <select id="selectAutoCompletable" resultMap="FlashDeliveryOrderResult">
- select * from flash_delivery_order
- where status='DELIVERED' and delivered_at <= #{deadline}
- order by delivered_at asc limit #{limit}
- </select>
- <!-- 先锁订单再读站点,凭证、日志和状态写入全部完成后才释放锁。 -->
- <select id="selectByIdForUpdate" resultMap="FlashDeliveryOrderResult">
- select * from flash_delivery_order where id = #{id} for update
- </select>
- <!-- 显式写入空目标恢复默认顺序,避免通用updateById跳过null字段。 -->
- <update id="updateFulfilment">
- update flash_delivery_order
- set status = #{order.status}, picked_up_stop_count = #{order.pickedUpStopCount},
- current_target_stop_id = #{order.currentTargetStopId}, picked_up_at = #{order.pickedUpAt},
- update_time = #{order.updateTime}, version = version + 1
- where id = #{order.id} and rider_id = #{order.riderId}
- and status = 'ACCEPTED' and version = #{order.version}
- </update>
- <!-- 预约时段结束仍无人接单时,由任务分批收口;只读取仍可取消的待接订单。 -->
- <select id="selectExpiredScheduled" resultMap="FlashDeliveryOrderResult">
- select * from flash_delivery_order
- where status='WAITING_ACCEPTANCE' and rider_id is null and delivery_mode='SCHEDULED'
- and scheduled_pickup_end_at <= #{now}
- order by scheduled_pickup_end_at asc limit #{limit}
- </select>
- <!-- 接单、用户取消与定时取消竞争时,以此条件更新决定唯一终态。 -->
- <update id="cancelExpiredScheduled">
- update flash_delivery_order
- set status='CANCELLED', cancelled_at=#{now}, cancel_reason='SCHEDULE_EXPIRED',
- cancel_operator_type='SYSTEM', cancel_operator_id=null,
- update_time=#{now}, version = version + 1
- where id=#{id} and status='WAITING_ACCEPTANCE' and rider_id is null and delivery_mode='SCHEDULED'
- and scheduled_pickup_end_at <= #{now}
- </update>
- <!-- 预约单只有进入取件窗口后才允许向骑手开放;is_display=false 同时充当未领取标记。 -->
- <select id="selectScheduledAvailabilityCandidates" resultMap="FlashDeliveryOrderResult">
- select * from flash_delivery_order
- where status='WAITING_ACCEPTANCE' and rider_id is null and delivery_mode='SCHEDULED'
- and is_display = false and scheduled_pickup_start_at <= #{now}
- and scheduled_pickup_end_at > #{now}
- order by scheduled_pickup_start_at asc limit #{limit}
- </select>
- <!-- false -> true 的条件更新保证多实例任务只由一个实例开放并推送。 -->
- <update id="openScheduledForRiders">
- update flash_delivery_order
- set is_display = true, update_time = #{now}
- where id=#{id} and status='WAITING_ACCEPTANCE' and rider_id is null and delivery_mode='SCHEDULED'
- and is_display = false and scheduled_pickup_start_at <= #{now}
- and scheduled_pickup_end_at > #{now}
- </update>
- </mapper>
|