云
2026-10-25 d54fbc9d4f8981fa0f7f613b33a28a8cd4f764ea
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
package cn.iocoder.yudao.module.mes.service.pro.maintenancecalendar;
 
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import cn.iocoder.yudao.framework.common.exception.ServiceException;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.mes.controller.admin.pro.maintenancecalendar.vo.MesProMaintenanceCalendarImportExcelVO;
import cn.iocoder.yudao.module.mes.controller.admin.pro.maintenancecalendar.vo.MesProMaintenanceCalendarImportRespVO;
import cn.iocoder.yudao.module.mes.controller.admin.pro.maintenancecalendar.vo.MesProMaintenanceCalendarPageReqVO;
import cn.iocoder.yudao.module.mes.controller.admin.pro.maintenancecalendar.vo.MesProMaintenanceCalendarSaveReqVO;
import cn.iocoder.yudao.module.mes.dal.dataobject.pro.maintenancecalendar.MesProMaintenanceCalendarDO;
import cn.iocoder.yudao.module.mes.dal.mysql.pro.maintenancecalendar.MesProMaintenanceCalendarMapper;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
 
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.atomic.AtomicInteger;
 
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.mes.enums.ErrorCodeConstants.MES_PRO_MAINTENANCE_CALENDAR_IMPORT_LIST_IS_EMPTY;
import static cn.iocoder.yudao.module.mes.enums.ErrorCodeConstants.MES_PRO_MAINTENANCE_CALENDAR_NOT_EXISTS;
import static cn.iocoder.yudao.module.mes.enums.ErrorCodeConstants.MES_PRO_MAINTENANCE_CALENDAR_NOT_PREPARE;
 
/**
 * MES 电力运维日历 Service 实现类
 *
 * @author xiaoyi
 */
@Service
@Validated
public class MesProMaintenanceCalendarServiceImpl implements MesProMaintenanceCalendarService {
 
    private static final Integer STATUS_DRAFT = 0;
    private static final Integer STATUS_PUBLISHED = 10;
 
    @Resource
    private MesProMaintenanceCalendarMapper calendarMapper;
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Long createCalendar(MesProMaintenanceCalendarSaveReqVO createReqVO) {
        MesProMaintenanceCalendarDO calendar = BeanUtils.toBean(createReqVO, MesProMaintenanceCalendarDO.class, req -> req
                .setCode(generateCode())
                .setStatus(STATUS_DRAFT));
        calendarMapper.insert(calendar);
        return calendar.getId();
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateCalendar(MesProMaintenanceCalendarSaveReqVO updateReqVO) {
        MesProMaintenanceCalendarDO oldCalendar = validateCalendarCanModify(updateReqVO.getId());
        MesProMaintenanceCalendarDO updateObj = BeanUtils.toBean(updateReqVO, MesProMaintenanceCalendarDO.class, req -> req
                .setCode(oldCalendar.getCode())
                .setStatus(oldCalendar.getStatus()));
        calendarMapper.updateById(updateObj);
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteCalendar(Collection<Long> ids) {
        if (CollUtil.isEmpty(ids)) {
            return;
        }
        for (Long id : ids) {
            validateCalendarCanModify(id);
            calendarMapper.deleteById(id);
        }
    }
 
    @Override
    public MesProMaintenanceCalendarDO getCalendar(Long id) {
        return calendarMapper.selectById(id);
    }
 
    @Override
    public PageResult<MesProMaintenanceCalendarDO> getCalendarPage(MesProMaintenanceCalendarPageReqVO pageReqVO) {
        return calendarMapper.selectPage(pageReqVO);
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void publishCalendar(Long id) {
        MesProMaintenanceCalendarDO calendar = validateCalendarExists(id);
        if (!Objects.equals(calendar.getStatus(), STATUS_DRAFT)) {
            throw exception(MES_PRO_MAINTENANCE_CALENDAR_NOT_PREPARE);
        }
        calendarMapper.updateById(new MesProMaintenanceCalendarDO().setId(id).setStatus(STATUS_PUBLISHED));
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public MesProMaintenanceCalendarImportRespVO importCalendarList(List<MesProMaintenanceCalendarImportExcelVO> importList,
                                                                    boolean updateSupport) {
        if (CollUtil.isEmpty(importList)) {
            throw exception(MES_PRO_MAINTENANCE_CALENDAR_IMPORT_LIST_IS_EMPTY);
        }
        MesProMaintenanceCalendarImportRespVO respVO = MesProMaintenanceCalendarImportRespVO.builder()
                .createNames(new ArrayList<>()).updateNames(new ArrayList<>())
                .failureNames(new LinkedHashMap<>()).build();
        AtomicInteger index = new AtomicInteger(1);
        importList.forEach(importItem -> {
            int currentIndex = index.getAndIncrement();
            String key = StrUtil.blankToDefault(importItem.getName(), "第 " + currentIndex + " 行");
            if (StrUtil.isBlank(importItem.getName())) {
                respVO.getFailureNames().put(key, "日历名称不能为空");
                return;
            }
            if (importItem.getWorkDate() == null) {
                respVO.getFailureNames().put(key, "作业日期不能为空");
                return;
            }
            MesProMaintenanceCalendarSaveReqVO reqVO = BeanUtils.toBean(importItem, MesProMaintenanceCalendarSaveReqVO.class);
            MesProMaintenanceCalendarDO existCalendar = calendarMapper.selectByName(importItem.getName());
            try {
                if (existCalendar == null) {
                    reqVO.setId(null);
                    createCalendar(reqVO);
                    respVO.getCreateNames().add(importItem.getName());
                } else if (updateSupport) {
                    reqVO.setId(existCalendar.getId());
                    updateCalendar(reqVO);
                    respVO.getUpdateNames().add(importItem.getName());
                } else {
                    respVO.getFailureNames().put(key, "日历名称已存在");
                }
            } catch (ServiceException e) {
                respVO.getFailureNames().put(key, e.getMessage());
            }
        });
        return respVO;
    }
 
    private String generateCode() {
        return "RL" + DateUtil.format(LocalDateTime.now(), "yyyyMMddHHmmss")
                + String.format("%03d", (int) (Math.random() * 1000));
    }
 
    private MesProMaintenanceCalendarDO validateCalendarExists(Long id) {
        MesProMaintenanceCalendarDO calendar = calendarMapper.selectById(id);
        if (calendar == null) {
            throw exception(MES_PRO_MAINTENANCE_CALENDAR_NOT_EXISTS);
        }
        return calendar;
    }
 
    private MesProMaintenanceCalendarDO validateCalendarCanModify(Long id) {
        MesProMaintenanceCalendarDO calendar = validateCalendarExists(id);
        if (!Objects.equals(calendar.getStatus(), STATUS_DRAFT)) {
            throw exception(MES_PRO_MAINTENANCE_CALENDAR_NOT_PREPARE);
        }
        return calendar;
    }
 
}