package cn.iocoder.yudao.module.mes.service.pro.worktime; import cn.hutool.core.util.ObjUtil; import cn.iocoder.yudao.framework.common.pojo.PageResult; import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum; import cn.iocoder.yudao.framework.common.util.object.BeanUtils; import cn.iocoder.yudao.module.mes.controller.admin.pro.worktime.vo.MesProWorktimeAuxPageReqVO; import cn.iocoder.yudao.module.mes.controller.admin.pro.worktime.vo.MesProWorktimeAuxSaveReqVO; import cn.iocoder.yudao.module.mes.dal.dataobject.pro.process.MesProProcessDO; import cn.iocoder.yudao.module.mes.dal.dataobject.pro.worktime.MesProWorktimeAuxDO; import cn.iocoder.yudao.module.mes.dal.mysql.pro.feedback.MesProFeedbackAuxMapper; import cn.iocoder.yudao.module.mes.dal.mysql.pro.worktime.MesProWorktimeAuxMapper; import cn.iocoder.yudao.module.mes.service.pro.process.MesProProcessService; import jakarta.annotation.Resource; import org.springframework.stereotype.Service; import org.springframework.validation.annotation.Validated; import java.math.BigDecimal; import java.util.List; import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception; import static cn.iocoder.yudao.module.mes.enums.ErrorCodeConstants.*; /** * MES 辅助工时配置 Service 实现类 * * @author 超级管理员 */ @Service @Validated public class MesProWorktimeAuxServiceImpl implements MesProWorktimeAuxService { @Resource private MesProWorktimeAuxMapper worktimeAuxMapper; @Resource private MesProFeedbackAuxMapper feedbackAuxMapper; @Resource private MesProProcessService processService; @Override public Long createWorktimeAux(MesProWorktimeAuxSaveReqVO createReqVO) { validateWorktimeAux(createReqVO, null); MesProWorktimeAuxDO aux = BeanUtils.toBean(createReqVO, MesProWorktimeAuxDO.class) .setId(null) .setStatus(ObjUtil.defaultIfNull(createReqVO.getStatus(), CommonStatusEnum.ENABLE.getStatus())) .setSort(ObjUtil.defaultIfNull(createReqVO.getSort(), 0)); worktimeAuxMapper.insert(aux); return aux.getId(); } @Override public void updateWorktimeAux(MesProWorktimeAuxSaveReqVO updateReqVO) { validateWorktimeAux(updateReqVO, updateReqVO.getId()); MesProWorktimeAuxDO updateObj = BeanUtils.toBean(updateReqVO, MesProWorktimeAuxDO.class); worktimeAuxMapper.updateById(updateObj); } @Override public void deleteWorktimeAux(Long id) { MesProWorktimeAuxDO aux = validateWorktimeAuxExists(id); Long usedCount = feedbackAuxMapper.selectCountByAuxItemId(id); if (usedCount != null && usedCount > 0) { throw exception(PRO_WORKTIME_AUX_IN_USE, aux.getItemName()); } worktimeAuxMapper.deleteById(id); } @Override public MesProWorktimeAuxDO getWorktimeAux(Long id) { return worktimeAuxMapper.selectById(id); } @Override public PageResult getWorktimeAuxPage(MesProWorktimeAuxPageReqVO pageReqVO) { return worktimeAuxMapper.selectPage(pageReqVO); } @Override public List getApplicableList(Long processId, Long workstationId) { if (processId == null) { return List.of(); } return worktimeAuxMapper.selectListByProcessIdAndWorkstationId(processId, workstationId).stream() .filter(aux -> CommonStatusEnum.ENABLE.getStatus().equals(aux.getStatus())) .toList(); } @Override public List getWorktimeAuxListByProcessId(Long processId) { return worktimeAuxMapper.selectListByProcessId(processId); } private MesProWorktimeAuxDO validateWorktimeAuxExists(Long id) { MesProWorktimeAuxDO aux = worktimeAuxMapper.selectById(id); if (aux == null) { throw exception(PRO_WORKTIME_AUX_NOT_EXISTS); } return aux; } private void validateWorktimeAux(MesProWorktimeAuxSaveReqVO reqVO, Long id) { // 1. 工序必须存在 MesProProcessDO process = processService.getProcess(reqVO.getProcessId()); if (process == null) { throw exception(PRO_WORKTIME_AUX_PROCESS_NOT_EXISTS, reqVO.getProcessId()); } // 2. 计量方式 + 对应的时间参数 Integer measureType = reqVO.getMeasureType(); boolean measureValid = MesProWorktimeAuxDO.MEASURE_TIMES == measureType || MesProWorktimeAuxDO.MEASURE_PIECES == measureType || MesProWorktimeAuxDO.MEASURE_REELS == measureType || MesProWorktimeAuxDO.MEASURE_METERS == measureType; if (!measureValid) { throw exception(PRO_WORKTIME_AUX_MEASURE_TYPE_INVALID, reqVO.getItemName(), measureType); } if (MesProWorktimeAuxDO.MEASURE_METERS == measureType) { BigDecimal speed = reqVO.getSpeedMPerMin(); if (speed == null || speed.signum() <= 0) { throw exception(PRO_WORKTIME_AUX_SPEED_REQUIRED, reqVO.getItemName()); } } else { BigDecimal duration = reqVO.getDurationMinutes(); if (duration == null || duration.signum() <= 0) { throw exception(PRO_WORKTIME_AUX_DURATION_REQUIRED, reqVO.getItemName(), measureTypeName(measureType)); } } // 3. 同一工序 + 同一机台范围下项目编码不能重复 MesProWorktimeAuxDO exists = worktimeAuxMapper.selectOneByProcessIdAndWorkstationIdAndCode( reqVO.getProcessId(), reqVO.getWorkstationId(), reqVO.getItemCode()); if (exists != null && !exists.getId().equals(id)) { String workstationScope = reqVO.getWorkstationId() == null ? "不限机台" : "机台 ID=" + reqVO.getWorkstationId(); throw exception(PRO_WORKTIME_AUX_CODE_DUPLICATE, process.getName(), reqVO.getItemCode(), workstationScope); } } private static String measureTypeName(Integer measureType) { if (measureType == null) { return "未知"; } return switch (measureType) { case MesProWorktimeAuxDO.MEASURE_TIMES -> "按次"; case MesProWorktimeAuxDO.MEASURE_PIECES -> "按个"; case MesProWorktimeAuxDO.MEASURE_REELS -> "按盘"; case MesProWorktimeAuxDO.MEASURE_METERS -> "按米折算"; default -> "未知"; }; } }