Browse Source

fixed 后台商品导入、下载导入模板功能

fushengqian 2 months ago
parent
commit
da837d2f46

+ 1 - 0
fuint-application/src/main/java/com/fuint/common/config/WebConfig.java

@@ -68,6 +68,7 @@ public class WebConfig extends WebMvcConfigurationSupport {
                 .excludePathPatterns("/backendApi/captcha/**")
                 .excludePathPatterns("/backendApi/userCoupon/exportList")
                 .excludePathPatterns("/backendApi/order/export")
+                .excludePathPatterns("/backendApi/file/download")
                 .excludePathPatterns("/backendApi/login/**");
 
         // 客户端拦截

+ 11 - 10
fuint-application/src/main/java/com/fuint/common/service/GoodsService.java

@@ -42,6 +42,17 @@ public interface GoodsService {
      */
     MtGoods saveGoods(MtGoods reqDto, String storeIds) throws BusinessCheckException;
 
+    /**
+     * 更新商品状态
+     *
+     * @param  goodsId 商品ID
+     * @param  status 状态
+     * @param  operator 操作人
+     * @throws BusinessCheckException
+     * @return
+     */
+    Boolean updateStatus(Integer goodsId, String status, String operator) throws BusinessCheckException;
+
     /**
      * 根据ID获取商品信息
      *
@@ -79,16 +90,6 @@ public interface GoodsService {
      */
     GoodsDto getGoodsDetail(Integer id, boolean getDeleteSpec) throws InvocationTargetException, IllegalAccessException;
 
-    /**
-     * 根据ID删除
-     *
-     * @param  id 商品ID
-     * @param  operator 操作人
-     * @throws BusinessCheckException
-     * @return
-     */
-    void deleteGoods(Integer id, String operator) throws BusinessCheckException;
-
     /**
      * 获取店铺的商品列表
      *

+ 54 - 24
fuint-application/src/main/java/com/fuint/common/service/impl/GoodsServiceImpl.java

@@ -406,6 +406,51 @@ public class GoodsServiceImpl extends ServiceImpl<MtGoodsMapper, MtGoods> implem
         return mtGoods;
     }
 
+    /**
+     * 更新商品状态
+     *
+     * @param  goodsId 商品ID
+     * @param  status 状态
+     * @param  operator 操作人
+     * @throws BusinessCheckException
+     * @return
+     */
+    @Override
+    public Boolean updateStatus(Integer goodsId, String status, String operator) throws BusinessCheckException {
+        MtGoods mtGoods = queryGoodsById(goodsId);
+        if (null == mtGoods) {
+            throw new BusinessCheckException("该商品不存在");
+        }
+        mtGoods.setStatus(status);
+        mtGoods.setUpdateTime(new Date());
+        mtGoods.setOperator(operator);
+        mtGoodsMapper.updateById(mtGoods);
+        // 删除商品
+        if (status.equals(StatusEnum.DISABLE.getKey())) {
+            Map<String, Object> param = new HashMap<>();
+            param.put("goods_id", goodsId);
+            param.put("status", StatusEnum.ENABLED.getKey());
+            List<MtGoodsSpec> goodsSpecList = mtGoodsSpecMapper.selectByMap(param);
+            if (goodsSpecList != null && goodsSpecList.size() > 0) {
+                for (MtGoodsSpec mtGoodsSpec : goodsSpecList) {
+                     mtGoodsSpec.setStatus(StatusEnum.DISABLE.getKey());
+                     mtGoodsSpecMapper.updateById(mtGoodsSpec);
+                }
+            }
+            Map<String, Object> param1 = new HashMap<>();
+            param1.put("goods_id", goodsId);
+            param1.put("status", StatusEnum.ENABLED.getKey());
+            List<MtGoodsSku> goodsSkuList = mtGoodsSkuMapper.selectByMap(param1);
+            if (goodsSkuList != null && goodsSkuList.size() > 0) {
+                for (MtGoodsSku mtGoodsSku : goodsSkuList) {
+                     mtGoodsSku.setStatus(StatusEnum.DISABLE.getKey());
+                     mtGoodsSkuMapper.updateById(mtGoodsSku);
+                }
+            }
+        }
+        return true;
+    }
+
     /**
      * 根据ID获取商品信息
      *
@@ -528,28 +573,6 @@ public class GoodsServiceImpl extends ServiceImpl<MtGoodsMapper, MtGoods> implem
         return goodsInfo;
     }
 
-    /**
-     * 根据ID删除商品信息
-     *
-     * @param  id ID
-     * @param  operator 操作人
-     * @throws BusinessCheckException
-     * @return
-     */
-    @Override
-    @OperationServiceLog(description = "删除商品信息")
-    @Transactional(rollbackFor = Exception.class)
-    public void deleteGoods(Integer id, String operator) throws BusinessCheckException {
-        MtGoods cateInfo = queryGoodsById(id);
-        if (null == cateInfo) {
-            throw new BusinessCheckException("该商品不存在");
-        }
-        cateInfo.setStatus(StatusEnum.DISABLE.getKey());
-        cateInfo.setUpdateTime(new Date());
-        cateInfo.setOperator(operator);
-        mtGoodsMapper.updateById(cateInfo);
-    }
-
     /**
      * 获取店铺的商品列表
      *
@@ -909,8 +932,9 @@ public class GoodsServiceImpl extends ServiceImpl<MtGoodsMapper, MtGoods> implem
                  mtGoods.setSalePoint(goods.get(11));
                  mtGoods.setDescription(goods.get(12));
                  mtGoods.setPrice(new BigDecimal("0"));
+                 mtGoods.setLinePrice(new BigDecimal("0"));
                  mtGoods.setStock(0);
-                 mtGoods.setStatus(StatusEnum.ENABLED.getKey());
+                 mtGoods.setStatus(StatusEnum.FORBIDDEN.getKey());
                  saveGoods(mtGoods, storeIds);
             }
         }
@@ -919,7 +943,13 @@ public class GoodsServiceImpl extends ServiceImpl<MtGoodsMapper, MtGoods> implem
         if (skuList != null && skuList.size() > 0) {
             for (int j = 0; j < skuList.size(); j++) {
                  List<String> sku = skuList.get(j);
-                 MtGoods mtGoods = mtGoodsMapper.getByGoodsName(accountInfo.getMerchantId(), sku.get(0));
+                 List<MtGoods> goodsList1 = mtGoodsMapper.getByGoodsName(accountInfo.getMerchantId(), sku.get(0));
+                 MtGoods mtGoods = null;
+                 if (goodsList1.size() == 1) {
+                     mtGoods = goodsList1.get(0);
+                 } else if (goodsList1.size() > 1) {
+                     throw new BusinessCheckException("商品导入失败,存在重复商品名称:" + sku.get(0));
+                 }
                  if (mtGoods != null) {
                      // 单规格
                      if (mtGoods.getIsSingleSpec().equals(YesOrNoEnum.YES.getKey())) {

+ 1 - 1
fuint-application/src/main/java/com/fuint/common/service/impl/OrderServiceImpl.java

@@ -2090,7 +2090,7 @@ public class OrderServiceImpl extends ServiceImpl<MtOrderMapper, MtOrder> implem
                         } else {
                             couponDto.setDescription("满" + couponInfo.getOutRule() + "元可用");
                             BigDecimal conditionAmount = new BigDecimal(couponInfo.getOutRule());
-                            if (totalPrice.compareTo(conditionAmount) > 0 && isEffective) {
+                            if (totalPrice.compareTo(conditionAmount) >= 0 && isEffective) {
                                 couponDto.setStatus(UserCouponStatusEnum.UNUSED.getKey());
                             }
                         }

+ 38 - 3
fuint-application/src/main/java/com/fuint/module/backendApi/controller/BackendFileController.java

@@ -13,6 +13,7 @@ import com.fuint.utils.StringUtil;
 import io.swagger.annotations.Api;
 import io.swagger.annotations.ApiOperation;
 import lombok.AllArgsConstructor;
+import org.springframework.security.access.prepost.PreAuthorize;
 import org.springframework.util.ResourceUtils;
 import org.springframework.web.bind.annotation.CrossOrigin;
 import org.springframework.web.bind.annotation.RequestMapping;
@@ -24,7 +25,9 @@ import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.core.env.Environment;
 import javax.servlet.http.HttpServletRequest;
-import java.io.File;
+import javax.servlet.http.HttpServletResponse;
+import java.io.*;
+import java.net.URL;
 import java.util.Date;
 import java.util.HashMap;
 import java.util.Map;
@@ -110,8 +113,6 @@ public class BackendFileController extends BaseController {
             return getFailureResult(201, "上传的文件不能大于" + maxSize + "MB");
         }
 
-        String fileType = file.getContentType();
-
         // 保存文件
         try {
             String fileName = saveFile(file);
@@ -158,6 +159,40 @@ public class BackendFileController extends BaseController {
         return getSuccessResult(resultMap);
     }
 
+    /**
+     * 下载文件
+     *
+     * @return
+     */
+    @ApiOperation(value = "下载文件")
+    @RequestMapping(value = "/download", method = RequestMethod.GET)
+    @CrossOrigin
+    public void download(HttpServletRequest request, HttpServletResponse response) {
+        String token = request.getParameter("token");
+        AccountInfo accountInfo = TokenUtil.getAccountInfoByToken(token);
+        try {
+            URL resourceUrl = getClass().getClassLoader().getResource("goodsTemplate.xlsx");
+            String path = resourceUrl.getPath();
+            File file = new File(path);
+            String filename = file.getName();
+            InputStream fis = new BufferedInputStream(new FileInputStream(path));
+            byte[] buffer = new byte[fis.available()];
+            fis.read(buffer);
+            fis.close();
+            response.reset();
+            response.addHeader("Content-Disposition", "attachment;filename=" + new String(filename.getBytes()));
+            response.addHeader("Content-Length", "" + file.length());
+            OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
+            response.setContentType("application/octet-stream");
+            toClient.write(buffer);
+            toClient.flush();
+            toClient.close();
+        } catch (IOException ex) {
+            ex.printStackTrace();
+            logger.error("下载文件出错:account = {}", accountInfo.getAccountName());
+        }
+    }
+
     public String saveFile(MultipartFile file) throws Exception {
         String fileName = file.getOriginalFilename();
 

+ 6 - 36
fuint-application/src/main/java/com/fuint/module/backendApi/controller/BackendGoodsController.java

@@ -139,33 +139,6 @@ public class BackendGoodsController extends BaseController {
         return getSuccessResult(result);
     }
 
-    /**
-     * 删除商品
-     *
-     * @param request
-     * @param goodsId 商品ID
-     * @return
-     */
-    @ApiOperation(value = "删除商品")
-    @RequestMapping(value = "/delete/{id}", method = RequestMethod.GET)
-    @CrossOrigin
-    @PreAuthorize("@pms.hasPermission('goods:goods:edit')")
-    public ResponseObject delete(HttpServletRequest request, @PathVariable("id") Integer goodsId) throws BusinessCheckException {
-        String token = request.getHeader("Access-Token");
-
-        AccountInfo accountInfo = TokenUtil.getAccountInfoByToken(token);
-
-        MtGoods mtGoods = goodsService.queryGoodsById(goodsId);
-        if (accountInfo.getMerchantId() != null && accountInfo.getMerchantId() > 0 && !mtGoods.getMerchantId().equals(accountInfo.getMerchantId())) {
-            return getFailureResult(1004);
-        }
-
-        String operator = accountInfo.getAccountName();
-        goodsService.deleteGoods(goodsId, operator);
-
-        return getSuccessResult(true);
-    }
-
     /**
      * 更新商品状态
      *
@@ -178,11 +151,11 @@ public class BackendGoodsController extends BaseController {
     public ResponseObject updateStatus(HttpServletRequest request, @RequestBody Map<String, Object> params) throws BusinessCheckException {
         String token = request.getHeader("Access-Token");
         String status = params.get("status") != null ? params.get("status").toString() : StatusEnum.ENABLED.getKey();
-        Integer id = params.get("id") == null ? 0 : Integer.parseInt(params.get("id").toString());
+        Integer goodsId = params.get("id") == null ? 0 : Integer.parseInt(params.get("id").toString());
 
         AccountInfo accountInfo = TokenUtil.getAccountInfoByToken(token);
 
-        MtGoods mtGoods = goodsService.queryGoodsById(id);
+        MtGoods mtGoods = goodsService.queryGoodsById(goodsId);
         if (mtGoods == null) {
             return getFailureResult(201, "该商品不存在");
         }
@@ -192,12 +165,7 @@ public class BackendGoodsController extends BaseController {
         }
 
         String operator = accountInfo.getAccountName();
-
-        MtGoods goodsInfo = new MtGoods();
-        goodsInfo.setOperator(operator);
-        goodsInfo.setId(id);
-        goodsInfo.setStatus(status);
-        goodsService.saveGoods(goodsInfo, null);
+        goodsService.updateStatus(goodsId, status, operator);
 
         return getSuccessResult(true);
     }
@@ -462,7 +430,9 @@ public class BackendGoodsController extends BaseController {
             // 库存等于所有sku库存相加
             Integer allStock = 0;
             for (LinkedHashMap item : skuList) {
-                 allStock = allStock + Integer.parseInt(item.get("stock").toString());
+                 if (StringUtil.isNotEmpty(item.get("stock").toString())) {
+                     allStock = allStock + Integer.parseInt(item.get("stock").toString());
+                 }
             }
             stock = allStock.toString();
         }

BIN
fuint-application/src/main/resources/商品导入模板.xlsx → fuint-application/src/main/resources/GoodsTemplate.xlsx


+ 1 - 1
fuint-repository/src/main/java/com/fuint/repository/mapper/MtGoodsMapper.java

@@ -23,7 +23,7 @@ public interface MtGoodsMapper extends BaseMapper<MtGoods> {
 
     MtGoods getByGoodsNo(@Param("merchantId") Integer merchantId, @Param("goodsNo") String goodsNo);
 
-    MtGoods getByGoodsName(@Param("merchantId") Integer merchantId, @Param("goodsName") String goodsName);
+    List<MtGoods> getByGoodsName(@Param("merchantId") Integer merchantId, @Param("goodsName") String goodsName);
 
     Boolean updateInitSale(@Param("goodsId") Integer goodsId, @Param("saleNum") Integer saleNum);
 

+ 1 - 1
fuint-repository/src/main/resources/mapper/MtGoodsMapper.xml

@@ -32,7 +32,7 @@
     </select>
 
     <select id="getByGoodsName" resultType="com.fuint.repository.model.MtGoods">
-        select * from mt_goods t where t.NAME = #{goodsName} and t.STATUS = 'A'
+        select * from mt_goods t where t.NAME = #{goodsName} and t.STATUS != 'D'
         <if test="merchantId != null and merchantId > 0">
             AND t.MERCHANT_ID = #{merchantId}
         </if>