liujie
2026-08-04 e9e42b621afdf6ac94f5327ee843b32b10a401b4
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package com.tourguide.modules.order.mapper;
 
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} " +
            "AND accepted_at >= CURDATE() AND accepted_at < DATE_ADD(CURDATE(), INTERVAL 1 DAY) " +
            "AND status NOT IN ('CANCELLED', 'REFUNDED')")
    GuideTodayStatsResponse selectGuideTodayStats(Long 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(*) 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 completed_at >= #{startTime} AND completed_at < #{endTime}")
    BigDecimal selectGuideEarningSum(@Param("guideId") Long guideId,
                                     @Param("startTime") LocalDateTime startTime,
                                     @Param("endTime") LocalDateTime endTime);
 
    @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 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() " +
            "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() " +
            "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() " +
            "WHERE receiving_session_id = #{sessionId} AND deleted = 0 AND status = 'PENDING_MEETING'")
    int startSessionOrders(@Param("sessionId") Long sessionId, @Param("startedAt") LocalDateTime startedAt);
}