Commit 59197718 authored by ='s avatar =

Merge remote-tracking branch 'origin/master'

parents fab102a5 7819a484
No preview for this file type
......@@ -5,3 +5,4 @@ out/
/campaign.iml
/lib
/log
/etc
This diff is collapsed.
......@@ -2,8 +2,11 @@ package com.viettel.campaign.service;
import com.viettel.campaign.model.ccms_full.Scenario;
import com.viettel.campaign.web.dto.*;
import com.viettel.econtact.filter.UserSession;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.util.List;
import java.util.Map;
/**
* @author anhvd_itsol
......@@ -19,4 +22,8 @@ public interface ScenarioService {
Integer countDuplicateScenarioCode(Long companySiteId, String code, Long scenarioId);
ResultDTO saveContacQuestResult(ContactQuestResultDTO dto);
XSSFWorkbook buildTemplate();
Map<String, Object> readAndValidateCustomer(String path, UserSession userSession);
}
......@@ -340,32 +340,4 @@ public class CampaignController {
return new ResponseEntity<>(result, HttpStatus.OK);
}
@RequestMapping(value = "/import-file", method = RequestMethod.POST)
public ResponseEntity<?> importFile(@RequestParam("file") MultipartFile file,
@RequestHeader("X-Auth-Token") String authToken) {
Locale locale = new Locale("vi", "VN");
try {
UserSession userSession = (UserSession) RedisUtil.getInstance().get(authToken);
if (file.isEmpty()) {
return new ResponseEntity<>(BundleUtils.getLangString("common.fileNotSelected"), HttpStatus.OK);
}
if (!Objects.equals(FilenameUtils.getExtension(file.getOriginalFilename()), Constants.FileType.xlsx)) {
return new ResponseEntity<>(BundleUtils.getLangString("common.fileInvalidFormat", locale), HttpStatus.OK);
}
//String path = saveUploadFile(file);
// List<CustomizeFields> dynamicHeaders = customerService.getDynamicHeader(userSession.getCompanySiteId());
// Map<String, Object> map = customerService.readAndValidateCustomer(path, dynamicHeaders, userSession, customerListId);
// File fileExport = (File) map.get("file");
// String message = (String) map.get("message");
// return ResponseEntity.ok()
// .header("Content-Type", Constants.MIME_TYPE.EXCEL_XLSX)
// .header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=template_import_customer.xlsx")
// .header("Message", message)
// .body(Files.readAllBytes(fileExport.toPath()));
return new ResponseEntity<>(null, HttpStatus.OK);
} catch (Exception e) {
logger.error(e.getMessage(), e);
return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
}
}
}
......@@ -2,15 +2,32 @@ package com.viettel.campaign.web.rest;
import com.viettel.campaign.model.ccms_full.Scenario;
import com.viettel.campaign.service.ScenarioService;
import com.viettel.campaign.utils.BundleUtils;
import com.viettel.campaign.utils.Constants;
import com.viettel.campaign.utils.RedisUtil;
import com.viettel.campaign.web.dto.ContactQuestResultDTO;
import com.viettel.campaign.web.dto.ResultDTO;
import com.viettel.campaign.web.dto.ScenarioDTO;
import com.viettel.econtact.filter.UserSession;
import org.apache.commons.io.FilenameUtils;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.text.SimpleDateFormat;
import java.util.*;
/**
* @author anhvd_itsol
......@@ -54,4 +71,82 @@ public class ScenarioController {
ResultDTO resultDTO = scenarioService.saveContacQuestResult(dto);
return new ResponseEntity<>(resultDTO, HttpStatus.OK);
}
@RequestMapping(value = "/download-file-template", method = RequestMethod.GET)
public ResponseEntity<?> downloadFileTemplate(HttpServletResponse response) {
XSSFWorkbook workbook = null;
byte[] contentReturn = null;
try {
String fileName = "import_scenario_template.xlsx";
response.setHeader("Content-Disposition", "attachment; filename=" + fileName);
OutputStream outputStream;
workbook = scenarioService.buildTemplate();
outputStream = response.getOutputStream();
workbook.write(outputStream);
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
workbook.write(byteArrayOutputStream);
contentReturn = byteArrayOutputStream.toByteArray();
} catch (Exception ex) {
logger.error(ex.getMessage(), ex);
return new ResponseEntity<byte[]>(null, null, HttpStatus.BAD_REQUEST);
} finally {
if (workbook != null) {
try {
workbook.close();
} catch (IOException e) {
logger.error(e.getMessage(), e);
}
}
}
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.parseMediaType("application/vnd.ms-excel"));
return new ResponseEntity<byte[]>(contentReturn, headers, HttpStatus.OK);
}
@RequestMapping(value = "/import-file", method = RequestMethod.POST)
public ResponseEntity<?> importFile(@RequestParam MultipartFile file) {
logger.info("------------IMPORT FILE TEMPLATE--------------");
Locale locale = new Locale("vi", "VN");
try {
UserSession userSession = new UserSession();
// UserSession userSession = (UserSession) RedisUtil.getInstance().get(authToken);
if (file.isEmpty()) {
return new ResponseEntity<>(BundleUtils.getLangString("common.fileNotSelected"), HttpStatus.OK);
}
if (!Objects.equals(FilenameUtils.getExtension(file.getOriginalFilename()), Constants.FileType.xlsx)) {
return new ResponseEntity<>(BundleUtils.getLangString("common.fileInvalidFormat", locale), HttpStatus.OK);
}
String path = saveUploadFile(file);
Map<String, Object> map = scenarioService.readAndValidateCustomer(path, userSession);
File fileExport = (File) map.get("file");
String message = (String) map.get("message");
return ResponseEntity.ok()
.header("Content-Type", Constants.MIME_TYPE.EXCEL_XLSX)
.header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=template_import_customer.xlsx")
.header("Message", message)
.body(Files.readAllBytes(fileExport.toPath()));
} catch (Exception e) {
logger.error(e.getMessage());
return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
}
}
private String saveUploadFile(MultipartFile file) {
try {
String currentTime = new SimpleDateFormat("yyyy_MM_dd_hh_mm_ss").format(new Date());
String fileName = FilenameUtils.getBaseName(file.getOriginalFilename()) + "_" + currentTime + "." + FilenameUtils.getExtension(file.getOriginalFilename());
byte[] content = file.getBytes();
File uploadFolder = new File(System.getProperty("user.dir") + File.separator + "etc" + File.separator + "upload");
if (!uploadFolder.exists()) {
uploadFolder.mkdir();
}
Path path = Paths.get(System.getProperty("user.dir") + File.separator + "etc", fileName);
Files.write(path, content);
return path.toString();
} catch (Exception e) {
logger.error(e.getMessage());
}
return null;
}
}
server:
port: 1111
port: 9999
spring:
application:
name: campaign
......
......@@ -108,3 +108,20 @@ customer.emailExists=Email exists;
common.fileNotSelected=Please select a file
common.fileInvalidFormat=File invalid
#IMPORT SCENARIO TEMPLATE
scenario.template.title=IMPORT QUESTIONS
scenario.template.questionCode=Question code
scenario.template.questionType=Question type
scenario.template.question=Question
scenario.template.answer=Answer
scenario.template.hashInput=Hash input?
scenario.template.required=Required?
scenario.template.default=Default?
scenario.template.mappingQuestion=Mapping question
scenario.template.yes=Yes
scenario.template.no=No
scenario.template.result=Result
scenario.template.singleChoice=SingleChoice
scenario.template.multiChoice=MultiChoice
scenario.template.text=Text
......@@ -110,3 +110,24 @@ customer.emailExists=Email đã tồn tại;
common.fileNotSelected=Bạn chưa chọn file
common.fileInvalidFormat=File không hợp lệ
#IMPORT SCENARIO TEMPLATE
scenario.template.title=IMPORT CÂU HỎI
scenario.template.questionCode=Mã câu hỏi
scenario.template.questionType=Loại câu hỏi
scenario.template.question=Câu hỏi
scenario.template.answer=Câu trả lời
scenario.template.hashInput=Nhập text?
scenario.template.required=Bắt buộc?
scenario.template.default=Mặc định?
scenario.template.mappingQuestion=Câu hỏi liên kết
scenario.template.yes=
scenario.template.no=Không
scenario.template.result=Kết quả
scenario.template.singleChoice=SingleChoice
scenario.template.multiChoice=MultiChoice
scenario.template.text=Text
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