Skip to content

Commit 95a59cf

Browse files
committed
add recruitments api
1 parent 2ab8cd5 commit 95a59cf

4 files changed

Lines changed: 182 additions & 33 deletions

File tree

.idea/workspace.xml

Lines changed: 19 additions & 20 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package com.phantoms.phantomsbackend.controller;
2+
3+
import com.phantoms.phantomsbackend.pojo.entity.primary.Recruitment;
4+
import com.phantoms.phantomsbackend.service.RecruitmentService;
5+
import org.springframework.beans.factory.annotation.Autowired;
6+
import org.springframework.data.domain.Page;
7+
import org.springframework.data.domain.PageRequest;
8+
import org.springframework.data.domain.Pageable;
9+
import org.springframework.data.domain.Sort;
10+
import org.springframework.http.ResponseEntity;
11+
import org.springframework.web.bind.annotation.*;
12+
13+
@RestController
14+
@RequestMapping("/api/recruitments")
15+
public class RecruitmentController {
16+
17+
@Autowired
18+
private RecruitmentService recruitmentService;
19+
20+
/**
21+
* 多条件组合查询招募信息
22+
*
23+
* @param name 名称(模糊)
24+
* @param description 描述(模糊)
25+
* @param homeWorld 所在世界
26+
* @param category 类别
27+
* @param minItemLevel 最低装备等级
28+
* @param isCrossWorld 是否跨服
29+
* @param datacenter 数据中心
30+
* @param page 页码,默认 0
31+
* @param size 每页大小,默认 10
32+
* @param sortBy 排序字段,默认 updatedAt
33+
* @param direction 排序方向,默认 DESC
34+
* @return 分页结果
35+
*/
36+
@GetMapping("/search")
37+
public ResponseEntity<Page<Recruitment>> searchRecruitments(
38+
@RequestParam(required = false) String name,
39+
@RequestParam(required = false) String description,
40+
@RequestParam(required = false) String homeWorld,
41+
@RequestParam(required = false) String category,
42+
@RequestParam(required = false) Integer minItemLevel,
43+
@RequestParam(required = false) Boolean isCrossWorld,
44+
@RequestParam(required = false) String datacenter,
45+
@RequestParam(defaultValue = "0") int page,
46+
@RequestParam(defaultValue = "10") int size,
47+
@RequestParam(defaultValue = "updatedAt") String sortBy,
48+
@RequestParam(defaultValue = "DESC") String direction) {
49+
50+
Sort sort = direction.equalsIgnoreCase("DESC") ?
51+
Sort.by(sortBy).descending() :
52+
Sort.by(sortBy).ascending();
53+
54+
Pageable pageable = PageRequest.of(page, size, sort);
55+
56+
Page<Recruitment> result = recruitmentService.findByConditions(
57+
name, description, homeWorld, category, minItemLevel, isCrossWorld, datacenter, pageable);
58+
59+
return ResponseEntity.ok(result);
60+
}
61+
}

src/main/java/com/phantoms/phantomsbackend/repository/primary/RecruitmentRepository.java

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,29 @@
22

33
import com.phantoms.phantomsbackend.pojo.entity.primary.Recruitment;
44
import org.springframework.data.jpa.repository.JpaRepository;
5+
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
56
import org.springframework.data.jpa.repository.Modifying;
67
import org.springframework.data.jpa.repository.Query;
78
import org.springframework.data.repository.query.Param;
9+
import org.springframework.stereotype.Repository;
810

911
// RecruitmentRepository.java
10-
// RecruitmentRepository.java
11-
public interface RecruitmentRepository extends JpaRepository<Recruitment, Long> {
12+
@Repository
13+
public interface RecruitmentRepository extends JpaRepository<Recruitment, Long>, JpaSpecificationExecutor<Recruitment> {
1214

1315
// 使用 PostgreSQL 的 ON CONFLICT
1416
@Modifying
1517
@Query(value = "INSERT INTO recruitments " +
16-
"(id, name, description, created_world, created_world_id, home_world, home_world_id, " +
17-
"category, category_id, duty, min_item_level, slots_filled, slots_available, " +
18-
"time_left, updated_at, is_cross_world, datacenter) " +
19-
"VALUES (:#{#recruitment.id}, :#{#recruitment.name}, :#{#recruitment.description}, " +
20-
":#{#recruitment.createdWorld}, :#{#recruitment.createdWorldId}, :#{#recruitment.homeWorld}, " +
21-
":#{#recruitment.homeWorldId}, :#{#recruitment.category}, :#{#recruitment.categoryId}, " +
22-
":#{#recruitment.duty}, :#{#recruitment.minItemLevel}, :#{#recruitment.slotsFilled}, " +
23-
":#{#recruitment.slotsAvailable}, :#{#recruitment.timeLeft}, :#{#recruitment.updatedAt}, " +
24-
":#{#recruitment.crossWorld}, :#{#recruitment.datacenter}) " +
25-
"ON CONFLICT (id) DO NOTHING",
26-
nativeQuery = true)
18+
"(id, name, description, created_world, created_world_id, home_world, home_world_id, " +
19+
"category, category_id, duty, min_item_level, slots_filled, slots_available, " +
20+
"time_left, updated_at, is_cross_world, datacenter) " +
21+
"VALUES (:#{#recruitment.id}, :#{#recruitment.name}, :#{#recruitment.description}, " +
22+
":#{#recruitment.createdWorld}, :#{#recruitment.createdWorldId}, :#{#recruitment.homeWorld}, " +
23+
":#{#recruitment.homeWorldId}, :#{#recruitment.category}, :#{#recruitment.categoryId}, " +
24+
":#{#recruitment.duty}, :{#recruitment.minItemLevel}, :#{#recruitment.slotsFilled}, " +
25+
":#{#recruitment.slotsAvailable}, :#{#recruitment.timeLeft}, :#{#recruitment.updatedAt}, " +
26+
":#{#recruitment.crossWorld}, :#{#recruitment.datacenter}) " +
27+
"ON CONFLICT (id) DO NOTHING",
28+
nativeQuery = true)
2729
void insertIgnore(@Param("recruitment") Recruitment recruitment);
2830
}
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
package com.phantoms.phantomsbackend.service;
2+
3+
import com.phantoms.phantomsbackend.pojo.entity.primary.Recruitment;
4+
import com.phantoms.phantomsbackend.repository.primary.RecruitmentRepository;
5+
import org.springframework.beans.factory.annotation.Autowired;
6+
import org.springframework.data.domain.Page;
7+
import org.springframework.data.domain.Pageable;
8+
import org.springframework.data.jpa.domain.Specification;
9+
import org.springframework.stereotype.Service;
10+
11+
import jakarta.persistence.criteria.Predicate;
12+
import java.util.ArrayList;
13+
import java.util.List;
14+
15+
@Service
16+
public class RecruitmentService {
17+
18+
@Autowired
19+
private RecruitmentRepository recruitmentRepository;
20+
21+
/**
22+
* 多条件组合查询,支持模糊查询 name 和 description
23+
*
24+
* @param name 名称(模糊查询)
25+
* @param description 描述(模糊查询)
26+
* @param homeWorld 所在世界
27+
* @param category 类别
28+
* @param minItemLevel 最低装备等级
29+
* @param isCrossWorld 是否跨服
30+
* @param datacenter 数据中心
31+
* @param pageable 分页参数
32+
* @return 分页结果
33+
*/
34+
public Page<Recruitment> findByConditions(String name,
35+
String description,
36+
String homeWorld,
37+
String category,
38+
Integer minItemLevel,
39+
Boolean isCrossWorld,
40+
String datacenter,
41+
Pageable pageable) {
42+
Specification<Recruitment> spec = (root, query, criteriaBuilder) -> {
43+
List<Predicate> predicates = new ArrayList<>();
44+
45+
// 名称模糊查询
46+
if (name != null && !name.trim().isEmpty()) {
47+
predicates.add(criteriaBuilder.like(
48+
criteriaBuilder.lower(root.get("name")),
49+
"%" + name.toLowerCase() + "%"
50+
));
51+
}
52+
53+
// 描述模糊查询
54+
if (description != null && !description.trim().isEmpty()) {
55+
predicates.add(criteriaBuilder.like(
56+
criteriaBuilder.lower(root.get("description")),
57+
"%" + description.toLowerCase() + "%"
58+
));
59+
}
60+
61+
// 其他精确查询条件
62+
if (homeWorld != null && !homeWorld.trim().isEmpty()) {
63+
predicates.add(criteriaBuilder.equal(root.get("homeWorld"), homeWorld));
64+
}
65+
66+
if (category != null && !category.trim().isEmpty()) {
67+
predicates.add(criteriaBuilder.equal(root.get("category"), category));
68+
}
69+
70+
if (minItemLevel != null) {
71+
predicates.add(criteriaBuilder.greaterThanOrEqualTo(root.get("minItemLevel"), minItemLevel));
72+
}
73+
74+
if (isCrossWorld != null) {
75+
predicates.add(criteriaBuilder.equal(root.get("crossWorld"), isCrossWorld));
76+
}
77+
78+
if (datacenter != null && !datacenter.trim().isEmpty()) {
79+
predicates.add(criteriaBuilder.equal(root.get("datacenter"), datacenter));
80+
}
81+
82+
return criteriaBuilder.and(predicates.toArray(new Predicate[0]));
83+
};
84+
85+
return recruitmentRepository.findAll(spec, pageable);
86+
}
87+
}

0 commit comments

Comments
 (0)