maven
3 天以前 5b83294fac4605b569a5a0d1c5e0f310026d10d4
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package com.ruoyi.projectManagement.service.impl.handle;
 
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.ruoyi.basic.service.CustomerFollowUpFileService;
import com.ruoyi.common.enums.IsDeleteEnum;
import com.ruoyi.common.enums.PlanStageEnum;
import com.ruoyi.projectManagement.dto.InfoStageDto;
import com.ruoyi.projectManagement.dto.PlanStageDto;
import com.ruoyi.projectManagement.dto.SaveInfoDto;
import com.ruoyi.projectManagement.mapper.InfoStageMapper;
import com.ruoyi.projectManagement.pojo.Info;
import com.ruoyi.projectManagement.pojo.InfoStage;
import com.ruoyi.projectManagement.vo.InfoStageVo;
import com.ruoyi.projectManagement.vo.SaveInfoStageVo;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
 
import javax.validation.constraints.NotNull;
import java.util.*;
import java.util.stream.Collectors;
 
/**
 * @author buhuazhen
 * @date 2026/3/11
 * @email 3038525872@qq.com
 */
@Component
@RequiredArgsConstructor
@Transactional(rollbackFor = Exception.class, readOnly = true)
public class InfoStageHandleService {
    private final InfoStageMapper infoStageMapper;
    private final InfoHandleService infoHandleService;
    private final CustomerFollowUpFileService customerFollowUpFileService;
    @Autowired
    @Lazy
    private InfoStageHandleService infoStageHandleService;
 
    @Transactional
    public void save(@NotNull SaveInfoStageVo saveInfoStageVo) {
        InfoStage infoStage = BeanUtil.copyProperties(saveInfoStageVo, InfoStage.class);
        // 附件处理
        String attachmentIds = StrUtil.join(",", Optional.ofNullable(saveInfoStageVo.getAttachmentIds()).orElse(Collections.emptyList()));
        infoStage.setAttachment(attachmentIds);
 
        if (infoStage.getId() == null) {
            infoStageMapper.insert(infoStage);
        } else {
            infoStageMapper.updateById(infoStage);
        }
 
        infoStageHandleService.syncInfoStage(infoStage.getProjectManagementInfoId());
    }
 
    /**
     * 同步项目阶段状态
     *
     * @param infoId
     */
    @Transactional
    public void syncInfoStage(@NotNull Long infoId) {
        SaveInfoDto infoById = infoHandleService.getInfoById(infoId);
        List<PlanStageDto> planStage = infoById.getPlanStage();
 
        // 获取依旧存在的阶段信息
        List<InfoStageDto> infoStageNow = getListDtoByInfoId(infoId);
 
        Map<Long, Integer> maxProgressMap =
                infoStageNow.stream()
                        .filter(e -> e.getProjectManagementPlanNodeId() != null)
                        .collect(Collectors.toMap(
                                InfoStageDto::getProjectManagementPlanNodeId,
                                e -> Optional.ofNullable(e.getProgress()).orElse(0),
                                Integer::max
                        ));
        for (PlanStageDto stage : planStage) {
            int progress = maxProgressMap.getOrDefault(stage.getId(), 0);
 
            stage.setPlanStageEnum(calcStage(progress));
        }
 
        // 进行调整
        Info info = new Info();
        info.setId(infoId);
        info.setPlanStage(planStage);
        infoHandleService.updateById(info);
    }
 
    private PlanStageEnum calcStage(int progress) {
        if (progress <= 0) {
            return PlanStageEnum.TO_BEGIN;
        } else if (progress < 100) {
            return PlanStageEnum.ON_GOING;
        }
        return PlanStageEnum.ENDED;
    }
 
    @Transactional(rollbackFor = Exception.class)
    public void deleteById(@NotNull Long id) {
        InfoStage infoStage = infoStageMapper.selectById(id);
        if (infoStage == null) {
            return;
        }
        LambdaUpdateWrapper<InfoStage> updateWrapper = new LambdaUpdateWrapper<InfoStage>();
        updateWrapper.eq(InfoStage::getId, id);
        updateWrapper.set(InfoStage::getIsDelete, IsDeleteEnum.DELETED.getCode());
        infoStageMapper.update(null, updateWrapper);
 
        infoStageHandleService.syncInfoStage(infoStage.getProjectManagementInfoId());
    }
 
    public List<InfoStageDto> getListDtoByInfoId(@NotNull Long infoId) {
        LambdaQueryWrapper<InfoStage> queryWrapper = new LambdaQueryWrapper<InfoStage>();
        queryWrapper.eq(InfoStage::getProjectManagementInfoId, infoId);
        queryWrapper.eq(InfoStage::getIsDelete, IsDeleteEnum.NOT_DELETED.getCode());
        List<InfoStageDto> infoStageDtos = BeanUtil.copyToList(infoStageMapper.selectList(queryWrapper), InfoStageDto.class);
 
        /*
         * 进行排序 先按照对应的sort进行排序,后按照创建时间排序
         * 可能会出现后部情况 比如
         * 立项 100%
         * 立项 50%   这种情况,当然也支持修改操作
         */
        SaveInfoDto infoById = infoHandleService.getInfoById(infoId);
        List<PlanStageDto> planStage = infoById.getPlanStage();
 
        // 构建 id -> sort 映射
        Map<Long, Integer> stageSortMap = planStage.stream()
                .collect(Collectors.toMap(
                        PlanStageDto::getId,
                        PlanStageDto::getSort
                ));
 
        // 排序
        infoStageDtos.sort(
                Comparator
                        // 先按阶段排序
                        .comparing((InfoStageDto dto) ->
                                stageSortMap.getOrDefault(dto.getProjectManagementPlanNodeId(), Integer.MAX_VALUE)
                        )
                        // 再按创建时间排序
                        .thenComparing(InfoStageDto::getCreateTime)
        );
        return infoStageDtos;
    }
 
    public List<InfoStageVo> getListVoByInfoId(@NotNull Long infoId) {
        List<InfoStageDto> listByInfoId = getListDtoByInfoId(infoId);
        List<InfoStageVo> infoStageVos = BeanUtil.copyToList(listByInfoId, InfoStageVo.class);
        // 处理附件
        customerFollowUpFileService.fillAttachment(infoStageVos, InfoStageVo::getAttachment, InfoStageVo::setAttachmentList);
        return infoStageVos;
    }
 
 
}