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()));
|
});
|
}
|
|
}
|