Commit 69955fcd by xiaoyj

订单导出

git-svn-id: svn://192.168.3.233/frms/code/car-service/trunk/car-service@21409 aeb6102e-e053-11e5-9fec-f9ebc45caf4b
1 parent 2ec9e70b
......@@ -38,7 +38,8 @@ public enum ResultCodeEnum {
USER_TYPE_NOT_SP(30012, "非服务商类型用户,没有权限"),
ORDER_AFTER_SALEING_CAN_NOT_CANCEL(30013, "检修进行中不能取消订单"),
ORDER_ACTIVE_OR_CANCEL(30014, "订单被取消或已被激活"),
EXPORT_UNKOWN_OEDER_OUTOF_NUMBER(30015,"导出未知订单超过10000条请修改搜索条件"),
EXPORT_OEDER_OUTOF_NUMBER(30016,"导出未知订单超过10000条请修改搜索条件"),
/**
* 渠道订单相关状态码
*/
......
package com.glsx.ordercenter.controller;
import com.glsx.excel.poi.ExcelUtil;
import com.glsx.excel.poi.Version;
import com.glsx.ordercenter.enums.MimeType;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLEncoder;
import java.util.List;
@Controller
public class BaseReportController extends BaseController {
/**
* 导出通用泛型
*
* @param response
* @param list
* @param fileName
* @throws IOException
*/
protected void exportObject(HttpServletResponse response, List list, String fileName)throws IOException {
InputStream inputStream = null;
if (CollectionUtils.isEmpty(list)) {
Workbook workbook = ExcelUtil.createEmptyWorkbook(Version.EXCEL2007, "无数据");
inputStream = ExcelUtil.toInputStream(workbook);
download(inputStream, MimeType.EXCEL2007, fileName, response);
return;
}
ExcelUtil<Object> exportModelExcelUtil = new ExcelUtil<Object>(1000);
SXSSFWorkbook sxssfWorkbook = null;
try {
sxssfWorkbook = (SXSSFWorkbook) exportModelExcelUtil.exportDefaultStyle(fileName, list);
inputStream = exportModelExcelUtil.toInputStream();
download(inputStream, MimeType.CVS, fileName, response);
} finally {
if (sxssfWorkbook != null) {
sxssfWorkbook.dispose();
}
IOUtils.closeQuietly(inputStream);
}
}
/**
* 下载流
* @param input
* @param mime
* @param fileName
* @param request
* @param response
* @throws IOException
*/
protected void download(InputStream input, MimeType mime, String fileName, HttpServletResponse response) throws IOException {
if (input == null) {
return;
}
boolean isAuto = false;
if (mime == MimeType.AUTO) {
String extension = FilenameUtils.getExtension(fileName);
mime = MimeType.parseMIME(extension);
isAuto = true;
}
response.setContentType(mime.getContentType());
response.setHeader("Set-Cookie", "fileDownload=true; path=/");
String suffix = mime.getSuffix();
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
String encodeFileName = encodeFilename(fileName, request);
if (StringUtils.isNotBlank(suffix) && !isAuto) {
encodeFileName += ("." + mime.getSuffix());
}
response.setHeader("Content-Disposition", "attachment;filename=\"" + encodeFileName + "\"");
long length = input.available();
if (length != -1 && length != 0) {
response.setContentLength((int) length);
}
ServletOutputStream output = response.getOutputStream();
IOUtils.copy(input, output);
output.flush();
}
/**
* 编码文件名
* @param filename
* @param request
* @return
*/
private static String encodeFilename(String filename, HttpServletRequest request) {
/**
* 获取客户端浏览器和操作系统信息
* 在IE浏览器中得到的是:User-Agent=Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; Maxthon; Alexa Toolbar)
* 在Firefox中得到的是:User-Agent=Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.7.10) Gecko/20050717 Firefox/1.0.6
*/
String agent = request.getHeader("USER-AGENT");
try {
if ((agent != null) && (-1 != agent.indexOf("MSIE"))) {
String newFileName = URLEncoder.encode(filename, "UTF-8");
newFileName = StringUtils.replace(newFileName, "+", "%20");
if (newFileName.length() > 150) {
newFileName = new String(filename.getBytes("GB2312"), "ISO8859-1");
newFileName = StringUtils.replace(newFileName, " ", "%20");
}
return newFileName;
}
if ((agent != null) && (-1 != agent.indexOf("Mozilla"))) {
return new String(filename.getBytes("UTF-8"), "ISO8859-1");
}
return filename;
} catch (Exception ex) {
return filename;
}
}
}
package com.glsx.ordercenter.controller;
import com.glsx.common.annotation.AuthCheck;
import com.glsx.common.utils.UserUtils;
import com.glsx.ordercenter.constants.ResultCodeEnum;
import com.glsx.ordercenter.dto.QueryOrderReq;
import com.glsx.ordercenter.exception.SysException;
import com.glsx.ordercenter.pojo.ExportOrderVo;
import com.glsx.ordercenter.pojo.ExportUnknownOrderVo;
import com.glsx.ordercenter.pojo.SysOrder;
import com.glsx.ordercenter.service.IOrderService;
import com.glsx.ordercenter.service.IUnknownOrderService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
@AuthCheck
@RestController
@RequestMapping(value="/report")
public class ReportController extends BaseReportController{
@Autowired
private IUnknownOrderService unknownOrderService;
@Autowired
private IOrderService orderService;
@RequestMapping(value="/exportUnknownOrder",method={RequestMethod.GET, RequestMethod.POST})
public void exportUnknownOrder(HttpServletRequest request, HttpServletResponse response, SysOrder order){
List<ExportUnknownOrderVo> list1=new ArrayList<>();
Integer userId=UserUtils.getUserId(request);
List<SysOrder> list=unknownOrderService.getUnknowOrderList(null,order, userId);
if(list.size()>10000){
throw SysException.create(String.valueOf(ResultCodeEnum.EXPORT_UNKOWN_OEDER_OUTOF_NUMBER.getCode()),ResultCodeEnum.EXPORT_UNKOWN_OEDER_OUTOF_NUMBER.getMsg());
}
if(list.size()>0){
for(SysOrder sysOrder:list){
ExportUnknownOrderVo exportUnknownOrderVo=new ExportUnknownOrderVo();
BeanUtils.copyProperties(sysOrder,exportUnknownOrderVo);
if(sysOrder.getShopId()==null){
exportUnknownOrderVo.setReason("未绑定商户");
}else{
exportUnknownOrderVo.setReason("未绑定服务商");
}
list1.add(exportUnknownOrderVo);
}
}
try {
exportObject(response,list1,"导出未知订单列表");
} catch (IOException e) {
logger.info("导出未知订单列表异常");
}
}
@RequestMapping(value="/exportOrder",method={RequestMethod.GET, RequestMethod.POST})
public void exportOrder(HttpServletRequest request, HttpServletResponse response, QueryOrderReq oueryOrderReq){
List<ExportOrderVo> list1=new ArrayList<>();
oueryOrderReq.setUserId( UserUtils.getUserId(request));
List<SysOrder> list=orderService.getOrderListByExample(null, null, oueryOrderReq);
if(list.size()>10000){
throw SysException.create(String.valueOf(ResultCodeEnum.EXPORT_OEDER_OUTOF_NUMBER.getCode()),ResultCodeEnum.EXPORT_OEDER_OUTOF_NUMBER.getMsg());
}
if(list.size()>0){
for(SysOrder sysOrder:list){
ExportOrderVo exportOrderVo=new ExportOrderVo();
BeanUtils.copyProperties(sysOrder,exportOrderVo);
exportOrderVo.setActiveStatus(tranceActiveStatus(sysOrder.getActiveStatus()));
if(sysOrder.getMachanicInfo()!=null){
exportOrderVo.setMerchanicInfo(sysOrder.getMachanicInfo().getName()+sysOrder.getMachanicInfo().getPhone());
}
list1.add(exportOrderVo);
}
}
try {
exportObject(response,list1,"导出订单列表");
} catch (IOException e) {
logger.info("导出订单列表异常");
}
}
private String tranceActiveStatus(Byte activeStatus){
if(activeStatus==1){
return "商户已派单";
}else if(activeStatus==2){
return "服务商已接单,未指派给技工";
}else if(activeStatus==3){
return "服务商拒单";
}else if(activeStatus==4){
return "服务商已派单,技工未接单";
}else if(activeStatus==5){
return "技工拒单";
}else if(activeStatus==6){
return "技工接单未预约";
} else if(activeStatus==7){
return "技工已预约,未完成";
}else{
return "已完成";
}
}
}
package com.glsx.ordercenter.enums;
import org.apache.commons.lang.StringUtils;
public enum MimeType {
AUTO("自动获取", ""),
STREAM("application/octet-stream", "jpg"),
PDF("application/pdf", "pdf"),
ZIP("application/zip", "zip"),
RAR("application/zip", "rar"),
EXCEL2003("application/vnd.ms-excel", "xls"),
EXCEL2007("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "xlsx"),
EXE("application/octet-stream", "exe"),
CVS("application/cvs", "cvs"),
TXT("text/plain", "txt"),
JAVA("text/plain", "java"),
PYTHON("text/plain", "py"),
JAVASCRIPT("text/plain", "js"),
CSS("text/plain", "css"),
JPG("application/x-jpg", "jpg"),
JPEG("image/jpeg", "jpg"),
GIF("image/gif", "gif"),
PNG("application/x-png", "png");
private String contentType;
private String suffix;
public String getContentType() {
return contentType + ";charset=utf-8";
}
private MimeType(String contentType, String suffix) {
this.contentType = contentType;
this.suffix = suffix;
}
public String getSuffix() {
return suffix;
}
public static MimeType parseMIME(String fileType) {
MimeType[] values = MimeType.values();
for (MimeType mimeType : values) {
if (mimeType.getSuffix().equalsIgnoreCase(fileType)) {
return mimeType;
}
}
return null;
}
}
\ No newline at end of file
package com.glsx.ordercenter.exception;
import com.glsx.common.constants.ResultObject;
import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang.ObjectUtils;
/**
* 系统业务异常,异常号段为 :
* 0 : 成功
* 1 ~ 9999 内定系统异常段
* 10000 ~ 99999 自定义异常码段
* 100000 ~ Integer.MAX_VALUE 动态异常码段
*/
public class BusinessException extends RuntimeException {
protected ResultObject resultEntity;
protected static final int MIN_AUTO_CODE = 100000;
protected BusinessException(BusinessException parent) {
if(parent != null){
this.resultEntity = parent.resultEntity;
}
}
public static BusinessException create(String message) {
int value= (int) (MIN_AUTO_CODE + Math.round((Integer.MAX_VALUE - MIN_AUTO_CODE) * Math.random()));
return create(value + "",message);
}
public static BusinessException create(String returnCode,String message){
BusinessException businessException = new BusinessException(null);
businessException.resultEntity = ResultObject.err(returnCode).message(message);
return businessException;
}
public static BusinessException create(ResultObject resultEntity){
BusinessException businessException = new BusinessException(null);
businessException.resultEntity = resultEntity;
return businessException;
}
public static BusinessException create(ExceptionCause exceptionCause ,Object...args){
ResultObject resultEntity = exceptionCause.result();
String message = resultEntity.getMessage();
if(ArrayUtils.isNotEmpty(args)){
String [] argsStringArray = new String [args.length];
for (int i=0;i<args.length;i++) {
Object arg = args[i];
argsStringArray[i] = ObjectUtils.toString(arg);
}
String formatMessage = String.format(message, argsStringArray);
resultEntity.setMessage(formatMessage);
}
BusinessException businessException = new BusinessException(null);
businessException.resultEntity = resultEntity;
return businessException;
}
@Override
public String getMessage() {
return resultEntity.getMessage();
}
public ResultObject getResultEntity() {
return resultEntity;
}
}
package com.glsx.ordercenter.exception;
import com.glsx.common.constants.ResultObject;
public interface ExceptionCause<T extends Exception> {
/**
* 创建异常
* @return
*/
T exception(Object... args);
ResultObject result();
}
......@@ -24,6 +24,9 @@ public class GlobalExceptionHandler {
@ExceptionHandler(Exception.class)
public ResultObject exceptionHandler(Exception e){
if(e instanceof BusinessException){
return new ResultObject(((BusinessException) e).getResultEntity().getCode(), e.getMessage());
}
logger.error("系统异常-->",e);
return new ResultObject(ResultCodeEnum.ERROR.getCode(), ResultCodeEnum.ERROR.getMsg());
}
......
package com.glsx.ordercenter.exception;
public class SysException extends BusinessException {
protected SysException(BusinessException parent) {
super(parent);
}
public static SysException create(ExceptionCause exceptionCause, Object... args) {
BusinessException businessException = BusinessException.create(exceptionCause, args);
return new SysException(businessException);
}
}
package com.glsx.ordercenter.pojo;
import com.glsx.excel.poi.annotation.ExcelColumn;
import com.glsx.excel.poi.annotation.ExcelExport;
import java.util.Date;
@ExcelExport
public class ExportOrderVo {
@ExcelColumn(value = "订单编号",index = 0)
private String orderSn;
@ExcelColumn(value = "订单渠道",index = 1)
private String orderChannel;
@ExcelColumn(value = "服务类型",index = 2)
private String installDeviceType;
@ExcelColumn(value = "派单日期",index = 3,pattern = "yyyy-MM-dd HH:mm:ss")
private Date createTime;
@ExcelColumn(value = "状态",index = 4)
private String activeStatus;
@ExcelColumn(value = "上门类型",index = 5)
private String serviceType;
@ExcelColumn(value = "技工信息",index = 6)
private String merchanicInfo;
@ExcelColumn(value = "服务地址",index = 7)
private String adress;
@ExcelColumn(value = "车架号",index = 8)
private String vinNo;
@ExcelColumn(value = "车主姓名",index = 9)
private String carOwner;
@ExcelColumn(value = "商户名称",index = 10)
private String merchantName;
@ExcelColumn(value = "(所在省",index = 11)
private String province;
@ExcelColumn(value = "所在市",index = 12)
private String city;
@ExcelColumn(value = "第三方工单号",index = 13)
private String workOrderNo;
public String getOrderSn() {
return orderSn;
}
public void setOrderSn(String orderSn) {
this.orderSn = orderSn;
}
public String getOrderChannel() {
return orderChannel;
}
public void setOrderChannel(String orderChannel) {
this.orderChannel = orderChannel;
}
public String getInstallDeviceType() {
return installDeviceType;
}
public void setInstallDeviceType(String installDeviceType) {
this.installDeviceType = installDeviceType;
}
public Date getCreateTime() {
return createTime;
}
public void setCreateTime(Date createTime) {
this.createTime = createTime;
}
public String getActiveStatus() {
return activeStatus;
}
public void setActiveStatus(String activeStatus) {
this.activeStatus = activeStatus;
}
public String getServiceType() {
return serviceType;
}
public void setServiceType(String serviceType) {
this.serviceType = serviceType;
}
public String getMerchanicInfo() {
return merchanicInfo;
}
public void setMerchanicInfo(String merchanicInfo) {
this.merchanicInfo = merchanicInfo;
}
public String getAdress() {
return adress;
}
public void setAdress(String adress) {
this.adress = adress;
}
public String getVinNo() {
return vinNo;
}
public void setVinNo(String vinNo) {
this.vinNo = vinNo;
}
public String getCarOwner() {
return carOwner;
}
public void setCarOwner(String carOwner) {
this.carOwner = carOwner;
}
public String getMerchantName() {
return merchantName;
}
public void setMerchantName(String merchantName) {
this.merchantName = merchantName;
}
public String getProvince() {
return province;
}
public void setProvince(String province) {
this.province = province;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getWorkOrderNo() {
return workOrderNo;
}
public void setWorkOrderNo(String workOrderNo) {
this.workOrderNo = workOrderNo;
}
}
package com.glsx.ordercenter.pojo;
import com.glsx.excel.poi.annotation.ExcelColumn;
import com.glsx.excel.poi.annotation.ExcelExport;
import java.util.Date;
@ExcelExport
public class ExportUnknownOrderVo {
@ExcelColumn(value = "订单编号",index = 0)
private String orderSn;
@ExcelColumn(value = "订单渠道",index = 1)
private String rootOrgName;
@ExcelColumn(value = "商户名称",index = 2)
private String merchantName;
@ExcelColumn(value = "服务类型",index = 3)
private String installDeviceTypeName;
@ExcelColumn(value = "派单日期",index = 4,pattern = "yyyy-MM-dd HH:mm:ss")
private Date createTime;
@ExcelColumn(value = "未派单原因",index = 5)
private String reason;
@ExcelColumn(value = "服务地址",index = 6)
private String address;
@ExcelColumn(value = "(所在省",index = 7)
private String province;
@ExcelColumn(value = "所在市",index = 8)
private String city;
@ExcelColumn(value = "第三方工单号",index = 9)
private String workOrderNo;
public String getOrderSn() {
return orderSn;
}
public void setOrderSn(String orderSn) {
this.orderSn = orderSn;
}
public String getRootOrgName() {
return rootOrgName;
}
public void setRootOrgName(String rootOrgName) {
this.rootOrgName = rootOrgName;
}
public String getMerchantName() {
return merchantName;
}
public void setMerchantName(String merchantName) {
this.merchantName = merchantName;
}
public String getInstallDeviceTypeName() {
return installDeviceTypeName;
}
public void setInstallDeviceTypeName(String installDeviceTypeName) {
this.installDeviceTypeName = installDeviceTypeName;
}
public Date getCreateTime() {
return createTime;
}
public void setCreateTime(Date createTime) {
this.createTime = createTime;
}
public String getReason() {
return reason;
}
public void setReason(String reason) {
this.reason = reason;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getProvince() {
return province;
}
public void setProvince(String province) {
this.province = province;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getWorkOrderNo() {
return workOrderNo;
}
public void setWorkOrderNo(String workOrderNo) {
this.workOrderNo = workOrderNo;
}
}
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!