package cn.iocoder.yudao.module.mdm.api.item; import cn.iocoder.yudao.framework.common.pojo.CommonResult; import cn.iocoder.yudao.framework.common.util.object.BeanUtils; import cn.iocoder.yudao.module.mdm.api.item.dto.MdmItemSyncReqDTO; import cn.iocoder.yudao.module.mdm.dal.dataobject.item.MdmItemBatchConfigDO; import cn.iocoder.yudao.module.mdm.dal.dataobject.item.MdmItemDO; import cn.iocoder.yudao.module.mdm.dal.mysql.item.MdmItemBatchConfigMapper; import cn.iocoder.yudao.module.mdm.dal.mysql.item.MdmItemMapper; import jakarta.annotation.Resource; import org.springframework.transaction.annotation.Transactional; import org.springframework.validation.annotation.Validated; import org.springframework.web.bind.annotation.RestController; import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success; /** * MDM 物料同步 API 实现类 * * @author 芋道源码 */ @RestController @Validated public class MdmItemSyncApiImpl implements MdmItemSyncApi { @Resource private MdmItemMapper itemMapper; @Resource private MdmItemBatchConfigMapper itemBatchConfigMapper; @Override @Transactional(rollbackFor = Exception.class) public CommonResult syncItemToMdm(MdmItemSyncReqDTO syncReqDTO) { // 1. 按 code 查询是否已存在 MdmItemDO existingItem = itemMapper.selectByCode(syncReqDTO.getCode()); // 2. 保存或更新物料主数据 MdmItemDO item = BeanUtils.toBean(syncReqDTO, MdmItemDO.class); Long itemId; if (existingItem != null) { // 更新 item.setId(existingItem.getId()); itemMapper.updateById(item); itemId = existingItem.getId(); } else { // 新增 itemMapper.insert(item); itemId = item.getId(); } // 3. 同步批次属性配置 syncBatchConfig(itemId, syncReqDTO); return success(itemId); } /** * 同步批次属性配置 */ private void syncBatchConfig(Long itemId, MdmItemSyncReqDTO syncReqDTO) { // 检查是否有批次配置数据 if (syncReqDTO.getProduceDateFlag() == null && syncReqDTO.getExpireDateFlag() == null && syncReqDTO.getReceiptDateFlag() == null && syncReqDTO.getVendorFlag() == null && syncReqDTO.getPurchaseOrderCodeFlag() == null && syncReqDTO.getLotNumberFlag() == null && syncReqDTO.getQualityStatusFlag() == null) { return; } // 查询已有配置 MdmItemBatchConfigDO existing = itemBatchConfigMapper.selectOne(MdmItemBatchConfigDO::getItemId, itemId); MdmItemBatchConfigDO config = new MdmItemBatchConfigDO(); config.setItemId(itemId); config.setProduceDateFlag(Boolean.TRUE.equals(syncReqDTO.getProduceDateFlag())); config.setExpireDateFlag(Boolean.TRUE.equals(syncReqDTO.getExpireDateFlag())); config.setReceiptDateFlag(Boolean.TRUE.equals(syncReqDTO.getReceiptDateFlag())); config.setVendorFlag(Boolean.TRUE.equals(syncReqDTO.getVendorFlag())); config.setPurchaseOrderCodeFlag(Boolean.TRUE.equals(syncReqDTO.getPurchaseOrderCodeFlag())); config.setLotNumberFlag(Boolean.TRUE.equals(syncReqDTO.getLotNumberFlag())); config.setQualityStatusFlag(Boolean.TRUE.equals(syncReqDTO.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); } } }