zxl
2025-03-21 c4ab6a24d2825f11a0de0f165667dc533c458a01
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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
package com.ycl.service.impl;
 
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.conditions.update.LambdaUpdateChainWrapper;
import com.ycl.common.base.Result;
import com.ycl.common.enums.business.ProjectCategoryEnum;
import com.ycl.common.enums.business.ProjectStatusEnum;
import com.ycl.common.enums.business.ProjectTypeEnum;
import com.ycl.common.utils.DateUtils;
import com.ycl.domain.entity.Plan;
import com.ycl.domain.vo.ProjectPlanResponseVO;
import com.ycl.domain.vo.ProjetPlanRecordItem;
import com.ycl.framework.utils.PageUtil;
import com.ycl.mapper.PlanMapper;
import com.ycl.mapper.ProjectPlanRecordMapper;
import com.ycl.service.PlanService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ycl.domain.form.PlanForm;
import com.ycl.domain.vo.PlanVO;
import com.ycl.domain.query.PlanQuery;
import org.springframework.stereotype.Service;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.util.Assert;
 
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
 
/**
 * 项目计划表 服务实现类
 *
 * @author lhr
 * @since 2024-11-22
 */
@Service
@RequiredArgsConstructor
public class PlanServiceImpl extends ServiceImpl<PlanMapper, Plan> implements PlanService {
 
    private final PlanMapper planMapper;
    private final ProjectPlanRecordServiceImpl projectPlanRecordServiceImpl;
    private final ProjectPlanRecordMapper projectPlanRecordMapper;
 
    private static final Integer MONTH_FLAG = 0;
    private static final Integer SEASON_FLAG = 1;
    private static final Integer YEAR_FLAG = 2;
 
    /**
     * 添加
     * @param form
     * @return
     */
    @Override
    public Result add(PlanForm form) {
        Plan entity = PlanForm.getEntityByForm(form, null);
        baseMapper.insert(entity);
        return Result.ok("添加成功");
    }
 
    /**
     * 修改
     * @param form
     * @return
     */
    @Override
    public Result update(PlanForm form) {
        Plan 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(PlanQuery query) {
        IPage<ProjectPlanResponseVO> page = PageUtil.getPage(query, ProjectPlanResponseVO.class);
        baseMapper.getPage(query, page);
        // 对分页后的属性进行处理
        List<ProjectPlanResponseVO> records = page.getRecords();
        for (ProjectPlanResponseVO record : records) {
            record.setProjectType(ProjectTypeEnum.getDescByType(record.getProjectType()));
            record.setProjectStatus(ProjectStatusEnum.getDescByType(record.getProjectStatus()));
            record.setProjectColorCode("green");
        }
        updateException(records);
        return Result.ok().data(page.getRecords()).total(page.getTotal());
    }
 
    // 对查询后的结果进行异常问题更新
    public void updateException(List<ProjectPlanResponseVO> records) {
        records.forEach(record -> {
            if (null != record.getProjectPhase() && record.getProjectPhase().equals(ProjectCategoryEnum.IMPLEMENT.getDesc())){ // 实施阶段
                List<ProjetPlanRecordItem> month = projectPlanRecordMapper.selectPlanList(record.getId(), MONTH_FLAG);
                List<ProjetPlanRecordItem> season = projectPlanRecordMapper.selectPlanList(record.getId(), SEASON_FLAG);
                List<ProjetPlanRecordItem> year = projectPlanRecordMapper.selectPlanList(record.getId(), YEAR_FLAG);
 
                Date now = DateUtils.getNowDate();
                Calendar calendar = Calendar.getInstance();
                // 月度判断
                if (month.size() > 0) {
                    Integer planMonth = month.get(month.size() - 1).getPlanTime();
                    calendar.setTime(now);
                    calendar.add(Calendar.DAY_OF_MONTH, 3);
                    int monthAfterThreeDays = calendar.get(Calendar.MONTH) + 1;
 
                    if ((planMonth < 12 && planMonth < monthAfterThreeDays)) {
                        new LambdaUpdateChainWrapper<>(planMapper)
                                .eq(Plan::getProjectInfoId, record.getId())
                                .set(Plan::getException, 0)
                                .update();
                    }else if((planMonth == 12 && monthAfterThreeDays == 1)){
                        new LambdaUpdateChainWrapper<>(planMapper)
                                .eq(Plan::getProjectInfoId, record.getId())
                                .set(Plan::getException, 0)
                                .update();
                    } else {
                        new LambdaUpdateChainWrapper<>(planMapper)
                                .eq(Plan::getProjectInfoId, record.getId())
                                .set(Plan::getException, null)
                                .update();
                    }
                }else {
                    new LambdaUpdateChainWrapper<>(planMapper)
                            .eq(Plan::getProjectInfoId, record.getId())
                            .set(Plan::getException, 0)
                            .update();
                }
 
                if (season.size() > 0) {
                    // 季度判断
                    int seasonNum =season.get(season.size() - 1).getPlanTime();
                    Date createTime = season.get(0).getCreateTime();
                    calendar.setTime(createTime);
                    calendar.add(Calendar.MONTH, seasonNum * 3);
                    Date createTimeAfterMonths = calendar.getTime();
                    // 计算两个日期之间的差值,单位为毫秒
                    long diffInMillies = createTimeAfterMonths.getTime() - now.getTime();
                    // 将差值转换为天数
                    long diffInDays = TimeUnit.DAYS.convert(diffInMillies, TimeUnit.MILLISECONDS);
                    if (diffInDays < 3) {
                        new LambdaUpdateChainWrapper<>(planMapper)
                                .eq(Plan::getProjectInfoId, record.getId())
                                .set(Plan::getException, 0)
                                .update();
                    }else {
                        new LambdaUpdateChainWrapper<>(planMapper)
                                .eq(Plan::getProjectInfoId, record.getId())
                                .set(Plan::getException, null)
                                .update();
                    }
                }else {
                    new LambdaUpdateChainWrapper<>(planMapper)
                            .eq(Plan::getProjectInfoId, record.getId())
                            .set(Plan::getException, 0)
                            .update();
                }
 
                if (year.size() > 0) {
                    // 年度判断
                    Integer planYear = year.get(year.size() - 1).getPlanTime();
                    calendar.setTime(now);
                    calendar.add(Calendar.DAY_OF_YEAR, 3); // 添加3天
                    int planYearAfter3Days = calendar.get(Calendar.YEAR);
                    if (planYearAfter3Days > planYear) {
                        new LambdaUpdateChainWrapper<>(planMapper)
                                .eq(Plan::getProjectInfoId, record.getId())
                                .set(Plan::getException, 0)
                                .update();
                    }else {
                        new LambdaUpdateChainWrapper<>(planMapper)
                                .eq(Plan::getProjectInfoId, record.getId())
                                .set(Plan::getException, null)
                                .update();
                    }
                }else {
                    new LambdaUpdateChainWrapper<>(planMapper)
                            .eq(Plan::getProjectInfoId, record.getId())
                            .set(Plan::getException, 0)
                            .update();
                }
 
                month.forEach(item -> {
                    if (item.getReportStatus() == 1) { // 未上报
                        new LambdaUpdateChainWrapper<>(planMapper)
                                .eq(Plan::getProjectInfoId, record.getId())
                                .set(Plan::getException, 0)
                                .update();
                    }
                });
                season.forEach(item -> {
                    if (item.getReportStatus() == 1) { // 未上报
                        new LambdaUpdateChainWrapper<>(planMapper)
                                .eq(Plan::getProjectInfoId, record.getId())
                                .set(Plan::getException, 0)
                                .update();
                    }
                });
                year.forEach(item -> {
                    if (item.getReportStatus() == 1) { // 未上报
                        new LambdaUpdateChainWrapper<>(planMapper)
                                .eq(Plan::getProjectInfoId, record.getId())
                                .set(Plan::getException, 0)
                                .update();
                    }
                });
 
 
            }
        });
    }
 
    /**
     * 根据id查找
     * @param id
     * @return
     */
    @Override
    public Result detail(Long id) {
        PlanVO vo = baseMapper.getById(id);
        Assert.notNull(vo, "记录不存在");
        return Result.ok().data(vo);
    }
 
    /**
     * 列表
     * @return
     */
    @Override
    public Result all() {
        List<Plan> entities = baseMapper.selectList(null);
        List<PlanVO> vos = entities.stream()
                .map(entity -> PlanVO.getVoByEntity(entity, null))
                .collect(Collectors.toList());
        return Result.ok().data(vos);
    }
}