|
|
@@ -20,6 +20,9 @@ import org.dromara.product.service.IProductAssociateService;
|
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
|
import java.util.Collection;
|
|
|
+import java.util.Set;
|
|
|
+import java.util.HashSet;
|
|
|
+import java.util.Arrays;
|
|
|
|
|
|
/**
|
|
|
* 产品关联Service业务层处理
|
|
|
@@ -45,6 +48,20 @@ public class ProductAssociateServiceImpl extends ServiceImpl<ProductAssociateMa
|
|
|
return baseMapper.selectVoById(id);
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 根据产品ID查询关联信息
|
|
|
+ *
|
|
|
+ * @param productId 产品ID
|
|
|
+ * @return 产品关联
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public ProductAssociateVo queryByProductId(Long productId){
|
|
|
+ LambdaQueryWrapper<ProductAssociate> lqw = Wrappers.lambdaQuery();
|
|
|
+ lqw.eq(ProductAssociate::getProductId, productId);
|
|
|
+ lqw.last("LIMIT 1"); // 防止多条记录报错
|
|
|
+ return baseMapper.selectVoOne(lqw);
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* 分页查询产品关联列表
|
|
|
*
|
|
|
@@ -91,13 +108,20 @@ public class ProductAssociateServiceImpl extends ServiceImpl<ProductAssociateMa
|
|
|
*/
|
|
|
@Override
|
|
|
public Boolean insertByBo(ProductAssociateBo bo) {
|
|
|
- ProductAssociate add = MapstructUtils.convert(bo, ProductAssociate.class);
|
|
|
- validEntityBeforeSave(add);
|
|
|
- boolean flag = baseMapper.insert(add) > 0;
|
|
|
- if (flag) {
|
|
|
- bo.setId(add.getId());
|
|
|
+ // 保存当前商品的关联
|
|
|
+ boolean result = saveOrUpdateAssociate(bo.getProductId(), bo.getProductIds(), bo.getIsUnidirectional(), bo.getRelatedTitle(), bo.getRemark());
|
|
|
+
|
|
|
+ // 如果是双向关联(isUnidirectional = "0"),给被关联的商品也添加关联
|
|
|
+ if ("0".equals(bo.getIsUnidirectional()) && StringUtils.isNotBlank(bo.getProductIds())) {
|
|
|
+ String[] targetIds = bo.getProductIds().split(",");
|
|
|
+ for (String targetId : targetIds) {
|
|
|
+ if (StringUtils.isNotBlank(targetId)) {
|
|
|
+ addBidirectionalAssociate(Long.parseLong(targetId.trim()), bo.getProductId());
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
- return flag;
|
|
|
+
|
|
|
+ return result;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -108,9 +132,112 @@ public class ProductAssociateServiceImpl extends ServiceImpl<ProductAssociateMa
|
|
|
*/
|
|
|
@Override
|
|
|
public Boolean updateByBo(ProductAssociateBo bo) {
|
|
|
- ProductAssociate update = MapstructUtils.convert(bo, ProductAssociate.class);
|
|
|
- validEntityBeforeSave(update);
|
|
|
- return baseMapper.updateById(update) > 0;
|
|
|
+ // 先获取旧的关联数据,用于处理双向关联的移除
|
|
|
+ ProductAssociate oldAssociate = baseMapper.selectById(bo.getId());
|
|
|
+
|
|
|
+ // 保存当前商品的关联
|
|
|
+ boolean result = saveOrUpdateAssociate(bo.getProductId(), bo.getProductIds(), bo.getIsUnidirectional(), bo.getRelatedTitle(), bo.getRemark());
|
|
|
+
|
|
|
+ // 如果是双向关联,处理新增和移除
|
|
|
+ if ("0".equals(bo.getIsUnidirectional())) {
|
|
|
+ Set<String> oldIds = new HashSet<>();
|
|
|
+ Set<String> newIds = new HashSet<>();
|
|
|
+
|
|
|
+ if (oldAssociate != null && StringUtils.isNotBlank(oldAssociate.getProductIds())) {
|
|
|
+ oldIds.addAll(Arrays.asList(oldAssociate.getProductIds().split(",")));
|
|
|
+ }
|
|
|
+ if (StringUtils.isNotBlank(bo.getProductIds())) {
|
|
|
+ newIds.addAll(Arrays.asList(bo.getProductIds().split(",")));
|
|
|
+ }
|
|
|
+
|
|
|
+ // 新增的关联:在newIds中但不在oldIds中
|
|
|
+ for (String newId : newIds) {
|
|
|
+ if (StringUtils.isNotBlank(newId) && !oldIds.contains(newId)) {
|
|
|
+ addBidirectionalAssociate(Long.parseLong(newId.trim()), bo.getProductId());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 移除的关联:在oldIds中但不在newIds中
|
|
|
+ for (String oldId : oldIds) {
|
|
|
+ if (StringUtils.isNotBlank(oldId) && !newIds.contains(oldId)) {
|
|
|
+ removeBidirectionalAssociate(Long.parseLong(oldId.trim()), bo.getProductId());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 保存或更新关联记录
|
|
|
+ */
|
|
|
+ private boolean saveOrUpdateAssociate(Long productId, String productIds, String isUnidirectional, String relatedTitle, String remark) {
|
|
|
+ LambdaQueryWrapper<ProductAssociate> lqw = Wrappers.lambdaQuery();
|
|
|
+ lqw.eq(ProductAssociate::getProductId, productId);
|
|
|
+ ProductAssociate existing = baseMapper.selectOne(lqw);
|
|
|
+
|
|
|
+ if (existing != null) {
|
|
|
+ existing.setProductIds(productIds);
|
|
|
+ existing.setIsUnidirectional(isUnidirectional);
|
|
|
+ existing.setRelatedTitle(relatedTitle);
|
|
|
+ existing.setRemark(remark);
|
|
|
+ return baseMapper.updateById(existing) > 0;
|
|
|
+ }
|
|
|
+
|
|
|
+ ProductAssociate add = new ProductAssociate();
|
|
|
+ add.setProductId(productId);
|
|
|
+ add.setProductIds(productIds);
|
|
|
+ add.setIsUnidirectional(isUnidirectional);
|
|
|
+ add.setRelatedTitle(relatedTitle);
|
|
|
+ add.setRemark(remark);
|
|
|
+ return baseMapper.insert(add) > 0;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 给目标商品添加双向关联(把sourceProductId加到目标商品的关联列表中)
|
|
|
+ */
|
|
|
+ private void addBidirectionalAssociate(Long targetProductId, Long sourceProductId) {
|
|
|
+ LambdaQueryWrapper<ProductAssociate> lqw = Wrappers.lambdaQuery();
|
|
|
+ lqw.eq(ProductAssociate::getProductId, targetProductId);
|
|
|
+ ProductAssociate targetAssociate = baseMapper.selectOne(lqw);
|
|
|
+
|
|
|
+ String sourceIdStr = String.valueOf(sourceProductId);
|
|
|
+
|
|
|
+ if (targetAssociate != null) {
|
|
|
+ // 已有记录,追加关联
|
|
|
+ Set<String> ids = new HashSet<>();
|
|
|
+ if (StringUtils.isNotBlank(targetAssociate.getProductIds())) {
|
|
|
+ ids.addAll(Arrays.asList(targetAssociate.getProductIds().split(",")));
|
|
|
+ }
|
|
|
+ if (!ids.contains(sourceIdStr)) {
|
|
|
+ ids.add(sourceIdStr);
|
|
|
+ targetAssociate.setProductIds(String.join(",", ids));
|
|
|
+ baseMapper.updateById(targetAssociate);
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ // 没有记录,新建
|
|
|
+ ProductAssociate newAssociate = new ProductAssociate();
|
|
|
+ newAssociate.setProductId(targetProductId);
|
|
|
+ newAssociate.setProductIds(sourceIdStr);
|
|
|
+ newAssociate.setIsUnidirectional("0"); // 双向
|
|
|
+ baseMapper.insert(newAssociate);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 从目标商品移除双向关联(把sourceProductId从目标商品的关联列表中移除)
|
|
|
+ */
|
|
|
+ private void removeBidirectionalAssociate(Long targetProductId, Long sourceProductId) {
|
|
|
+ LambdaQueryWrapper<ProductAssociate> lqw = Wrappers.lambdaQuery();
|
|
|
+ lqw.eq(ProductAssociate::getProductId, targetProductId);
|
|
|
+ ProductAssociate targetAssociate = baseMapper.selectOne(lqw);
|
|
|
+
|
|
|
+ if (targetAssociate != null && StringUtils.isNotBlank(targetAssociate.getProductIds())) {
|
|
|
+ Set<String> ids = new HashSet<>(Arrays.asList(targetAssociate.getProductIds().split(",")));
|
|
|
+ ids.remove(String.valueOf(sourceProductId));
|
|
|
+ targetAssociate.setProductIds(String.join(",", ids));
|
|
|
+ baseMapper.updateById(targetAssociate);
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
/**
|