wfw-erp-api-web服务
ErpMasterDataExcelController
package cn.com.esgcc.wfw.web.controller.web;
import cn.com.esgcc.wfw.common.service.ErpServiceResult;
import cn.com.esgcc.wfw.common.service.ServiceResult;
import cn.com.esgcc.wfw.erp.company.entity.po.ErpCompany;
import cn.com.esgcc.wfw.erp.company.service.ErpCompanyServiceBiz;
import cn.com.esgcc.wfw.erp.enums.MasterDataEnum;
import cn.com.esgcc.wfw.erp.enums.MasterDataImportTaskStatus;
import cn.com.esgcc.wfw.erp.masterdata.entity.po.MasterDataImportTask;
import cn.com.esgcc.wfw.erp.masterdata.entity.vo.ImportTaskVo;
import cn.com.esgcc.wfw.erp.masterdata.service.MasterDataImportTaskService;
import cn.com.esgcc.wfw.erp.masterdata.service.MasterDataImportTaskServiceBiz;
import cn.com.esgcc.wfw.fastdfs.service.FastdfsServiceBiz;
import cn.com.esgcc.wfw.gwutil.log.LogUtil;
import cn.com.esgcc.wfw.organisation.entity.po.Organisation;
import cn.com.esgcc.wfw.organisation.service.OrganisationServiceBiz;
import cn.com.esgcc.wfw.web.ApiConstants;
import cn.com.esgcc.wfw.web.ApiResultCode;
import cn.com.esgcc.wfw.web.base.APIResponse;
import cn.com.esgcc.wfw.web.base.BaseController;
import cn.com.esgcc.wfw.web.entity.mapper.ImportTaskMapper;
import cn.com.esgcc.wfw.web.entity.req.ImportTaskReq;
import cn.com.esgcc.wfw.web.session.SessionUser;
import cn.com.esgcc.wfw.web.util.easyexcel.ExcelHeadListener;
import com.alibaba.excel.EasyExcel;
import com.alibaba.fastjson.JSON;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
@RestController
public class ErpMasterDataExcelController extends BaseController {
private static final Logger logger = LoggerFactory.getLogger(ErpMasterDataExcelController.class);
@Autowired
private ErpCompanyServiceBiz erpCompanyServiceBiz;
@Autowired
private MasterDataImportTaskService masterDataImportTaskService;
@Autowired
private MasterDataImportTaskServiceBiz masterDataImportTaskServiceBiz;
@Autowired
private ImportTaskMapper importTaskMapper;
@Autowired
private FastdfsServiceBiz fastdfsServiceBiz;
@Autowired
private OrganisationServiceBiz organisationServiceBiz;
@PostMapping(ApiConstants.SYS_IMPORT_EXCEL+"{type}")
public APIResponse companyExcleImport(MultipartFile file, HttpServletRequest request,@PathVariable Integer type){
String check = fileCheck(file);
if(StringUtils.isNotBlank(check)){
return APIResponse.fail(ApiResultCode.FAIL,check);
}
String orgCode = request.getParameter("orgCode");
if(StringUtils.isBlank(orgCode)){
return APIResponse.fail(ApiResultCode.FAIL, "公司集成编码不能为空");
}
ErpServiceResult result = erpCompanyServiceBiz.selectByCompanyCode(orgCode);
ErpCompany erpCompany= (ErpCompany) result.getResult();
if(erpCompany == null){
return APIResponse.fail(ApiResultCode.FAIL, "公司集成编码不存在,无法导入数据");
}
MasterDataEnum masterDataEnum = MasterDataEnum.type(type);
if(masterDataEnum == null){
return APIResponse.fail(ApiResultCode.FAIL, "数据类型不存在,无法导入数据");
}
String aggregateType = request.getParameter("aggregateType");
if(StringUtils.isBlank(aggregateType)){
return APIResponse.fail(ApiResultCode.FAIL, "集成方式不能为空");
}
String filename = file.getOriginalFilename();
try {
InputStream inputStream = file.getInputStream();
ExcelHeadListener headlis = new ExcelHeadListener(masterDataEnum);
EasyExcel.read(inputStream, headlis).sheet().headRowNumber(2).doRead();
if(headlis.getFlag()){
String filePath = fastdfsServiceBiz.uploadFaceImg(file.getBytes(), filename);
MasterDataImportTask task = buildTask(filename, filePath, erpCompany.getId(), erpCompany.getCompanyName(),type,orgCode,aggregateType);
masterDataImportTaskService.save(task);
return APIResponse.success(ApiResultCode.SUCCESS);
}else{
return APIResponse.fail(ApiResultCode.FAIL,"导入模板不正确,无法导入");
}
} catch (IOException e) {
LogUtil.encError(logger, e.getMessage(), e);
return APIResponse.fail(ApiResultCode.FAIL, "上传文件失败");
}
}
@PostMapping(ApiConstants.SYS_IMPORT_EXCEL_LIST)
public APIResponse importTaskList(@RequestBody ImportTaskReq req) {
try {
ImportTaskVo taskVo = importTaskMapper.importTaskReqToVo(req);
ServiceResult result = masterDataImportTaskServiceBiz.findByVo(taskVo);
if (!result.getStatus()) {
return new APIResponse(ApiResultCode.FAIL, result.getMsg());
}
return new APIResponse(result.getResult());
} catch (Exception e) {
LogUtil.encError(logger, "查询任务列表方法(findByVo)出现异常: ", e);
return new APIResponse(ApiResultCode.SYSTEM_ERROR);
}
}
@PostMapping(ApiConstants.SYS_IMPORT_EXCEL_DOWNLOAD)
public void downloadExcel(@PathVariable("id") Integer id, HttpServletResponse resp) {
MasterDataImportTask importTask = masterDataImportTaskService.getByPrimaryKey(id);
if(importTask != null || importTask.getId() != null){
OutputStream os = null;
try {
resp.setContentType("application/octet-stream");
resp.setHeader("Connection", "close");
resp.setHeader("Accept-Ranges", "bytes");
resp.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(importTask.getFileName(), "UTF-8"));
resp.setCharacterEncoding("UTF-8");
os = resp.getOutputStream();
byte[] download = fastdfsServiceBiz.download(importTask.getDownloadUrl());
LogUtil.encInfo(logger, "文件服务器,下载excel:{}", null == download);
os.write(download);
} catch (IOException e) {
LogUtil.encError(logger, "下载excel异常:{}", e);
} finally {
try {
if (os != null) {
os.close();
}
} catch (IOException e) {
LogUtil.encError(logger, "下载excel关闭流异常", e);
}
}
}
}
private String fileCheck(MultipartFile file){
String fileExt = FilenameUtils.getExtension(file.getOriginalFilename());
if (!"xlsx".equalsIgnoreCase(fileExt) && !"xls".