feat:优化设备台账新增接口,修复关联关键件、备件回显问题

main
HuangHuiKang 3 weeks ago
parent a5698d1449
commit cae6e64c65

@ -121,6 +121,10 @@ public class ErpProductServiceImpl implements ErpProductService {
// TODO 芋艿:校验分类
// 插入
ErpProductCategoryDO productCategory = productCategoryMapper.selectById(product.getCategoryId());
if(productCategory == null ){
throw exception(PRODUCT_CATEGORY_NOT_EXISTS);
}
Long id = productCategory.getParentId();
product.setSubCategoryName(productCategory.getName());
product.setSubCategoryId(product.getCategoryId());

@ -0,0 +1,67 @@
package cn.iocoder.yudao.module.mes.controller.admin.deviceledger;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.module.mes.dal.dataobject.deviceledger.DeviceLedgerComponentRelDO;
import cn.iocoder.yudao.module.mes.service.deviceledger.DeviceLedgerComponentRelService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
@Tag(name = "管理后台 - 设备台账关键件关系")
@RestController
@RequestMapping("/mes/device-ledger-component-rel")
@Validated
public class DeviceLedgerComponentRelController {
@Resource
private DeviceLedgerComponentRelService deviceLedgerComponentRelService;
@PostMapping("/create")
@Operation(summary = "创建设备台账关键件关系")
@PreAuthorize("@ss.hasPermission('mes:device-ledger:update')")
public CommonResult<Long> create(@Valid @RequestBody DeviceLedgerComponentRelDO createReqVO) {
return success(deviceLedgerComponentRelService.create(createReqVO));
}
@PutMapping("/update")
@Operation(summary = "更新设备台账关键件关系")
@PreAuthorize("@ss.hasPermission('mes:device-ledger:update')")
public CommonResult<Boolean> update(@Valid @RequestBody DeviceLedgerComponentRelDO updateReqVO) {
deviceLedgerComponentRelService.update(updateReqVO);
return success(true);
}
@DeleteMapping("/delete")
@Operation(summary = "删除设备台账关键件关系")
@Parameter(name = "id", description = "编号", required = true)
@PreAuthorize("@ss.hasPermission('mes:device-ledger:update')")
public CommonResult<Boolean> delete(@RequestParam("id") Long id) {
deviceLedgerComponentRelService.delete(id);
return success(true);
}
@GetMapping("/get")
@Operation(summary = "获得设备台账关键件关系")
@Parameter(name = "id", description = "编号", required = true)
@PreAuthorize("@ss.hasPermission('mes:device-ledger:query')")
public CommonResult<DeviceLedgerComponentRelDO> get(@RequestParam("id") Long id) {
return success(deviceLedgerComponentRelService.get(id));
}
@GetMapping("/list-by-device-ledger-id")
@Operation(summary = "根据设备台账ID获得关键件关系列表")
@Parameter(name = "deviceLedgerId", description = "设备台账ID", required = true)
@PreAuthorize("@ss.hasPermission('mes:device-ledger:query')")
public CommonResult<List<DeviceLedgerComponentRelDO>> listByDeviceLedgerId(@RequestParam("deviceLedgerId") Long deviceLedgerId) {
return success(deviceLedgerComponentRelService.getListByDeviceLedgerId(deviceLedgerId));
}
}

@ -0,0 +1,67 @@
package cn.iocoder.yudao.module.mes.controller.admin.deviceledger;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.module.mes.dal.dataobject.deviceledger.DeviceLedgerSpareRelDO;
import cn.iocoder.yudao.module.mes.service.deviceledger.DeviceLedgerSpareRelService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
@Tag(name = "管理后台 - 设备台账备件关系")
@RestController
@RequestMapping("/mes/device-ledger-spare-rel")
@Validated
public class DeviceLedgerSpareRelController {
@Resource
private DeviceLedgerSpareRelService deviceLedgerSpareRelService;
@PostMapping("/create")
@Operation(summary = "创建设备台账备件关系")
@PreAuthorize("@ss.hasPermission('mes:device-ledger:update')")
public CommonResult<Long> create(@Valid @RequestBody DeviceLedgerSpareRelDO createReqVO) {
return success(deviceLedgerSpareRelService.create(createReqVO));
}
@PutMapping("/update")
@Operation(summary = "更新设备台账备件关系")
@PreAuthorize("@ss.hasPermission('mes:device-ledger:update')")
public CommonResult<Boolean> update(@Valid @RequestBody DeviceLedgerSpareRelDO updateReqVO) {
deviceLedgerSpareRelService.update(updateReqVO);
return success(true);
}
@DeleteMapping("/delete")
@Operation(summary = "删除设备台账备件关系")
@Parameter(name = "id", description = "编号", required = true)
@PreAuthorize("@ss.hasPermission('mes:device-ledger:update')")
public CommonResult<Boolean> delete(@RequestParam("id") Long id) {
deviceLedgerSpareRelService.delete(id);
return success(true);
}
@GetMapping("/get")
@Operation(summary = "获得设备台账备件关系")
@Parameter(name = "id", description = "编号", required = true)
@PreAuthorize("@ss.hasPermission('mes:device-ledger:query')")
public CommonResult<DeviceLedgerSpareRelDO> get(@RequestParam("id") Long id) {
return success(deviceLedgerSpareRelService.get(id));
}
@GetMapping("/list-by-device-ledger-id")
@Operation(summary = "根据设备台账ID获得备件关系列表")
@Parameter(name = "deviceLedgerId", description = "设备台账ID", required = true)
@PreAuthorize("@ss.hasPermission('mes:device-ledger:query')")
public CommonResult<List<DeviceLedgerSpareRelDO>> listByDeviceLedgerId(@RequestParam("deviceLedgerId") Long deviceLedgerId) {
return success(deviceLedgerSpareRelService.getListByDeviceLedgerId(deviceLedgerId));
}
}

@ -132,7 +132,7 @@ public class DeviceLedgerRespVO extends BaseDO {
private List<TicketResultsDO> maintainList;
@Schema(description = "维修列表")
private Map<String,List<DvRepairLineDO>> repairList;
;
@Schema(description = "关键件列表")
private List<CriticalComponentDO> componentList;

@ -0,0 +1,34 @@
package cn.iocoder.yudao.module.mes.dal.dataobject.deviceledger;
import cn.iocoder.yudao.framework.mybatis.core.dataobject.BaseDO;
import com.baomidou.mybatisplus.annotation.KeySequence;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import lombok.ToString;
@TableName("mes_device_ledger_component_rel")
@KeySequence("mes_device_ledger_component_rel_seq")
@Data
@EqualsAndHashCode(callSuper = true)
@ToString(callSuper = true)
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class DeviceLedgerComponentRelDO extends BaseDO {
@TableId
private Long id;
private Long deviceLedgerId;
private Long componentId;
private String componentNameSnapshot;
private Integer sort;
}

@ -0,0 +1,34 @@
package cn.iocoder.yudao.module.mes.dal.dataobject.deviceledger;
import cn.iocoder.yudao.framework.mybatis.core.dataobject.BaseDO;
import com.baomidou.mybatisplus.annotation.KeySequence;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import lombok.ToString;
@TableName("mes_device_ledger_spare_rel")
@KeySequence("mes_device_ledger_spare_rel_seq")
@Data
@EqualsAndHashCode(callSuper = true)
@ToString(callSuper = true)
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class DeviceLedgerSpareRelDO extends BaseDO {
@TableId
private Long id;
private Long deviceLedgerId;
private Long productId;
private String productNameSnapshot;
private Integer sort;
}

@ -0,0 +1,28 @@
package cn.iocoder.yudao.module.mes.dal.mysql.deviceledger;
import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
import cn.iocoder.yudao.module.mes.dal.dataobject.deviceledger.DeviceLedgerComponentRelDO;
import org.apache.ibatis.annotations.Mapper;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
@Mapper
public interface DeviceLedgerComponentRelMapper extends BaseMapperX<DeviceLedgerComponentRelDO> {
default List<DeviceLedgerComponentRelDO> selectListByDeviceLedgerId(Long deviceLedgerId) {
return selectList(DeviceLedgerComponentRelDO::getDeviceLedgerId, deviceLedgerId);
}
default List<DeviceLedgerComponentRelDO> selectListByDeviceLedgerIds(Collection<Long> deviceLedgerIds) {
if (deviceLedgerIds == null || deviceLedgerIds.isEmpty()) {
return Collections.emptyList();
}
return selectList(DeviceLedgerComponentRelDO::getDeviceLedgerId, deviceLedgerIds);
}
default int deleteByDeviceLedgerId(Long deviceLedgerId) {
return delete(DeviceLedgerComponentRelDO::getDeviceLedgerId, deviceLedgerId);
}
}

@ -0,0 +1,28 @@
package cn.iocoder.yudao.module.mes.dal.mysql.deviceledger;
import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
import cn.iocoder.yudao.module.mes.dal.dataobject.deviceledger.DeviceLedgerSpareRelDO;
import org.apache.ibatis.annotations.Mapper;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
@Mapper
public interface DeviceLedgerSpareRelMapper extends BaseMapperX<DeviceLedgerSpareRelDO> {
default List<DeviceLedgerSpareRelDO> selectListByDeviceLedgerId(Long deviceLedgerId) {
return selectList(DeviceLedgerSpareRelDO::getDeviceLedgerId, deviceLedgerId);
}
default List<DeviceLedgerSpareRelDO> selectListByDeviceLedgerIds(Collection<Long> deviceLedgerIds) {
if (deviceLedgerIds == null || deviceLedgerIds.isEmpty()) {
return Collections.emptyList();
}
return selectList(DeviceLedgerSpareRelDO::getDeviceLedgerId, deviceLedgerIds);
}
default int deleteByDeviceLedgerId(Long deviceLedgerId) {
return delete(DeviceLedgerSpareRelDO::getDeviceLedgerId, deviceLedgerId);
}
}

@ -131,11 +131,11 @@ public class CriticalComponentServiceImpl implements CriticalComponentService {
throw exception(CRITICAL_COMPONENT_NOT_EXISTS);
}
// 检验设备台账引用
validateDeviceLedgerBatchReferences(ids);
//检验项目维护引用
validateDvjectBatchReferences(ids);
// // 检验设备台账引用
// validateDeviceLedgerBatchReferences(ids);
//
// //检验项目维护引用
// validateDvjectBatchReferences(ids);
// 删除
criticalComponentMapper.deleteByIds(ids);

@ -0,0 +1,26 @@
package cn.iocoder.yudao.module.mes.service.deviceledger;
import cn.iocoder.yudao.module.mes.dal.dataobject.deviceledger.DeviceLedgerComponentRelDO;
import java.util.Collection;
import java.util.List;
import java.util.Map;
public interface DeviceLedgerComponentRelService {
Long create(DeviceLedgerComponentRelDO createObj);
void update(DeviceLedgerComponentRelDO updateObj);
void delete(Long id);
DeviceLedgerComponentRelDO get(Long id);
void replaceByDeviceLedgerId(Long deviceLedgerId, String componentIdsText);
List<DeviceLedgerComponentRelDO> getListByDeviceLedgerId(Long deviceLedgerId);
Map<Long, List<DeviceLedgerComponentRelDO>> getMapByDeviceLedgerIds(Collection<Long> deviceLedgerIds);
void deleteByDeviceLedgerId(Long deviceLedgerId);
}

@ -0,0 +1,115 @@
package cn.iocoder.yudao.module.mes.service.deviceledger;
import cn.iocoder.yudao.module.mes.dal.dataobject.criticalcomponent.CriticalComponentDO;
import cn.iocoder.yudao.module.mes.dal.dataobject.deviceledger.DeviceLedgerComponentRelDO;
import cn.iocoder.yudao.module.mes.dal.mysql.criticalcomponent.CriticalComponentMapper;
import cn.iocoder.yudao.module.mes.dal.mysql.deviceledger.DeviceLedgerComponentRelMapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.function.Function;
import java.util.stream.Collectors;
@Service
public class DeviceLedgerComponentRelServiceImpl implements DeviceLedgerComponentRelService {
@Resource
private DeviceLedgerComponentRelMapper componentRelMapper;
@Resource
private CriticalComponentMapper criticalComponentMapper;
@Override
public Long create(DeviceLedgerComponentRelDO createObj) {
componentRelMapper.insert(createObj);
return createObj.getId();
}
@Override
public void update(DeviceLedgerComponentRelDO updateObj) {
componentRelMapper.updateById(updateObj);
}
@Override
public void delete(Long id) {
componentRelMapper.deleteById(id);
}
@Override
public DeviceLedgerComponentRelDO get(Long id) {
return componentRelMapper.selectById(id);
}
@Override
public void replaceByDeviceLedgerId(Long deviceLedgerId, String componentIdsText) {
List<Long> componentIds = parseIds(componentIdsText);
List<DeviceLedgerComponentRelDO> oldRelList = componentRelMapper.selectListByDeviceLedgerId(deviceLedgerId);
Map<Long, String> oldSnapshotMap = oldRelList.stream()
.filter(item -> item.getComponentId() != null)
.collect(Collectors.toMap(DeviceLedgerComponentRelDO::getComponentId,
DeviceLedgerComponentRelDO::getComponentNameSnapshot, (a, b) -> a));
componentRelMapper.deleteByDeviceLedgerId(deviceLedgerId);
if (componentIds.isEmpty()) {
return;
}
Map<Long, CriticalComponentDO> componentMap = criticalComponentMapper.selectBatchIds(componentIds).stream()
.collect(Collectors.toMap(CriticalComponentDO::getId, Function.identity(), (a, b) -> a));
List<DeviceLedgerComponentRelDO> list = new ArrayList<>();
for (int i = 0; i < componentIds.size(); i++) {
Long componentId = componentIds.get(i);
CriticalComponentDO componentDO = componentMap.get(componentId);
String snapshotName = componentDO != null && StringUtils.isNotBlank(componentDO.getName())
? componentDO.getName()
: oldSnapshotMap.getOrDefault(componentId, "");
list.add(DeviceLedgerComponentRelDO.builder()
.deviceLedgerId(deviceLedgerId)
.componentId(componentId)
.componentNameSnapshot(snapshotName)
.sort(i + 1)
.build());
}
componentRelMapper.insertBatch(list);
}
@Override
public List<DeviceLedgerComponentRelDO> getListByDeviceLedgerId(Long deviceLedgerId) {
return componentRelMapper.selectListByDeviceLedgerId(deviceLedgerId).stream()
.sorted(Comparator.comparing(item -> Optional.ofNullable(item.getSort()).orElse(Integer.MAX_VALUE)))
.collect(Collectors.toList());
}
@Override
public Map<Long, List<DeviceLedgerComponentRelDO>> getMapByDeviceLedgerIds(Collection<Long> deviceLedgerIds) {
return componentRelMapper.selectListByDeviceLedgerIds(deviceLedgerIds).stream()
.sorted(Comparator.comparing(item -> Optional.ofNullable(item.getSort()).orElse(Integer.MAX_VALUE)))
.collect(Collectors.groupingBy(DeviceLedgerComponentRelDO::getDeviceLedgerId,
LinkedHashMap::new, Collectors.toList()));
}
@Override
public void deleteByDeviceLedgerId(Long deviceLedgerId) {
componentRelMapper.deleteByDeviceLedgerId(deviceLedgerId);
}
private List<Long> parseIds(String idsText) {
if (StringUtils.isBlank(idsText)) {
return Collections.emptyList();
}
return Arrays.stream(idsText.split(","))
.map(String::trim)
.filter(StringUtils::isNotBlank)
.map(Long::valueOf)
.collect(Collectors.toList());
}
}

@ -24,7 +24,9 @@ import cn.iocoder.yudao.module.mes.dal.mysql.devicetype.DeviceTypeMapper;
import cn.iocoder.yudao.module.erp.dal.mysql.product.ErpProductMapper;
import cn.iocoder.yudao.module.erp.service.product.ErpProductUnitService;
import cn.iocoder.yudao.module.mes.dal.dataobject.criticalcomponent.CriticalComponentDO;
import cn.iocoder.yudao.module.mes.dal.dataobject.deviceledger.DeviceLedgerComponentRelDO;
import cn.iocoder.yudao.module.mes.dal.dataobject.dvrepair.DvRepairLineDO;
import cn.iocoder.yudao.module.mes.dal.dataobject.deviceledger.DeviceLedgerSpareRelDO;
import cn.iocoder.yudao.module.mes.dal.mysql.criticalcomponent.CriticalComponentMapper;
import cn.iocoder.yudao.module.mes.dal.mysql.organization.OrganizationMapper;
import cn.iocoder.yudao.module.mes.dal.mysql.plan.PlanMapper;
@ -102,6 +104,12 @@ public class DeviceLedgerServiceImpl implements DeviceLedgerService {
@Resource
private ProductDeviceRelMapper productDeviceRelMapper;
@Resource
private DeviceLedgerComponentRelService deviceLedgerComponentRelService;
@Resource
private DeviceLedgerSpareRelService deviceLedgerSpareRelService;
@Resource
@Lazy
private DeviceMapper deviceMapper;
@ -163,6 +171,8 @@ public class DeviceLedgerServiceImpl implements DeviceLedgerService {
// 创建台账
deviceLedgerMapper.insert(deviceLedger);
deviceLedgerComponentRelService.replaceByDeviceLedgerId(deviceLedger.getId(), createReqVO.getComponentId());
deviceLedgerSpareRelService.replaceByDeviceLedgerId(deviceLedger.getId(), createReqVO.getBeijianId());
// 数采设备设置
// if (deviceLedger.getWorkshop() != null) {
@ -217,6 +227,8 @@ public class DeviceLedgerServiceImpl implements DeviceLedgerService {
updateObj.setComponentId(updateObj.getComponentId()==null?"":updateObj.getComponentId());
updateObj.setBeijianId(updateObj.getBeijianId()==null?"":updateObj.getBeijianId());
deviceLedgerMapper.updateById(updateObj);
deviceLedgerComponentRelService.replaceByDeviceLedgerId(updateObj.getId(), updateReqVO.getComponentId());
deviceLedgerSpareRelService.replaceByDeviceLedgerId(updateObj.getId(), updateReqVO.getBeijianId());
}
@Override
@ -225,6 +237,8 @@ public class DeviceLedgerServiceImpl implements DeviceLedgerService {
for (Long id : idList) {
// 校验存在
validateDeviceLedgerExists(id);
deviceLedgerComponentRelService.deleteByDeviceLedgerId(id);
deviceLedgerSpareRelService.deleteByDeviceLedgerId(id);
}
// 删除
@ -278,21 +292,31 @@ public class DeviceLedgerServiceImpl implements DeviceLedgerService {
// }
//关键件列表
List<CriticalComponentDO> componentList = new ArrayList<>();
if (deviceLedgerDO != null && StringUtils.isNotBlank(deviceLedgerDO.getComponentId())) {
// 将逗号分隔的字符串转换为Long类型的List
List<Long> idList = Arrays.stream(deviceLedgerDO.getComponentId().split(","))
.map(String::trim) // 去除可能存在的空格
.map(Long::valueOf)
.collect(Collectors.toList());
for (Long componentId : idList) {
CriticalComponentDO criticalComponentDO = criticalComponentMapper.selectById(componentId);
if(criticalComponentDO!=null){
List<DeviceLedgerComponentRelDO> componentRelList = deviceLedgerComponentRelService.getListByDeviceLedgerId(id);
if (CollUtil.isNotEmpty(componentRelList)) {
Set<Long> componentIds = componentRelList.stream().map(DeviceLedgerComponentRelDO::getComponentId)
.filter(Objects::nonNull).collect(Collectors.toSet());
Map<Long, CriticalComponentDO> componentMap = componentIds.isEmpty() ? Collections.emptyMap()
: criticalComponentMapper.selectBatchIds(componentIds).stream()
.collect(Collectors.toMap(CriticalComponentDO::getId, Function.identity(), (a, b) -> a));
for (DeviceLedgerComponentRelDO relDO : componentRelList) {
CriticalComponentDO criticalComponentDO = componentMap.get(relDO.getComponentId());
if (criticalComponentDO != null) {
componentList.add(criticalComponentDO);
continue;
}
CriticalComponentDO deletedComponent = new CriticalComponentDO();
deletedComponent.setId(relDO.getComponentId());
deletedComponent.setName(buildDeletedName(relDO.getComponentNameSnapshot()));
componentList.add(deletedComponent);
}
deviceLedgerDO.setComponentList(componentList);
deviceLedgerDO.setComponentId(componentRelList.stream()
.map(DeviceLedgerComponentRelDO::getComponentId)
.filter(Objects::nonNull)
.map(String::valueOf)
.collect(Collectors.joining(",")));
}
//模具列表
@ -312,25 +336,42 @@ public class DeviceLedgerServiceImpl implements DeviceLedgerService {
deviceLedgerDO.setMoldList(moldList);
}
//备件列表
List<ErpProductDO> beijianList = new ArrayList<>();
if (deviceLedgerDO != null && StringUtils.isNotBlank(deviceLedgerDO.getBeijianId())) {
// 将逗号分隔的字符串转换为Long类型的List
List<Long> idList = Arrays.stream(deviceLedgerDO.getBeijianId().split(","))
.map(String::trim) // 去除可能存在的空格
.map(Long::valueOf)
.collect(Collectors.toList());
for (Long beijianId : idList) {
ErpProductDO beijianDO = productMapper.selectById(beijianId);
if(beijianDO!=null){
beijianList.add(beijianDO);
List<DeviceLedgerSpareRelDO> spareRelList = deviceLedgerSpareRelService.getListByDeviceLedgerId(id);
if (CollUtil.isNotEmpty(spareRelList)) {
Set<Long> productIds = spareRelList.stream().map(DeviceLedgerSpareRelDO::getProductId)
.filter(Objects::nonNull).collect(Collectors.toSet());
Map<Long, ErpProductDO> productMap = productIds.isEmpty() ? Collections.emptyMap()
: productMapper.selectBatchIds(productIds).stream()
.collect(Collectors.toMap(ErpProductDO::getId, Function.identity(), (a, b) -> a));
for (DeviceLedgerSpareRelDO relDO : spareRelList) {
ErpProductDO productDO = productMap.get(relDO.getProductId());
if (productDO != null) {
beijianList.add(productDO);
continue;
}
ErpProductDO deletedProduct = new ErpProductDO();
deletedProduct.setId(relDO.getProductId());
deletedProduct.setName(buildDeletedName(relDO.getProductNameSnapshot()));
beijianList.add(deletedProduct);
}
deviceLedgerDO.setBeijianList(beijianList);
deviceLedgerDO.setBeijianId(spareRelList.stream()
.map(DeviceLedgerSpareRelDO::getProductId)
.filter(Objects::nonNull)
.map(String::valueOf)
.collect(Collectors.joining(",")));
}
return deviceLedgerDO;
}
private String buildDeletedName(String snapshotName) {
if (StringUtils.isNotBlank(snapshotName)) {
return snapshotName + "(已删除)";
}
return "已删除";
}
@Override
public DeviceLedgerDO getDeviceLedger(Long id, String code) {
if (id != null) {

@ -0,0 +1,26 @@
package cn.iocoder.yudao.module.mes.service.deviceledger;
import cn.iocoder.yudao.module.mes.dal.dataobject.deviceledger.DeviceLedgerSpareRelDO;
import java.util.Collection;
import java.util.List;
import java.util.Map;
public interface DeviceLedgerSpareRelService {
Long create(DeviceLedgerSpareRelDO createObj);
void update(DeviceLedgerSpareRelDO updateObj);
void delete(Long id);
DeviceLedgerSpareRelDO get(Long id);
void replaceByDeviceLedgerId(Long deviceLedgerId, String productIdsText);
List<DeviceLedgerSpareRelDO> getListByDeviceLedgerId(Long deviceLedgerId);
Map<Long, List<DeviceLedgerSpareRelDO>> getMapByDeviceLedgerIds(Collection<Long> deviceLedgerIds);
void deleteByDeviceLedgerId(Long deviceLedgerId);
}

@ -0,0 +1,115 @@
package cn.iocoder.yudao.module.mes.service.deviceledger;
import cn.iocoder.yudao.module.erp.dal.dataobject.product.ErpProductDO;
import cn.iocoder.yudao.module.erp.dal.mysql.product.ErpProductMapper;
import cn.iocoder.yudao.module.mes.dal.dataobject.deviceledger.DeviceLedgerSpareRelDO;
import cn.iocoder.yudao.module.mes.dal.mysql.deviceledger.DeviceLedgerSpareRelMapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.function.Function;
import java.util.stream.Collectors;
@Service
public class DeviceLedgerSpareRelServiceImpl implements DeviceLedgerSpareRelService {
@Resource
private DeviceLedgerSpareRelMapper spareRelMapper;
@Resource
private ErpProductMapper productMapper;
@Override
public Long create(DeviceLedgerSpareRelDO createObj) {
spareRelMapper.insert(createObj);
return createObj.getId();
}
@Override
public void update(DeviceLedgerSpareRelDO updateObj) {
spareRelMapper.updateById(updateObj);
}
@Override
public void delete(Long id) {
spareRelMapper.deleteById(id);
}
@Override
public DeviceLedgerSpareRelDO get(Long id) {
return spareRelMapper.selectById(id);
}
@Override
public void replaceByDeviceLedgerId(Long deviceLedgerId, String productIdsText) {
List<Long> productIds = parseIds(productIdsText);
List<DeviceLedgerSpareRelDO> oldRelList = spareRelMapper.selectListByDeviceLedgerId(deviceLedgerId);
Map<Long, String> oldSnapshotMap = oldRelList.stream()
.filter(item -> item.getProductId() != null)
.collect(Collectors.toMap(DeviceLedgerSpareRelDO::getProductId,
DeviceLedgerSpareRelDO::getProductNameSnapshot, (a, b) -> a));
spareRelMapper.deleteByDeviceLedgerId(deviceLedgerId);
if (productIds.isEmpty()) {
return;
}
Map<Long, ErpProductDO> productMap = productMapper.selectBatchIds(productIds).stream()
.collect(Collectors.toMap(ErpProductDO::getId, Function.identity(), (a, b) -> a));
List<DeviceLedgerSpareRelDO> list = new ArrayList<>();
for (int i = 0; i < productIds.size(); i++) {
Long productId = productIds.get(i);
ErpProductDO productDO = productMap.get(productId);
String snapshotName = productDO != null && StringUtils.isNotBlank(productDO.getName())
? productDO.getName()
: oldSnapshotMap.getOrDefault(productId, "");
list.add(DeviceLedgerSpareRelDO.builder()
.deviceLedgerId(deviceLedgerId)
.productId(productId)
.productNameSnapshot(snapshotName)
.sort(i + 1)
.build());
}
spareRelMapper.insertBatch(list);
}
@Override
public List<DeviceLedgerSpareRelDO> getListByDeviceLedgerId(Long deviceLedgerId) {
return spareRelMapper.selectListByDeviceLedgerId(deviceLedgerId).stream()
.sorted(Comparator.comparing(item -> Optional.ofNullable(item.getSort()).orElse(Integer.MAX_VALUE)))
.collect(Collectors.toList());
}
@Override
public Map<Long, List<DeviceLedgerSpareRelDO>> getMapByDeviceLedgerIds(Collection<Long> deviceLedgerIds) {
return spareRelMapper.selectListByDeviceLedgerIds(deviceLedgerIds).stream()
.sorted(Comparator.comparing(item -> Optional.ofNullable(item.getSort()).orElse(Integer.MAX_VALUE)))
.collect(Collectors.groupingBy(DeviceLedgerSpareRelDO::getDeviceLedgerId,
LinkedHashMap::new, Collectors.toList()));
}
@Override
public void deleteByDeviceLedgerId(Long deviceLedgerId) {
spareRelMapper.deleteByDeviceLedgerId(deviceLedgerId);
}
private List<Long> parseIds(String idsText) {
if (StringUtils.isBlank(idsText)) {
return Collections.emptyList();
}
return Arrays.stream(idsText.split(","))
.map(String::trim)
.filter(StringUtils::isNotBlank)
.map(Long::valueOf)
.collect(Collectors.toList());
}
}
Loading…
Cancel
Save