|  |  |  | 
|---|
|  |  |  | package com.matrix.system.shopXcx.action; | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | import cn.hutool.core.collection.CollUtil; | 
|---|
|  |  |  | import cn.hutool.core.util.ObjectUtil; | 
|---|
|  |  |  | import com.google.gson.Gson; | 
|---|
|  |  |  | import com.matrix.component.asyncmessage.AsyncMessageManager; | 
|---|
|  |  |  | import com.matrix.core.anotations.RemoveRequestToken; | 
|---|
|  |  |  | import com.matrix.core.anotations.SaveRequestToken; | 
|---|
|  |  |  | import com.matrix.core.constance.MatrixConstance; | 
|---|
|  |  |  | 
|---|
|  |  |  | import com.matrix.system.hive.action.BaseController; | 
|---|
|  |  |  | import com.matrix.system.hive.action.util.QueryUtil; | 
|---|
|  |  |  | import com.matrix.system.hive.bean.SysShopInfo; | 
|---|
|  |  |  | import com.matrix.system.hive.bean.SysVipInfo; | 
|---|
|  |  |  | import com.matrix.system.hive.dao.SysShopInfoDao; | 
|---|
|  |  |  | import com.matrix.system.hive.service.SysVipInfoService; | 
|---|
|  |  |  | import com.matrix.system.shopXcx.bean.*; | 
|---|
|  |  |  | import com.matrix.system.shopXcx.dao.*; | 
|---|
|  |  |  | import com.matrix.system.shopXcx.dto.DiscountExplain; | 
|---|
|  |  |  | import com.matrix.system.shopXcx.mqTask.AsyncMessageRouting; | 
|---|
|  |  |  | import com.matrix.system.shopXcx.pojo.ShopOrderQueryPOJO; | 
|---|
|  |  |  | import com.matrix.system.shopXcx.shopEnum.OrderStatusEnum; | 
|---|
|  |  |  | import com.matrix.system.shopXcx.vo.LogisticsImportVo; | 
|---|
|  |  |  | import org.apache.commons.collections.CollectionUtils; | 
|---|
|  |  |  | import org.springframework.beans.factory.annotation.Autowired; | 
|---|
|  |  |  | import org.springframework.stereotype.Controller; | 
|---|
|  |  |  | import org.springframework.transaction.annotation.Transactional; | 
|---|
|  |  |  | import org.springframework.web.bind.annotation.RequestMapping; | 
|---|
|  |  |  | import org.springframework.web.bind.annotation.RequestParam; | 
|---|
|  |  |  | import org.springframework.web.bind.annotation.ResponseBody; | 
|---|
|  |  |  | import org.springframework.web.multipart.MultipartFile; | 
|---|
|  |  |  | import org.springframework.web.servlet.ModelAndView; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | import javax.servlet.http.HttpServletRequest; | 
|---|
|  |  |  | import javax.servlet.http.HttpServletResponse; | 
|---|
|  |  |  | import java.io.File; | 
|---|
|  |  |  | import java.io.IOException; | 
|---|
|  |  |  | import java.io.OutputStream; | 
|---|
|  |  |  | import java.math.BigDecimal; | 
|---|
|  |  |  | 
|---|
|  |  |  | private WxApplyOrderRelateDao wxApplyOrderRelateDao; | 
|---|
|  |  |  | @Autowired | 
|---|
|  |  |  | private SysShopInfoDao sysShopInfoDao; | 
|---|
|  |  |  | @Autowired | 
|---|
|  |  |  | private SysVipInfoService vipInfoService; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | //记录编辑前的值Before_Edit_Value | 
|---|
|  |  |  | public static final String BEV = "ShopOrder_BEV"; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | public static final List<LogisticsImportVo> logisticsImportVoLists = new ArrayList<>(); | 
|---|
|  |  |  | @Autowired | 
|---|
|  |  |  | private AsyncMessageManager asyncMessageManager; | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * 导入快递单 | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | @RequestMapping(value = "/logisticsImport") | 
|---|
|  |  |  | @ResponseBody | 
|---|
|  |  |  | public AjaxResult logisticsImport(HttpServletResponse response, HttpServletRequest request, | 
|---|
|  |  |  | @RequestParam(value = "file", required = false) MultipartFile file) throws IOException { | 
|---|
|  |  |  |  | 
|---|
|  |  |  | String fileName = file.getOriginalFilename(); | 
|---|
|  |  |  | String dirPath = PropertiesUtil.getString(AppConstance.FILES_TORAGE_PATH); | 
|---|
|  |  |  | //        String dirPath = "E:/xcshop"; | 
|---|
|  |  |  | File fileDir = new File(dirPath); | 
|---|
|  |  |  | LogUtil.info("#----->{}#", fileDir.exists()); | 
|---|
|  |  |  | if (!fileDir.exists()) { | 
|---|
|  |  |  | fileDir.mkdirs(); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | File saveFile = new File(dirPath + "/" + fileName); | 
|---|
|  |  |  | file.transferTo(saveFile); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | String infoRemind = "提示:"; | 
|---|
|  |  |  | AjaxResult ajaxResult = new AjaxResult(); | 
|---|
|  |  |  | List<LogisticsImportVo> logisticsImportVos = logisticsImportFile(saveFile); | 
|---|
|  |  |  | if(CollUtil.isNotEmpty(logisticsImportVos)){ | 
|---|
|  |  |  | ajaxResult = AjaxResult.buildSuccessInstance("部分数据导入失败"); | 
|---|
|  |  |  | ajaxResult.setStatus(AjaxResult.STATUS_FAIL); | 
|---|
|  |  |  | }else{ | 
|---|
|  |  |  | ajaxResult = AjaxResult.buildSuccessInstance("导入成功"); | 
|---|
|  |  |  | ajaxResult.setStatus(AjaxResult.STATUS_SUCCESS); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | return ajaxResult; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @RequestMapping(value = "/exportLogisticsImportExcel") | 
|---|
|  |  |  | public void exportLogisticsImportExcel(HttpServletResponse res) { | 
|---|
|  |  |  | OutputStream os = null; | 
|---|
|  |  |  | try { | 
|---|
|  |  |  | res.setCharacterEncoding("UTF-8"); | 
|---|
|  |  |  | res.setHeader("content-type", "application/octet-stream;charset=UTF-8"); | 
|---|
|  |  |  | res.setContentType("application/octet-stream;charset=UTF-8"); | 
|---|
|  |  |  | Date date = new Date(); | 
|---|
|  |  |  | SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy年MM月dd日HH时mm分ss"); | 
|---|
|  |  |  | res.setHeader("Content-Disposition", "attachment;filename=" + | 
|---|
|  |  |  | java.net.URLEncoder.encode("快递单导入列表" + dateFormat.format(date) + ".xls".trim(), "UTF-8")); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | os = res.getOutputStream(); | 
|---|
|  |  |  | ExcelUtil.createWorkbookAtOutStream(ExcelVersion.V2007, disPoseLogisticsImportExcel(logisticsImportVoLists), os, true); | 
|---|
|  |  |  | logisticsImportVoLists.clear(); | 
|---|
|  |  |  | } catch (Exception e) { | 
|---|
|  |  |  | LogUtil.error("快递单导入异常", e); | 
|---|
|  |  |  | } finally { | 
|---|
|  |  |  | if (os != null) { | 
|---|
|  |  |  | try { | 
|---|
|  |  |  | os.close(); | 
|---|
|  |  |  | } catch (IOException e) { | 
|---|
|  |  |  | LogUtil.error("关闭资源异常", e); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | private List<ExcelSheetPO> disPoseLogisticsImportExcel(List<LogisticsImportVo> logisticsImportVos) { | 
|---|
|  |  |  | List<ExcelSheetPO> res = new ArrayList<>(); | 
|---|
|  |  |  | ExcelSheetPO orderSheet = new ExcelSheetPO(); | 
|---|
|  |  |  | orderSheet.setSheetName("快递单导入"); | 
|---|
|  |  |  | orderSheet.setTitle("快递单导入失败列表"); | 
|---|
|  |  |  | String[] header = new String[]{"订单编号", "物流公司编码", "物流公司名称", "快递单号", "失败原因"}; | 
|---|
|  |  |  | orderSheet.setHeaders(header); | 
|---|
|  |  |  | List<List<Object>> body = new ArrayList<>(); | 
|---|
|  |  |  | SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss"); | 
|---|
|  |  |  | if(CollUtil.isNotEmpty(logisticsImportVos)){ | 
|---|
|  |  |  | for (LogisticsImportVo logisticsImportVo : logisticsImportVos) { | 
|---|
|  |  |  | List<Object> bodyItem = new ArrayList<>(); | 
|---|
|  |  |  | bodyItem.add(logisticsImportVo.getOrderNo()); | 
|---|
|  |  |  | bodyItem.add(logisticsImportVo.getLogisticsCompanyCode()); | 
|---|
|  |  |  | bodyItem.add(logisticsImportVo.getLogisticsCompany()); | 
|---|
|  |  |  | bodyItem.add(logisticsImportVo.getWaybillNo()); | 
|---|
|  |  |  | bodyItem.add(logisticsImportVo.getFailReason()); | 
|---|
|  |  |  | body.add(bodyItem); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | orderSheet.setDataList(body); | 
|---|
|  |  |  | res.add(orderSheet); | 
|---|
|  |  |  | return res; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | public List<LogisticsImportVo> logisticsImportFile(File file) throws IOException { | 
|---|
|  |  |  | SysUsers sysUsers = WebUtil.getSessionAttribute(MatrixConstance.LOGIN_KEY); | 
|---|
|  |  |  | List<ExcelSheetPO> excelList = ExcelUtil.readExcel(file, null,null); | 
|---|
|  |  |  | ArrayList<LogisticsImportVo> logisticsImportVos = new ArrayList<>(); | 
|---|
|  |  |  | for (int i = 0; i < excelList.size(); i++) { | 
|---|
|  |  |  | List<List<Object>> dataList = excelList.get(i).getDataList(); | 
|---|
|  |  |  | List<SysVipInfo> vipInfos = new ArrayList<>(); | 
|---|
|  |  |  | if (CollectionUtils.isNotEmpty(dataList)) { | 
|---|
|  |  |  | for (int j = 1; j < dataList.size(); j++) { | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * 订单处于【待付款】【待配送】【待收货】都可以导入 | 
|---|
|  |  |  | * 其他状态生成一个提示 | 
|---|
|  |  |  | */ | 
|---|
|  |  |  |  | 
|---|
|  |  |  | LogisticsImportVo logisticsImportVo = new LogisticsImportVo(); | 
|---|
|  |  |  | List<Object> objects = dataList.get(j); | 
|---|
|  |  |  | //订单编号 | 
|---|
|  |  |  | String orderNo = objects.get(0).toString(); | 
|---|
|  |  |  | ShopOrder shopOrder = shopOrderDao.selectShopOrderByOrderNo(orderNo); | 
|---|
|  |  |  | if(ObjectUtil.isNotEmpty(shopOrder) && | 
|---|
|  |  |  | (shopOrder.getOrderStatus() == ShopOrder.ORDER_STATUS_WAIT_SEND || | 
|---|
|  |  |  | shopOrder.getOrderStatus() == ShopOrder.ORDER_STATUS_WAIT_RECEIVE )){ | 
|---|
|  |  |  | //物流公司编码 | 
|---|
|  |  |  | String logisticsCompanyCode = objects.get(1).toString(); | 
|---|
|  |  |  | //物流公司名称 | 
|---|
|  |  |  | String logisticsCompany = objects.get(2).toString(); | 
|---|
|  |  |  | //快递单号 | 
|---|
|  |  |  | String waybillNo = objects.get(3).toString(); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | ShopDeliveryInfo shopDeliveryInfo = new ShopDeliveryInfo(); | 
|---|
|  |  |  | shopDeliveryInfo.setOrderId(shopOrder.getId()); | 
|---|
|  |  |  | List<ShopDeliveryInfo> infoList = shopDeliveryInfoDao.selectByModel(shopDeliveryInfo); | 
|---|
|  |  |  | if (CollectionUtils.isEmpty(infoList)) { | 
|---|
|  |  |  | logisticsImportVo.setOrderNo(objects.get(0).toString()); | 
|---|
|  |  |  | logisticsImportVo.setLogisticsCompanyCode(objects.get(1).toString()); | 
|---|
|  |  |  | logisticsImportVo.setLogisticsCompany(objects.get(2).toString()); | 
|---|
|  |  |  | logisticsImportVo.setWaybillNo(objects.get(3).toString()); | 
|---|
|  |  |  | logisticsImportVo.setFailReason("未找到发货信息"); | 
|---|
|  |  |  | logisticsImportVos.add(logisticsImportVo); | 
|---|
|  |  |  | logisticsImportVoLists.add(logisticsImportVo); | 
|---|
|  |  |  | }else{ | 
|---|
|  |  |  | shopDeliveryInfo = infoList.get(0); | 
|---|
|  |  |  | shopDeliveryInfo.setOrderNo(orderNo); | 
|---|
|  |  |  | shopDeliveryInfo.setLogisticsCompanyCode(logisticsCompanyCode); | 
|---|
|  |  |  | shopDeliveryInfo.setLogisticsCompany(logisticsCompany); | 
|---|
|  |  |  | shopDeliveryInfo.setWaybillNo(waybillNo); | 
|---|
|  |  |  | shopDeliveryInfo.setUpdateBy(sysUsers.getSuName()); | 
|---|
|  |  |  | shopDeliveryInfo.setLogisticsStatus(AppConstance.LOGISTICS_STATUS_OF_RECEIVE); | 
|---|
|  |  |  | shopDeliveryInfo.setDeliveryTime(new Date()); | 
|---|
|  |  |  | shopDeliveryInfoDao.updateByModel(shopDeliveryInfo); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | //构建需要修改订单信息Map | 
|---|
|  |  |  | Map<String, Object> modifyMap = new HashMap<>(); | 
|---|
|  |  |  | modifyMap.put("id", shopOrder.getId()); | 
|---|
|  |  |  | modifyMap.put("orderStatus", ShopOrder.ORDER_STATUS_WAIT_RECEIVE); | 
|---|
|  |  |  | shopOrderDao.updateByMap(modifyMap); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | //发送创建订单的消息 | 
|---|
|  |  |  | asyncMessageManager.sendMsg(AsyncMessageRouting.ORDER_OUT_SOTORE ,"orderNo=%s"); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | } | 
|---|
|  |  |  | }else{ | 
|---|
|  |  |  | if(ObjectUtil.isEmpty(shopOrder)){ | 
|---|
|  |  |  | logisticsImportVo.setOrderNo(objects.get(0).toString()); | 
|---|
|  |  |  | logisticsImportVo.setLogisticsCompanyCode(objects.get(1).toString()); | 
|---|
|  |  |  | logisticsImportVo.setLogisticsCompany(objects.get(2).toString()); | 
|---|
|  |  |  | logisticsImportVo.setWaybillNo(objects.get(3).toString()); | 
|---|
|  |  |  | logisticsImportVo.setFailReason("未找到订单信息"); | 
|---|
|  |  |  | logisticsImportVos.add(logisticsImportVo); | 
|---|
|  |  |  | logisticsImportVoLists.add(logisticsImportVo); | 
|---|
|  |  |  | }else if(shopOrder.getOrderStatus() != ShopOrder.ORDER_STATUS_WAIT_SEND || | 
|---|
|  |  |  | shopOrder.getOrderStatus() != ShopOrder.ORDER_STATUS_WAIT_RECEIVE ){ | 
|---|
|  |  |  | logisticsImportVo.setOrderNo(objects.get(0).toString()); | 
|---|
|  |  |  | logisticsImportVo.setLogisticsCompanyCode(objects.get(1).toString()); | 
|---|
|  |  |  | logisticsImportVo.setLogisticsCompany(objects.get(2).toString()); | 
|---|
|  |  |  | logisticsImportVo.setWaybillNo(objects.get(3).toString()); | 
|---|
|  |  |  | logisticsImportVo.setFailReason("订单状态不允许发货"); | 
|---|
|  |  |  | logisticsImportVos.add(logisticsImportVo); | 
|---|
|  |  |  | logisticsImportVoLists.add(logisticsImportVo); | 
|---|
|  |  |  | }else{ | 
|---|
|  |  |  | logisticsImportVo.setOrderNo(objects.get(0).toString()); | 
|---|
|  |  |  | logisticsImportVo.setLogisticsCompanyCode(objects.get(1).toString()); | 
|---|
|  |  |  | logisticsImportVo.setLogisticsCompany(objects.get(2).toString()); | 
|---|
|  |  |  | logisticsImportVo.setWaybillNo(objects.get(3).toString()); | 
|---|
|  |  |  | logisticsImportVo.setFailReason("导入失败"); | 
|---|
|  |  |  | logisticsImportVos.add(logisticsImportVo); | 
|---|
|  |  |  | logisticsImportVoLists.add(logisticsImportVo); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | return logisticsImportVos; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * 导出Excel | 
|---|