| | |
| | | package cn.iocoder.yudao.module.mes.service.pd.ctc; |
| | | |
| | | import cn.hutool.core.collection.CollUtil; |
| | | 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.mdm.api.item.MdmItemApi; |
| | | import cn.iocoder.yudao.module.mdm.api.item.dto.MdmItemRespDTO; |
| | | import cn.iocoder.yudao.module.mes.controller.admin.pd.ctc.vo.*; |
| | | import cn.iocoder.yudao.module.mes.dal.dataobject.pd.ctc.ProductCtcDO; |
| | | import cn.iocoder.yudao.module.mes.dal.mysql.pd.ctc.ProductCtcMapper; |
| | | import cn.iocoder.yudao.module.mes.service.pd.reel.ProductCtcReelService; |
| | | import jakarta.annotation.Resource; |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.transaction.annotation.Transactional; |
| | | import org.springframework.validation.annotation.Validated; |
| | | |
| | | import java.math.BigDecimal; |
| | | import java.math.RoundingMode; |
| | | import java.util.ArrayList; |
| | | import java.util.LinkedHashMap; |
| | | import java.util.List; |
| | | 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.PD_CTC_CALC_PARAM_INVALID; |
| | | import static cn.iocoder.yudao.module.mes.enums.ErrorCodeConstants.PD_CTC_IMPORT_LIST_IS_EMPTY; |
| | | import static cn.iocoder.yudao.module.mes.enums.ErrorCodeConstants.PD_CTC_ITEM_DUPLICATE; |
| | | import static cn.iocoder.yudao.module.mes.enums.ErrorCodeConstants.PD_CTC_NOT_EXISTS; |
| | | |
| | |
| | | } |
| | | |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public void deleteCtc(Long id) { |
| | | // 校验存在 |
| | | validateCtcExists(id); |
| | |
| | | @Override |
| | | public ProductCtcDO getCtc(Long id) { |
| | | return ctcMapper.selectById(id); |
| | | } |
| | | |
| | | @Override |
| | | public ProductCtcDO getCtcByOrderSpec(String orderSpec) { |
| | | return ctcMapper.selectByOrderSpec(orderSpec); |
| | | } |
| | | |
| | | @Override |
| | |
| | | return respVO; |
| | | } |
| | | |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public ProductCtcImportRespVO importCtcList(List<ProductCtcImportExcelVO> importList, boolean updateSupport) { |
| | | if (CollUtil.isEmpty(importList)) { |
| | | throw exception(PD_CTC_IMPORT_LIST_IS_EMPTY); |
| | | } |
| | | ProductCtcImportRespVO respVO = ProductCtcImportRespVO.builder() |
| | | .createCodes(new ArrayList<>()).updateCodes(new ArrayList<>()) |
| | | .failureCodes(new LinkedHashMap<>()).build(); |
| | | AtomicInteger index = new AtomicInteger(1); |
| | | importList.forEach(importItem -> { |
| | | int currentIndex = index.getAndIncrement(); |
| | | String key = StrUtil.blankToDefault(importItem.getItemCode(), "第 " + currentIndex + " 行"); |
| | | if (StrUtil.isBlank(importItem.getItemCode())) { |
| | | respVO.getFailureCodes().put(key, "物料编码不能为空"); |
| | | return; |
| | | } |
| | | // 1. 解析物料(关联 mdm_item) |
| | | MdmItemRespDTO mdmItem = mdmItemApi.getItemByCode(importItem.getItemCode().trim()).getCheckedData(); |
| | | if (mdmItem == null) { |
| | | respVO.getFailureCodes().put(key, "物料编码[" + importItem.getItemCode() + "]不存在"); |
| | | return; |
| | | } |
| | | // 2. 构建请求并创建/更新(复用现有 create/update 服务) |
| | | ProductCtcSaveReqVO reqVO = BeanUtils.toBean(importItem, ProductCtcSaveReqVO.class); |
| | | reqVO.setItemId(mdmItem.getId()); |
| | | ProductCtcDO existCtc = ctcMapper.selectByItemId(mdmItem.getId()); |
| | | try { |
| | | if (existCtc == null) { |
| | | reqVO.setId(null); |
| | | createCtc(reqVO); |
| | | respVO.getCreateCodes().add(importItem.getItemCode()); |
| | | } else if (updateSupport) { |
| | | reqVO.setId(existCtc.getId()); |
| | | updateCtc(reqVO); |
| | | respVO.getUpdateCodes().add(importItem.getItemCode()); |
| | | } else { |
| | | respVO.getFailureCodes().put(key, "物料编码已存在技术参数"); |
| | | } |
| | | } catch (ServiceException e) { |
| | | respVO.getFailureCodes().put(key, e.getMessage()); |
| | | } |
| | | }); |
| | | return respVO; |
| | | } |
| | | |
| | | private void validateItemUnique(Long itemId, Long excludeId) { |
| | | ProductCtcDO ctc = ctcMapper.selectByItemId(itemId); |
| | | if (ctc != null && !ctc.getId().equals(excludeId)) { |