|
@@ -3,6 +3,7 @@ package com.izouma.dingdong.service.merchant;
|
|
|
import cn.hutool.core.collection.CollUtil;
|
|
import cn.hutool.core.collection.CollUtil;
|
|
|
import cn.hutool.core.util.ObjectUtil;
|
|
import cn.hutool.core.util.ObjectUtil;
|
|
|
import cn.hutool.core.util.StrUtil;
|
|
import cn.hutool.core.util.StrUtil;
|
|
|
|
|
+import com.izouma.dingdong.converter.LongArrayConverter;
|
|
|
import com.izouma.dingdong.converter.StringArrayConverter;
|
|
import com.izouma.dingdong.converter.StringArrayConverter;
|
|
|
import com.izouma.dingdong.domain.merchant.Goods;
|
|
import com.izouma.dingdong.domain.merchant.Goods;
|
|
|
import com.izouma.dingdong.domain.merchant.MerchantClassification;
|
|
import com.izouma.dingdong.domain.merchant.MerchantClassification;
|
|
@@ -14,6 +15,7 @@ import org.apache.commons.lang3.StringUtils;
|
|
|
import org.springframework.stereotype.Service;
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
|
|
import java.util.List;
|
|
import java.util.List;
|
|
|
|
|
+import java.util.Set;
|
|
|
|
|
|
|
|
@Service
|
|
@Service
|
|
|
@AllArgsConstructor
|
|
@AllArgsConstructor
|
|
@@ -31,7 +33,7 @@ public class MerchantClassificationService {
|
|
|
*/
|
|
*/
|
|
|
public MerchantClassification add(Long id, MerchantClassification classification) {
|
|
public MerchantClassification add(Long id, MerchantClassification classification) {
|
|
|
Long merchantId = merchantService.findMerchantId(id);
|
|
Long merchantId = merchantService.findMerchantId(id);
|
|
|
- if (ObjectUtil.isNotNull(merchantClassificationRepo.findByMerchantIdAndName(merchantId, classification.getName()))){
|
|
|
|
|
|
|
+ if (ObjectUtil.isNotNull(merchantClassificationRepo.findByMerchantIdAndName(merchantId, classification.getName()))) {
|
|
|
throw new BusinessException("该分类已存在");
|
|
throw new BusinessException("该分类已存在");
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -57,12 +59,10 @@ public class MerchantClassificationService {
|
|
|
*/
|
|
*/
|
|
|
public List<Goods> showGoods(Long classificationId) {
|
|
public List<Goods> showGoods(Long classificationId) {
|
|
|
MerchantClassification merchantClassification = merchantClassificationRepo.findById(classificationId).orElseThrow(new BusinessException("分类不存在"));
|
|
MerchantClassification merchantClassification = merchantClassificationRepo.findById(classificationId).orElseThrow(new BusinessException("分类不存在"));
|
|
|
-
|
|
|
|
|
- StringArrayConverter converter = new StringArrayConverter();
|
|
|
|
|
- List<String> string = converter.convertToEntityAttribute(merchantClassification.getGoodsIds());
|
|
|
|
|
|
|
+ Set<Long> goodsIds = merchantClassification.getGoodsIds();
|
|
|
List<Goods> goods = CollUtil.newArrayList();
|
|
List<Goods> goods = CollUtil.newArrayList();
|
|
|
- string.forEach(s -> {
|
|
|
|
|
- goods.add(goodsRepo.findById(Long.parseLong(s)).orElseThrow(new BusinessException("商品不存在")));
|
|
|
|
|
|
|
+ goodsIds.forEach(s -> {
|
|
|
|
|
+ goods.add(goodsRepo.findById(s).orElseThrow(new BusinessException("商品不存在")));
|
|
|
});
|
|
});
|
|
|
return goods;
|
|
return goods;
|
|
|
}
|
|
}
|
|
@@ -77,24 +77,14 @@ public class MerchantClassificationService {
|
|
|
public MerchantClassification saveGoods(Long classificationId, String string) {
|
|
public MerchantClassification saveGoods(Long classificationId, String string) {
|
|
|
//查找分类
|
|
//查找分类
|
|
|
MerchantClassification merchantClassification = merchantClassificationRepo.findById(classificationId).orElseThrow(new BusinessException("无分类"));
|
|
MerchantClassification merchantClassification = merchantClassificationRepo.findById(classificationId).orElseThrow(new BusinessException("无分类"));
|
|
|
-/* //转成String
|
|
|
|
|
- StringArrayConverter converter = new StringArrayConverter();
|
|
|
|
|
- String string = converter.convertToDatabaseColumn(goodId);*/
|
|
|
|
|
-
|
|
|
|
|
|
|
|
|
|
- String goodsIds = merchantClassification.getGoodsIds();
|
|
|
|
|
|
|
+ Set<Long> goodsIds = merchantClassification.getGoodsIds();
|
|
|
|
|
+ LongArrayConverter converter = new LongArrayConverter();
|
|
|
|
|
+ List<Long> longs = converter.convertToEntityAttribute(string);
|
|
|
|
|
+ goodsIds.addAll(longs);
|
|
|
|
|
|
|
|
-// StringArrayConverter converter = new StringArrayConverter();
|
|
|
|
|
-// List<String> strings1 = converter.convertToEntityAttribute(string);
|
|
|
|
|
-// List<String> strings = converter.convertToEntityAttribute(goodsIds);
|
|
|
|
|
-// strings1.forEach(strings::contains);
|
|
|
|
|
|
|
+ merchantClassification.setGoodsIds(goodsIds);
|
|
|
|
|
|
|
|
- if (StrUtil.isBlank(goodsIds)) {
|
|
|
|
|
- merchantClassification.setGoodsIds(string);
|
|
|
|
|
- } else {
|
|
|
|
|
- //保存
|
|
|
|
|
- merchantClassification.setGoodsIds(goodsIds + "," + string);
|
|
|
|
|
- }
|
|
|
|
|
return merchantClassificationRepo.save(merchantClassification);
|
|
return merchantClassificationRepo.save(merchantClassification);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -104,18 +94,12 @@ public class MerchantClassificationService {
|
|
|
public void saveOneGoods(Long classificationId, Long goodsId) {
|
|
public void saveOneGoods(Long classificationId, Long goodsId) {
|
|
|
//查找分类
|
|
//查找分类
|
|
|
MerchantClassification merchantClassification = merchantClassificationRepo.findById(classificationId).orElseThrow(new BusinessException("无分类"));
|
|
MerchantClassification merchantClassification = merchantClassificationRepo.findById(classificationId).orElseThrow(new BusinessException("无分类"));
|
|
|
- String goodsIds = merchantClassification.getGoodsIds();
|
|
|
|
|
-
|
|
|
|
|
- StringArrayConverter converter = new StringArrayConverter();
|
|
|
|
|
- List<String> strings = converter.convertToEntityAttribute(goodsIds);
|
|
|
|
|
-
|
|
|
|
|
- if (!strings.contains(goodsId.toString())) {
|
|
|
|
|
- if (StrUtil.isBlank(goodsIds)) {
|
|
|
|
|
- merchantClassification.setGoodsIds(goodsId.toString());
|
|
|
|
|
- } else {
|
|
|
|
|
- //保存
|
|
|
|
|
- merchantClassification.setGoodsIds(goodsIds + "," + goodsId);
|
|
|
|
|
- }
|
|
|
|
|
|
|
+
|
|
|
|
|
+ Set<Long> ids = merchantClassification.getGoodsIds();
|
|
|
|
|
+
|
|
|
|
|
+ if (!ids.contains(goodsId)) {
|
|
|
|
|
+ ids.add(goodsId);
|
|
|
|
|
+ merchantClassification.setGoodsIds(ids);
|
|
|
merchantClassificationRepo.save(merchantClassification);
|
|
merchantClassificationRepo.save(merchantClassification);
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
@@ -127,10 +111,10 @@ public class MerchantClassificationService {
|
|
|
//查找分类
|
|
//查找分类
|
|
|
MerchantClassification merchantClassification = merchantClassificationRepo.findById(classificationId).orElseThrow(new BusinessException("无分类"));
|
|
MerchantClassification merchantClassification = merchantClassificationRepo.findById(classificationId).orElseThrow(new BusinessException("无分类"));
|
|
|
|
|
|
|
|
- //替换
|
|
|
|
|
- String replaceAll = merchantClassification.getGoodsIds().replaceAll(",|''" + goodId.toString() + "''|,", "");
|
|
|
|
|
- //保存
|
|
|
|
|
- merchantClassification.setGoodsIds(replaceAll);
|
|
|
|
|
|
|
+ Set<Long> goodsIds = merchantClassification.getGoodsIds();
|
|
|
|
|
+ goodsIds.remove(goodId);
|
|
|
|
|
+ merchantClassification.setGoodsIds(goodsIds);
|
|
|
|
|
+
|
|
|
merchantClassificationRepo.save(merchantClassification);
|
|
merchantClassificationRepo.save(merchantClassification);
|
|
|
}
|
|
}
|
|
|
|
|
|