|
@@ -10,6 +10,7 @@ import com.qs.mp.admin.domain.Goods;
|
|
|
import com.qs.mp.admin.domain.GoodsCard;
|
|
|
import com.qs.mp.admin.domain.GoodsSku;
|
|
|
import com.qs.mp.admin.domain.GoodsTagRel;
|
|
|
+import com.qs.mp.admin.domain.excel.GoodsCardImportExcel;
|
|
|
import com.qs.mp.admin.domain.excel.GoodsExcel;
|
|
|
import com.qs.mp.admin.domain.excel.UserTicketOrderItemExcel;
|
|
|
import com.qs.mp.admin.domain.param.GoodsParam;
|
|
@@ -24,11 +25,13 @@ import com.qs.mp.admin.service.IGoodsTagRelService;
|
|
|
import com.qs.mp.common.annotation.Log;
|
|
|
import com.qs.mp.common.core.domain.AjaxResult;
|
|
|
import com.qs.mp.common.core.page.TableDataInfo;
|
|
|
+import com.qs.mp.common.core.redis.DistributedLocker;
|
|
|
import com.qs.mp.common.enums.BusinessType;
|
|
|
import com.qs.mp.common.enums.ErrorCodeEnum;
|
|
|
import com.qs.mp.common.enums.GoodsStatusEnum;
|
|
|
import com.qs.mp.common.enums.GoodsTypeEnum;
|
|
|
import com.qs.mp.common.utils.LogUtil;
|
|
|
+import com.qs.mp.framework.redis.RedisLockKey;
|
|
|
import com.qs.mp.utils.ExcelUtil;
|
|
|
import com.qs.mp.web.controller.common.BaseApiController;
|
|
|
import io.swagger.annotations.*;
|
|
@@ -40,6 +43,7 @@ import java.util.ArrayList;
|
|
|
import java.util.HashSet;
|
|
|
import java.util.LinkedHashMap;
|
|
|
import java.util.List;
|
|
|
+import java.util.concurrent.TimeUnit;
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
import ma.glasnost.orika.MapperFacade;
|
|
@@ -52,6 +56,7 @@ import org.springframework.stereotype.Component;
|
|
|
import org.springframework.util.CollectionUtils;
|
|
|
import org.springframework.validation.annotation.Validated;
|
|
|
import org.springframework.web.bind.annotation.*;
|
|
|
+import org.springframework.web.multipart.MultipartFile;
|
|
|
|
|
|
/**
|
|
|
* @auther liugl
|
|
@@ -79,6 +84,78 @@ public class GoodsMgrController extends BaseApiController {
|
|
|
@Autowired
|
|
|
private IGoodsCardService goodsCardService;
|
|
|
|
|
|
+ @Autowired
|
|
|
+ private DistributedLocker distributedLocker;
|
|
|
+
|
|
|
+
|
|
|
+ @Log(title = "导入卡密", businessType = BusinessType.INSERT)
|
|
|
+ @PostMapping("/card/import/{goodsId}")
|
|
|
+ @ApiOperation("导入卡密")
|
|
|
+ @PreAuthorize("@ss.hasPermi('business:goods:edit')")
|
|
|
+ public AjaxResult cardImport(@PathVariable("goodsId") Long goodsId, @RequestParam("file") MultipartFile file){
|
|
|
+ Goods goods = goodsService.getById(goodsId);
|
|
|
+ if (goods == null) {
|
|
|
+ return AjaxResult.error("商品不存在");
|
|
|
+ }
|
|
|
+ if (file == null) {
|
|
|
+ return AjaxResult.error("文件不能为空");
|
|
|
+ }
|
|
|
+ String lockKey = RedisLockKey.build(RedisLockKey.GOODS_CARD_LOCK_KEY, goodsId);
|
|
|
+ // 加锁,自动续期,防止重复导入
|
|
|
+ if (!distributedLocker.tryLock(lockKey,0,-1, TimeUnit.SECONDS)) {
|
|
|
+ return AjaxResult.error("频繁操作,请稍后再试");
|
|
|
+ }
|
|
|
+ try {
|
|
|
+ // 解析excel中的数据
|
|
|
+ ExcelUtil<GoodsCardImportExcel> excelUtil = new ExcelUtil<>(GoodsCardImportExcel.class);
|
|
|
+ List<GoodsCardImportExcel> cardList = excelUtil.importExcel(file.getInputStream());
|
|
|
+ if (CollectionUtils.isEmpty(cardList)) {
|
|
|
+ return AjaxResult.error("导入数据为空");
|
|
|
+ }
|
|
|
+
|
|
|
+ // 校验excel中的卡密是否重复和空值
|
|
|
+ List<String> cardPwdList = new ArrayList<>();
|
|
|
+ for (GoodsCardImportExcel goodsCardImportExcel : cardList) {
|
|
|
+ if (StringUtils.isBlank(goodsCardImportExcel.getCardPwd())) {
|
|
|
+ return AjaxResult.error("导入卡密存在空值");
|
|
|
+ }
|
|
|
+ cardPwdList.add(goodsCardImportExcel.getCardPwd());
|
|
|
+ }
|
|
|
+ long count = cardPwdList.stream().distinct().count();
|
|
|
+ if (cardPwdList.size() != count) {
|
|
|
+ return AjaxResult.error("导入卡密存在重复值");
|
|
|
+ }
|
|
|
+
|
|
|
+ // 库中是否存在卡密
|
|
|
+ int goodsCardCount = goodsCardService.count(new LambdaQueryWrapper<GoodsCard>()
|
|
|
+ .eq(GoodsCard::getGoodsId, goodsId)
|
|
|
+ .in(GoodsCard::getCardPwd, cardPwdList));
|
|
|
+ if (goodsCardCount > 0) {
|
|
|
+ return AjaxResult.error("库中已存在导入的部分卡密");
|
|
|
+ }
|
|
|
+
|
|
|
+ // 导入卡密
|
|
|
+ List<GoodsCard> goodsCardList = cardList.stream().map(goodsCardImportExcel -> {
|
|
|
+ GoodsCard goodsCard = new GoodsCard();
|
|
|
+ BeanUtils.copyProperties(goodsCardImportExcel, goodsCard);
|
|
|
+ goodsCard.setGoodsId(goodsId);
|
|
|
+ return goodsCard;
|
|
|
+ }).collect(Collectors.toList());
|
|
|
+
|
|
|
+ boolean rtn = goodsCardService.saveBatch(goodsCardList);
|
|
|
+ if (!rtn) {
|
|
|
+ return AjaxResult.error("导入失败,请重试");
|
|
|
+ }
|
|
|
+
|
|
|
+ } catch (Exception e) {
|
|
|
+ LogUtil.error(logger, e, "导入卡密异常,goodsId:{0}", goodsId);
|
|
|
+ return AjaxResult.error("导入卡密异常");
|
|
|
+ } finally {
|
|
|
+ distributedLocker.unlock(lockKey);
|
|
|
+ }
|
|
|
+ return AjaxResult.success();
|
|
|
+ }
|
|
|
+
|
|
|
|
|
|
/**
|
|
|
* 查询商品列表, 支持翻页
|