package com.ruoyi.basic.service.impl;
|
|
import cn.hutool.core.date.DateUtil;
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
import com.baomidou.mybatisplus.core.metadata.IPage;
|
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
|
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
import com.ruoyi.basic.dto.ProductPartDto;
|
import com.ruoyi.basic.mapper.ProductPartLogMapper;
|
import com.ruoyi.basic.pojo.ProductPartLog;
|
import com.ruoyi.common.exception.base.BaseException;
|
import com.ruoyi.common.utils.QueryWrappers;
|
import com.ruoyi.basic.mapper.ProductPartMapper;
|
import com.ruoyi.basic.mapper.StructureTestObjectPartMapper;
|
import com.ruoyi.basic.pojo.ProductPart;
|
import com.ruoyi.basic.pojo.StructureTestObjectPart;
|
import com.ruoyi.basic.service.ProductPartService;
|
import com.ruoyi.common.utils.SecurityUtils;
|
import lombok.AllArgsConstructor;
|
import org.springframework.stereotype.Service;
|
import org.springframework.transaction.annotation.Transactional;
|
|
import java.time.LocalDateTime;
|
import java.util.List;
|
import java.util.Map;
|
|
@Transactional
|
@Service
|
@AllArgsConstructor
|
public class ProductPartServiceImpl extends ServiceImpl<ProductPartMapper, ProductPart> implements ProductPartService {
|
|
private ProductPartMapper productPartMapper;
|
private StructureTestObjectPartMapper structureTestObjectPartMapper;
|
|
private ProductPartLogMapper productPartLogMapper;
|
|
|
@Override
|
public IPage<ProductPart> selectByProductId(IPage<ProductPart> page, ProductPart productPart) {
|
return productPartMapper.selectListByProductId(page, QueryWrappers.queryWrappers(productPart), productPart.getProductId());
|
}
|
|
@Override
|
public void addProductPart(ProductPart productPart) {
|
if (productPart.getProductId() == null) {
|
throw new BaseException("缺少产品对象id");
|
}
|
this.isPartNoExist(productPart.getPartNo(), productPart.getProductId(), null);
|
productPart.setReview("待复核");
|
productPartMapper.insert(productPart);
|
}
|
|
@Override
|
public int updateProductPartById(ProductPart productPart) {
|
if (productPart.getProductId() == null) {
|
throw new BaseException("缺少产品对象id");
|
}
|
// this.isPartNoExist(productPart.getPartNo(), productPart.getProductId(), null);
|
if (!"已复核".equals(productPart.getReview())) {
|
productPart.setReview("待复核");
|
}
|
return productPartMapper.updateById(productPart);
|
}
|
|
@Override
|
public List<Map<String, Object>> inspectionItems(String sample, String productName) {
|
// 优先处理两个参数均非空的场景
|
if (StringUtils.isNotEmpty(productName) && StringUtils.isNotEmpty(sample)) {
|
//先差产品,有过有值直接返回,没值再查大类
|
List<Map<String, Object>> result = productPartMapper.inspectionItems(productName);
|
return result.isEmpty() ? productPartMapper.inspectionItems(sample) : result;
|
}
|
|
// 其他情况直接选取有效参数(优先productName)或空字符串
|
String param = StringUtils.isNotEmpty(productName) ? productName :
|
StringUtils.isNotEmpty(sample) ? sample : "";
|
return productPartMapper.inspectionItems(param);
|
|
}
|
|
@Override
|
public int productPartReviewById(ProductPartDto productPartDto) {
|
int userId = SecurityUtils.getUserId().intValue();
|
String username = SecurityUtils.getUsername();
|
|
// 定义通用的产品部分对象
|
Object productPart = null;
|
if ("对象".equals(productPartDto.getType())) {
|
productPart = structureTestObjectPartMapper.selectById(productPartDto.getId());
|
if (productPart != null && ((StructureTestObjectPart) productPart).getTestObjectId() == null) {
|
throw new BaseException("缺少产品对象id");
|
}
|
} else {
|
productPart = productPartMapper.selectById(productPartDto.getId());
|
if (productPart != null && ((ProductPart) productPart).getProductId() == null) {
|
throw new BaseException("缺少产品对象id");
|
}
|
}
|
|
if (productPart == null) {
|
return 0;
|
}
|
|
// 设置审核状态
|
if (productPart instanceof StructureTestObjectPart) {
|
((StructureTestObjectPart) productPart).setReview("已复核");
|
} else {
|
((ProductPart) productPart).setReview("已复核");
|
}
|
|
// 更新产品部分信息
|
int num = 0;
|
if (productPart instanceof StructureTestObjectPart) {
|
num = structureTestObjectPartMapper.updateById((StructureTestObjectPart) productPart);
|
} else {
|
num = productPartMapper.updateById((ProductPart) productPart);
|
}
|
|
// 如果更新成功,创建或更新日志
|
if (num > 0) {
|
createOrUpdateProductPartLog(productPart, username, userId, productPartDto.getType(), productPartDto);
|
}
|
|
return num;
|
}
|
|
private void createOrUpdateProductPartLog(Object productPart, String username, int userId, String type, ProductPartDto productPartDto) {
|
ProductPartLog productPartLog = new ProductPartLog();
|
if (productPart instanceof StructureTestObjectPart) {
|
StructureTestObjectPart part = (StructureTestObjectPart) productPart;
|
productPartLog.setProductPartId(part.getId());
|
productPartLog.setColor(part.getColor());
|
productPartLog.setColorCode(part.getColorCode());
|
productPartLog.setPartNo(part.getPartNo());
|
productPartLog.setInspectionItem(part.getInspectionItem());
|
productPartLog.setReview(part.getReview());
|
} else {
|
ProductPart part = (ProductPart) productPart;
|
productPartLog.setProductPartId(part.getId());
|
productPartLog.setColor(part.getColor());
|
productPartLog.setColorCode(part.getColorCode());
|
productPartLog.setPartNo(part.getPartNo());
|
productPartLog.setInspectionItem(part.getInspectionItem());
|
productPartLog.setReview(part.getReview());
|
}
|
productPartLog.setOperName(username);
|
productPartLog.setOperId(userId);
|
productPartLog.setOperTime(LocalDateTime.now());
|
productPartLog.setType(type);
|
|
// 检查是否存在相同 ProductPartId 的日志记录
|
ProductPartLog existingLog = productPartLogMapper.selectOne(new LambdaQueryWrapper<ProductPartLog>().eq(ProductPartLog::getProductPartId, productPartDto.getId()));
|
if (existingLog != null) {
|
// 如果存在,更新记录
|
productPartLog.setId(existingLog.getId()); // 设置更新记录的 ID
|
productPartLogMapper.updateById(productPartLog);
|
} else {
|
// 如果不存在,插入新记录
|
productPartLogMapper.insert(productPartLog);
|
}
|
}
|
|
|
@Override
|
public IPage<ProductPartLog> productPartLogList(Page page, Integer id, String type) {
|
return productPartLogMapper.selectPage(page, Wrappers.<ProductPartLog>lambdaQuery()
|
.eq(ProductPartLog::getProductPartId, id)
|
.eq(ProductPartLog::getType, type));
|
}
|
|
// 判断零件号是否存在
|
public void isPartNoExist(String partNo, Integer productId, Integer id) {
|
// 零件号唯一 但不必填
|
if (StringUtils.isNotBlank(partNo)) {
|
Long count = productPartMapper.selectCount(new LambdaQueryWrapper<ProductPart>()
|
// .eq(ProductPart::getProductId, productId)
|
.eq(ProductPart::getPartNo, partNo)
|
.ne(id != null, ProductPart::getId, id));
|
Long selectCount = structureTestObjectPartMapper.selectCount(Wrappers.<StructureTestObjectPart>lambdaQuery()
|
.eq(StructureTestObjectPart::getPartNo, partNo));
|
if (count > 0 || selectCount > 0) {
|
throw new BaseException("该零件号已绑定过检验对象");
|
}
|
} else {
|
throw new BaseException("请输入零件号");
|
}
|
}
|
}
|