gongchunyi
6 天以前 f34a0ddd5ddb24035e846be9f1f2dde7545e2fcc
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package com.ruoyi.projectManagement.service.impl;
 
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.Assert;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ruoyi.basic.service.CustomerFollowUpFileService;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.projectManagement.mapper.PlanMapper;
import com.ruoyi.projectManagement.mapper.PlanNodeMapper;
import com.ruoyi.projectManagement.pojo.Plan;
import com.ruoyi.projectManagement.pojo.PlanNode;
import com.ruoyi.projectManagement.service.PlanService;
import com.ruoyi.projectManagement.vo.*;
import lombok.RequiredArgsConstructor;
import org.jetbrains.annotations.Nullable;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
 
/**
 * @author buhuazhen
 * @description 项目管理计划的实现
 * @createDate 2026-03-06 15:29:26
 */
@Service
@RequiredArgsConstructor
@Transactional(readOnly = true)
public class PlanServiceImpl implements PlanService {
 
    private final PlanMapper planMapper;
 
    private final CustomerFollowUpFileService customerFollowUpFileService;
 
    private final PlanNodeMapper planNodeMapper;
 
    @Lazy
    @Autowired
    private PlanService planService;
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void savePlan(SavePlanVo savePlanVo) {
        Plan plan = BeanUtil.copyProperties(savePlanVo, Plan.class);
        // 附件处理 , 拼接
        String attachments = String.join(",", Optional.ofNullable(savePlanVo.getAttachmentIds()).orElse(Collections.emptyList()));
        plan.setAttachment(attachments);
        if (savePlanVo.getId() == null) {
            planMapper.insert(plan);
        } else {
            planMapper.updateById(plan);
        }
        planService.savePlanNode(plan.getId(), savePlanVo.getSavePlanNodeList());
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void savePlanNode(Long planId, @Nullable List<SavePlanNodeVo> savePlanNodeVos) {
        Assert.notNull(planId, () -> new ServiceException("计划ID不能为空"));
        if (CollUtil.isEmpty(savePlanNodeVos)) {
            return;
        }
 
        // 删除多余节点
        List<Long> existNodeIds = savePlanNodeVos.stream().map(SavePlanNodeVo::getId).filter(Objects::nonNull).collect(Collectors.toList());
        LambdaQueryWrapper<PlanNode> planNodeLambdaQueryWrapper = new LambdaQueryWrapper<PlanNode>()
                .select(PlanNode::getId)
                .eq(PlanNode::getProjectManagementPlanId, planId);
        if(CollUtil.isNotEmpty(existNodeIds)){
            planNodeLambdaQueryWrapper.notIn(PlanNode::getId, existNodeIds);
        }
        List<PlanNode> needDeleteNode = planNodeMapper.selectList(planNodeLambdaQueryWrapper);
 
        deletePlanNode(needDeleteNode.stream().map(PlanNode::getId).collect(Collectors.toList()));
 
        List<PlanNode> planNodes = BeanUtil.copyToList(savePlanNodeVos, PlanNode.class);
        // 设置排序索引
        IntStream.range(0, savePlanNodeVos.size()).forEach(i -> {
            planNodes.get(i).setSort(i);
            planNodes.get(i).setProjectManagementPlanId(planId);
            if (planNodes.get(i).getId() == null) {
                planNodeMapper.insert(planNodes.get(i));
            } else {
                planNodeMapper.updateById(planNodes.get(i));
            }
        });
    }
 
    private List<PlanNode> getPlanNodeByPlanId(Long planId) {
        return planNodeMapper.selectList(new LambdaQueryWrapper<PlanNode>()
                .eq(PlanNode::getIsDelete, 0)
                .eq(PlanNode::getProjectManagementPlanId, planId));
    }
 
    private List<PlanNode> getPlanNodeByPlanIds(List<Long> planIds) {
        return planNodeMapper.selectList(new LambdaQueryWrapper<PlanNode>()
                .eq(PlanNode::getIsDelete, 0)
                .in(PlanNode::getProjectManagementPlanId, planIds));
    }
 
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deletePlan(Long id) {
        planMapper.update(null,
                new LambdaUpdateWrapper<Plan>()
                        .eq(Plan::getId, id)
                        .set(Plan::getIsDelete, 1));
        planService.deletePlanNode(getPlanNodeByPlanId(id).stream().map(PlanNode::getId).collect(Collectors.toList()));
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deletePlanNode(List<Long> ids) {
        if (CollUtil.isNotEmpty(ids)) {
            planNodeMapper.update(null,
                    new LambdaUpdateWrapper<PlanNode>()
                            .in(PlanNode::getId, ids)
                            .set(PlanNode::getIsDelete, 1));
        }
    }
 
    @Override
    public IPage<PlanVo> searchPlan(SearchPlanVo searchPlanVo) {
        IPage<Plan> planIPage = planMapper.selectPlanPage(searchPlanVo);
        IPage<PlanVo> resultPage = planIPage.convert(plan -> BeanUtil.copyProperties(plan, PlanVo.class));
        // 文件获取
        customerFollowUpFileService.fillAttachment(resultPage.getRecords(), PlanVo::getAttachment, PlanVo::setAttachmentList);
        Map<Long, List<PlanNodeVo>> collect = getPlanNodeByPlanIds(resultPage.getRecords().stream().map(PlanVo::getId).collect(Collectors.toList()))
                .stream()
                .map(it -> BeanUtil.copyProperties(it, PlanNodeVo.class))
                .collect(Collectors.groupingBy(PlanNodeVo::getProjectManagementPlanId, Collectors.toList()));
        resultPage.getRecords().forEach(planVo -> planVo.setPlanNodeList(collect.getOrDefault(planVo.getId(), Collections.emptyList())));
        return resultPage;
    }
 
}