gaoluyang
2026-10-18 f0d1f1ad146e284c0adcc851a9501f468ca1aa5d
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
package cn.iocoder.yudao.module.mes.service.em.quota;
 
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
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.em.meter.vo.MesEmOptionRespVO;
import cn.iocoder.yudao.module.mes.controller.admin.em.quota.vo.MesEmQuotaPageReqVO;
import cn.iocoder.yudao.module.mes.controller.admin.em.quota.vo.MesEmQuotaRespVO;
import cn.iocoder.yudao.module.mes.controller.admin.em.quota.vo.MesEmQuotaSaveReqVO;
import cn.iocoder.yudao.module.mes.dal.dataobject.dv.machinery.MesDvMachineryDO;
import cn.iocoder.yudao.module.mes.dal.dataobject.em.quota.MesEmQuotaDO;
import cn.iocoder.yudao.module.mes.dal.dataobject.md.workstation.MesMdWorkshopDO;
import cn.iocoder.yudao.module.mes.dal.mysql.dv.machinery.MesDvMachineryMapper;
import cn.iocoder.yudao.module.mes.dal.mysql.em.meter.MesEmMeterMapper;
import cn.iocoder.yudao.module.mes.dal.mysql.em.quota.MesEmQuotaMapper;
import cn.iocoder.yudao.module.mes.dal.mysql.md.workstation.MesMdWorkshopMapper;
import cn.iocoder.yudao.module.mes.enums.em.MesEmEnergyTypeEnum;
import cn.iocoder.yudao.module.mes.enums.em.MesEmMeterStatusEnum;
import cn.iocoder.yudao.module.mes.enums.em.MesEmQuotaObjectTypeEnum;
import cn.iocoder.yudao.module.mes.enums.em.MesEmQuotaPeriodEnum;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;
 
import java.math.BigDecimal;
import java.util.Collections;
import java.util.List;
 
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.mes.enums.ErrorCodeConstants.EM_QUOTA_NOT_EXISTS;
import static cn.iocoder.yudao.module.mes.enums.ErrorCodeConstants.EM_QUOTA_OBJECT_NOT_EXISTS;
 
/**
 * MES 能耗考核指标 Service 实现类
 *
 * @author 超级管理员
 */
@Service
@Validated
public class MesEmQuotaServiceImpl implements MesEmQuotaService {
 
    @Resource
    private MesEmQuotaMapper quotaMapper;
    @Resource
    private MesEmMeterMapper meterMapper;
    @Resource
    private MesMdWorkshopMapper workshopMapper;
    @Resource
    private MesDvMachineryMapper machineryMapper;
 
    @Override
    public Long createQuota(MesEmQuotaSaveReqVO createReqVO) {
        MesEmQuotaDO quota = BeanUtils.toBean(createReqVO, MesEmQuotaDO.class);
        fillQuotaDefaultsAndObjectName(quota);
        quotaMapper.insert(quota);
        return quota.getId();
    }
 
    @Override
    public void updateQuota(MesEmQuotaSaveReqVO updateReqVO) {
        validateQuotaExists(updateReqVO.getId());
        MesEmQuotaDO quota = BeanUtils.toBean(updateReqVO, MesEmQuotaDO.class);
        fillQuotaDefaultsAndObjectName(quota);
        quotaMapper.updateById(quota);
    }
 
    @Override
    public void deleteQuota(Long id) {
        validateQuotaExists(id);
        quotaMapper.deleteById(id);
    }
 
    @Override
    public MesEmQuotaDO getQuota(Long id) {
        return quotaMapper.selectById(id);
    }
 
    @Override
    public void validateQuotaExists(Long id) {
        if (quotaMapper.selectById(id) == null) {
            throw exception(EM_QUOTA_NOT_EXISTS);
        }
    }
 
    private void fillQuotaDefaultsAndObjectName(MesEmQuotaDO quota) {
        MesEmEnergyTypeEnum energyType = MesEmEnergyTypeEnum.getByType(quota.getEnergyType());
        if (StrUtil.isBlank(quota.getUnit()) && energyType != null) {
            quota.setUnit(energyType.getUnit());
        }
        if (quota.getWarnRatio() == null) {
            quota.setWarnRatio(new BigDecimal("10"));
        }
        if (quota.getAlarmRatio() == null) {
            quota.setAlarmRatio(new BigDecimal("20"));
        }
        if (quota.getMomWarnRatio() == null) {
            quota.setMomWarnRatio(new BigDecimal("30"));
        }
        if (quota.getYoyWarnRatio() == null) {
            quota.setYoyWarnRatio(new BigDecimal("30"));
        }
        if (quota.getStatus() == null) {
            quota.setStatus(MesEmMeterStatusEnum.ENABLE.getStatus());
        }
        // 考核对象名称
        Integer objectType = quota.getObjectType();
        if (MesEmQuotaObjectTypeEnum.FACTORY.getType().equals(objectType)) {
            quota.setObjectId(0L);
            quota.setObjectName("全厂");
            return;
        }
        if (quota.getObjectId() == null) {
            throw exception(EM_QUOTA_OBJECT_NOT_EXISTS);
        }
        if (MesEmQuotaObjectTypeEnum.WORKSHOP.getType().equals(objectType)) {
            MesMdWorkshopDO workshop = workshopMapper.selectById(quota.getObjectId());
            if (workshop == null) {
                throw exception(EM_QUOTA_OBJECT_NOT_EXISTS);
            }
            quota.setObjectName(workshop.getName());
        } else if (MesEmQuotaObjectTypeEnum.LINE.getType().equals(objectType)) {
            MesEmOptionRespVO line = meterMapper.selectLineOptionById(quota.getObjectId());
            if (line == null) {
                throw exception(EM_QUOTA_OBJECT_NOT_EXISTS);
            }
            quota.setObjectName(line.getName());
        } else if (MesEmQuotaObjectTypeEnum.MACHINERY.getType().equals(objectType)) {
            MesDvMachineryDO machinery = machineryMapper.selectById(quota.getObjectId());
            if (machinery == null) {
                throw exception(EM_QUOTA_OBJECT_NOT_EXISTS);
            }
            quota.setObjectName(machinery.getName());
        } else {
            throw exception(EM_QUOTA_OBJECT_NOT_EXISTS);
        }
    }
 
    @Override
    public PageResult<MesEmQuotaDO> getQuotaPage(MesEmQuotaPageReqVO pageReqVO) {
        return quotaMapper.selectPage(pageReqVO);
    }
 
    @Override
    public List<MesEmQuotaRespVO> getQuotaRespVOList(List<MesEmQuotaDO> list) {
        if (CollUtil.isEmpty(list)) {
            return Collections.emptyList();
        }
        return BeanUtils.toBean(list, MesEmQuotaRespVO.class, vo -> {
            vo.setEnergyTypeName(MesEmEnergyTypeEnum.getName(vo.getEnergyType()));
            vo.setObjectTypeName(MesEmQuotaObjectTypeEnum.getName(vo.getObjectType()));
            vo.setPeriodTypeName(MesEmQuotaPeriodEnum.getName(vo.getPeriodType()));
            vo.setStatusName(MesEmMeterStatusEnum.getName(vo.getStatus()));
        });
    }
 
}