车间能级提升-智能设备管理系统
zhuguifei
2025-03-10 6648e74a007254e167c5508de5d25402cd4bb63b
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
package org.dromara.eims.service.impl;
 
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.SneakyThrows;
import org.dromara.common.core.constant.DictConstants;
import org.dromara.common.core.utils.DateUtils;
import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.dromara.eims.domain.vo.EimsRepairResVo;
import org.dromara.system.domain.SysDept;
import org.dromara.system.domain.vo.SysDeptVo;
import org.dromara.system.mapper.SysDeptMapper;
import org.springframework.stereotype.Service;
import org.dromara.eims.domain.bo.EimsMaintPlanBo;
import org.dromara.eims.domain.vo.EimsMaintPlanVo;
import org.dromara.eims.domain.EimsMaintPlan;
import org.dromara.eims.mapper.EimsMaintPlanMapper;
import org.dromara.eims.service.IEimsMaintPlanService;
 
import java.util.*;
 
/**
 * 保养计划Service业务层处理
 *
 * @author zhuguifei
 * @date 2025-03-04
 */
@RequiredArgsConstructor
@Service
public class EimsMaintPlanServiceImpl implements IEimsMaintPlanService {
 
    private final EimsMaintPlanMapper baseMapper;
    private final SysDeptMapper sysDeptMapper;
 
    /**
     * 查询保养计划
     *
     * @param id 主键
     * @return 保养计划
     */
    @Override
    public EimsMaintPlanVo queryById(Long id) {
        return baseMapper.selectVoById(id);
    }
 
    /**
     * 分页查询保养计划列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 保养计划分页列表
     */
    @Override
    public TableDataInfo<EimsMaintPlanVo> queryPageList(EimsMaintPlanBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<EimsMaintPlan> lqw = buildQueryWrapper(bo);
        Page<EimsMaintPlanVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }
 
    @Override
    public TableDataInfo<EimsMaintPlanVo> queryPageListCustom(EimsMaintPlanBo bo, PageQuery pageQuery) {
        Page<EimsMaintPlanVo> page = baseMapper.selectMaintPlanList(pageQuery.build(), buildWrapper(bo));
        return TableDataInfo.build(page);
    }
 
    /**
     * 查询符合条件的保养计划列表
     *
     * @param bo 查询条件
     * @return 保养计划列表
     */
    @Override
    public List<EimsMaintPlanVo> queryList(EimsMaintPlanBo bo) {
        LambdaQueryWrapper<EimsMaintPlan> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }
 
    private LambdaQueryWrapper<EimsMaintPlan> buildQueryWrapper(EimsMaintPlanBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<EimsMaintPlan> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getEquId() != null, EimsMaintPlan::getEquId, bo.getEquId());
        lqw.eq(StringUtils.isNotBlank(bo.getMaintType()), EimsMaintPlan::getMaintType, bo.getMaintType());
        lqw.eq(StringUtils.isNotBlank(bo.getMaintCycleUnit()), EimsMaintPlan::getMaintCycleUnit, bo.getMaintCycleUnit());
        lqw.eq(StringUtils.isNotBlank(bo.getMaintRule()), EimsMaintPlan::getMaintRule, bo.getMaintRule());
        lqw.eq(bo.getMaintUser() != null, EimsMaintPlan::getMaintUser, bo.getMaintUser());
        lqw.eq(bo.getMaintDept() != null, EimsMaintPlan::getMaintDept, bo.getMaintDept());
        lqw.eq(bo.getStatus() != null, EimsMaintPlan::getStatus, bo.getStatus());
        return lqw;
    }
 
    private QueryWrapper<EimsMaintPlan> buildWrapper(EimsMaintPlanBo bo) {
        Map<String, Object> params = bo.getParams();
        QueryWrapper<EimsMaintPlan> qw = Wrappers.query();
        qw.eq(bo.getEquId() != null, "mp.equ_id", bo.getEquId());
        qw.like(bo.getEquName() != null, "equ.equ_name", bo.getEquName());
        qw.like(bo.getAssetNo() != null, "equ.asset_no", bo.getAssetNo());
        qw.eq(StringUtils.isNotBlank(bo.getMaintType()), "mp.maint_type", bo.getMaintType());
        qw.eq(StringUtils.isNotBlank(bo.getMaintCycleUnit()), "mp.maint_cycle_unit", bo.getMaintCycleUnit());
        qw.eq(StringUtils.isNotBlank(bo.getMaintRule()), "mp.maint_rule", bo.getMaintRule());
        qw.eq(bo.getMaintUser() != null, "mp.maint_user", bo.getMaintUser());
        qw.in(bo.getMaintDept() != null, "mp.maint_dept", getAllDescendantIds(bo.getMaintDept()));
        qw.eq(bo.getStatus() != null, "mp.status", bo.getStatus());
        return qw;
    }
 
    /**
     * 根据id,获取所有后代id
     *
     * @param rootId
     * @return
     */
    public List<Long> getAllDescendantIds(Long rootId) {
        List<Long> result = new ArrayList<>();
        result.add(rootId);
        collectDescendants(rootId, result);
        return result;
    }
 
    private void collectDescendants(Long currentId, List<Long> collector) {
        QueryWrapper<SysDept> sysDeptWrapper = new QueryWrapper<>();
        sysDeptWrapper.lambda().eq(SysDept::getParentId, currentId);
 
        List<SysDeptVo> children = sysDeptMapper.selectVoList(sysDeptWrapper);
        if (children != null && !children.isEmpty()) {
            for (SysDeptVo child : children) {
                Long childId = child.getDeptId();
                collector.add(childId);
                collectDescendants(childId, collector);
            }
        }
    }
 
    /**
     * 新增保养计划
     *
     * @param bo 保养计划
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(EimsMaintPlanBo bo) {
        setMaintNextTime(bo);
        EimsMaintPlan add = MapstructUtils.convert(bo, EimsMaintPlan.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }
 
    /**
     * 计算保养计划下次执行时间
     */
    @SneakyThrows
    private void setMaintNextTime(EimsMaintPlanBo bo) {
        Date maintFirstTime = bo.getMaintFirstTime();
        Date maintLastTime = bo.getMaintLastTime();
        //下次保养时间计算规则 0-按固定周期  1-按上次保养时间
        Date nextDate = (maintLastTime != null && bo.getMaintRule().equals(DictConstants.MAINT_TIME_RULE_DETAIL.LAST)) ? maintLastTime :maintFirstTime;
 
        //首次执行时间为空抛出异常
        if (maintFirstTime == null) {
            throw new Exception("首次执行时间不能为空!");
        }
        //周期
        Long maintCycle = bo.getMaintCycle();
        //单位 1-天 2-周 3-月 4-季 5-年
        String maintCycleUnit = bo.getMaintCycleUnit();
        switch (maintCycleUnit) {
            case "1":
                nextDate = DateUtils.addDays(nextDate, maintCycle.intValue());
                break;
            case "2":
                nextDate = DateUtils.addWeeks(nextDate, maintCycle.intValue());
                break;
            case "3":
                nextDate = DateUtils.addMonths(nextDate, maintCycle.intValue());
                break;
            case "4":
                nextDate = DateUtils.addMonths(nextDate, maintCycle.intValue() * 3);
                break;
            case "5":
                nextDate = DateUtils.addYears(nextDate, maintCycle.intValue());
                break;
 
        }
        bo.setMaintNextTime(nextDate);
 
 
    }
 
    /**
     * 修改保养计划
     *
     * @param bo 保养计划
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(EimsMaintPlanBo bo) {
        //setMaintNextTime(bo);
        EimsMaintPlan update = MapstructUtils.convert(bo, EimsMaintPlan.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }
 
    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(EimsMaintPlan entity) {
        //TODO 做一些数据校验,如唯一约束
    }
 
    /**
     * 校验并批量删除保养计划信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteByIds(ids) > 0;
    }
 
 
}