“djh”
2024-12-04 d1549b8445c65a6775cf1ba093f5040ace0f87e7
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package com.gkhy.huataiFourierSpecialGasMonitor.service.impl;
 
import com.gkhy.huataiFourierSpecialGasMonitor.commons.domain.Result;
import com.gkhy.huataiFourierSpecialGasMonitor.commons.domain.SearchResult;
import com.gkhy.huataiFourierSpecialGasMonitor.commons.enums.ResultCode;
import com.gkhy.huataiFourierSpecialGasMonitor.commons.exception.BusinessException;
import com.gkhy.huataiFourierSpecialGasMonitor.commons.model.PageQuery;
import com.gkhy.huataiFourierSpecialGasMonitor.entity.MonitorDailyReport;
import com.gkhy.huataiFourierSpecialGasMonitor.entity.query.FindDailyReportPageQuery;
import com.gkhy.huataiFourierSpecialGasMonitor.entity.resp.FindDailyReportPageRespDTO;
import com.gkhy.huataiFourierSpecialGasMonitor.repository.MonitorDailyReportRepository;
import com.gkhy.huataiFourierSpecialGasMonitor.service.MonitorDailyReportService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
 
import javax.persistence.criteria.*;
import java.time.LocalDate;
import java.time.temporal.TemporalAdjusters;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
 
/**
 * @author Mr.huang
 * @decription
 * @date 2023/8/9 15:44
 */
@Service
public class MonitorDailyReportServiceImpl implements MonitorDailyReportService {
 
    @Autowired
    private MonitorDailyReportRepository monitorDailyReportRepository;
 
    @Override
    public Result findDailyReportPage(PageQuery<FindDailyReportPageQuery> pageQuery) {
        if (pageQuery == null || pageQuery.getPageIndex() == null || pageQuery.getPageSize() == null){
            throw new BusinessException(this.getClass(), ResultCode.PARAM_ERROR_NULL,"分页参数不能为空");
        }
        Pageable pageable = PageRequest.of(pageQuery.getPageIndex()-1, pageQuery.getPageSize(), Sort.Direction.DESC, "gmtCreate");
        Specification<MonitorDailyReport> specification = new Specification<MonitorDailyReport>() {
            @Override
            public Predicate toPredicate(Root root, CriteriaQuery query, CriteriaBuilder criteriaBuilder) {
                Set<Predicate> predicateList = new HashSet<>();
                FindDailyReportPageQuery searchParams = pageQuery.getSearchParams();
                if (searchParams != null && searchParams.getYear() != null && searchParams.getMonth() != null){
                    // 获取指定月份的第一天
                    LocalDate firstDayOfMonth = LocalDate.of(searchParams.getYear(), searchParams.getMonth(), 1);
                    // 获取指定月份的最后一天
                    LocalDate lastDayOfMonth = LocalDate.of(searchParams.getYear(), searchParams.getMonth(), 1).with(TemporalAdjusters.lastDayOfMonth());
                    predicateList.add(criteriaBuilder.between(root.get("gmtCreate").as(LocalDate.class),firstDayOfMonth,lastDayOfMonth));
                }
                return criteriaBuilder.and(predicateList.toArray(new Predicate[predicateList.size()]));
            }
        };
        SearchResult<List<FindDailyReportPageRespDTO>> searchResult = new SearchResult<>();
        searchResult.setPageIndex(pageQuery.getPageIndex());
        searchResult.setPageSize(pageQuery.getPageSize());
        searchResult.setSuccess();
        Page<MonitorDailyReport> pageResult = monitorDailyReportRepository.findAll(specification,pageable);
        searchResult.setTotal(pageResult.getTotalElements());
        searchResult.setPages(pageResult.getTotalPages());
        if (!CollectionUtils.isEmpty(pageResult.getContent())){
            List<FindDailyReportPageRespDTO> dtos = pageResult.getContent().stream().map(monitorDailyReport -> {
                FindDailyReportPageRespDTO dto = new FindDailyReportPageRespDTO();
                BeanUtils.copyProperties(monitorDailyReport, dto);
                return dto;
            }).collect(Collectors.toList());
            searchResult.setData(dtos);
        }
        return searchResult;
    }
 
    @Override
    public MonitorDailyReport save(MonitorDailyReport report) {
        return monitorDailyReportRepository.save(report);
    }
}