|
@@ -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;
|
|
|
+ }
|
|
|
}
|