com.mbv.web.rest.controller.DeliveryController.java Source code

Java tutorial

Introduction

Here is the source code for com.mbv.web.rest.controller.DeliveryController.java

Source

/*
 * Copyright 2015 Metersbonwe.com All right reserved. This software is the
 * confidential and proprietary information of Metersbonwe.com ("Confidential
 * Information"). You shall not disclose such Confidential Information and shall
 * use it only in accordance with the terms of the license agreement you entered
 * into with Metersbonwe.com.
 */
package com.mbv.web.rest.controller;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import net.sf.jasperreports.engine.JRDataSource;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JRExporterParameter;
import net.sf.jasperreports.engine.JasperCompileManager;
import net.sf.jasperreports.engine.JasperExportManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;
import net.sf.jasperreports.engine.export.JRPdfExporter;
import net.sf.jasperreports.j2ee.servlets.ImageServlet;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.mbv.biz.config.entity.SysParameterEntity;
import com.mbv.biz.config.service.SysParameterService;
import com.mbv.common.constant.MbvConstant;
import com.mbv.common.exception.MbvException;
import com.mbv.inventory.bean.DeliveryBean;
import com.mbv.inventory.bean.DeliveryDataSource;
import com.mbv.inventory.bean.DeliveryGoodBean;
import com.mbv.inventory.bean.DeliveryParamBean;
import com.mbv.inventory.bean.GoodBean;
import com.mbv.inventory.bean.PdfDetail;
import com.mbv.inventory.bean.PickBean;
import com.mbv.inventory.bean.ResultBean;
import com.mbv.inventory.bean.VpDegBean;
import com.mbv.inventory.entity.VpSenderEntity;
import com.mbv.inventory.service.DeliveryService;
import com.mbv.web.rest.util.DoubleUtil;

/**
 * @????controller
 * @201596
 * @version
 */
@Controller
@RequestMapping("/delivery")
public class DeliveryController extends BaseController {

    private Logger log = LoggerFactory.getLogger(DeliveryController.class);

    @Resource
    private DeliveryService deliveryService;

    @Resource
    private SysParameterService sysParameterService;

    // 
    public static final int Bill_Query_PAGENATION_LENGTH = 10;

    java.text.DecimalFormat df = new java.text.DecimalFormat("0.00");

    /**
      * @????
      * @201596
      * @param
      * @version
      * @throws MbvException 
     */
    @RequestMapping(value = "/queryByParams", method = RequestMethod.POST)
    public void queryByParams(HttpServletRequest request, HttpServletResponse response) throws MbvException {
        Map<String, Object> map = new HashMap<String, Object>();
        try {
            String status = request.getParameter("status");
            String currentPage = request.getParameter("currentPage");
            String orderSn = request.getParameter("orderSn");
            String docCode = request.getParameter("docCode");
            HttpSession session = request.getSession();
            String unitCode = (String) session.getAttribute(MbvConstant.UNIT_CODE);
            if (currentPage == null || currentPage.isEmpty()) {
                currentPage = "1";
            }
            int firstPage = Bill_Query_PAGENATION_LENGTH * (Integer.parseInt(currentPage) - 1);
            Map<String, Object> paramsMap = new HashMap<String, Object>();
            paramsMap.put("docState", status);
            paramsMap.put("unitCode", unitCode);
            paramsMap.put("firstPage", firstPage);
            paramsMap.put("length", Bill_Query_PAGENATION_LENGTH);
            paramsMap.put("orderSn", orderSn);
            paramsMap.put("docCode", docCode);
            List<VpDegBean> deliveryList = deliveryService.queryOrderByParams(paramsMap);
            int totalItems = deliveryService.queryByParamsCount(paramsMap);
            int totalPage = totalItems % Bill_Query_PAGENATION_LENGTH == 0
                    ? totalItems / Bill_Query_PAGENATION_LENGTH
                    : totalItems / Bill_Query_PAGENATION_LENGTH + 1;
            totalPage = totalPage < 1 ? 1 : totalPage;
            // ???
            map.put("currentPage", currentPage);
            map.put("totalPage", totalPage);
            map.put("totalItems", totalItems);
            map.put("deliveryList", deliveryList);
        } catch (MbvException e) {
            throw new MbvException(MbvConstant.MBV_SYS_ERROR_TIP);
        } catch (RuntimeException e) {
            throw new MbvException(MbvConstant.MBV_SYS_ERROR_TIP);
        }

        this.returnSuccess(response, map);
    }

    //?
    @RequestMapping(value = "/send", method = RequestMethod.POST)
    @ResponseBody
    public void send(HttpServletRequest request, HttpServletResponse response, Long degId) throws MbvException {
        Map<String, Object> map = new HashMap<String, Object>();
        try {
            ResultBean result = deliveryService.send(degId);
            map.put("flag", result.isFlag());
            map.put("message", result.getMessage());
        } catch (MbvException e) {
            map.put("flag", false);
            map.put("message", e.getMessage());
        } catch (RuntimeException e) {
            map.put("flag", false);
            map.put("message", MbvConstant.MBV_SYS_ERROR_TIP);
        } catch (Exception e) {
            map.put("flag", false);
            map.put("message", MbvConstant.MBV_SYS_ERROR_TIP);
        }

        returnSuccess(response, map);
    }

    //??
    @RequestMapping(value = "/checkDelivery", method = RequestMethod.POST)
    @ResponseBody
    public String checkDelivery(HttpServletRequest request, HttpServletResponse response,
            @RequestBody DeliveryParamBean delivery) throws MbvException {
        boolean flag = true;
        String message = "";
        Long degId = delivery.getDegId();
        List<DeliveryGoodBean> delGoodList = delivery.getDelGoodList();
        DeliveryBean deliveryEntity = deliveryService.getDeliveryBeanById(degId);
        if (deliveryEntity != null) {
            String province = deliveryEntity.getProvince();
            String city = deliveryEntity.getCity();
            String district = deliveryEntity.getDistrict();
            String address = deliveryEntity.getDelivAddress();
            deliveryEntity.setDelivAddress(province + city + district + address);
        }

        //??
        if (deliveryEntity != null && "8".equals(deliveryEntity.getDocState())) {
            message = "?";

            Map<String, Object> map = new HashMap<String, Object>();
            map.put("message", message);
            returnSuccess(response, map);
            return null;
        }

        //??
        if (deliveryEntity != null && !"1".equals(deliveryEntity.getDocState())) {
            message = "???";

            Map<String, Object> map = new HashMap<String, Object>();
            map.put("message", message);
            returnSuccess(response, map);
            return null;
        }

        List<GoodBean> goodList = deliveryService.queryGoodByParams(degId);
        //??????
        if (delGoodList != null && goodList != null && delGoodList.size() == goodList.size()) {
            List<DeliveryGoodBean> newDelGoodList = new ArrayList<DeliveryGoodBean>();
            for (GoodBean good : goodList) {
                DeliveryGoodBean delGood = new DeliveryGoodBean();
                delGood.setProdNum(good.getProdNum());
                delGood.setQty(good.getQty());
                //flag  = delGoodList.contains(delGood);
                newDelGoodList.add(delGood);
            }
            flag = (delGoodList.containsAll(newDelGoodList) && newDelGoodList.containsAll(delGoodList));
            if (!flag)
                message = "???";
        } else {
            flag = false;
            message = "???";
        }
        //????
        //flag = deliveryService.isChangeGoodInfo(deliveryEntity.getDocCode());
        if (!flag) {
            message = "???";
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("message", message);
            map.put("goodList", goodList);
            returnSuccess(response, map);
            return null;
        }

        //???
        flag = deliveryService.isChangeReceiveAddress(deliveryEntity.getDocCode());
        if (flag) {
            message = "??";
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("message", message);
            map.put("delivery", deliveryEntity);
            returnSuccess(response, map);
            return null;
        }

        message = "success";
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("message", message);
        returnSuccess(response, map);

        return null;
    }

    //???
    @RequestMapping(value = "/getGoodList", method = RequestMethod.POST)
    @ResponseBody
    public void getGoodList(HttpServletRequest request, HttpServletResponse response) throws MbvException {
        Map<String, Object> map = new HashMap<String, Object>();
        try {
            Long degId = Long.parseLong(request.getParameter("degId"));
            List<GoodBean> goodList = deliveryService.queryGoodByParams(degId);
            map.put("degId", degId);
            map.put("goodList", goodList);
        } catch (MbvException e) {
            throw new MbvException(MbvConstant.MBV_SYS_ERROR_TIP);
        } catch (RuntimeException e) {
            throw new MbvException(MbvConstant.MBV_SYS_ERROR_TIP);
        }

        this.returnSuccess(response, map);
    }

    //??
    @RequestMapping(value = "/getLackGoodList", method = RequestMethod.POST)
    public void getLackGoodList(HttpServletRequest request, HttpServletResponse response) throws MbvException {
        Map<String, Object> map = new HashMap<String, Object>();
        Long degId = Long.parseLong(request.getParameter("degId"));
        try {
            DeliveryBean delivery = deliveryService.getDeliveryBeanById(degId);
            List<GoodBean> goodList = deliveryService.queryGoodByParams(degId);

            map.put("delivery", delivery);
            map.put("goodList", goodList);
        } catch (MbvException e) {
            throw new MbvException(MbvConstant.MBV_SYS_ERROR_TIP);
        } catch (RuntimeException e) {
            throw new MbvException(MbvConstant.MBV_SYS_ERROR_TIP);
        }

        this.returnSuccess(response, map);
    }

    //???
    @RequestMapping(value = "/isUndelivered", method = RequestMethod.POST)
    @ResponseBody
    public void isUndelivered(HttpServletRequest request, HttpServletResponse response, Long degId)
            throws MbvException {
        boolean flag = true;
        DeliveryBean delivery = deliveryService.getDeliveryBeanById(degId);
        if (delivery != null && "1".equals(delivery.getDocState())) {
            flag = true;
        } else {
            flag = false;
        }
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("message", flag);

        this.returnSuccess(response, map);
    }

    //
    @RequestMapping(value = "/pack", method = RequestMethod.GET)
    public ModelAndView pack(HttpServletRequest request, HttpServletResponse response) throws MbvException {
        Map<String, Object> map = new HashMap<String, Object>();
        String message = "";
        Long degId = Long.parseLong(request.getParameter("degId"));
        try {
            DeliveryBean delivery = deliveryService.getDeliveryBeanById(degId);
            if (delivery != null) {
                String province = delivery.getProvince();
                String city = delivery.getCity();
                String district = delivery.getDistrict();
                String address = delivery.getDelivAddress();
                delivery.setDelivAddress(province + city + district + address);
            }

            List<GoodBean> goodList = deliveryService.queryGoodByParams(degId);
            HttpSession session = request.getSession();
            String warehCode = (String) session.getAttribute(MbvConstant.WAREH_CODE);
            VpSenderEntity sendAddress = deliveryService.getSendAddressByWarehCode(warehCode);
            if (sendAddress == null) {
                message = "????";
            } else {
                String province = sendAddress.getProvince();
                String city = sendAddress.getCity();
                String district = sendAddress.getDistrict();
                String address = sendAddress.getAddress();
                sendAddress.setAddress(province + city + district + address);
            }

            map.put("message", message);
            map.put("delivery", delivery);
            map.put("goodList", goodList);
            map.put("sendAddress", sendAddress);
            return new ModelAndView("/inventory/pack", map);
        } catch (MbvException e) {
            ModelAndView error = new ModelAndView();
            error.setViewName("/error/error");
            return error;
        } catch (RuntimeException e) {
            ModelAndView error = new ModelAndView();
            error.setViewName("/error/error");
            return error;
        }

    }

    //????
    @RequestMapping(value = "/isExistWn", method = RequestMethod.POST)
    @ResponseBody
    public void isExistWn(HttpServletRequest request, HttpServletResponse response, String docCode, String orderSn)
            throws MbvException {
        boolean flag = true;
        HttpSession session = request.getSession();
        String unitCode = (String) session.getAttribute(MbvConstant.UNIT_CODE);
        Map<String, Object> paramsMap = new HashMap<String, Object>();
        paramsMap.put("docCode", docCode);
        paramsMap.put("unitCode", unitCode);
        paramsMap.put("orderSn", orderSn);

        flag = deliveryService.isExistWn(paramsMap);
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("message", flag);

        this.returnSuccess(response, map);
    }

    //
    @RequestMapping(value = "/saveWn", method = RequestMethod.POST)
    @ResponseBody
    public void saveWn(HttpServletRequest request, HttpServletResponse response, String remark, Long degId,
            String content, String type) throws MbvException {
        HttpSession session = request.getSession();
        String user = (String) session.getAttribute(MbvConstant.USER_CODE);
        String unitCode = (String) session.getAttribute(MbvConstant.UNIT_CODE);

        Map<String, Object> paramsMap = new HashMap<String, Object>();
        paramsMap.put("degId", degId);
        paramsMap.put("user", user);
        paramsMap.put("unitCode", unitCode);
        paramsMap.put("content", content);
        paramsMap.put("remark", remark);
        paramsMap.put("type", type);

        boolean flag = false;
        Map<String, Object> map = new HashMap<String, Object>();
        try {
            flag = deliveryService.saveWn(paramsMap);
            map.put("flag", flag);
            map.put("message", "");
        } catch (MbvException e) {
            map.put("flag", false);
            map.put("message", e.getMessage());
        } catch (RuntimeException e) {
            throw new MbvException(MbvConstant.MBV_SYS_ERROR_TIP);
        } catch (Exception e) {
            throw new MbvException(MbvConstant.MBV_SYS_ERROR_TIP);
        }

        returnSuccess(response, map);
    }

    //??
    @RequestMapping(value = "/printPick", method = RequestMethod.GET)
    public String printPick(HttpServletRequest request, HttpServletResponse response) throws MbvException {
        Long degId = Long.parseLong(request.getParameter("degId"));
        List<Long> degIds = new ArrayList<Long>();
        degIds.add(degId);

        List<GoodBean> goodList = deliveryService.queryGoodByParams(degId);

        String jasperPath = request.getSession().getServletContext().getRealPath("/reports/pick.jrxml");

        Map<String, Object> parameters = new HashMap<String, Object>();
        parameters.put("SUBREPORT_DIR", jasperPath);
        List<DeliveryDataSource> list = new ArrayList<DeliveryDataSource>();
        List<PickBean> goods = new ArrayList<PickBean>();
        int i = 0;
        for (GoodBean goodBean : goodList) {
            i++;
            PickBean good = new PickBean();
            good.setId(i);
            good.setCode(goodBean.getProdNum());
            good.setName(goodBean.getBrandName() + " " + goodBean.getProductName() + " " + goodBean.getColorName()
                    + " " + goodBean.getSizeName());
            good.setNum(goodBean.getQty());
            goods.add(good);
        }

        DeliveryDataSource deliverySource = new DeliveryDataSource();
        deliverySource.setOrdercode(degId + "");
        deliverySource.setTable_datas(new JRBeanCollectionDataSource(goods));
        list.add(deliverySource);

        try {
            JRDataSource dataSource = new JRBeanCollectionDataSource(list);
            JasperReport jasperReport = JasperCompileManager.compileReport(jasperPath);
            JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, parameters, dataSource);
            request.getSession().setAttribute(ImageServlet.DEFAULT_JASPER_PRINT_SESSION_ATTRIBUTE, jasperPrint);

            byte[] bytes = JasperExportManager.exportReportToPdf(jasperPrint);

            response.setContentType("application/pdf");
            response.setContentLength(bytes.length);

            ServletOutputStream out = response.getOutputStream();
            out.write(bytes);
            out.flush();
            //end 

            //?
            deliveryService.updatePickPrintCount(degIds);

        } catch (IOException ioe) {
            System.err.println(ioe.getMessage());
            return null;
        } catch (JRException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return null;
        }

        return null;
    }

    //???
    @RequestMapping(value = "/printPickBatch", method = RequestMethod.GET)
    public void printPickBatch(HttpServletRequest request, HttpServletResponse response) throws MbvException {
        String degIdStr = request.getParameter("degIds");
        String[] degIdStrs = degIdStr.split(",");
        String orderCodes = request.getParameter("orderCodes").replace(",", "?");
        List<Long> degIds = new ArrayList<Long>();
        for (String idStr : degIdStrs) {
            degIds.add(Long.parseLong(idStr));
        }

        List<GoodBean> goodList = deliveryService.queryGoodListByIds(degIds);

        String jasperPath = request.getSession().getServletContext().getRealPath("/reports/pick.jrxml");

        Map<String, Object> parameters = new HashMap<String, Object>();
        parameters.put("SUBREPORT_DIR", jasperPath);
        List<DeliveryDataSource> list = new ArrayList<DeliveryDataSource>();
        List<PickBean> goods = new ArrayList<PickBean>();
        int i = 0;
        for (GoodBean goodBean : goodList) {
            i++;
            PickBean good = new PickBean();
            good.setId(i);
            good.setCode(goodBean.getProdNum());
            good.setName(goodBean.getBrandName() + " " + goodBean.getProductName() + " " + goodBean.getColorName()
                    + " " + goodBean.getSizeName());
            good.setNum(goodBean.getQty());
            goods.add(good);
        }

        DeliveryDataSource deliverySource = new DeliveryDataSource();
        deliverySource.setOrdercode(orderCodes);
        deliverySource.setTable_datas(new JRBeanCollectionDataSource(goods));
        list.add(deliverySource);

        try {
            JRDataSource dataSource = new JRBeanCollectionDataSource(list);
            JasperReport jasperReport = JasperCompileManager.compileReport(jasperPath);
            JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, parameters, dataSource);
            request.getSession().setAttribute(ImageServlet.DEFAULT_JASPER_PRINT_SESSION_ATTRIBUTE, jasperPrint);

            byte[] bytes = JasperExportManager.exportReportToPdf(jasperPrint);

            response.setContentType("application/pdf");
            response.setContentLength(bytes.length);

            ServletOutputStream out = response.getOutputStream();
            out.write(bytes);
            out.flush();
            //end 

            //??
            deliveryService.updatePickPrintCount(degIds);

        } catch (IOException ioe) {
            System.err.println(ioe.getMessage());
        } catch (JRException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

    //??
    @RequestMapping(value = "/printExpress", method = RequestMethod.GET)
    public void printExpress(HttpServletRequest request, HttpServletResponse response) throws MbvException {
        Long degId = Long.parseLong(request.getParameter("degId"));
        int printCount = Integer.parseInt(request.getParameter("printCount"));
        String tspComCode = (String) request.getParameter("tspComCode");
        DeliveryBean delivery = deliveryService.getDeliveryBeanById(degId);
        HttpSession session = request.getSession();
        String warehCode = (String) session.getAttribute(MbvConstant.WAREH_CODE);
        VpSenderEntity sendAddress = deliveryService.getSendAddressByWarehCode(warehCode);
        if (sendAddress != null) {
            if (!"sto".equals(tspComCode) && !"yto".equals(tspComCode)) {
                String province = sendAddress.getProvince();
                String city = sendAddress.getCity();
                String district = sendAddress.getDistrict();
                String address = sendAddress.getAddress();
                sendAddress.setAddress(province + " " + city + " " + district + " " + address);
            }
        }

        List jasperPrintList = new ArrayList<JasperReport>();

        if (!"sto".equals(tspComCode) && !"yto".equals(tspComCode)) {
            String province = delivery.getProvince();
            String city = delivery.getCity();
            String district = delivery.getDistrict();
            String address = delivery.getDelivAddress();
            delivery.setDelivAddress(province + " " + city + " " + district + " " + address);
        }

        try {
            for (int i = 0; i < printCount; i++) {
                String templateName = tspComCode + "_express.jrxml";
                String imageName = tspComCode + ".jpg";
                String jasperPath = request.getSession().getServletContext()
                        .getRealPath("/reports/" + templateName);
                String imageurl = request.getSession().getServletContext().getRealPath("/images/" + imageName);

                List tmpList = new ArrayList();
                Map<String, Object> pram = new HashMap<String, Object>();
                pram.put("name", delivery.getConsignee() == null ? "" : delivery.getConsignee());
                pram.put("phone", delivery.getMoblie() == null ? "" : delivery.getMoblie());
                pram.put("code", delivery.getDelivPstd() == null ? "" : delivery.getDelivPstd());
                pram.put("province", delivery.getProvince() == null ? "" : delivery.getProvince());
                pram.put("city", delivery.getCity() == null ? "" : delivery.getCity());
                pram.put("district", delivery.getDistrict() == null ? "" : delivery.getDistrict());
                pram.put("company", "");
                pram.put("address", delivery.getDelivAddress() == null ? "" : delivery.getDelivAddress());
                pram.put("sendName", sendAddress == null ? "" : sendAddress.getConsignor());
                pram.put("sendPhone", sendAddress == null ? "" : sendAddress.getMobile());
                pram.put("sendCode", sendAddress == null ? "" : sendAddress.getZip());
                pram.put("sendAddress", sendAddress == null ? "" : sendAddress.getAddress());
                pram.put("sendProvince", sendAddress == null ? "" : sendAddress.getProvince());
                pram.put("sendCity", sendAddress == null ? "" : sendAddress.getCity());
                pram.put("sendDistrict", sendAddress == null ? "" : sendAddress.getDistrict());
                pram.put("sendCompany", "");
                pram.put("docCode", delivery.getDocCode() == null ? "" : delivery.getDocCode());
                pram.put("imageurl", imageurl);
                tmpList.add(pram);
                JasperReport jasperReport = JasperCompileManager.compileReport(jasperPath);
                JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, pram,
                        new JRBeanCollectionDataSource(tmpList));
                request.getSession().setAttribute(ImageServlet.DEFAULT_JASPER_PRINT_SESSION_ATTRIBUTE, jasperPrint);

                jasperPrintList.add(jasperPrint);
            }

            //byte[] bytes = JasperExportManager.exportReportToPdf(jasperPrint);
            //??
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            JRPdfExporter exporter = new JRPdfExporter();
            exporter.setParameter(JRExporterParameter.JASPER_PRINT_LIST, jasperPrintList);
            exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, baos);
            exporter.exportReport();
            byte[] bytes = baos.toByteArray();

            response.setContentType("application/pdf");
            response.setContentLength(bytes.length);

            ServletOutputStream out = response.getOutputStream();
            out.write(bytes);
            out.flush();
            //end 

        } catch (IOException ioe) {
            System.err.println(ioe.getMessage());
        } catch (JRException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

    //???
    @RequestMapping(value = "/printExpressBatch", method = RequestMethod.GET)
    public void printExpressBatch(HttpServletRequest request, HttpServletResponse response) throws MbvException {
        HttpSession session = request.getSession();
        String warehCode = (String) session.getAttribute(MbvConstant.WAREH_CODE);
        VpSenderEntity sendAddress = deliveryService.getSendAddressByWarehCode(warehCode);
        String tspComCode = (String) request.getParameter("tspComCode");
        if (sendAddress != null) {
            if (!"sto".equals(tspComCode) && !"yto".equals(tspComCode)) {
                String province = sendAddress.getProvince();
                String city = sendAddress.getCity();
                String district = sendAddress.getDistrict();
                String address = sendAddress.getAddress();
                sendAddress.setAddress(province + " " + city + " " + district + " " + address);
            }
        }

        String degIdStr = request.getParameter("degIds");
        String[] degIds = degIdStr.split(",");
        List jasperPrintList = new ArrayList<JasperReport>();

        try {
            for (String degStr : degIds) {
                Long degId = Long.parseLong(degStr);
                DeliveryBean delivery = deliveryService.getDeliveryBeanById(degId);

                if (!"sto".equals(tspComCode) && !"yto".equals(tspComCode)) {
                    String province = delivery.getProvince();
                    String city = delivery.getCity();
                    String district = delivery.getDistrict();
                    String address = delivery.getDelivAddress();
                    delivery.setDelivAddress(province + " " + city + " " + district + " " + address);
                }

                String templateName = tspComCode + "_express.jrxml";
                String imageName = tspComCode + ".jpg";
                String jasperPath = request.getSession().getServletContext()
                        .getRealPath("/reports/" + templateName);
                String imageurl = request.getSession().getServletContext().getRealPath("/images/" + imageName);

                List tmpList = new ArrayList();
                Map<String, Object> pram = new HashMap<String, Object>();
                pram.put("name", delivery.getConsignee() == null ? "" : delivery.getConsignee());
                pram.put("phone", delivery.getMoblie() == null ? "" : delivery.getMoblie());
                pram.put("code", delivery.getDelivPstd() == null ? "" : delivery.getDelivPstd());
                pram.put("province", delivery.getProvince() == null ? "" : delivery.getProvince());
                pram.put("city", delivery.getCity() == null ? "" : delivery.getCity());
                pram.put("district", delivery.getDistrict() == null ? "" : delivery.getDistrict());
                pram.put("company", "");
                pram.put("address", delivery.getDelivAddress() == null ? "" : delivery.getDelivAddress());
                pram.put("sendName", sendAddress == null ? "" : sendAddress.getConsignor());
                pram.put("sendPhone", sendAddress == null ? "" : sendAddress.getMobile());
                pram.put("sendCode", sendAddress == null ? "" : sendAddress.getZip());
                pram.put("sendAddress", sendAddress == null ? "" : sendAddress.getAddress());
                pram.put("sendProvince", sendAddress == null ? "" : sendAddress.getProvince());
                pram.put("sendCity", sendAddress == null ? "" : sendAddress.getCity());
                pram.put("sendDistrict", sendAddress == null ? "" : sendAddress.getDistrict());
                pram.put("sendCompany", "");
                pram.put("docCode", delivery.getDocCode() == null ? "" : delivery.getDocCode());
                pram.put("imageurl", imageurl);
                tmpList.add(pram);
                JasperReport jasperReport = JasperCompileManager.compileReport(jasperPath);
                JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, pram,
                        new JRBeanCollectionDataSource(tmpList));
                request.getSession().setAttribute(ImageServlet.DEFAULT_JASPER_PRINT_SESSION_ATTRIBUTE, jasperPrint);

                jasperPrintList.add(jasperPrint);
            }

            //byte[] bytes = JasperExportManager.exportReportToPdf(jasperPrint);
            //??
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            JRPdfExporter exporter = new JRPdfExporter();
            exporter.setParameter(JRExporterParameter.JASPER_PRINT_LIST, jasperPrintList);
            exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, baos);
            exporter.exportReport();
            byte[] bytes = baos.toByteArray();

            response.setContentType("application/pdf");
            response.setContentLength(bytes.length);

            ServletOutputStream out = response.getOutputStream();
            out.write(bytes);
            out.flush();
            //end 

        } catch (IOException ioe) {
            System.err.println(ioe.getMessage());
        } catch (JRException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

    //???
    @RequestMapping(value = "/printDelivery", method = RequestMethod.GET)
    public String printDelivery(HttpServletRequest request, HttpServletResponse response) throws MbvException {
        Long degId = Long.parseLong(request.getParameter("degId"));
        DeliveryBean delivery = deliveryService.getDeliveryBeanById(degId);
        if (delivery != null) {
            String province = delivery.getProvince();
            String city = delivery.getCity();
            String district = delivery.getDistrict();
            String address = delivery.getDelivAddress();
            delivery.setDelivAddress(province + city + district + address);
        }
        List<GoodBean> goodList = deliveryService.queryGoodByParams(degId);

        String jasperPath = request.getSession().getServletContext().getRealPath("/reports/delivery.jrxml");

        try {
            List<PickBean> goods = new ArrayList<PickBean>();
            int i = 0;
            for (GoodBean goodBean : goodList) {
                i++;
                PickBean good = new PickBean();
                good.setId(i);
                good.setCode(goodBean.getProdNum());
                good.setName(goodBean.getBrandName() + " " + goodBean.getProductName() + " "
                        + goodBean.getColorName() + " " + goodBean.getSizeName());
                good.setNum(goodBean.getQty());
                good.setOnBrandPrc(goodBean.getOnBrandPrc());
                good.setUnitPrice(goodBean.getUnitPrice());
                String total = df.format(mul(goodBean.getQty(), goodBean.getUnitPrice(), 2));
                good.setTotal(total);
                goods.add(good);
            }

            SysParameterEntity sysParam = sysParameterService.selectByPramCode("SYSTEM_PHONE");
            String sysPhone = sysParam.getParaVal();
            Map<String, Object> parameters = new HashMap<String, Object>();
            parameters.put("SUBREPORT_DIR", jasperPath);
            List<PdfDetail> pdfDetail = new ArrayList<PdfDetail>();
            PdfDetail detail = new PdfDetail();
            detail.setDoc_code(delivery.getDocCode());
            detail.setOrdernum(delivery.getOrderSn());
            detail.setConsignee(delivery.getConsignee());
            detail.setDeliv_address(delivery.getDelivAddress());
            detail.setMobile(delivery.getMoblie());
            detail.setTtl_qty(delivery.getTtlQty());
            detail.setTtl_val(delivery.getTtlVal());
            detail.setOrderRemark(delivery.getOrderRemark() == null ? "" : delivery.getOrderRemark());
            detail.setSysPhone(sysPhone);
            detail.setTable_datas(new JRBeanCollectionDataSource(goods));
            pdfDetail.add(detail);

            JasperReport jasperReport = JasperCompileManager.compileReport(jasperPath);
            JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, parameters,
                    new JRBeanCollectionDataSource(pdfDetail));
            request.getSession().setAttribute(ImageServlet.DEFAULT_JASPER_PRINT_SESSION_ATTRIBUTE, jasperPrint);

            byte[] bytes = JasperExportManager.exportReportToPdf(jasperPrint);

            response.setContentType("application/pdf");
            response.setContentLength(bytes.length);

            ServletOutputStream out = response.getOutputStream();
            out.write(bytes);
            out.flush();
            //end 

        } catch (IOException ioe) {
            System.err.println(ioe.getMessage());
            return null;
        } catch (JRException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return null;
        }

        return null;
    }

    //????
    @RequestMapping(value = "/printDeliveryBacth", method = RequestMethod.GET)
    public void printDeliveryBacth(HttpServletRequest request, HttpServletResponse response) throws MbvException {
        String degIdStr = request.getParameter("degIds");
        String[] degIds = degIdStr.split(",");
        List jasperPrintList = new ArrayList<JasperReport>();
        SysParameterEntity sysParam = sysParameterService.selectByPramCode("SYSTEM_PHONE");
        String sysPhone = sysParam.getParaVal();

        try {
            for (String degStr : degIds) {
                Long degId = Long.parseLong(degStr);
                DeliveryBean delivery = deliveryService.getDeliveryBeanById(degId);
                if (delivery != null) {
                    String province = delivery.getProvince();
                    String city = delivery.getCity();
                    String district = delivery.getDistrict();
                    String address = delivery.getDelivAddress();
                    delivery.setDelivAddress(province + city + district + address);
                }
                List<GoodBean> goodList = deliveryService.queryGoodByParams(degId);

                String jasperPath = request.getSession().getServletContext().getRealPath("/reports/delivery.jrxml");
                List<PickBean> goods = new ArrayList<PickBean>();
                int i = 0;
                for (GoodBean goodBean : goodList) {
                    i++;
                    PickBean good = new PickBean();
                    good.setId(i);
                    good.setCode(goodBean.getProdNum());
                    good.setName(goodBean.getBrandName() + " " + goodBean.getProductName() + " "
                            + goodBean.getColorName() + " " + goodBean.getSizeName());
                    good.setNum(goodBean.getQty());
                    good.setOnBrandPrc(goodBean.getOnBrandPrc());
                    good.setUnitPrice(goodBean.getUnitPrice());
                    String total = df.format(mul(goodBean.getQty(), goodBean.getUnitPrice(), 2));
                    good.setTotal(total);
                    goods.add(good);
                }

                Map<String, Object> parameters = new HashMap<String, Object>();
                parameters.put("SUBREPORT_DIR", jasperPath);
                List<PdfDetail> pdfDetail = new ArrayList<PdfDetail>();
                PdfDetail detail = new PdfDetail();
                detail.setDoc_code(delivery.getDocCode());
                detail.setOrdernum(delivery.getOrderSn());
                detail.setConsignee(delivery.getConsignee());
                detail.setDeliv_address(delivery.getDelivAddress());
                detail.setMobile(delivery.getMoblie());
                detail.setTtl_qty(delivery.getTtlQty());
                detail.setTtl_val(delivery.getTtlVal());
                detail.setOrderRemark(delivery.getOrderRemark() == null ? "" : delivery.getOrderRemark());
                detail.setSysPhone(sysPhone);
                detail.setTable_datas(new JRBeanCollectionDataSource(goods));
                pdfDetail.add(detail);

                JasperReport jasperReport = JasperCompileManager.compileReport(jasperPath);
                JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, parameters,
                        new JRBeanCollectionDataSource(pdfDetail));
                request.getSession().setAttribute(ImageServlet.DEFAULT_JASPER_PRINT_SESSION_ATTRIBUTE, jasperPrint);
                jasperPrintList.add(jasperPrint);
            }

            //byte[] bytes = JasperExportManager.exportReportToPdf(jasperPrint);
            //??
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            JRPdfExporter exporter = new JRPdfExporter();
            exporter.setParameter(JRExporterParameter.JASPER_PRINT_LIST, jasperPrintList);
            exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, baos);
            exporter.exportReport();
            byte[] bytes = baos.toByteArray();

            response.setContentType("application/pdf");
            response.setContentLength(bytes.length);

            ServletOutputStream out = response.getOutputStream();
            out.write(bytes);
            out.flush();
            //end 

        } catch (IOException ioe) {
            System.err.println(ioe.getMessage());
        } catch (JRException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

    //?????
    @RequestMapping(value = "/getSendAddress", method = RequestMethod.POST)
    @ResponseBody
    public void getSendAddress(HttpServletRequest request, HttpServletResponse response) throws MbvException {
        HttpSession session = request.getSession();
        String warehCode = (String) session.getAttribute(MbvConstant.WAREH_CODE);
        VpSenderEntity sendAddress = deliveryService.getSendAddressByWarehCode(warehCode);
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("sendAddress", sendAddress);

        this.returnSuccess(response, map);
    }

    //????
    @RequestMapping(value = "/isExistSendAddress", method = RequestMethod.POST)
    @ResponseBody
    public void isExistSendAddress(HttpServletRequest request, HttpServletResponse response) throws MbvException {
        boolean flag = true;
        HttpSession session = request.getSession();
        String warehCode = (String) session.getAttribute(MbvConstant.WAREH_CODE);
        flag = deliveryService.isExistSendAddress(warehCode);
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("message", flag);

        this.returnSuccess(response, map);
    }

    //??
    @RequestMapping(value = "/saveSenderAddress", method = RequestMethod.POST)
    @ResponseBody
    public void saveSenderAddress(HttpServletRequest request, HttpServletResponse response) throws MbvException {
        HttpSession session = request.getSession();
        String user = (String) session.getAttribute(MbvConstant.USER_CODE);
        String unitCode = (String) session.getAttribute(MbvConstant.UNIT_CODE);
        String warehCode = (String) session.getAttribute(MbvConstant.WAREH_CODE);
        String senderId = request.getParameter("senderId");
        String sender = request.getParameter("sender");
        String address = request.getParameter("address");
        String mobile = request.getParameter("mobile");
        String zip = request.getParameter("zip");
        String province = request.getParameter("province");
        String city = request.getParameter("city");
        String district = request.getParameter("district");

        boolean flag = false;

        VpSenderEntity entity = new VpSenderEntity();
        entity.setUnitCode(unitCode);
        entity.setWarehCode(warehCode);
        entity.setConsignor(sender);
        entity.setAddress(address);
        entity.setZip(zip);
        entity.setMobile(mobile);
        entity.setProvince(province);
        entity.setCity(city);
        entity.setDistrict(district);
        entity.setCreateDate(new Date());
        entity.setCreateUser(user);
        entity.setLastModifiedDate(new Date());
        entity.setLastModifiedUser(user);
        if (senderId == null || senderId.equals("")) {
            flag = deliveryService.saveSender(entity);
        } else {
            entity.setId(Integer.parseInt(senderId));
            flag = deliveryService.updateSender(entity);
        }

        Map<String, Object> map = new HashMap<String, Object>();
        map.put("message", flag);
        returnSuccess(response, map);
    }

    //?????
    @RequestMapping(value = "/updateDelAddressStatus", method = RequestMethod.POST)
    @ResponseBody
    public void updateDelAddressStatus(HttpServletRequest request, HttpServletResponse response, String docCode)
            throws MbvException {
        boolean flag = deliveryService.updateDelAddressStatus(docCode);

        Map<String, Object> map = new HashMap<String, Object>();
        if (flag) {
            map.put("flag", true);
        } else {
            map.put("flag", false);
        }

        this.returnSuccess(response, map);
    }

    //????
    @RequestMapping(value = "/updateDelGoodStatus", method = RequestMethod.POST)
    @ResponseBody
    public void updateDelGoodStatus(HttpServletRequest request, HttpServletResponse response, String docCode)
            throws MbvException {
        Map<String, Object> map = new HashMap<String, Object>();
        boolean flag = true;
        try {
            flag = deliveryService.updateDelGoodStatus(docCode);
        } catch (MbvException e) {
            flag = false;
            e.printStackTrace();
        }

        if (flag) {
            map.put("flag", true);
        } else {
            map.put("flag", false);
        }

        this.returnSuccess(response, map);
    }

    //???
    @RequestMapping(value = "/updateExpressCode", method = RequestMethod.POST)
    @ResponseBody
    public void updateExpressCode(HttpServletRequest request, HttpServletResponse response, String expressCode,
            Long degId) throws MbvException {
        Map<String, Object> paramsMap = new HashMap<String, Object>();
        paramsMap.put("expressCode", expressCode);
        paramsMap.put("degId", degId);
        boolean flag = deliveryService.updateExpressCode(paramsMap);

        Map<String, Object> map = new HashMap<String, Object>();
        if (flag) {
            map.put("flag", true);
        } else {
            map.put("flag", false);
        }

        this.returnSuccess(response, map);
    }

    //?
    @RequestMapping(value = "/updateExpress", method = RequestMethod.POST)
    @ResponseBody
    public void updateExpress(HttpServletRequest request, HttpServletResponse response, String tspComCode,
            Long degId) throws MbvException {
        Map<String, Object> paramsMap = new HashMap<String, Object>();
        paramsMap.put("tspComCode", tspComCode);
        paramsMap.put("degId", degId);
        boolean flag = deliveryService.updateExpress(paramsMap);

        Map<String, Object> map = new HashMap<String, Object>();
        if (flag) {
            map.put("flag", true);
        } else {
            map.put("flag", false);
        }

        this.returnSuccess(response, map);
    }

    /* 
       * region_type 
       * region_id   id
       * region_type  region_id ??
       * region_type???0? 1?? 2? 3? 4??
       *  region_id?id??
       */
    @RequestMapping(value = "/getSystemRegionArea", method = RequestMethod.POST)
    public void getSystemRegionArea(HttpServletResponse response, String region_type, String region_id)
            throws MbvException {
        String systemRegionArea = deliveryService.getSystemRegionArea(region_type, region_id);
        this.outPrintJson(response, systemRegionArea);
    }

    //??
    public Double mul(Integer value1, String value2, Integer scale) {
        if (scale < 0) {
            throw new IllegalArgumentException("The scale must be a positive integer or zero");
        }
        BigDecimal b1 = new BigDecimal(Double.toString(value1.doubleValue()));
        BigDecimal b2 = new BigDecimal(value2);

        return DoubleUtil.round(b1.multiply(b2).doubleValue(), scale);
    }
}