| ¶Ô±ÈÐÂÎļþ |
| | |
| | | package cn.iocoder.yudao.module.mdm.service.item; |
| | | |
| | | import cn.hutool.core.collection.CollUtil; |
| | | import cn.hutool.core.util.ObjUtil; |
| | | import cn.iocoder.yudao.framework.common.pojo.CommonResult; |
| | | import cn.iocoder.yudao.framework.common.pojo.PageResult; |
| | | import cn.iocoder.yudao.framework.common.util.object.BeanUtils; |
| | | import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX; |
| | | import cn.iocoder.yudao.module.mdm.controller.admin.item.vo.MdmItemPageReqVO; |
| | | import cn.iocoder.yudao.module.mdm.controller.admin.item.vo.MdmItemSaveReqVO; |
| | | import cn.iocoder.yudao.module.mdm.dal.dataobject.category.MdmItemCategoryDO; |
| | | import cn.iocoder.yudao.module.mdm.dal.dataobject.item.MdmItemDO; |
| | | import cn.iocoder.yudao.module.mdm.dal.dataobject.unit.MdmUnitMeasureDO; |
| | | import cn.iocoder.yudao.module.mdm.dal.mysql.item.MdmItemBatchConfigMapper; |
| | | import cn.iocoder.yudao.module.mdm.dal.mysql.item.MdmItemMapper; |
| | | import cn.iocoder.yudao.module.mdm.service.brand.MdmBrandService; |
| | | import cn.iocoder.yudao.module.mdm.service.category.MdmItemCategoryService; |
| | | import cn.iocoder.yudao.module.mdm.service.unit.MdmUnitMeasureService; |
| | | import cn.iocoder.yudao.module.mes.api.item.MesMdItemApi; |
| | | import cn.iocoder.yudao.module.mes.api.item.MesMdItemTypeApi; |
| | | import cn.iocoder.yudao.module.mes.api.item.dto.MesMdItemSyncReqDTO; |
| | | import cn.iocoder.yudao.module.mes.api.item.dto.MesMdItemTypeRespDTO; |
| | | import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |
| | | import jakarta.annotation.Resource; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.context.annotation.Lazy; |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.transaction.annotation.Transactional; |
| | | import org.springframework.validation.annotation.Validated; |
| | | |
| | | import java.util.*; |
| | | import java.util.stream.Collectors; |
| | | |
| | | import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception; |
| | | import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertSet; |
| | | import static cn.iocoder.yudao.module.mdm.enums.ErrorCodeConstants.*; |
| | | |
| | | /** |
| | | * MDM ç©æä¸»æ°æ® Service å®ç°ç±» |
| | | * |
| | | * @author èéæºç |
| | | */ |
| | | @Service |
| | | @Validated |
| | | public class MdmItemServiceImpl implements MdmItemService { |
| | | |
| | | @Resource |
| | | private MdmItemMapper itemMapper; |
| | | |
| | | @Resource |
| | | private MdmItemCategoryService itemCategoryService; |
| | | @Resource |
| | | private MdmUnitMeasureService unitMeasureService; |
| | | @Resource |
| | | private MdmBrandService brandService; |
| | | |
| | | @Resource |
| | | @Lazy |
| | | private MesMdItemApi mesMdItemApi; |
| | | @Resource |
| | | private MdmItemBatchConfigMapper itemBatchConfigMapper; |
| | | |
| | | @Resource |
| | | private MesMdItemTypeApi mesMdItemTypeApi; |
| | | |
| | | |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public Long createItem(MdmItemSaveReqVO createReqVO) { |
| | | // æ ¡éªæ°æ® |
| | | validateItemSaveData(createReqVO); |
| | | |
| | | // æå
¥ MDM ç©æ |
| | | MdmItemDO item = BeanUtils.toBean(createReqVO, MdmItemDO.class); |
| | | itemMapper.insert(item); |
| | | |
| | | // ä¿åæ¹æ¬¡é
ç½®ï¼å¦ææï¼ |
| | | saveBatchConfig(item.getId(), createReqVO); |
| | | |
| | | // åæ¥å° MESï¼å¾é syncMes æ¶ï¼ |
| | | createReqVO.setId(item.getId()); |
| | | if (Boolean.TRUE.equals(createReqVO.getSyncMes())) { |
| | | syncItemToMes(createReqVO); |
| | | } |
| | | return item.getId(); |
| | | } |
| | | |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public void updateItem(MdmItemSaveReqVO updateReqVO) { |
| | | // æ ¡éªåå¨ |
| | | validateItemExists(updateReqVO.getId()); |
| | | // æ ¡éªæ°æ® |
| | | validateItemSaveData(updateReqVO); |
| | | |
| | | // æ´æ° MDM ç©æ |
| | | MdmItemDO updateObj = BeanUtils.toBean(updateReqVO, MdmItemDO.class); |
| | | itemMapper.updateById(updateObj); |
| | | |
| | | // ä¿åæ¹æ¬¡é
ç½®ï¼å¦ææï¼ |
| | | saveBatchConfig(updateReqVO.getId(), updateReqVO); |
| | | |
| | | // åæ¥å° MESï¼å¾é syncMes æ¶ï¼ |
| | | if (Boolean.TRUE.equals(updateReqVO.getSyncMes())) { |
| | | syncItemToMes(updateReqVO); |
| | | } |
| | | } |
| | | |
| | | private void validateItemSaveData(MdmItemSaveReqVO reqVO) { |
| | | // æ ¡éªç©æç¼ç çå¯ä¸æ§ |
| | | validateItemCodeUnique(reqVO.getId(), reqVO.getCode()); |
| | | } |
| | | |
| | | /** |
| | | * åæ¥ç©æå° MESï¼åä½/åç±»æ code æ å°ï¼ |
| | | * |
| | | * MDM ç unitMeasureId/categoryId ä¸ MES ç unitMeasureId/itemTypeId åå±ä¸¤å¥ä¸»æ°æ®ï¼ |
| | | * è¿éå
å¨ MDM ä¾§è§£æåº codeï¼åäº¤ç± MesMdItemApi å¨ MES ä¾§è§£æä¸ºå¯¹åº idã |
| | | * åæ¥å¤±è´¥å°æåºå¼å¸¸ï¼è§¦åå¤å± @Transactional æ´ä½åæ»ï¼MDM æä½ä¸å¹¶åæ»ï¼ã |
| | | */ |
| | | private void syncItemToMes(MdmItemSaveReqVO reqVO) { |
| | | // è§£æè®¡éåä½ id -> code |
| | | String unitCode = null; |
| | | if (reqVO.getUnitMeasureId() != null) { |
| | | MdmUnitMeasureDO unitMeasure = unitMeasureService.getUnitMeasure(reqVO.getUnitMeasureId()); |
| | | if (unitMeasure == null) { |
| | | throw exception(MDM_UNIT_MEASURE_NOT_EXISTS); |
| | | } |
| | | unitCode = unitMeasure.getCode(); |
| | | } |
| | | // è§£æåç±» id -> codeï¼ä½¿ç¨ MDM æ¬å°åç±»æå¡ï¼ |
| | | String itemTypeCode = null; |
| | | if (reqVO.getCategoryId() != null) { |
| | | CommonResult<MesMdItemTypeRespDTO> itemType = mesMdItemTypeApi.getItemType(reqVO.getCategoryId()); |
| | | itemTypeCode = itemType.getData().getCode(); |
| | | } |
| | | // ç»è£
忥 DTO |
| | | MesMdItemSyncReqDTO syncReqDTO = new MesMdItemSyncReqDTO(); |
| | | syncReqDTO.setCode(reqVO.getCode()); |
| | | syncReqDTO.setMdmItemId(reqVO.getId()); |
| | | syncReqDTO.setName(reqVO.getName()); |
| | | syncReqDTO.setSpecification(reqVO.getSpecification()); |
| | | syncReqDTO.setUnitCode(unitCode); |
| | | syncReqDTO.setItemTypeCode(itemTypeCode); |
| | | syncReqDTO.setSafeStockFlag(Boolean.TRUE); // ä¿ç MDM ç minStock/maxStockï¼é¿å
被 MES æ¸
é¶ |
| | | syncReqDTO.setMinStock(reqVO.getMinStock()); |
| | | syncReqDTO.setMaxStock(reqVO.getMaxStock()); |
| | | syncReqDTO.setHighValue(reqVO.getIsHighValue()); |
| | | syncReqDTO.setBatchFlag(reqVO.getIsBatchManaged()); |
| | | syncReqDTO.setRemark(reqVO.getRemark()); |
| | | // æ¹æ¬¡é
ç½®åæ®µ |
| | | syncReqDTO.setProduceDateFlag(reqVO.getProduceDateFlag()); |
| | | syncReqDTO.setExpireDateFlag(reqVO.getExpireDateFlag()); |
| | | syncReqDTO.setReceiptDateFlag(reqVO.getReceiptDateFlag()); |
| | | syncReqDTO.setVendorFlag(reqVO.getVendorFlag()); |
| | | syncReqDTO.setPurchaseOrderCodeFlag(reqVO.getPurchaseOrderCodeFlag()); |
| | | syncReqDTO.setLotNumberFlag(reqVO.getLotNumberFlag()); |
| | | syncReqDTO.setQualityStatusFlag(reqVO.getQualityStatusFlag()); |
| | | // create èµ° createItemï¼update èµ° updateItemï¼MES ä¾§æ code upsertï¼ |
| | | if (reqVO.getId() == null) { |
| | | mesMdItemApi.createItem(syncReqDTO); |
| | | } else { |
| | | mesMdItemApi.updateItem(syncReqDTO); |
| | | } |
| | | } |
| | | |
| | | @Override |
| | | public void updateItemStatus(Long id, Integer status) { |
| | | // æ ¡éªåå¨ |
| | | validateItemExists(id); |
| | | // æ´æ°ç¶æ |
| | | itemMapper.updateById(new MdmItemDO().setId(id).setStatus(status)); |
| | | } |
| | | |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public void deleteItem(Long id) { |
| | | // æ ¡éªåå¨ |
| | | MdmItemDO item = validateItemExists(id); |
| | | // è§£å³è½¯å é¤å¯ä¸é®å²çªï¼å¦æå·²åå¨ç¸å code ä¸å·²è½¯å é¤çè®°å½ï¼å
ç©çå é¤å® |
| | | // MdmItemDO existingDeleted = itemMapper.selectByCodeAndDeleted(item.getCode(), true); |
| | | // if (existingDeleted != null && !existingDeleted.getId().equals(id)) { |
| | | // itemMapper.physicalDeleteById(existingDeleted.getId()); |
| | | // } |
| | | // 软å é¤å½åè®°å½ |
| | | itemMapper.deleteById(id); |
| | | } |
| | | |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public void deleteItemList(Collection<Long> ids) { |
| | | if (CollUtil.isEmpty(ids)) { |
| | | return; |
| | | } |
| | | // é个å é¤ï¼å¤ç¨åæ¡å é¤é»è¾ï¼ä¿ææ ¡éªï¼ |
| | | ids.forEach(this::deleteItem); |
| | | } |
| | | |
| | | @Override |
| | | public MdmItemDO validateItemExists(Long id) { |
| | | MdmItemDO item = itemMapper.selectById(id); |
| | | if (item == null) { |
| | | throw exception(MDM_ITEM_NOT_EXISTS); |
| | | } |
| | | return item; |
| | | } |
| | | |
| | | @Override |
| | | public MdmItemDO validateItemExistsAndEnable(Long id) { |
| | | MdmItemDO item = validateItemExists(id); |
| | | if (ObjUtil.notEqual(cn.iocoder.yudao.framework.common.enums.CommonStatusEnum.ENABLE.getStatus(), item.getStatus())) { |
| | | throw exception(MDM_ITEM_IS_DISABLE); |
| | | } |
| | | return item; |
| | | } |
| | | |
| | | private void validateItemCodeUnique(Long id, String code) { |
| | | MdmItemDO item = itemMapper.selectByCode(code); |
| | | if (item == null) { |
| | | return; |
| | | } |
| | | if (ObjUtil.notEqual(item.getId(), id)) { |
| | | throw exception(MDM_ITEM_CODE_DUPLICATE); |
| | | } |
| | | } |
| | | |
| | | private void validateCategoryExists(Long categoryId) { |
| | | if (categoryId == null) { |
| | | return; |
| | | } |
| | | // ä½¿ç¨ MDM æ¬å°åç±»æå¡æ ¡éª |
| | | MdmItemCategoryDO category = itemCategoryService.getItemCategory(categoryId); |
| | | if (category == null) { |
| | | throw exception(MDM_ITEM_CATEGORY_NOT_EXISTS); |
| | | } |
| | | } |
| | | |
| | | private void validateUnitMeasureExists(Long unitMeasureId) { |
| | | if (unitMeasureService.getUnitMeasure(unitMeasureId) == null) { |
| | | throw exception(MDM_UNIT_MEASURE_NOT_EXISTS); |
| | | } |
| | | } |
| | | |
| | | private void validateBrandExists(Long brandId) { |
| | | if (brandService.getBrand(brandId) == null) { |
| | | throw exception(MDM_BRAND_NOT_EXISTS); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * å° itemType 转æ¢ä¸ºä¸æåç§° |
| | | * |
| | | * @param itemType ç©æç±»åï¼1åæ, 2åæå, 3æå, 4è¾
æ |
| | | * @return 䏿åç§° |
| | | */ |
| | | private String convertItemTypeToName(Integer itemType) { |
| | | if (itemType == null) { |
| | | return null; |
| | | } |
| | | switch (itemType) { |
| | | case 1: |
| | | return "åæ"; |
| | | case 2: |
| | | return "åæå"; |
| | | case 3: |
| | | return "æå"; |
| | | case 4: |
| | | return "è¾
æ"; |
| | | default: |
| | | return null; |
| | | } |
| | | } |
| | | |
| | | @Override |
| | | public MdmItemDO getItem(Long id) { |
| | | return itemMapper.selectById(id); |
| | | } |
| | | |
| | | @Override |
| | | public PageResult<MdmItemDO> getItemPage(MdmItemPageReqVO pageReqVO) { |
| | | // æ¥æ¾æ¶ï¼å¦ææ¥æ¾æä¸ªåç±»ç¼å·ï¼åå
å«å®çååç±» |
| | | Set<Long> categoryIds = null; |
| | | if (pageReqVO.getCategoryId() != null) { |
| | | categoryIds = new HashSet<>(); |
| | | categoryIds.add(pageReqVO.getCategoryId()); |
| | | List<cn.iocoder.yudao.module.mdm.dal.dataobject.category.MdmItemCategoryDO> children = |
| | | itemCategoryService.getItemCategoryChildrenList(pageReqVO.getCategoryId()); |
| | | categoryIds.addAll(convertSet(children, cn.iocoder.yudao.module.mdm.dal.dataobject.category.MdmItemCategoryDO::getId)); |
| | | } |
| | | // å页æ¥è¯¢ |
| | | return itemMapper.selectPage(pageReqVO, categoryIds); |
| | | } |
| | | |
| | | @Override |
| | | public List<MdmItemDO> getItemList(Collection<Long> ids) { |
| | | if (CollUtil.isEmpty(ids)) { |
| | | return Collections.emptyList(); |
| | | } |
| | | return itemMapper.selectBatchIds(ids); |
| | | } |
| | | |
| | | @Override |
| | | public Long getItemCountByCategoryId(Long categoryId) { |
| | | return itemMapper.selectCountByCategoryId(categoryId); |
| | | } |
| | | |
| | | @Override |
| | | public Long getItemCountByUnitMeasureId(Long unitMeasureId) { |
| | | return itemMapper.selectCountByUnitMeasureId(unitMeasureId); |
| | | } |
| | | |
| | | @Override |
| | | public Long getItemCountByBrandId(Long brandId) { |
| | | return itemMapper.selectCountByBrandId(brandId); |
| | | } |
| | | |
| | | @Override |
| | | public MdmItemDO getItemByCode(String code) { |
| | | return itemMapper.selectByCode(code); |
| | | } |
| | | |
| | | /** |
| | | * ä¿åæ¹æ¬¡é
ç½®ï¼å° MDM æ¬å°è¡¨ï¼ |
| | | */ |
| | | private void saveBatchConfig(Long itemId, MdmItemSaveReqVO reqVO) { |
| | | // æ£æ¥æ¯å¦ææ¹æ¬¡é
ç½®æ°æ® |
| | | if (reqVO.getProduceDateFlag() == null |
| | | && reqVO.getExpireDateFlag() == null |
| | | && reqVO.getReceiptDateFlag() == null |
| | | && reqVO.getVendorFlag() == null |
| | | && reqVO.getPurchaseOrderCodeFlag() == null |
| | | && reqVO.getLotNumberFlag() == null |
| | | && reqVO.getQualityStatusFlag() == null) { |
| | | return; |
| | | } |
| | | |
| | | // æ¥è¯¢å·²æé
ç½® |
| | | cn.iocoder.yudao.module.mdm.dal.dataobject.item.MdmItemBatchConfigDO existing = |
| | | itemBatchConfigMapper.selectOne( |
| | | cn.iocoder.yudao.module.mdm.dal.dataobject.item.MdmItemBatchConfigDO::getItemId, itemId); |
| | | |
| | | cn.iocoder.yudao.module.mdm.dal.dataobject.item.MdmItemBatchConfigDO config = |
| | | new cn.iocoder.yudao.module.mdm.dal.dataobject.item.MdmItemBatchConfigDO(); |
| | | config.setItemId(itemId); |
| | | config.setProduceDateFlag(Boolean.TRUE.equals(reqVO.getProduceDateFlag())); |
| | | config.setExpireDateFlag(Boolean.TRUE.equals(reqVO.getExpireDateFlag())); |
| | | config.setReceiptDateFlag(Boolean.TRUE.equals(reqVO.getReceiptDateFlag())); |
| | | config.setVendorFlag(Boolean.TRUE.equals(reqVO.getVendorFlag())); |
| | | config.setPurchaseOrderCodeFlag(Boolean.TRUE.equals(reqVO.getPurchaseOrderCodeFlag())); |
| | | config.setLotNumberFlag(Boolean.TRUE.equals(reqVO.getLotNumberFlag())); |
| | | config.setQualityStatusFlag(Boolean.TRUE.equals(reqVO.getQualityStatusFlag())); |
| | | // æ¸
空ä¸éè¦çåæ®µ |
| | | config.setClientFlag(false); |
| | | config.setSalesOrderCodeFlag(false); |
| | | config.setWorkOrderFlag(false); |
| | | config.setTaskFlag(false); |
| | | config.setWorkstationFlag(false); |
| | | config.setToolFlag(false); |
| | | config.setMoldFlag(false); |
| | | |
| | | if (existing != null) { |
| | | config.setId(existing.getId()); |
| | | itemBatchConfigMapper.updateById(config); |
| | | } else { |
| | | itemBatchConfigMapper.insert(config); |
| | | } |
| | | } |
| | | |
| | | } |