ソースを参照

修改品牌制造商导入模块

zhh 3 年 前
コミット
4fc95f7798

+ 20 - 8
kmall-admin/src/main/java/com/kmall/admin/controller/BrandController.java

@@ -17,6 +17,8 @@ import com.kmall.common.utils.excel.ExcelExport;
 import com.kmall.common.utils.excel.ExcelUtil;
 import org.apache.commons.lang3.StringUtils;
 import org.apache.shiro.authz.annotation.RequiresPermissions;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.web.bind.annotation.*;
 import org.springframework.web.multipart.MultipartFile;
@@ -31,19 +33,21 @@ import java.util.*;
  * @author Scott
  * @email
  * @date 2017-08-19 17:59:15
+ *
+ * @author zhuhh
+ * @date 2021-12-9 15:57:53
  */
 @RestController
 @RequestMapping("brand")
 public class BrandController {
+    private static final Logger logger = LoggerFactory.getLogger(BrandController.class);
+
     @Autowired
     private BrandService brandService;
 
     @Autowired
     private ExcelUtil excelUtil;
 
-    @Autowired
-    private CategoryService categoryService;
-
     /**
      * 查看列表
      */
@@ -223,11 +227,14 @@ public class BrandController {
 
     /**
      * 导入品牌
+     *
+     * @author zhuhh
+     * @date 2021-12-9 15:59:33
      */
     @RequestMapping("/upload")
     @ResponseBody
     public R upload(@RequestParam("file") MultipartFile file) {
-        List<BrandEntity> brandEntityList = new ArrayList<>();//信息
+        List<BrandEntity> brandEntityList = new ArrayList<>();
         try {
             BrandEntity brandEntity = new BrandEntity();
             Map<String, Object> beans = new HashMap<String, Object>();
@@ -238,16 +245,21 @@ public class BrandController {
             }
             excelUtil.readExcel(JxlsXmlTemplateName.BRAND_DTO_LIST, beans, file.getInputStream());
         } catch (Exception e) {
-            e.printStackTrace();
-            return R.error("导入失败!");
+            logger.error("读取文件失败!", e);
+            return R.error("读取文件失败!");
         }
         try {
+            if (brandEntityList == null || brandEntityList.size() == 0) {
+                logger.error("读取数据为空!");
+                return R.error("读取数据为空!");
+            }
+
             brandService.uploadExcel(brandEntityList);
         } catch (Exception e) {
-            e.printStackTrace();
+            logger.error("保存数据失败!", e);
             return R.error(e.getMessage());
         }
-        //上传文件
+        // 上传文件
         return R.ok("导入成功!");
     }
 

+ 39 - 45
kmall-admin/src/main/java/com/kmall/admin/service/impl/BrandServiceImpl.java

@@ -12,6 +12,7 @@ import com.kmall.common.utils.RRException;
 import com.kmall.common.utils.ValidatorUtil;
 import com.kmall.manager.manager.express.sf.ServiceException;
 import com.kmall.manager.manager.redis.JedisUtil;
+import org.apache.commons.lang3.StringUtils;
 import org.springframework.beans.BeanUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
@@ -235,55 +236,48 @@ public class BrandServiceImpl implements BrandService {
     @Override
     @Transactional
     public void uploadExcel(List<BrandEntity> brandEntityList) {
-        if (brandEntityList != null && brandEntityList.size() > 0) {
-            for (int i = 0; i < brandEntityList.size(); i++) {
-                BrandEntity brandEntity = brandEntityList.get(i);
-                Integer brandId = brandEntity.getId();
-                Integer isShow = brandEntity.getIsShow();
-                Integer isNew = brandEntity.getIsNew();
-                // 品牌唯一标识符正则校验
-                String uniqueIdentifier = brandEntity.getUniqueIdentifier();
-
-                if (isShow.intValue() != 0 && isShow.intValue() != 1 && isNew.intValue() != 0 && isNew != 1) {
-                    throw new ServiceException("第" + (i + 1) + "行的是否显示或者是否新品牌只可填(0:否 1:是)");
-                }
+        for (int i = 0; i < brandEntityList.size(); i++) {
+            BrandEntity brandEntity = brandEntityList.get(i);
+            String name = brandEntity.getName();
+            Integer isShow = brandEntity.getIsShow();
+            Integer isNew = brandEntity.getIsNew();
+            // 品牌唯一标识符正则校验
+            String uniqueIdentifier = brandEntity.getUniqueIdentifier();
+
+            if (isShow.intValue() != 0 && isShow.intValue() != 1 && isNew.intValue() != 0 && isNew != 1) {
+                throw new ServiceException("第" + (i + 1) + "行的是否显示或者是否新品牌只可填(0:否 1:是)");
+            }
 
+            if (StringUtils.isNotBlank(uniqueIdentifier)) {
+                String regex = "(^[0-9a-zA-Z_&]{1,10}$)";
+                if (!Pattern.matches(regex, uniqueIdentifier)) {
+                    throw new RuntimeException("请输入正确的品牌唯一简码,有问题的简码:" + uniqueIdentifier);
+                }
+            }
 
+            // 数据库查询是否存在对应名字的对象
+            BrandEntity isExist = brandDao.queryByName(name);
 
-                // 如果是0就添加数据
-                if (brandId.intValue() == 0) {
-
-
-                    String regex = "(^[0-9a-zA-Z_&]{1,10}$)";
-                    if(!Pattern.matches(regex,uniqueIdentifier)){
-                        throw new RuntimeException("请输入正确的品牌唯一简码,有问题的简码:"+uniqueIdentifier);
-                    }
-
-                    // 如果是正确的,根据唯一简码查询该简码是否存在
-                    BrandEntity queryBrandEntity = brandDao.queryByUniqueIdentifier(uniqueIdentifier);
-
-                    if (Objects.nonNull(queryBrandEntity)){
-                        throw new ServiceException("第" + (i + 1) + "行的该品牌:【" + queryBrandEntity.getName() + "】已存在!");
-                    }
-                    BrandEntity insertBrandEntity = new BrandEntity();
-                    insertBrandEntity.setName(brandEntity.getName());
-                    insertBrandEntity.setSimpleDesc(brandEntity.getSimpleDesc());
-                    insertBrandEntity.setUniqueIdentifier(uniqueIdentifier);
-                    insertBrandEntity.setIsShow(isShow);
-                    insertBrandEntity.setIsNew(isNew);
-                    this.save(insertBrandEntity);
-                } else {
-                    String regex = "(^[0-9a-zA-Z_&]{1,10}$)";
-                    if(!Pattern.matches(regex,uniqueIdentifier)){
-                        throw new RuntimeException("请输入正确的品牌唯一简码,有问题的简码:"+uniqueIdentifier);
-                    }
-                    BrandEntity updateBrandEntity = brandDao.queryObject(brandId);
-                    if (Objects.isNull(updateBrandEntity)) {
-                        throw new ServiceException("第" + (i + 1) + "行的该品牌编号:【" + brandId + "】不存在!");
-                    }
-                    BeanUtils.copyProperties(brandEntity,updateBrandEntity);
-                    this.update(updateBrandEntity);
+            // 不存在则添加
+            if (isExist == null) {
+                // 如果是正确的,根据唯一简码查询该简码是否存在
+                BrandEntity queryBrandEntity = brandDao.queryByUniqueIdentifier(uniqueIdentifier);
+                if (Objects.nonNull(queryBrandEntity)) {
+                    throw new ServiceException("第" + (i + 1) + "行的该品牌:【" + queryBrandEntity.getName() + "】已存在!");
                 }
+
+                BrandEntity insertBrandEntity = new BrandEntity();
+                insertBrandEntity.setName(brandEntity.getName());
+                insertBrandEntity.setSimpleDesc(brandEntity.getSimpleDesc());
+                insertBrandEntity.setUniqueIdentifier(uniqueIdentifier);
+                insertBrandEntity.setIsShow(isShow);
+                insertBrandEntity.setIsNew(isNew);
+                this.save(insertBrandEntity);
+
+            } else {
+                // 存在则修改
+                BeanUtils.copyProperties(brandEntity, isExist);
+                this.update(isExist);
             }
         }
     }

+ 5 - 6
kmall-admin/src/main/resources/XmlTemplate/BrandDtoList.xml

@@ -5,12 +5,11 @@
         <loop startRow="1" endRow="1" items="BrandEntityList" var="BrandEntity"
               varType="com.kmall.admin.entity.BrandEntity">
             <section startRow="1" endRow="1">
-                <mapping row="1" col="0">BrandEntity.id</mapping>
-                <mapping row="1" col="1">BrandEntity.name</mapping>
-                <mapping row="1" col="2">BrandEntity.uniqueIdentifier</mapping>
-                <mapping row="1" col="3">BrandEntity.simpleDesc</mapping>
-                <mapping row="1" col="4">BrandEntity.isShow</mapping>
-                <mapping row="1" col="5">BrandEntity.isNew</mapping>
+                <mapping row="1" col="0">BrandEntity.name</mapping>
+                <mapping row="1" col="1">BrandEntity.uniqueIdentifier</mapping>
+                <mapping row="1" col="2">BrandEntity.simpleDesc</mapping>
+                <mapping row="1" col="3">BrandEntity.isShow</mapping>
+                <mapping row="1" col="4">BrandEntity.isNew</mapping>
             </section>
             <loopbreakcondition>
                 <rowcheck offset="0">

BIN
kmall-admin/src/main/webapp/statics/file/brand_export_yyyy_mm_dd_v1.0.0.xls