Commit 6c3fab6c authored by Đào Nhật Quang's avatar Đào Nhật Quang

quangdn

parent 4f525c8d
...@@ -3,7 +3,6 @@ package com.viettel.campaign.service.impl; ...@@ -3,7 +3,6 @@ package com.viettel.campaign.service.impl;
import com.github.tennaito.rsql.jpa.JpaCriteriaCountQueryVisitor; import com.github.tennaito.rsql.jpa.JpaCriteriaCountQueryVisitor;
import com.github.tennaito.rsql.jpa.JpaCriteriaQueryVisitor; import com.github.tennaito.rsql.jpa.JpaCriteriaQueryVisitor;
import com.viettel.campaign.config.DataSourceQualify; import com.viettel.campaign.config.DataSourceQualify;
import com.viettel.campaign.mapper.CustomerListMapper;
import com.viettel.campaign.mapper.CustomerMapper; import com.viettel.campaign.mapper.CustomerMapper;
import com.viettel.campaign.model.ccms_full.*; import com.viettel.campaign.model.ccms_full.*;
import com.viettel.campaign.repository.ccms_full.*; import com.viettel.campaign.repository.ccms_full.*;
...@@ -44,7 +43,7 @@ import javax.persistence.criteria.CriteriaQuery; ...@@ -44,7 +43,7 @@ import javax.persistence.criteria.CriteriaQuery;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.io.File; import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.FileOutputStream; import java.nio.file.Files;
import java.text.DateFormat; import java.text.DateFormat;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.util.*; import java.util.*;
...@@ -945,7 +944,7 @@ public class CustomerServiceImpl implements CustomerService { ...@@ -945,7 +944,7 @@ public class CustomerServiceImpl implements CustomerService {
SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy"); SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
Map<String, Object> result = new HashMap<>(); Map<String, Object> result = new HashMap<>();
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
boolean validateOk = false; int failedCount = 0;
try { try {
//<editor-fold desc="Khởi tạo mảng header tĩnh" defaultstate="collapsed"> //<editor-fold desc="Khởi tạo mảng header tĩnh" defaultstate="collapsed">
...@@ -970,6 +969,7 @@ public class CustomerServiceImpl implements CustomerService { ...@@ -970,6 +969,7 @@ public class CustomerServiceImpl implements CustomerService {
File file = new File(path); File file = new File(path);
FileInputStream fis = new FileInputStream(file); FileInputStream fis = new FileInputStream(file);
XSSFWorkbook workbook = new XSSFWorkbook(fis); XSSFWorkbook workbook = new XSSFWorkbook(fis);
ByteArrayOutputStream os = new ByteArrayOutputStream();
Sheet sheet = workbook.getSheetAt(0); Sheet sheet = workbook.getSheetAt(0);
Row row = sheet.getRow(3); Row row = sheet.getRow(3);
...@@ -989,7 +989,7 @@ public class CustomerServiceImpl implements CustomerService { ...@@ -989,7 +989,7 @@ public class CustomerServiceImpl implements CustomerService {
for (int i = 0; i < header.size(); i++) { for (int i = 0; i < header.size(); i++) {
Cell cell = row.getCell(i); Cell cell = row.getCell(i);
if (!cell.getStringCellValue().equals(header.get(i).getTitle().split("#")[0])) { if (!cell.getStringCellValue().equals(header.get(i).getTitle().split("#")[0])) {
result.put("file", file); result.put("content", Files.readAllBytes(file.toPath()));
result.put("message", BundleUtils.getLangString("customer.invalidTemplate", locale)); result.put("message", BundleUtils.getLangString("customer.invalidTemplate", locale));
return result; return result;
} }
...@@ -1001,7 +1001,7 @@ public class CustomerServiceImpl implements CustomerService { ...@@ -1001,7 +1001,7 @@ public class CustomerServiceImpl implements CustomerService {
//<editor-fold desc="Kiểm tra file dữ liệu rỗng" defaultstate="collapsed"> //<editor-fold desc="Kiểm tra file dữ liệu rỗng" defaultstate="collapsed">
if (sheet.getPhysicalNumberOfRows() == 3) { if (sheet.getPhysicalNumberOfRows() == 3) {
result.put("message", BundleUtils.getLangString("customer.noData", locale)); result.put("message", BundleUtils.getLangString("customer.noData", locale));
result.put("file", file); result.put("content", Files.readAllBytes(file.toPath()));
return result; return result;
} else { } else {
Cell resultCell = sheet.getRow(3).createCell(sheet.getRow(3).getPhysicalNumberOfCells()); Cell resultCell = sheet.getRow(3).createCell(sheet.getRow(3).getPhysicalNumberOfCells());
...@@ -1014,8 +1014,8 @@ public class CustomerServiceImpl implements CustomerService { ...@@ -1014,8 +1014,8 @@ public class CustomerServiceImpl implements CustomerService {
for (int i = 4; i < sheet.getPhysicalNumberOfRows(); i++) { for (int i = 4; i < sheet.getPhysicalNumberOfRows(); i++) {
Row dataRow = sheet.getRow(i); Row dataRow = sheet.getRow(i);
if (dataRow != null) { if (dataRow != null) {
Object[] obj = new Object[row.getPhysicalNumberOfCells()]; Object[] obj = new Object[row.getPhysicalNumberOfCells() - 1];
for (int j = 0; j < row.getPhysicalNumberOfCells(); j++) { for (int j = 0; j < row.getPhysicalNumberOfCells() - 1; j++) {
Cell dataCell = dataRow.getCell(j); Cell dataCell = dataRow.getCell(j);
if (dataCell != null) { if (dataCell != null) {
obj[j] = dataFormat.formatCellValue(dataCell); obj[j] = dataFormat.formatCellValue(dataCell);
...@@ -1026,7 +1026,6 @@ public class CustomerServiceImpl implements CustomerService { ...@@ -1026,7 +1026,6 @@ public class CustomerServiceImpl implements CustomerService {
} else if (headerCell.getStringCellValue().equals(BundleUtils.getLangString("customer.callAllowed", locale)) || headerCell.getStringCellValue().equals(BundleUtils.getLangString("customer.emailAllowed", locale)) } else if (headerCell.getStringCellValue().equals(BundleUtils.getLangString("customer.callAllowed", locale)) || headerCell.getStringCellValue().equals(BundleUtils.getLangString("customer.emailAllowed", locale))
|| headerCell.getStringCellValue().equals(BundleUtils.getLangString("customer.smsAllowed", locale))) { || headerCell.getStringCellValue().equals(BundleUtils.getLangString("customer.smsAllowed", locale))) {
obj[j] = BundleUtils.getLangString("customer.yes", locale); obj[j] = BundleUtils.getLangString("customer.yes", locale);
break;
} else { } else {
obj[j] = null; obj[j] = null;
} }
...@@ -1089,9 +1088,8 @@ public class CustomerServiceImpl implements CustomerService { ...@@ -1089,9 +1088,8 @@ public class CustomerServiceImpl implements CustomerService {
Cell resultCell = dataRow.createCell(row.getPhysicalNumberOfCells() - 1); Cell resultCell = dataRow.createCell(row.getPhysicalNumberOfCells() - 1);
if (sb.length() > 0) { if (sb.length() > 0) {
resultCell.setCellValue(sb.toString()); resultCell.setCellValue(sb.toString());
validateOk = false; failedCount++;
} else { } else {
validateOk = true;
resultCell.setCellValue("Ok"); resultCell.setCellValue("Ok");
} }
sb = new StringBuilder(); sb = new StringBuilder();
...@@ -1099,7 +1097,7 @@ public class CustomerServiceImpl implements CustomerService { ...@@ -1099,7 +1097,7 @@ public class CustomerServiceImpl implements CustomerService {
//</editor-fold> //</editor-fold>
//<editor-fold desc="Insert dữ liệu nếu validate Ok" defaultstate="collapsed"> //<editor-fold desc="Insert dữ liệu nếu validate Ok" defaultstate="collapsed">
if (validateOk) { if (failedCount == 0) {
for (int i = 0; i < rawDataList.size(); i++) { for (int i = 0; i < rawDataList.size(); i++) {
Customer c = new Customer(); Customer c = new Customer();
c.setName(rawDataList.get(i)[1].toString()); c.setName(rawDataList.get(i)[1].toString());
...@@ -1200,9 +1198,9 @@ public class CustomerServiceImpl implements CustomerService { ...@@ -1200,9 +1198,9 @@ public class CustomerServiceImpl implements CustomerService {
customerContactRepository.save(cc); customerContactRepository.save(cc);
} }
} }
for (int j = 0; j < dynamicHeader.size(); j++) {
CustomizeFieldObject cfo = new CustomizeFieldObject(); CustomizeFieldObject cfo = new CustomizeFieldObject();
cfo.setObjectId(saved.getCustomerId()); cfo.setObjectId(saved.getCustomerId());
for (int j = 0; j < dynamicHeader.size(); j++) {
cfo.setCustomizeFieldId(dynamicHeader.get(j).getCustomizeFieldId()); cfo.setCustomizeFieldId(dynamicHeader.get(j).getCustomizeFieldId());
if (rawDataList.get(i).length > (12 + j) && rawDataList.get(i)[12 + j] != null) { if (rawDataList.get(i).length > (12 + j) && rawDataList.get(i)[12 + j] != null) {
switch (dynamicHeader.get(j).getType()) { switch (dynamicHeader.get(j).getType()) {
...@@ -1243,14 +1241,18 @@ public class CustomerServiceImpl implements CustomerService { ...@@ -1243,14 +1241,18 @@ public class CustomerServiceImpl implements CustomerService {
clm.setCustomerListId(customerListId); clm.setCustomerListId(customerListId);
customerListMappingRepository.save(clm); customerListMappingRepository.save(clm);
} }
FileOutputStream fos = new FileOutputStream(file); workbook.write(os);
workbook.write(fos); os.flush();
result.put("file", file); os.close();
workbook.close();
result.put("content", os.toByteArray());
result.put("message", BundleUtils.getLangString("customer.importSuccess", locale)); result.put("message", BundleUtils.getLangString("customer.importSuccess", locale));
} else { } else {
FileOutputStream fos = new FileOutputStream(file); workbook.write(os);
workbook.write(fos); os.flush();
result.put("file", file); os.close();
workbook.close();
result.put("content", os.toByteArray());
result.put("message", BundleUtils.getLangString("customer.importFailed", locale)); result.put("message", BundleUtils.getLangString("customer.importFailed", locale));
} }
//</editor-fold> //</editor-fold>
......
package com.viettel.campaign.web.rest; package com.viettel.campaign.web.rest;
import com.viettel.campaign.model.ccms_full.CampaignCustomer;
import com.viettel.campaign.model.ccms_full.Customer; import com.viettel.campaign.model.ccms_full.Customer;
import com.viettel.campaign.model.ccms_full.CustomizeFieldObject; import com.viettel.campaign.model.ccms_full.CustomizeFieldObject;
import com.viettel.campaign.model.ccms_full.CustomizeFields; import com.viettel.campaign.model.ccms_full.CustomizeFields;
import com.viettel.campaign.repository.ccms_full.CustomerQueryRepository; import com.viettel.campaign.repository.ccms_full.CustomerQueryRepository;
import com.viettel.campaign.repository.ccms_full.impl.CustomerQueryRepositoryImpl;
import com.viettel.campaign.service.CustomerService; import com.viettel.campaign.service.CustomerService;
import com.viettel.campaign.utils.BundleUtils; import com.viettel.campaign.utils.BundleUtils;
import com.viettel.campaign.utils.Config; import com.viettel.campaign.utils.Config;
...@@ -20,7 +18,6 @@ import org.apache.commons.io.FilenameUtils; ...@@ -20,7 +18,6 @@ import org.apache.commons.io.FilenameUtils;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType; import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
...@@ -117,12 +114,6 @@ public class CustomerController { ...@@ -117,12 +114,6 @@ public class CustomerController {
// VIẾT ĐI VIẾT LẠI 4 LẦN RỒI ĐẤY // VIẾT ĐI VIẾT LẠI 4 LẦN RỒI ĐẤY
String xAuthToken = request.getHeader("X-Auth-Token"); String xAuthToken = request.getHeader("X-Auth-Token");
UserSession userSession = (UserSession) RedisUtil.getInstance().get(xAuthToken); UserSession userSession = (UserSession) RedisUtil.getInstance().get(xAuthToken);
if (userSession == null) {
userSession = new UserSession();
userSession.setSiteId(customerListDTO.getCompanySiteId());
userSession.setUserName("its4");
}
return customerService.createCustomerList(customerListDTO, userSession.getUserName()); return customerService.createCustomerList(customerListDTO, userSession.getUserName());
} }
...@@ -184,14 +175,15 @@ public class CustomerController { ...@@ -184,14 +175,15 @@ public class CustomerController {
} }
//<editor-fold desc="Download and import excel" defaultstate="collapsed"> //<editor-fold desc="Download and import excel" defaultstate="collapsed">
@GetMapping(value = "/downloadFileTemplate") @PostMapping(value = "/downloadFileTemplate")
public ResponseEntity<byte[]> downloadFileTemplate(@RequestParam("companySiteId") Long companySiteId) { public ResponseEntity<byte[]> downloadFileTemplate(@RequestBody Long companySiteId) {
LOGGER.info("--------DOWNLOAD FILE TEMPLATE---------"); LOGGER.info("--------DOWNLOAD FILE TEMPLATE---------");
try { try {
byte[] content = customerService.buildTemplate(companySiteId); byte[] content = customerService.buildTemplate(companySiteId);
return ResponseEntity.ok() return ResponseEntity.ok()
.header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=template_import_customer.xlsx") // 2 dòng này không hiểu sao lại không hoạt động nữa
.header("Content-Type", Constants.MIME_TYPE.EXCEL_XLSX) // .header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=template_import_customer.xlsx")
// .header("Content-Type", Constants.MIME_TYPE.EXCEL_XLSX)
.body(content); .body(content);
} catch (Exception e) { } catch (Exception e) {
LOGGER.error(e.getMessage()); LOGGER.error(e.getMessage());
...@@ -200,18 +192,13 @@ public class CustomerController { ...@@ -200,18 +192,13 @@ public class CustomerController {
} }
@PostMapping(value = "/importFile") @PostMapping(value = "/importFile")
public ResponseEntity<?> importFile(@RequestParam("file") MultipartFile file, public ResponseEntity<?> importFile(@RequestPart("file") MultipartFile file,
@RequestParam("customerListId") String customerListId, @RequestPart("customerListId") String customerListId,
HttpServletRequest request) { HttpServletRequest request) {
LOGGER.info("------------IMPORT FILE TEMPLATE--------------"); LOGGER.info("------------IMPORT FILE TEMPLATE--------------");
Locale locale = new Locale("vi", "VN"); Locale locale = new Locale("vi", "VN");
try { try {
UserSession userSession = (UserSession) RedisUtil.getInstance().get(request.getHeader("X-Auth-Token")); UserSession userSession = (UserSession) RedisUtil.getInstance().get(request.getHeader("X-Auth-Token"));
if (userSession == null) {
userSession = new UserSession();
userSession.setSiteId(662691L);
userSession.setUserName("its3");
}
if (file.isEmpty()) { if (file.isEmpty()) {
return new ResponseEntity<>(BundleUtils.getLangString("customer.fileNotSelected"), HttpStatus.OK); return new ResponseEntity<>(BundleUtils.getLangString("customer.fileNotSelected"), HttpStatus.OK);
} }
...@@ -221,13 +208,11 @@ public class CustomerController { ...@@ -221,13 +208,11 @@ public class CustomerController {
String path = saveUploadFile(file); String path = saveUploadFile(file);
List<CustomizeFields> dynamicHeaders = customerService.getDynamicHeader(userSession.getCompanySiteId()); List<CustomizeFields> dynamicHeaders = customerService.getDynamicHeader(userSession.getCompanySiteId());
Map<String, Object> map = customerService.readAndValidateCustomer(path, dynamicHeaders, userSession, Long.parseLong(customerListId)); Map<String, Object> map = customerService.readAndValidateCustomer(path, dynamicHeaders, userSession, Long.parseLong(customerListId));
File fileExport = (File) map.get("file");
String message = (String) map.get("message"); String message = (String) map.get("message");
byte[] content = (byte[]) map.get("content");
return ResponseEntity.ok() return ResponseEntity.ok()
.header("Content-Type", Constants.MIME_TYPE.EXCEL_XLSX)
.header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=template_import_customer.xlsx")
.header("Message", message) .header("Message", message)
.body(Files.readAllBytes(fileExport.toPath())); .body(content);
} catch (Exception e) { } catch (Exception e) {
LOGGER.error(e.getMessage()); LOGGER.error(e.getMessage());
return new ResponseEntity<>(HttpStatus.BAD_REQUEST); return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment