无关风月
4 天以前 baed9b68c8cb6b693d3c010f039d2c55bf242e0e
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
86
87
88
89
90
91
92
93
94
95
96
97
package com.ruoyi.system.service.impl;
 
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.basic.PageInfo;
import com.ruoyi.system.mapper.ProjectStorageItemMapper;
import com.ruoyi.system.mapper.ProjectStorageMapper;
import com.ruoyi.system.model.ContractTemplate;
import com.ruoyi.system.model.ProjectStorage;
import com.ruoyi.system.model.ProjectStorageItem;
import com.ruoyi.system.query.ProjectStorageListQuery;
import com.ruoyi.system.query.YearTaskListQuery;
import com.ruoyi.system.service.ProjectStorageItemService;
import com.ruoyi.system.service.ProjectStorageService;
import com.ruoyi.system.vo.ProjectStorageListVO;
import com.ruoyi.system.vo.YearTaskListVO;
import io.swagger.annotations.ApiModelProperty;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
 
import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
 
/**
 * <p>
 * 在库项目管理 服务实现类
 * </p>
 *
 * @author WuGuanFengYue
 * @since 2025-10-16
 */
@Service
public class ProjectStorageServiceImpl extends ServiceImpl<ProjectStorageMapper, ProjectStorage> implements ProjectStorageService {
 
 
    @Autowired
    private ProjectStorageItemMapper projectStorageItemMapper;
    @Override
    public PageInfo<ProjectStorageListVO> pageList(ProjectStorageListQuery query) {
        PageInfo<ProjectStorageListVO> pageInfo = new PageInfo<>(query.getPageNum(), query.getPageSize());
        List<ProjectStorageListVO> list = this.baseMapper.pageList(query,pageInfo);
        List<Integer> storageIds = list.stream().map(ProjectStorage::getId).collect(Collectors.toList());
 
        int year = LocalDate.now().getYear();
        if (storageIds.isEmpty()){
            return new PageInfo<>();
        }
        // 本年
        Map<Integer, List<ProjectStorageItem>> itemMaps = projectStorageItemMapper.selectList(new LambdaQueryWrapper<ProjectStorageItem>()
                        .like(ProjectStorageItem::getMonth, year)
                        .in(ProjectStorageItem::getProjectStorageId, storageIds)).stream()
                .collect(Collectors.groupingBy(ProjectStorageItem::getProjectStorageId));
        // 所有
        Map<Integer, List<ProjectStorageItem>> itemMaps1 = projectStorageItemMapper.selectList(new LambdaQueryWrapper<ProjectStorageItem>()
                        .in(ProjectStorageItem::getProjectStorageId, storageIds)).stream()
                .collect(Collectors.groupingBy(ProjectStorageItem::getProjectStorageId));
        // 查询
        Map<Integer, List<ProjectStorageItem>> itemMaps2 = projectStorageItemMapper.selectList(new LambdaQueryWrapper<ProjectStorageItem>()
                        .like(StringUtils.hasLength(query.getYear()),ProjectStorageItem::getMonth, query.getYear())
                        .in(ProjectStorageItem::getProjectStorageId, storageIds)).stream()
                .collect(Collectors.groupingBy(ProjectStorageItem::getProjectStorageId));
 
        for (ProjectStorageListVO projectStorageListVO : list) {
             BigDecimal yearAmount          = new BigDecimal("0");
             BigDecimal allAmount           = new BigDecimal("0");
            List<ProjectStorageItem> projectStorageItems2 = itemMaps2.get(projectStorageListVO.getId());
            for (ProjectStorageItem projectStorageItem : projectStorageItems2) {
                if (StringUtils.hasLength(query.getYear())){
                        allAmount = allAmount.add(projectStorageItem.getAmount());
                        yearAmount = yearAmount.add(projectStorageItem.getAmount());
                }else{
                    // 本年所有
                    List<ProjectStorageItem> projectStorageItems1 = itemMaps1.get(projectStorageListVO.getId());
                    for (ProjectStorageItem storageItem : projectStorageItems1) {
                        allAmount = allAmount.add(storageItem.getAmount());
                    }
                    // 本年
                    List<ProjectStorageItem> projectStorageItems = itemMaps.get(projectStorageListVO.getId());
                    for (ProjectStorageItem storageItem : projectStorageItems) {
                        yearAmount = yearAmount.add(storageItem.getAmount());
                    }
 
                }
            }
            projectStorageListVO.setAllAmount(allAmount);
            projectStorageListVO.setYearAmount(yearAmount);
            projectStorageListVO.setRemainingAmount(projectStorageListVO.getTotalAmount().subtract(allAmount));
 
        }
        pageInfo.setRecords(list);
        return pageInfo;
    }
}