package com.fdkankan.manage.service.impl;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.fdkankan.manage.common.ExcelErrorUtil;
import com.fdkankan.manage.common.ResultCode;
import com.fdkankan.manage.entity.AgentNew;
import com.fdkankan.manage.exception.BusinessException;
import com.fdkankan.manage.constant.CameraOutTypeEnum;
import com.fdkankan.manage.entity.Company;
import com.fdkankan.manage.service.IAgentNewService;
import com.fdkankan.manage.service.ICameraService;
import com.fdkankan.manage.service.ICompanyService;
import com.fdkankan.manage.service.IExcelService;
import com.fdkankan.manage.util.ExcelUtil;
import com.fdkankan.manage.vo.request.CameraInOutParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.Size;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
/**
*
* TODO
*
*
* @author dengsixing
* @since 2022/6/6
**/
@Service
@Slf4j
public class ExcelServiceImpl implements IExcelService {
@Autowired
ICompanyService companyService;
@Autowired
ICameraService cameraService;
@Autowired
IAgentNewService agentNewService;
public void commonExport(HttpServletRequest request, HttpServletResponse response,String name,List> result,ExcelWriter excelWriter) throws Exception {
response.setContentType("application/vnd.ms-excel");
response.setCharacterEncoding("utf-8");
String fileName = name + ".xlsx";
fileName = URLEncoder.encode(fileName, "UTF-8");
response.setHeader("Content-disposition", "attachment;filename=" + fileName);
WriteSheet writeSheet = EasyExcel.writerSheet(name).build();
excelWriter.write(result, writeSheet);
}
public void commonExport(HttpServletRequest request, HttpServletResponse response,String name,List> result,Class> clazz) throws Exception {
response.setContentType("application/vnd.ms-excel");
response.setCharacterEncoding("utf-8");
String fileName = name + ".xlsx";
fileName = URLEncoder.encode(fileName, "UTF-8");
response.setHeader("Content-disposition", "attachment;filename=" + fileName);
ExcelWriter excelWriter = null;
try {
excelWriter = EasyExcel.write(response.getOutputStream(), clazz).build();
WriteSheet writeSheet = EasyExcel.writerSheet(name).build();
excelWriter.write(result, writeSheet);
}catch (Exception e){
e.printStackTrace();
}finally {
if(excelWriter != null){
excelWriter.finish();
}
}
}
@Override
public Integer uploadExcel(MultipartFile file, Integer type) {
String originalFilename = file.getOriginalFilename();
assert originalFilename != null;
String fileType=originalFilename.substring(originalFilename.lastIndexOf(".")+1);
if (!fileType.equalsIgnoreCase("xlsx")) {
throw new BusinessException(ResultCode.FILE_TYPE_ERROR);
}
List> excelRowList = new ArrayList<>();
try {
excelRowList = ExcelUtil.getExcelRowList(file);
}catch (Exception e){
throw new BusinessException(ResultCode.TEMPLATE_TYPE_ERROR);
}
List wifiNameList = new ArrayList<>();
List params = new ArrayList<>();
List companyParams = new ArrayList<>();
List errorIndex = new ArrayList<>();
Integer index = 0;
for (HashMap map : excelRowList) {
index ++;
if(type == 0){ //入库
if(index == 0 && !map.get(0).equals("设备入库模板")){
throw new BusinessException(ResultCode.TEMPLATE_TYPE_ERROR);
}
if(index <4){ //从第四行开始
continue;
}
String wifiName = map.get(0);
if(StringUtils.isBlank(wifiName)){
errorIndex.add(index -3);
}
wifiNameList.add(wifiName);
}else if(type == 1){ //出库
if(index == 0 && !map.get(0).equals("设备出库模板")){
throw new BusinessException(ResultCode.TEMPLATE_TYPE_ERROR);
}
if(index <4){
continue;
}
String snCode = map.get(0);
String outTypeString = map.get(1);
String companyName = map.get(2);
String orderSn = map.get(3);
String agentName = map.get(4);
CameraOutTypeEnum outTypeEnum = CameraOutTypeEnum.getByMsg(outTypeString);
if(outTypeEnum == null || StringUtils.isBlank(snCode)){
log.error("outError-->出库错误:出库类型为空或snCode为空:snCode:{},outType:{},companyName:{},orderSn:{},agentName:{}"
,snCode,outTypeString,companyName,orderSn,agentName);
errorIndex.add(index -3);
}
CameraInOutParam param = new CameraInOutParam();
if(outTypeEnum != null){
int outType = outTypeEnum.getCode();
param.setOutType(outType);
}
if(param.getOutType() != null && param.getOutType() == 4 && StringUtils.isBlank(agentName)){
log.error("outError-->出库错误:经销商为空错误:snCode:{},outType:{},companyName:{},orderSn:{},agentName:{}"
,snCode,outTypeString,companyName,orderSn,agentName);
errorIndex.add(index -3);
}
if(param.getOutType() != null && param.getOutType() != 4 && StringUtils.isNotBlank(agentName)){
log.error("outError-->出库错误:出库类型错误:snCode:{},outType:{},companyName:{},orderSn:{},agentName:{}"
,snCode,outTypeString,companyName,orderSn,agentName);
errorIndex.add(index -3);
}
if(StringUtils.isNotBlank(agentName)){
AgentNew agentNew = agentNewService.getByName(agentName);
if(agentNew == null){
log.error("outError-->出库错误:代理商不存在:snCode:{},outType:{},companyName:{},orderSn:{},agentName:{}"
,snCode,outTypeString,companyName,orderSn,agentName);
errorIndex.add(index -3);
}else {
param.setAgentId(agentNew.getId());
}
}
param.setCompanyName(companyName);
param.setOrderSn(orderSn);
param.setSnCode(snCode);
params.add(param);
}else if(type == 2){ //关联客户
if(index == 0 && !map.get(0).equals("客户关联模板")){
throw new BusinessException(ResultCode.TEMPLATE_TYPE_ERROR);
}
if(index <4){
continue;
}
CameraInOutParam param = new CameraInOutParam();
param.setCompanyName(map.get(0));
param.setSnCode(map.get(1));
if(StringUtils.isBlank(param.getSnCode()) || StringUtils.isBlank(param.getCompanyName())){
errorIndex.add(index -3 );
}
companyParams.add(param);
}
}
this.toExcelError(errorIndex);
if(type == 0 && wifiNameList.size() <=0){
throw new BusinessException(ResultCode.IN_TEMPLATE_EMPTY);
}else if(type == 1 && params.size() <=0){
throw new BusinessException(ResultCode.OUT_TEMPLATE_EMPTY);
}else if(type == 2 && companyParams.size() <=0){
throw new BusinessException(ResultCode.COMPANY_TEMPLATE_EMPTY);
}
if(wifiNameList.size() >0){
return cameraService.ins(wifiNameList);
}
if(params.size() >0){
return cameraService.outs(params);
}
if(companyParams.size() >0){
return cameraService.updateCompany(companyParams);
}
return 0;
}
@Override
public void toExcelError(List errorList) {
String resultIn = ExcelErrorUtil.getResultIn(errorList);
if(StringUtils.isNotBlank(resultIn)){
throw new BusinessException(-1,resultIn);
}
}
}