严智鑫
2025-11-13 945bc394f40d8af1072a53da9a94f24207124e6d
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
package com.northglass.service.shelf;
 
import java.util.Map;
 
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.domain.Sort.Order;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springside.modules.persistence.DynamicSpecifications;
import org.springside.modules.persistence.SearchFilter;
 
import com.northglass.entity.ShelfMessage;
import com.northglass.repository.ShelfMessageDao;
 
 
@Component
@Transactional
public class ShelfMessageService {
 
    @Autowired
    private ShelfMessageDao messageDao;
    
    private PageRequest buildPageRequest(int pageNumber, int pageSize, String sortType) {
        Order hasRead = new Order(Direction.ASC, "read");
        Order idOrder = new Order(Direction.DESC, "id");
        Sort sort = new Sort(hasRead, idOrder);
        return new PageRequest(pageNumber - 1, pageSize, sort);
    }
    
    private Specification<ShelfMessage> buildSpecification(Map<String, Object> searchParams) {
        Map<String, SearchFilter> filters = SearchFilter.parse(searchParams);
        Specification<ShelfMessage> spec = DynamicSpecifications.bySearchFilter(filters.values(), ShelfMessage.class);
        return spec;
    }
    
    public Page<ShelfMessage> get(Map<String, Object> searchParams, int pageNumber, int pageSize, String sortType) {
        PageRequest pageRequest = buildPageRequest(pageNumber, pageSize, sortType);
        Specification<ShelfMessage> spec = buildSpecification(searchParams);
        Page<ShelfMessage> data = messageDao.findAll(spec, pageRequest);
        return data;
    }
    
    public ShelfMessage save(ShelfMessage message) {
        return messageDao.save(message);
    }
}