package com.ruoyi.production.service.impl;
|
|
import com.baomidou.mybatisplus.core.metadata.IPage;
|
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
import com.ruoyi.production.dto.ProductionMachineRecordDto;
|
import com.ruoyi.production.mapper.ProductWorkOrderMapper;
|
import com.ruoyi.production.mapper.ProductionMachineRecordMapper;
|
import com.ruoyi.production.pojo.ProductWorkOrder;
|
import com.ruoyi.production.pojo.ProductionMachineRecord;
|
import com.ruoyi.production.service.ProductionMachineRecordService;
|
import lombok.RequiredArgsConstructor;
|
import org.springframework.stereotype.Service;
|
import org.springframework.transaction.annotation.Transactional;
|
|
import java.util.ArrayList;
|
import java.util.Arrays;
|
import java.util.List;
|
|
/**
|
* <p>
|
* 生产上机记录表 服务实现类
|
* </p>
|
*
|
* @author 芯导软件(江苏)有限公司
|
* @since 2026-04-27 06:12:04
|
*/
|
@Service
|
@RequiredArgsConstructor
|
public class ProductionMachineRecordServiceImpl extends ServiceImpl<ProductionMachineRecordMapper, ProductionMachineRecord> implements ProductionMachineRecordService {
|
|
private final ProductionMachineRecordMapper productionMachineRecordMapper;
|
private final ProductWorkOrderMapper productWorkOrderMapper;
|
|
@Override
|
public IPage<ProductionMachineRecordDto> listPage(Page<ProductionMachineRecordDto> page, ProductionMachineRecordDto productionMachineRecord) {
|
return productionMachineRecordMapper.listPage(page, productionMachineRecord);
|
}
|
|
@Override
|
@Transactional(rollbackFor = Exception.class)
|
public Boolean add(List<ProductionMachineRecordDto> productionMachineRecord) {
|
for (ProductionMachineRecordDto productionMachineRecordDto : productionMachineRecord) {
|
this.saveOrUpdate(productionMachineRecordDto);
|
ProductWorkOrder productWorkOrder = productWorkOrderMapper.selectById(productionMachineRecordDto.getWorkOrderId());
|
if (productWorkOrder != null) {
|
String userIds = productWorkOrder.getUserIds();
|
String operatorId = productionMachineRecordDto.getOperatorId();
|
if (ObjectUtils.isNotEmpty(operatorId)) {
|
if (userIds == null || userIds.isEmpty()) {
|
productWorkOrder.setUserIds(operatorId);
|
} else {
|
List<String> userIdList = new ArrayList<>(Arrays.asList(userIds.split(",")));
|
List<String> operatorIdList = Arrays.asList(operatorId.split(","));
|
for (String opId : operatorIdList) {
|
String trimmedOpId = opId.trim();
|
if (!userIdList.contains(trimmedOpId)) {
|
userIdList.add(trimmedOpId);
|
}
|
}
|
productWorkOrder.setUserIds(String.join(",", userIdList));
|
}
|
}
|
productWorkOrder.setDeviceId(productionMachineRecordDto.getMachineId());
|
productWorkOrderMapper.updateById(productWorkOrder);
|
} else {
|
throw new RuntimeException("操作失败:工单信息不存在");
|
}
|
}
|
|
return true;
|
}
|
|
@Override
|
@Transactional(rollbackFor = Exception.class)
|
public Object delete(List<Long> ids) {
|
List<ProductionMachineRecord> productionMachineRecords = this.listByIds(ids);
|
for (ProductionMachineRecord productionMachineRecord : productionMachineRecords) {
|
ProductWorkOrder productWorkOrder = productWorkOrderMapper.selectById(productionMachineRecord.getWorkOrderId());
|
if (productWorkOrder != null) {
|
String operatorId = productionMachineRecord.getOperatorId();
|
if (operatorId != null && !operatorId.isEmpty()) {
|
List<String> operatorIdList = Arrays.asList(operatorId.split(","));
|
List<String> userIdsToRemove = new ArrayList<>();
|
for (String opId : operatorIdList) {
|
String trimmedOpId = opId.trim();
|
Long count = this.lambdaQuery()
|
.eq(ProductionMachineRecord::getWorkOrderId, productionMachineRecord.getWorkOrderId())
|
.ne(ProductionMachineRecord::getId, productionMachineRecord.getId())
|
.apply("FIND_IN_SET({0}, operator_id)", trimmedOpId)
|
.count();
|
if (count == 0) {
|
userIdsToRemove.add(trimmedOpId);
|
}
|
}
|
if (!userIdsToRemove.isEmpty()) {
|
String userIds = productWorkOrder.getUserIds();
|
if (userIds != null && !userIds.isEmpty()) {
|
List<String> userIdList = new ArrayList<>(Arrays.asList(userIds.split(",")));
|
userIdList.removeAll(userIdsToRemove);
|
productWorkOrder.setUserIds(String.join(",", userIdList));
|
productWorkOrderMapper.updateById(productWorkOrder);
|
}
|
}
|
}
|
}
|
}
|
return this.removeByIds(ids);
|
}
|
}
|