FlashDeliveryOrderMapper.xml 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  3. <mapper namespace="com.ruoyi.system.mapper.flash.FlashDeliveryOrderMapper">
  4. <resultMap id="FlashDeliveryOrderResult" type="FlashDeliveryOrder">
  5. <id property="id" column="id"/>
  6. <result property="orderNo" column="order_no"/>
  7. <result property="clientRequestId" column="client_request_id"/>
  8. <result property="userId" column="user_id"/>
  9. <result property="receiverUserId" column="receiver_user_id"/>
  10. <result property="riderId" column="rider_id"/>
  11. <result property="serviceType" column="service_type"/>
  12. <result property="deliveryType" column="delivery_type"/>
  13. <result property="vehicleType" column="vehicle_type"/>
  14. <result property="status" column="status"/>
  15. <!-- canAdminCancel 是服务层按站点计算的只读字段,不映射/写入数据库。 -->
  16. <result property="isDisplay" column="is_display"/>
  17. <result property="packageType" column="package_type"/>
  18. <result property="quantity" column="quantity"/>
  19. <result property="weightRange" column="weight_range"/>
  20. <result property="specification" column="specification"/>
  21. <result property="deliveryMode" column="delivery_mode"/>
  22. <result property="scheduledPickupStartAt" column="scheduled_pickup_start_at"/>
  23. <result property="scheduledPickupEndAt" column="scheduled_pickup_end_at"/>
  24. <result property="pinRequired" column="pin_required"/>
  25. <result property="deliveryPinCode" column="delivery_pin_code"/>
  26. <result property="pickupName" column="pickup_name"/>
  27. <result property="pickupPhone" column="pickup_phone"/>
  28. <result property="pickupAddress" column="pickup_address"/>
  29. <result property="pickupAddressDetail" column="pickup_address_detail"/>
  30. <result property="pickupCity" column="pickup_city"/>
  31. <result property="pickupArea" column="pickup_area"/>
  32. <result property="pickupHandoffMethod" column="pickup_handoff_method"/>
  33. <result property="pickupLongitude" column="pickup_longitude"/>
  34. <result property="pickupLatitude" column="pickup_latitude"/>
  35. <result property="deliveryName" column="delivery_name"/>
  36. <result property="deliveryPhone" column="delivery_phone"/>
  37. <result property="deliveryAddress" column="delivery_address"/>
  38. <result property="deliveryAddressDetail" column="delivery_address_detail"/>
  39. <result property="deliveryCity" column="delivery_city"/>
  40. <result property="deliveryArea" column="delivery_area"/>
  41. <result property="deliveryHandoffMethod" column="delivery_handoff_method"/>
  42. <result property="deliveryLongitude" column="delivery_longitude"/>
  43. <result property="deliveryLatitude" column="delivery_latitude"/>
  44. <result property="distanceMeters" column="distance_meters"/>
  45. <result property="distanceSource" column="distance_source"/>
  46. <result property="estimatedDurationSeconds" column="estimated_duration_seconds"/>
  47. <result property="amount" column="amount"/>
  48. <result property="currency" column="currency"/>
  49. <result property="payType" column="pay_type"/>
  50. <result property="paymentStatus" column="payment_status"/>
  51. <result property="pricingId" column="pricing_id"/>
  52. <result property="pricingVersion" column="pricing_version"/>
  53. <result property="pricingStartTime" column="pricing_start_time"/>
  54. <result property="pricingEndTime" column="pricing_end_time"/>
  55. <result property="startingDistance" column="starting_distance"/>
  56. <result property="startingFare" column="starting_fare"/>
  57. <result property="distance" column="distance"/>
  58. <result property="freight" column="freight"/>
  59. <result property="distanceFee" column="distance_fee"/>
  60. <result property="baseDeliveryFee" column="base_delivery_fee"/>
  61. <result property="urgentRate" column="urgent_rate"/>
  62. <result property="minimumUrgentFee" column="minimum_urgent_fee"/>
  63. <result property="urgentFee" column="urgent_fee"/>
  64. <result property="tipAmount" column="tip_amount"/>
  65. <result property="userNote" column="user_note"/>
  66. <result property="version" column="version"/>
  67. <result property="acceptedAt" column="accepted_at"/>
  68. <result property="pickedUpAt" column="picked_up_at"/>
  69. <result property="deliveredAt" column="delivered_at"/>
  70. <result property="completedAt" column="completed_at"/>
  71. <result property="cancelledAt" column="cancelled_at"/>
  72. <result property="riderStars" column="rider_stars"/>
  73. <result property="cancelReason" column="cancel_reason"/>
  74. <result property="cancelOperatorType" column="cancel_operator_type"/>
  75. <result property="cancelOperatorId" column="cancel_operator_id"/>
  76. <result property="createTime" column="create_time"/>
  77. <result property="updateTime" column="update_time"/>
  78. <result property="pickupStopCount" column="pickup_stop_count"/>
  79. <result property="pickedUpStopCount" column="picked_up_stop_count"/>
  80. <result property="currentTargetStopId" column="current_target_stop_id"/>
  81. <result property="problemStopId" column="problem_stop_id"/>
  82. <result property="handlingResult" column="handling_result"/>
  83. <result property="pickedGoodsDisposition" column="picked_goods_disposition"/>
  84. </resultMap>
  85. <select id="selectByUserRequestId" resultMap="FlashDeliveryOrderResult">
  86. select * from flash_delivery_order where user_id = #{userId} and client_request_id = #{clientRequestId} limit 1
  87. </select>
  88. <!-- 抢单必须在单条 SQL 中同时校验状态和骑手为空,避免并发查询后重复绑定。 -->
  89. <update id="accept">
  90. update flash_delivery_order
  91. set rider_id=#{riderId}, status='ACCEPTED', accepted_at=#{now}, update_time=#{now}, version = version + 1
  92. where id=#{id} and status = 'WAITING_ACCEPTANCE' and rider_id is null and is_display = true
  93. and (delivery_mode = 'NOW' or (scheduled_pickup_start_at &lt;= #{now}
  94. and scheduled_pickup_end_at &gt; #{now}))
  95. </update>
  96. <!-- 编辑与抢单竞争时,由同一订单行的原子条件更新决定谁先成功。 -->
  97. <update id="updateWaitingAddress">
  98. update flash_delivery_order
  99. set pickup_stop_count = #{order.pickupStopCount}, picked_up_stop_count = 0, current_target_stop_id = null,
  100. package_type = #{order.packageType}, quantity = #{order.quantity},
  101. weight_range = #{order.weightRange}, specification = #{order.specification},
  102. pickup_name = #{order.pickupName}, pickup_phone = #{order.pickupPhone},
  103. pickup_address = #{order.pickupAddress}, pickup_address_detail = #{order.pickupAddressDetail},
  104. pickup_city = #{order.pickupCity}, pickup_area = #{order.pickupArea},
  105. pickup_handoff_method = #{order.pickupHandoffMethod},
  106. pickup_longitude = #{order.pickupLongitude}, pickup_latitude = #{order.pickupLatitude},
  107. delivery_name = #{order.deliveryName}, delivery_phone = #{order.deliveryPhone},
  108. delivery_address = #{order.deliveryAddress}, delivery_address_detail = #{order.deliveryAddressDetail},
  109. delivery_city = #{order.deliveryCity}, delivery_area = #{order.deliveryArea},
  110. delivery_handoff_method = #{order.deliveryHandoffMethod},
  111. delivery_longitude = #{order.deliveryLongitude}, delivery_latitude = #{order.deliveryLatitude},
  112. receiver_user_id = #{order.receiverUserId},
  113. distance_meters = #{order.distanceMeters}, distance_source = #{order.distanceSource},
  114. estimated_duration_seconds = #{order.estimatedDurationSeconds},
  115. pricing_id = #{order.pricingId}, pricing_version = #{order.pricingVersion},
  116. pricing_start_time = #{order.pricingStartTime}, pricing_end_time = #{order.pricingEndTime},
  117. starting_distance = #{order.startingDistance}, starting_fare = #{order.startingFare},
  118. distance = #{order.distance}, freight = #{order.freight},
  119. urgent_rate = #{order.urgentRate}, minimum_urgent_fee = #{order.minimumUrgentFee},
  120. distance_fee = #{order.distanceFee}, base_delivery_fee = #{order.baseDeliveryFee},
  121. urgent_fee = #{order.urgentFee}, amount = #{order.amount},
  122. update_time = #{order.updateTime}, version = version + 1
  123. where id = #{order.id} and user_id = #{order.userId}
  124. and status = 'WAITING_ACCEPTANCE' and rider_id is null and version = #{expectedVersion}
  125. </update>
  126. <update id="updateWaitingTip">
  127. update flash_delivery_order
  128. set tip_amount = #{order.tipAmount}, amount = #{order.amount},
  129. update_time = #{order.updateTime}, version = version + 1
  130. where id = #{order.id} and user_id = #{order.userId}
  131. and status = 'WAITING_ACCEPTANCE' and rider_id is null and version = #{expectedVersion}
  132. </update>
  133. <!-- 骑手履约同时校验订单归属和预期状态,只有中单骑手能按顺序推进。 -->
  134. <update id="transitionByRider">
  135. update flash_delivery_order
  136. set status=#{nextStatus},
  137. <choose><when test="timeColumn == 'picked_up_at'">picked_up_at=#{now},</when><otherwise>delivered_at=#{now},</otherwise></choose>
  138. update_time=#{now}, version = version + 1
  139. where id=#{id} and rider_id=#{riderId} and status=#{expectedStatus}
  140. </update>
  141. <!-- 用户确认收货必须同时满足订单归属和已送达状态。 -->
  142. <update id="transitionByParticipant">
  143. update flash_delivery_order
  144. set status=#{nextStatus}, completed_at=#{now}, update_time=#{now}, version = version + 1
  145. where id=#{id} and (user_id=#{userId} or receiver_user_id = #{userId}) and status=#{expectedStatus}
  146. </update>
  147. <!-- 评分条件更新:仅寄件人的完成态订单、已接骑手且从未评价才成功,并发重复提交返回 0。 -->
  148. <update id="rateRider">
  149. update flash_delivery_order
  150. set rider_stars=#{riderStars}, update_time=#{now}
  151. where id=#{id} and user_id=#{userId} and status='COMPLETED'
  152. and rider_id is not null and rider_stars is null
  153. </update>
  154. <!-- 骑手闪送平均分只统计闪送订单内的评价,与外卖评分体系互不影响。 -->
  155. <select id="selectRiderAverageStars" resultType="java.lang.Double">
  156. select round(avg(rider_stars), 1) from flash_delivery_order
  157. where rider_id = #{riderId} and rider_stars is not null
  158. </select>
  159. <!-- 平台/系统完成同样使用预期状态条件,防止与用户确认并发重复成功。 -->
  160. <update id="transitionByStatus">
  161. update flash_delivery_order
  162. set status=#{nextStatus}, completed_at=#{now}, update_time=#{now}, version = version + 1
  163. where id=#{id} and status=#{expectedStatus}
  164. </update>
  165. <!-- 取消仅取件前;服务行锁和站点检查之外再加条件,防止旧入口绕过部分取件事实。 -->
  166. <update id="cancel">
  167. update flash_delivery_order
  168. set status='CANCELLED', cancelled_at=#{now}, cancel_reason=#{reason},
  169. cancel_operator_type=#{operatorType}, cancel_operator_id=#{operatorId},
  170. update_time=#{now}, version = version + 1
  171. where id=#{id} and status=#{expectedStatus}
  172. and status in ('WAITING_ACCEPTANCE','ACCEPTED') and picked_up_at is null
  173. and coalesce(picked_up_stop_count,0)=0
  174. and not exists (select 1 from flash_delivery_order_stop s where s.order_id=#{id}
  175. and s.stop_type='PICKUP' and (s.status='PICKED_UP' or s.picked_up_at is not null))
  176. </update>
  177. <!-- 定时任务只扫描达到 24 小时阈值且仍处于已送达状态的订单。 -->
  178. <select id="selectAutoCompletable" resultMap="FlashDeliveryOrderResult">
  179. select * from flash_delivery_order
  180. where status='DELIVERED' and delivered_at &lt;= #{deadline}
  181. order by delivered_at asc limit #{limit}
  182. </select>
  183. <!-- 先锁订单再读站点,凭证、日志和状态写入全部完成后才释放锁。 -->
  184. <select id="selectByIdForUpdate" resultMap="FlashDeliveryOrderResult">
  185. select * from flash_delivery_order where id = #{id} for update
  186. </select>
  187. <!-- 显式写入空目标恢复默认顺序,避免通用updateById跳过null字段。 -->
  188. <update id="updateFulfilment">
  189. update flash_delivery_order
  190. set status = #{order.status}, picked_up_stop_count = #{order.pickedUpStopCount},
  191. current_target_stop_id = #{order.currentTargetStopId}, picked_up_at = #{order.pickedUpAt},
  192. update_time = #{order.updateTime}, version = version + 1
  193. where id = #{order.id} and rider_id = #{order.riderId}
  194. and status = 'ACCEPTED' and version = #{order.version}
  195. </update>
  196. <!-- 预约时段结束仍无人接单时,由任务分批收口;只读取仍可取消的待接订单。 -->
  197. <select id="selectExpiredScheduled" resultMap="FlashDeliveryOrderResult">
  198. select * from flash_delivery_order
  199. where status='WAITING_ACCEPTANCE' and rider_id is null and delivery_mode='SCHEDULED'
  200. and scheduled_pickup_end_at &lt;= #{now}
  201. order by scheduled_pickup_end_at asc limit #{limit}
  202. </select>
  203. <!-- 接单、用户取消与定时取消竞争时,以此条件更新决定唯一终态。 -->
  204. <update id="cancelExpiredScheduled">
  205. update flash_delivery_order
  206. set status='CANCELLED', cancelled_at=#{now}, cancel_reason='SCHEDULE_EXPIRED',
  207. cancel_operator_type='SYSTEM', cancel_operator_id=null,
  208. update_time=#{now}, version = version + 1
  209. where id=#{id} and status='WAITING_ACCEPTANCE' and rider_id is null and delivery_mode='SCHEDULED'
  210. and scheduled_pickup_end_at &lt;= #{now}
  211. </update>
  212. <!-- 预约单只有进入取件窗口后才允许向骑手开放;is_display=false 同时充当未领取标记。 -->
  213. <select id="selectScheduledAvailabilityCandidates" resultMap="FlashDeliveryOrderResult">
  214. select * from flash_delivery_order
  215. where status='WAITING_ACCEPTANCE' and rider_id is null and delivery_mode='SCHEDULED'
  216. and is_display = false and scheduled_pickup_start_at &lt;= #{now}
  217. and scheduled_pickup_end_at &gt; #{now}
  218. order by scheduled_pickup_start_at asc limit #{limit}
  219. </select>
  220. <!-- false -> true 的条件更新保证多实例任务只由一个实例开放并推送。 -->
  221. <update id="openScheduledForRiders">
  222. update flash_delivery_order
  223. set is_display = true, update_time = #{now}
  224. where id=#{id} and status='WAITING_ACCEPTANCE' and rider_id is null and delivery_mode='SCHEDULED'
  225. and is_display = false and scheduled_pickup_start_at &lt;= #{now}
  226. and scheduled_pickup_end_at &gt; #{now}
  227. </update>
  228. </mapper>