lrj
2 天以前 c61d4fe27c97d2ecc907756aa571a4ef14a7b9b6
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
package com.rongyichuang.player.service;
 
import com.rongyichuang.player.dto.response.ActivityPlayerApplicationResponse;
import jakarta.persistence.EntityManager;
import jakarta.persistence.PersistenceContext;
import org.springframework.stereotype.Service;
 
import java.util.ArrayList;
import java.util.List;
 
@Service
public class PlayerApplicationService {
 
    @PersistenceContext
    private EntityManager em;
 
    /**
     * 读取报名申请,按报名时间倒序
     * 注意:实际库表为 t_avtivity_player(拼写以库为准)
     */
    @SuppressWarnings("unchecked")
    public List<ActivityPlayerApplicationResponse> listApplications(String name, Integer page, Integer size) {
        String baseSql =
            "SELECT ap.id, p.name AS player_name, a.name AS activity_name, p.phone AS phone, ap.create_time AS apply_time, p.audit_state AS state " +
            "FROM t_avtivity_player ap " +
            "JOIN t_player p ON p.id = ap.player_id " +
            "JOIN t_activity a ON a.id = ap.activity_id ";
        String where = "";
        if (name != null && !name.isEmpty()) {
            where = "WHERE p.name LIKE CONCAT('%', :name, '%') ";
        }
        String order = "ORDER BY ap.create_time DESC ";
        String limit = "";
        if (page != null && size != null && page > 0 && size > 0) {
            int offset = (page - 1) * size;
            limit = "LIMIT " + size + " OFFSET " + offset + " ";
        }
 
        var q = em.createNativeQuery(baseSql + where + order + limit);
        if (!where.isEmpty()) {
            q.setParameter("name", name);
        }
        List<Object[]> rows = q.getResultList();
        List<ActivityPlayerApplicationResponse> list = new ArrayList<>();
        for (Object[] r : rows) {
            ActivityPlayerApplicationResponse dto = new ActivityPlayerApplicationResponse();
            dto.setId(r[0] != null ? Long.valueOf(r[0].toString()) : null); // activity_player_id
            dto.setPlayerName(r[1] != null ? r[1].toString() : "");
            dto.setActivityName(r[2] != null ? r[2].toString() : "");
            dto.setPhone(r[3] != null ? r[3].toString() : "");
            dto.setApplyTime(r[4] != null ? r[4].toString() : "");
            // 映射状态:使用 t_player.audit_state(0=未审核,1=进行中,2=已驳回,3=结束)
            dto.setState(r[5] != null ? Integer.valueOf(r[5].toString()) : 0);
            list.add(dto);
        }
        return list;
    }
}