xiangpei
2025-03-06 e7243b4e61a96249bbe17173682a243c9a591609
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
package com.ycl.service.impl;
 
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.conditions.update.LambdaUpdateChainWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ycl.common.base.Result;
import com.ycl.domain.entity.ProjectPlanExamineRecord;
import com.ycl.domain.entity.ProjectPlanInfo;
import com.ycl.domain.entity.ProjectPlanRecord;
import com.ycl.domain.form.ProjectPlanInfoForm;
import com.ycl.domain.form.ProjectPlanInfoRequestForm;
import com.ycl.domain.query.ProjectPlanInfoQuery;
import com.ycl.domain.vo.ProjectPlanInfoResponseVO;
import com.ycl.domain.vo.ProjectPlanInfoVO;
import com.ycl.framework.utils.PageUtil;
import com.ycl.mapper.ProjectPlanExamineRecordMapper;
import com.ycl.mapper.ProjectPlanInfoMapper;
import com.ycl.mapper.ProjectPlanRecordMapper;
import com.ycl.service.ProjectPlanInfoService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
 
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;
 
/**
 * 项目计划项 服务实现类
 *
 * @author lhr
 * @since 2024-11-22
 */
@Service
@RequiredArgsConstructor
public class ProjectPlanInfoServiceImpl extends ServiceImpl<ProjectPlanInfoMapper, ProjectPlanInfo> implements ProjectPlanInfoService {
 
    private final ProjectPlanInfoMapper projectPlanInfoMapper;
    private final ProjectPlanRecordMapper projectPlanRecordMapper;
    private final ProjectPlanExamineRecordMapper projectPlanExamineRecordMapper;
 
    /**
     * 添加
     * @param form
     * @return
     */
    @Override
    public Result add(ProjectPlanInfoForm form) {
        ProjectPlanInfo entity = ProjectPlanInfoForm.getEntityByForm(form, null);
        baseMapper.insert(entity);
        return Result.ok("添加成功");
    }
 
    /**
     * 修改
     * @param form
     * @return
     */
    @Override
    public Result update(ProjectPlanInfoForm form) {
        ProjectPlanInfo entity = baseMapper.selectById(form.getId());
 
        // 为空抛IllegalArgumentException,做全局异常处理
        Assert.notNull(entity, "记录不存在");
        BeanUtils.copyProperties(form, entity);
        baseMapper.updateById(entity);
        return Result.ok("修改成功");
    }
 
    /**
     * 批量删除
     * @param ids
     * @return
     */
    @Override
    public Result remove(List<String> ids) {
        baseMapper.deleteBatchIds(ids);
        return Result.ok("删除成功");
    }
 
    /**
     * id删除
     * @param id
     * @return
     */
    @Override
    public Result removeById(String id) {
        baseMapper.deleteById(id);
        return Result.ok("删除成功");
    }
 
    /**
     * 分页查询
     * @param query
     * @return
     */
    @Override
    public Result page(ProjectPlanInfoQuery query) {
        IPage<ProjectPlanInfoVO> page = PageUtil.getPage(query, ProjectPlanInfoVO.class);
        baseMapper.getPage(page, query);
        return Result.ok().data(page.getRecords()).total(page.getTotal());
    }
 
    /**
     * 根据id查找
     * @param id
     * @return
     */
    @Override
    public Result detail(Long id) {
        ProjectPlanInfoResponseVO vo = new ProjectPlanInfoResponseVO();
        vo.setList(new LambdaQueryChainWrapper<>(baseMapper).eq(ProjectPlanInfo::getProjectPlanRecordId, id).list());
        vo.setActualInvest(new LambdaQueryChainWrapper<>(projectPlanRecordMapper).eq(ProjectPlanRecord::getId, id).one().getActualInvest());
        return Result.ok().data(vo);
    }
 
    /**
     * 列表
     * @return
     */
    @Override
    public Result all() {
        List<ProjectPlanInfo> entities = baseMapper.selectList(null);
        List<ProjectPlanInfoVO> vos = entities.stream()
                .map(entity -> ProjectPlanInfoVO.getVoByEntity(entity, null))
                .collect(Collectors.toList());
        return Result.ok().data(vos);
    }
 
    @Transactional(rollbackFor = Exception.class)
    @Override
    public Result addPlanInfo(ProjectPlanInfoRequestForm form) {
        // 删除原有记录
        new LambdaUpdateChainWrapper<>(baseMapper)
                .eq(ProjectPlanInfo::getProjectPlanRecordId, form.getProjectPlanRecordId())
                .remove();
        // 批量插入新记录
        List<ProjectPlanInfo> list = new ArrayList<>();
        form.getAddList().forEach(item -> {
            ProjectPlanInfo projectPlanInfo = new ProjectPlanInfo();
            projectPlanInfo.setProjectPlanRecordId(form.getProjectPlanRecordId());
            projectPlanInfo.setTitle(item.getTitle()); // 计划项标题
            projectPlanInfo.setPlanStatus(0); // 计划项状态为未开始
            projectPlanInfo.setProgressStatus(0); // 计划项进度为未开始
            projectPlanInfo.setStartTime(item.getStartTime()); // 计划项开始时间
            projectPlanInfo.setEndTime(item.getEndTime()); // 计划项结束时间
            list.add(projectPlanInfo);
        });
        this.saveBatch(list);
        // 更新计划记录的投资,以及上报状态
        new LambdaUpdateChainWrapper<>(projectPlanRecordMapper)
                .eq(ProjectPlanRecord::getId, form.getProjectPlanRecordId())
                .set(ProjectPlanRecord::getActualInvest, form.getActualInvest())
                .set(ProjectPlanRecord::getReportStatus, 0)
                .update();
 
        // 新增一条审核记录
        for (ProjectPlanInfo i : list){
            ProjectPlanExamineRecord item = new ProjectPlanExamineRecord();
            item.setProjectPlanRecordId(form.getProjectPlanRecordId());
            item.setEventType(0); // 计划上报
            item.setProjectPlanInfoId(i.getId());
            projectPlanExamineRecordMapper.insert(item);
        }
        return Result.ok("添加成功");
    }
 
    @Override
    public Result savePlanInfo(ProjectPlanInfoForm form, Long planRecordId) {
        ProjectPlanInfo entity = new ProjectPlanInfo();
        BeanUtils.copyProperties(form, entity);
        entity.setProjectPlanRecordId(planRecordId); // 项目计划记录id
        entity.setProgressStatus(0); // 未开始
        entity.setPlanStatus(0); // 未审核
        // 判断id是否存在,存在则更新,不存在则新增
        if (entity.getId() == null) {
            baseMapper.insert(entity);
        } else {
            new LambdaUpdateChainWrapper<>(baseMapper).eq(ProjectPlanInfo::getId, entity.getId())
                    .set(ProjectPlanInfo::getTitle, entity.getTitle())
                    .set(ProjectPlanInfo::getStartTime, entity.getStartTime())
                    .set(ProjectPlanInfo::getEndTime, entity.getEndTime())
                    .update();
        }
        return Result.ok("保存成功");
    }
 
    @Transactional(rollbackFor = Exception.class)
    @Override
    public Result delayPlanInfo(ProjectPlanInfoForm request) {
        // 更改计划项时间
        new LambdaUpdateChainWrapper<>(baseMapper)
                .eq(ProjectPlanInfo::getId, request.getId())
                .set(ProjectPlanInfo::getStartTime, request.getStartTime())
                .set(ProjectPlanInfo::getEndTime, request.getEndTime())
                .update();
        // 查询出原来的审核记录
        ProjectPlanExamineRecord item = new LambdaQueryChainWrapper<>(projectPlanExamineRecordMapper)
                .eq(ProjectPlanExamineRecord::getProjectPlanInfoId, request.getId())
                .eq(ProjectPlanExamineRecord::getProjectPlanRecordId, request.getProjectPlanRecordId())
                .ne(ProjectPlanExamineRecord::getEventType, 2)
                .eq(ProjectPlanExamineRecord::getDeleted, 0) // 未删除
                .orderByDesc(ProjectPlanExamineRecord::getGmtCreate)
                .last("LIMIT 1")
                .one();
        // 更新原来的审核记录
        item.setEventType(1);
        item.setDelayStartTime(request.getStartTime());
        item.setDelayEndTime(request.getEndTime());
        projectPlanExamineRecordMapper.updateById(item);
        return Result.ok("延期成功");
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result resubmitPlanInfo(ProjectPlanInfoForm form) {
        // 更新重新上报后的内容
        new LambdaUpdateChainWrapper<>(baseMapper)
                .eq(ProjectPlanInfo::getId, form.getId())
                .set(ProjectPlanInfo::getTitle, form.getTitle())
                .set(ProjectPlanInfo::getStartTime, form.getStartTime())
                .set(ProjectPlanInfo::getEndTime, form.getEndTime())
                .set(ProjectPlanInfo::getPlanStatus, 0) // 将该计划项设置为未审核的状态
                .update();
 
        // 新增一条计划上报的审核记录
        ProjectPlanExamineRecord item = new ProjectPlanExamineRecord();
        item.setProjectPlanRecordId(form.getProjectPlanRecordId());
        item.setProjectPlanInfoId(form.getId());
        item.setEventType(0); // 计划上报
        item.setGmtCreate(new Date());
        projectPlanExamineRecordMapper.insert(item);
        return Result.ok("重新上报成功");
    }
}