package com.jx.erp.utils;
import com.alibaba.druid.util.StringUtils;
import com.jx.erp.exception.MyException;
import org.apache.poi.hssf.usermodel.*;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.util.List;
import java.util.Map;
/**
* excel根据
*/
public class ExcelUtils {
public static HSSFWorkbook generateListContent(List
调用
/**
* desc: 导出发票列表 param: contractId return: author: CDN date: 2019/7/11
*/
@GetMapping("exportInvoce")
public JsonResult exportInvoce(@RequestParam HashMap map, HttpServletResponse response) {
logger.info("==================" + CommonUtils.generateUUID() + " TRANSACTION_NAME : " + LOG_ROUTE + "exportInvoce REQUEST_DATA : " + JSON.toJSONString(map));
Map map1 = invoiceService.getListPage(map);
if (map1.get("listNoPage") == null) {
return JsonResult.buildSuccess(new ArrayList());
}
List invoiceList = (List) map1.get("listNoPage");
List listData = new ArrayList<>();
if (invoiceList.size() > 0) {
for (Invoice invoice : invoiceList) {
CompanyInfo companyInfo = companyInfoService.getById(invoice.getCompanyId());
if (!StrUtils.isNullOrEmpty(companyInfo)) {
InvoiceListData invoiceListData = new InvoiceListData();
invoiceListData.setCompanyName(companyInfo.getCompanyName());
BeanUtils.copyProperties(invoice, invoiceListData);
listData.add(invoiceListData);
}
}
} else {
return JsonResult.buildSuccess(new ArrayList());
}
Map respMap;
List> mapList = new ArrayList<>();
if (listData.size() > 0) {
respMap = new LinkedHashMap<>();
respMap.put("no", "发票号码");
respMap.put("code", "发票代码");
respMap.put("type", "发票类型");
respMap.put("state", "发票状态");
respMap.put("customerCompany", "客户公司");
respMap.put("selfCompanyName", "我方公司");
respMap.put("productName", "商品名称");
respMap.put("taxPrice", "税额");
respMap.put("sumPrice", "合计金额");
respMap.put("taxSumPrice", "价税合计");
respMap.put("souceNo", "原发票号码");
respMap.put("souceCode", "原发票代码");
respMap.put("noticeNo", "通知单编码");
respMap.put("drawer", "开票人");
respMap.put("openDate", "开票日期");
respMap.put("cancelDate", "作废人");
respMap.put("customerNo", "客户识别号");
respMap.put("listId", "清单标识");
mapList.add(respMap);
} else {
throw new MyException("暂无可导出的数据");
}
for (InvoiceListData aListData : listData) {
respMap = new LinkedHashMap<>();
if (!StrUtils.isNullOrEmpty(aListData
.getInvoiceNo())) {
respMap.put("no", aListData.getInvoiceNo());
} else {
respMap.put("no", "");
}
if (!StrUtils.isNullOrEmpty(aListData.getCode())) {
respMap.put("code", aListData.getCode());
} else {
respMap.put("code", "");
}
if (aListData.getType() > 0) {
respMap.put("type", aListData.getType() == 1 ? "增值税专票" : "增值税普票");
} else {
respMap.put("type", "");
}
if (aListData.getState() > 0) {
respMap.put("state", aListData.getState() == 1 ? "正常发票" : "异常发票");
} else {
respMap.put("state", "");
}
if (!StrUtils.isNullOrEmpty(aListData.getCustomerCompany())) {
respMap.put("customerCompany", aListData.getCustomerCompany());
} else {
respMap.put("customerCompany", "");
}
if (aListData.getCompanyId() > 0) {
CompanyInfo companyInfo = companyInfoService.getById(aListData.getCompanyId());
if (StrUtils.isNullOrEmpty(companyInfo)) {
respMap.put("selfCompanyName", "");
} else {
respMap.put("selfCompanyName", companyInfo.getCompanyName());
}
} else {
respMap.put("selfCompanyName", "");
}
if (!StrUtils.isNullOrEmpty(aListData.getProductName())) {
respMap.put("productName", aListData.getProductName());
} else {
respMap.put("productName", "");
}
if (aListData.getTaxPrice().compareTo(new BigDecimal(0)) > 0) {
respMap.put("taxPrice", aListData.getTaxPrice());
} else {
respMap.put("taxPrice", 0);
}
if (aListData.getSumPrice().compareTo(new BigDecimal(0)) > 0) {
respMap.put("sumPrice", aListData.getSumPrice());
} else {
respMap.put("sumPrice", 0);
}
if (aListData.getTaxSumPrice().compareTo(new BigDecimal(0)) > 0) {
respMap.put("taxSumPrice", aListData.getTaxSumPrice());
} else {
respMap.put("taxSumPrice", 0);
}
if (!StrUtils.isNullOrEmpty(aListData.getSouceNo())) {
respMap.put("souceNo", aListData.getSouceNo());
} else {
respMap.put("souceNo", "");
}
if (!StrUtils.isNullOrEmpty(aListData.getSouceCode())) {
respMap.put("souceCode", aListData.getSouceCode());
} else {
respMap.put("souceCode", "");
}
if (!StrUtils.isNullOrEmpty(aListData.getNoticeNo())) {
respMap.put("noticeNo", aListData.getNoticeNo());
} else {
respMap.put("noticeNo", "");
}
if (!StrUtils.isNullOrEmpty(aListData.getDrawer())) {
respMap.put("drawer", aListData.getDrawer());
} else {
respMap.put("drawer", "");
}
if (!StrUtils.isNullOrEmpty(DateUtils.DateFormatString2(aListData.getOpenDate()))) {
respMap.put("openDate", DateUtils.DateFormatString2(aListData.getOpenDate()));
} else {
respMap.put("openDate", "");
}
if (!StrUtils.isNullOrEmpty(aListData.getCancelMan())) {
respMap.put("cancelDate", aListData.getCancelMan());
} else {
respMap.put("cancelDate", "");
}
if (!StrUtils.isNullOrEmpty(aListData.getCustomerCompany())) {
respMap.put("customerNo", aListData.getCustomerNo());
} else {
respMap.put("customerNo", "");
}
respMap.put("listId", aListData.getListId());
mapList.add(respMap);
}
try {
ExcelUtils.write(ExcelUtils.generateListContent(mapList, "导出发票结果"), "导出发票结果", response);
} catch (IOException e) {
logger.info("########################导出发票数据异常##########################," + e);
throw new MyException("导出发票结果异常");
}
return JsonResult.buildSuccess(new ArrayList());
}