Parcourir la source

导入新增更新客户

yurk il y a 3 ans
Parent
commit
f9a72d1cdc

+ 15 - 2
fhKeeper/formulahousekeeper/management-platform/src/main/java/com/management/platform/controller/CustomerInfoController.java

@@ -10,12 +10,13 @@ import com.management.platform.entity.User;
 import com.management.platform.mapper.CustomerInfoMapper;
 import com.management.platform.mapper.ProjectMapper;
 import com.management.platform.mapper.UserMapper;
+import com.management.platform.service.CustomerInfoService;
 import com.management.platform.util.HttpRespMsg;
 import org.springframework.util.StringUtils;
 import org.springframework.web.bind.annotation.RequestMapping;
-
 import org.springframework.web.bind.annotation.RequestParam;
 import org.springframework.web.bind.annotation.RestController;
+import org.springframework.web.multipart.MultipartFile;
 
 import javax.annotation.Resource;
 import javax.servlet.http.HttpServletRequest;
@@ -43,6 +44,8 @@ public class CustomerInfoController {
     ProjectMapper projectMapper;
     @Resource
     CustomerInfoMapper customerInfoMapper;
+    @Resource
+    CustomerInfoService customerInfoService;
 
     @RequestMapping("/addOrMod")
     public HttpRespMsg addOrMod(CustomerInfo info) {
@@ -51,11 +54,17 @@ public class CustomerInfoController {
         User user = userMapper.selectById(token);
         List<CustomerInfo> customerInfoMapperAll = customerInfoMapper.getAll(user.getCompanyId());
         List<String> nameList=new ArrayList<>();
+        List<String> codeList=new ArrayList<>();
         customerInfoMapperAll.forEach(cu->{
             nameList.add(cu.getCustomerName());
+            codeList.add(cu.getCustomerCode());
         });
         if(nameList.contains(info.getCustomerName())){
-            msg.setError("客户名称重复");
+            msg.setError("客户名称已存在");
+            return msg;
+        }
+        if(codeList.contains(info.getCustomerCode())){
+            msg.setError("客户编号已存在");
             return msg;
         }
         if (info.getId() == null) {
@@ -113,6 +122,10 @@ public class CustomerInfoController {
         msg.data = all;
         return msg;
     }
+    @RequestMapping("/importData")
+    public HttpRespMsg importData(HttpServletRequest request, MultipartFile file){
+        return customerInfoService.importData(request,file);
+    }
 
 }
 

+ 6 - 1
fhKeeper/formulahousekeeper/management-platform/src/main/java/com/management/platform/service/CustomerInfoService.java

@@ -1,7 +1,11 @@
 package com.management.platform.service;
 
-import com.management.platform.entity.CustomerInfo;
 import com.baomidou.mybatisplus.extension.service.IService;
+import com.management.platform.entity.CustomerInfo;
+import com.management.platform.util.HttpRespMsg;
+import org.springframework.web.multipart.MultipartFile;
+
+import javax.servlet.http.HttpServletRequest;
 
 /**
  * <p>
@@ -13,4 +17,5 @@ import com.baomidou.mybatisplus.extension.service.IService;
  */
 public interface CustomerInfoService extends IService<CustomerInfo> {
 
+    HttpRespMsg importData(HttpServletRequest request, MultipartFile file);
 }

+ 100 - 1
fhKeeper/formulahousekeeper/management-platform/src/main/java/com/management/platform/service/impl/CustomerInfoServiceImpl.java

@@ -1,10 +1,26 @@
 package com.management.platform.service.impl;
 
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
+import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
 import com.management.platform.entity.CustomerInfo;
 import com.management.platform.mapper.CustomerInfoMapper;
+import com.management.platform.mapper.UserMapper;
 import com.management.platform.service.CustomerInfoService;
-import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
+import com.management.platform.util.ExcelUtil;
+import com.management.platform.util.HttpRespMsg;
+import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
+import org.apache.poi.ss.usermodel.*;
 import org.springframework.stereotype.Service;
+import org.springframework.web.multipart.MultipartFile;
+
+import javax.annotation.Resource;
+import javax.servlet.http.HttpServletRequest;
+import java.io.*;
+import java.text.DateFormat;
+import java.text.SimpleDateFormat;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.stream.Collectors;
 
 /**
  * <p>
@@ -16,5 +32,88 @@ import org.springframework.stereotype.Service;
  */
 @Service
 public class CustomerInfoServiceImpl extends ServiceImpl<CustomerInfoMapper, CustomerInfo> implements CustomerInfoService {
+    @Resource
+    CustomerInfoMapper customerInfoMapper;
+    @Resource
+    UserMapper userMapper;
+    @Override
+    public HttpRespMsg importData(HttpServletRequest request, MultipartFile multipartFile) {
+        HttpRespMsg msg=new HttpRespMsg();
+        Integer companyId=userMapper.selectById(request.getHeader("token")).getCompanyId();
+        String fileName=multipartFile.getOriginalFilename();
+        File file=new File(fileName == null ? "file" : fileName);
+        InputStream inputStream=null;
+        OutputStream outputStream=null;
+        try {
+            inputStream= multipartFile.getInputStream();
+            outputStream=new FileOutputStream(file);
+            byte[] buffer = new byte[4096];
+            int temp = 0;
+            while ((temp = inputStream.read(buffer, 0, 4096)) != -1) {
+                outputStream.write(buffer, 0, temp);
+            }
+            inputStream.close();
+            outputStream.close();
 
+            //然后解析表格
+            Workbook workbook = WorkbookFactory.create(new FileInputStream(file));
+            DateFormat df = new SimpleDateFormat("yyyy-MM");
+            //获取公司所有客户
+            List<CustomerInfo> allCustomerInfo = customerInfoMapper.selectList(new QueryWrapper<CustomerInfo>().eq("company_id", companyId));
+            Sheet sheet = workbook.getSheetAt(0);
+            //由于第一行需要指明列对应的标题
+            int rowNum = sheet.getLastRowNum();
+            if (rowNum == 0) {
+                msg.setError("请填写客户数据");
+                return msg;
+            }
+            List<String> nameList=new ArrayList<>();
+            int dataCount = 0;
+            for (int rowIndex = 1; rowIndex <= rowNum; rowIndex++) {
+                Row row = sheet.getRow(rowIndex);
+                dataCount++;
+                if (row == null) {
+                    continue;
+                }
+                if (ExcelUtil.isRowEmpty(row)) {
+                    continue;
+                }
+                    if (row.getCell(1) == null) {
+                        msg.setError("第"+dataCount+"行缺少客户名称");
+                        return msg;
+                    }
+                    if(nameList.contains(row.getCell(1).toString())){
+                        msg.setError("当前导入数据存在重复客户名称["+row.getCell(1).toString()+"]");
+                        return msg;
+                    }
+                    nameList.add(row.getCell(1).toString());
+                CustomerInfo customerInfo=new CustomerInfo();
+                List<CustomerInfo> collect = allCustomerInfo.stream().filter(sv -> sv.getCustomerName().equals(row.getCell(1).toString())).collect(Collectors.toList());
+                customerInfo.setCustomerCode(row.getCell(0).toString());
+                customerInfo.setCustomerName(row.getCell(1).toString());
+                customerInfo.setContactName(row.getCell(2).toString());
+                row.getCell(3).setCellType(CellType.STRING);
+                customerInfo.setContactPhone(row.getCell(3).getStringCellValue());
+                customerInfo.setEmail(row.getCell(4).toString());
+                customerInfo.setAddress(row.getCell(5).toString());
+                customerInfo.setCompanyId(companyId);
+                if(collect.size()>0){
+                    Integer id=collect.get(0).getId();
+                    customerInfo.setId(id);
+                    customerInfoMapper.updateById(customerInfo);
+                }else{
+                    customerInfoMapper.insert(customerInfo);
+                }
+            }
+            msg.data=dataCount;
+            return msg;
+        } catch (IOException e){
+            e.printStackTrace();
+            msg.setError("文件处理出错");
+            return msg;
+        } catch (InvalidFormatException e) {
+            e.printStackTrace();
+        }
+        return null;
+    }
 }

+ 67 - 1
fhKeeper/formulahousekeeper/timesheet/src/views/customer/list.vue

@@ -15,6 +15,7 @@
                 </el-form-item>
                 <el-form-item style="float:right;">
                     <el-link type="primary" :underline="false" @click="handleAdd(-1,null)">新增客户</el-link>
+                    <el-link type="primary" :underline="false" @click="intocustomerRatio">批量导入客户</el-link>
                 </el-form-item>
             </el-form>
         </el-col>
@@ -87,6 +88,27 @@
                 <el-button type="primary" @click="submitInsert" :loading="addLoading">提交</el-button>
             </div>
         </el-dialog>
+         <!-- 导入结果说明 -->
+        <el-dialog title="分摊比例导入结果" v-if="showImportResult" :visible.sync="showImportResult" customClass="customWidth" width="500px">
+            <div>
+                <span>{{importResultMsg}}</span>
+                
+            </div>
+            <span slot="footer" class="dialog-footer">
+                <el-button type="primary" @click="showImportResult=false">确定</el-button>
+            </span>
+        </el-dialog>
+         <el-dialog title="导入客户数据" v-if="intocustomerDialog" :visible.sync="intocustomerDialog" customClass="customWidth" width="500px">
+            <p>1. 下载
+            <el-link type="primary" style="margin-left:5px;" :underline="false" href="./upload/新增客户导入模板.xlsx" download="新增客户导入模板.xlsx">新增客户导入模板.xlsx</el-link>
+            </p>
+            <!-- <p>2. 填写excel模板,请确保模板中的项目和人员已添加到系统中。</p> -->
+            <p style="display: flex;justify-content: center;padding:1em 0">
+                <el-upload ref="upload"  action="#" :limit="1" :http-request="batchImportData" :show-file-list="false">
+                <el-button type="primary" :underline="false" :loading="importingData">开始导入</el-button>
+            </el-upload>
+            </p>
+        </el-dialog>
     </section>
 </template>
 <style scoped>
@@ -141,7 +163,11 @@
                 },
                 rules: {
                     customerName: [{ required: true, message: "请输入客户名称", trigger: "blur" }],
-                }
+                },
+                intocustomerDialog:false,
+                importingData: false,
+                importResultMsg:null,
+                showImportResult:false,
             };
         },
         // 过滤器
@@ -166,6 +192,46 @@
             }
         },
         methods: {
+            intocustomerRatio(){
+                this.intocustomerDialog=true;
+            },
+            batchImportData(item) {
+                //首先判断文件类型
+                let str = item.file.name.split(".");
+                let format = str[str.length - 1];
+                if (format != "xls" && format != "xlsx") {
+                    this.$message({
+                        message: "请选择.xls或.xlsx文件",
+                        type: "error"
+                    });
+                } else {
+                    this.importingData = true;
+                    let formData = new FormData();
+                    formData.append("file", item.file);
+                    formData.append("companyId", this.user.companyId);
+                    this.http.uploadFile('//customer-info/importData', formData,
+                    res => {
+                        this.$refs.upload.clearFiles();
+                        this.importingData = false;
+                        this.showImportResult = true;
+                        if (res.code == "ok") {
+                            //换成弹出框,以免有人等了半天回来啥也没看到
+                            this.importResultMsg = "成功导入/更新"+res.data+"条客户数据。"+(res.msg?res.msg:"");
+                            this.getList();
+                        } else {
+                            this.importResultMsg = "导入失败:"+res.msg;
+                        }
+                    },
+                    error => {
+                        this.$refs.upload.clearFiles();
+                        this.importingData = false;
+                        this.$message({
+                            message: error,
+                            type: "error"
+                        });
+                    });
+                }
+            },
             importProject(item) {
                 //首先判断文件类型
                 let str = item.file.name.split(".");