| | |
| | | import com.baomidou.mybatisplus.core.mapper.BaseMapper; |
| | | import com.tourguide.modules.order.entity.TourOrder; |
| | | import com.tourguide.modules.order.dto.GuideTodayStatsResponse; |
| | | import com.tourguide.modules.order.dto.VisitorOrderStatRow; |
| | | import org.apache.ibatis.annotations.Select; |
| | | import org.apache.ibatis.annotations.Update; |
| | | import org.apache.ibatis.annotations.Param; |
| | | import java.math.BigDecimal; |
| | | import java.time.LocalDateTime; |
| | | import java.util.Collection; |
| | | import java.util.List; |
| | | |
| | | public interface TourOrderMapper extends BaseMapper<TourOrder> { |
| | | @Select("SELECT * FROM tour_order WHERE id = #{id} AND deleted = 0 FOR UPDATE") |
| | | TourOrder selectForUpdate(Long id); |
| | | |
| | | @Select("SELECT * FROM tour_order WHERE refund_order_no = #{refundOrderNo} AND deleted = 0 LIMIT 1 FOR UPDATE") |
| | | TourOrder selectByRefundOrderNoForUpdate(String refundOrderNo); |
| | | |
| | | @Select("SELECT COUNT(*) AS today_order_count, COALESCE(SUM(people_count), 0) AS today_people_count " + |
| | | "FROM tour_order WHERE deleted = 0 AND guide_id = #{guideId} " + |
| | |
| | | @Select("SELECT DISTINCT scenic_name FROM tour_order WHERE deleted = 0 AND scenic_name IS NOT NULL AND scenic_name <> '' ORDER BY scenic_name") |
| | | List<String> selectScenicOptions(); |
| | | |
| | | @Select({ |
| | | "<script>", |
| | | "SELECT visitor_id AS visitorId, COUNT(CASE WHEN status NOT IN ('PENDING_PAYMENT', 'CANCELLED', 'REFUNDED') THEN 1 END) AS orderCount, ", |
| | | "COALESCE(SUM(CASE WHEN status NOT IN ('PENDING_PAYMENT', 'CANCELLED', 'REFUNDED') ", |
| | | "THEN COALESCE(payment_amount, amount) ELSE 0 END), 0) AS totalConsumption ", |
| | | "FROM tour_order WHERE deleted = 0 AND visitor_id IN ", |
| | | "<foreach collection='visitorIds' item='visitorId' open='(' separator=',' close=')'>#{visitorId}</foreach>", |
| | | "GROUP BY visitor_id", |
| | | "</script>" |
| | | }) |
| | | List<VisitorOrderStatRow> selectVisitorOrderStats(@Param("visitorIds") Collection<Long> visitorIds); |
| | | |
| | | @Select("SELECT COALESCE(SUM(guide_earning_amount), 0) FROM tour_order " + |
| | | "WHERE deleted = 0 AND guide_id = #{guideId} AND status = 'COMPLETED' " + |
| | | "AND guide_earning_amount IS NOT NULL " + |
| | | "AND (refund_status IS NULL OR refund_status <> 'REFUNDED') " + |
| | | "AND (refund_status IS NULL OR refund_status IN ('NONE', 'REFUND_FAILED')) " + |
| | | "AND completed_at >= #{startTime} AND completed_at < #{endTime}") |
| | | BigDecimal selectGuideEarningSum(@Param("guideId") Long guideId, |
| | | @Param("startTime") LocalDateTime startTime, |
| | |
| | | @Select("SELECT DISTINCT scenic_name FROM tour_order " + |
| | | "WHERE deleted = 0 AND guide_id = #{guideId} AND status = 'COMPLETED' " + |
| | | "AND guide_earning_amount IS NOT NULL " + |
| | | "AND (refund_status IS NULL OR refund_status <> 'REFUNDED') " + |
| | | "AND (refund_status IS NULL OR refund_status IN ('NONE', 'REFUND_FAILED')) " + |
| | | "AND scenic_name IS NOT NULL AND scenic_name <> '' ORDER BY scenic_name") |
| | | List<String> selectGuideEarningScenics(Long guideId); |
| | | |
| | | @Update("UPDATE tour_order SET status = 'CANCELLED', refund_status = 'REFUNDING', updated_at = NOW() " + |
| | | @Select("SELECT id FROM tour_order WHERE deleted = 0 AND status = 'COMPLETED' " + |
| | | "AND settled_at IS NULL AND settlement_due_at IS NOT NULL " + |
| | | "AND settlement_due_at <= #{now} AND guide_id IS NOT NULL " + |
| | | "AND guide_earning_amount IS NOT NULL " + |
| | | "AND (refund_status IS NULL OR refund_status IN ('NONE', 'PARTIAL', 'REFUND_FAILED')) " + |
| | | "ORDER BY settlement_due_at, id LIMIT #{limit}") |
| | | List<Long> selectDueSettlementIds(@Param("now") LocalDateTime now, @Param("limit") int limit); |
| | | |
| | | @Update("UPDATE tour_order SET status = 'COMPLETED', completed_at = #{completedAt}, " + |
| | | "settlement_due_at = TIMESTAMPADD(DAY, COALESCE(settlement_days, 1), #{completedAt}), " + |
| | | "updated_at = NOW() WHERE guide_id = #{guideId} AND receiving_session_id = #{sessionId} " + |
| | | "AND deleted = 0 AND status = 'IN_SERVICE'") |
| | | int completeSessionOrders(@Param("guideId") Long guideId, |
| | | @Param("sessionId") Long sessionId, |
| | | @Param("completedAt") LocalDateTime completedAt); |
| | | |
| | | @Select("SELECT o.receiving_session_id FROM tour_order o " + |
| | | "INNER JOIN guide_receiving_session s ON s.id = o.receiving_session_id " + |
| | | "AND s.deleted = 0 AND s.status = 'IN_SERVICE' " + |
| | | "INNER JOIN operation_scenic_pricing p ON p.id = o.service_package_id " + |
| | | "AND p.deleted = 0 " + |
| | | "WHERE o.deleted = 0 AND o.status = 'IN_SERVICE' " + |
| | | "AND o.receiving_session_id IS NOT NULL AND o.service_started_at IS NOT NULL " + |
| | | "AND p.duration_minutes > 0 " + |
| | | "AND TIMESTAMPADD(MINUTE, p.duration_minutes, o.service_started_at) <= #{now} " + |
| | | "GROUP BY o.receiving_session_id ORDER BY MIN(o.service_started_at), o.receiving_session_id " + |
| | | "LIMIT #{limit}") |
| | | List<Long> selectDueServiceSessionIds(@Param("now") LocalDateTime now, @Param("limit") int limit); |
| | | |
| | | @Update("UPDATE tour_order SET status = 'CANCELLED', cancelled_at = COALESCE(cancelled_at, NOW()), refund_status = 'REFUNDING', updated_at = NOW() " + |
| | | "WHERE id = #{id} AND deleted = 0 AND status = 'PENDING_ACCEPT' " + |
| | | "AND payment_time IS NOT NULL AND (refund_status IS NULL OR refund_status = 'NONE')") |
| | | int claimUnacceptedOrderForRefund(Long id); |
| | | |
| | | @Update("UPDATE tour_order SET status = 'CANCELLED', updated_at = NOW() " + |
| | | @Update("UPDATE tour_order SET status = 'CANCELLED', cancelled_at = COALESCE(cancelled_at, NOW()), updated_at = NOW() " + |
| | | "WHERE id = #{id} AND deleted = 0 AND status = 'PENDING_PAYMENT'") |
| | | int cancelUnpaid(Long id); |
| | | |
| | | @Update("UPDATE tour_order SET status = 'IN_SERVICE', service_started_at = #{startedAt}, updated_at = NOW() " + |
| | | @Update("UPDATE tour_order SET status = 'IN_SERVICE', arrived_at = COALESCE(arrived_at, #{startedAt}), " + |
| | | "service_started_at = #{startedAt}, updated_at = NOW() " + |
| | | "WHERE receiving_session_id = #{sessionId} AND deleted = 0 AND status = 'PENDING_MEETING'") |
| | | int startSessionOrders(@Param("sessionId") Long sessionId, @Param("startedAt") LocalDateTime startedAt); |
| | | |
| | | @Select("SELECT COUNT(*) FROM tour_order WHERE receiving_session_id = #{sessionId} " + |
| | | "AND guide_id = #{guideId} AND deleted = 0 AND status = 'PENDING_MEETING'") |
| | | long countPendingMeetingBySession(@Param("sessionId") Long sessionId, @Param("guideId") Long guideId); |
| | | } |