zouxuan vor 1 Jahr
Ursprung
Commit
28cc9dcc10

+ 8 - 0
mec-application/src/main/java/com/ym/mec/web/controller/GoodsController.java

@@ -13,6 +13,7 @@ import com.ym.mec.biz.service.GoodsCategoryService;
 import com.ym.mec.biz.service.GoodsService;
 import com.ym.mec.biz.service.SysUserService;
 import com.ym.mec.common.controller.BaseController;
+import com.ym.mec.common.entity.HttpResponseResult;
 import io.swagger.annotations.Api;
 import io.swagger.annotations.ApiOperation;
 import io.swagger.annotations.ApiParam;
@@ -166,4 +167,11 @@ public class GoodsController extends BaseController {
     public Object queryGoodsBrandList() {
         return succeed(goodsService.queryGoodsBrandList());
     }
+
+    @ApiOperation(value = "复制商品")
+    @PostMapping("/copyGoods")
+    public HttpResponseResult<Object> copyGoods(Integer goodsId){
+        goodsService.copyGoods(goodsId);
+        return succeed();
+    }
 }

+ 2 - 0
mec-biz/src/main/java/com/ym/mec/biz/service/GoodsService.java

@@ -165,4 +165,6 @@ public interface GoodsService extends BaseService<Integer, Goods> {
     List<ProductCategoryDto> queryGoodsCategoryList();
 
     Map<String,String> getBrandMap();
+
+    void copyGoods(Integer goodsId);
 }

+ 33 - 1
mec-biz/src/main/java/com/ym/mec/biz/service/impl/GoodsServiceImpl.java

@@ -35,6 +35,7 @@ import com.ym.mec.util.excel.POIUtil;
 import com.ym.mec.util.ini.IniFileUtil;
 import com.ym.mec.util.upload.UploadUtil;
 import lombok.extern.slf4j.Slf4j;
+import org.apache.commons.collections.CollectionUtils;
 import org.apache.commons.lang.math.NumberUtils;
 import org.apache.commons.lang3.StringUtils;
 import org.apache.poi.ss.usermodel.PictureData;
@@ -47,7 +48,6 @@ import org.springframework.core.io.ClassPathResource;
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Isolation;
 import org.springframework.transaction.annotation.Transactional;
-import org.springframework.util.CollectionUtils;
 import org.springframework.web.multipart.MultipartFile;
 
 import javax.annotation.Resource;
@@ -1512,4 +1512,36 @@ public class GoodsServiceImpl extends BaseServiceImpl<Integer, Goods>  implement
         }
         return new HashMap<>();
     }
+
+    @Override
+	@Transactional(rollbackFor = Exception.class)
+    public void copyGoods(Integer goodsId) {
+		List<GoodsSub> goodsSubs = goodsSubService.lambdaQuery().eq(GoodsSub::getGoodsId, goodsId).list();
+		Goods goods = goodsDao.get(goodsId);
+		if (goods == null) {
+			throw new BizException("商品不存在");
+		}
+		Date now = new Date();
+		if(goods.getName().contains("-副本")){
+			//截取副本后的数字
+			String copyNum = goods.getName().substring(goods.getName().lastIndexOf("-副本") + 3);
+			if(NumberUtils.isNumber(copyNum)){
+				goods.setName(goods.getName().replace("-副本"+copyNum,"")+"-副本"+(Integer.parseInt(copyNum)+1));
+			}else{
+				goods.setName(goods.getName()+"-副本1");
+			}
+		}
+		goods.setId(null);
+		goods.setStatus(YesOrNoEnum.NO);
+		goods.setCreateTime(now);
+		goods.setUpdateTime(now);
+		goodsDao.insert(goods);
+		if(CollectionUtils.isNotEmpty(goodsSubs)){
+			List<GoodsSub> newGoodsSubs = goodsSubs.stream().peek(goodsSub -> {
+				goodsSub.setId(null);
+				goodsSub.setGoodsId(goods.getId());
+            }).collect(Collectors.toList());
+			goodsSubService.saveBatch(newGoodsSubs);
+		}
+	}
 }