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;
/**
*
* 在库项目管理 服务实现类
*
*
* @author WuGuanFengYue
* @since 2025-10-16
*/
@Service
public class ProjectStorageServiceImpl extends ServiceImpl implements ProjectStorageService {
@Autowired
private ProjectStorageItemMapper projectStorageItemMapper;
@Override
public PageInfo pageList(ProjectStorageListQuery query) {
PageInfo pageInfo = new PageInfo<>(query.getPageNum(), query.getPageSize());
List list = this.baseMapper.pageList(query,pageInfo);
List storageIds = list.stream().map(ProjectStorage::getId).collect(Collectors.toList());
int year = LocalDate.now().getYear();
if (storageIds.isEmpty()){
return new PageInfo<>();
}
// 本年
Map> itemMaps = projectStorageItemMapper.selectList(new LambdaQueryWrapper()
.like(ProjectStorageItem::getMonth, year)
.in(ProjectStorageItem::getProjectStorageId, storageIds)).stream()
.collect(Collectors.groupingBy(ProjectStorageItem::getProjectStorageId));
// 所有
Map> itemMaps1 = projectStorageItemMapper.selectList(new LambdaQueryWrapper()
.in(ProjectStorageItem::getProjectStorageId, storageIds)).stream()
.collect(Collectors.groupingBy(ProjectStorageItem::getProjectStorageId));
// 查询
Map> itemMaps2 = projectStorageItemMapper.selectList(new LambdaQueryWrapper()
.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 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 projectStorageItems1 = itemMaps1.get(projectStorageListVO.getId());
for (ProjectStorageItem storageItem : projectStorageItems1) {
allAmount = allAmount.add(storageItem.getAmount());
}
// 本年
List 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;
}
}