package com.yc.sdk.shopping.action.jiazhuang; import java.math.BigDecimal; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import java.util.List; import java.util.stream.Collectors; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Scope; import org.springframework.dao.DataAccessException; import org.springframework.dao.DataAccessResourceFailureException; import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import com.github.binarywang.wxpay.exception.WxPayException; import com.google.gson.JsonArray; import com.google.gson.JsonObject; import com.yc.action.BaseAction; import com.yc.action.grid.QrCodeRunable; import com.yc.entity.DataSourceEntity; import com.yc.factory.FactoryBean; import com.yc.multiData.MultiDataSource; import com.yc.multiData.SpObserver; import com.yc.sdk.WebSocketMessage.action.WebSocketMessageServer; import com.yc.sdk.WebSocketMessage.entity.MessageInfo; import com.yc.sdk.WebSocketMessage.entity.MessageType; import com.yc.sdk.WebSocketMessage.entity.UserFromType; import com.yc.sdk.miniapp.entity.MaSettingEntity; import com.yc.sdk.miniapp.service.MaSettingIfc; import com.yc.sdk.shopping.action.Wxtransactions; import com.yc.sdk.shopping.action.jiazhuang.task.PanicBuyingTaskIfc; import com.yc.sdk.shopping.entity.AddressEntity; import com.yc.sdk.shopping.entity.AllowBuyingResult; import com.yc.sdk.shopping.entity.CheckServiceBalanceWhenRefundResult; import com.yc.sdk.shopping.entity.ConfirmEntity; import com.yc.sdk.shopping.entity.CurrencyEntity; import com.yc.sdk.shopping.entity.CustomerEntity; import com.yc.sdk.shopping.entity.DocCodeStateEntity; import com.yc.sdk.shopping.entity.FreeEntity; import com.yc.sdk.shopping.entity.PanicBuyingEntity; import com.yc.sdk.shopping.entity.PanicBuyingOrderEntity; import com.yc.sdk.shopping.entity.PanicBuyingSellerEntity; import com.yc.sdk.shopping.entity.QrCodeForAppEntity; import com.yc.sdk.shopping.entity.ReturnOrderResult; import com.yc.sdk.shopping.entity.ReturnOrderResult.ReturnOrderDetailResult; import com.yc.sdk.shopping.entity.SaveOrderParamters; import com.yc.sdk.shopping.entity.SettingEntity; import com.yc.sdk.shopping.service.CurrencyIfc; import com.yc.sdk.shopping.service.FreeIfc; import com.yc.sdk.shopping.service.MatCodeIfc; import com.yc.sdk.shopping.service.SettingIfc; import com.yc.sdk.shopping.service.address.AddressIfc; import com.yc.sdk.shopping.service.imagedata.ShoppingImageDataIfc; import com.yc.sdk.shopping.service.panicBuying.PanicBuyingIfc; import com.yc.sdk.shopping.service.panicBuying.PanicBuyingOrderIfc; import com.yc.sdk.shopping.service.register.AccountIfc; import com.yc.sdk.shopping.service.wxpay.WxPayIfc; import com.yc.sdk.shopping.util.SettingKey; import com.yc.sdk.shopping.util.StringReplaceUtil; import com.yc.utils.SessionKey; @Controller @Scope("prototype") @RequestMapping("/shopping/panicBuyingOrder.do") public class PanicBuyingOrder extends BaseAction { @Autowired PanicBuyingOrderIfc panicBuyingOrderIfc; @Autowired CurrencyIfc currencyIfc; @Autowired SettingIfc settingIfc ; @Autowired AccountIfc accountIfc; @Autowired PanicBuyingIfc panicBuyingIfc; @Autowired FreeIfc freeIfc; @Autowired WxPayIfc wxPayIfc; @Autowired MaSettingIfc maSettingIfc; @Autowired MatCodeIfc matCodeIfc; @Autowired ShoppingImageDataIfc imgData; @Autowired PanicBuyingTaskIfc panicBuyingTaskIfc ; @RequestMapping(params = "m=saveOrder") public void saveOrder(@RequestBody SaveOrderParamters saveOrderParamters , HttpServletRequest request, HttpServletResponse response) { //HttpSession session = request.getSession(); JsonObject json = new JsonObject(); JsonObject errJson = new JsonObject(); String docCode = null; ConfirmEntity confirmEntity = null ; String corpId = null ; try { confirmEntity = new ConfirmEntity(request) ; corpId = request.getParameter(SessionKey.WEIXIN_CORPID) ; if (corpId == null || "".equals(corpId)) { corpId = request.getParameter(SessionKey.WEIXIN_APPID) ; } if (confirmEntity.getCltCode() == null || "".equals(confirmEntity.getCltCode())) { errJson.addProperty("warning", "客户不存在,请核实!"); json.add("error", errJson); this.printJson(response, json.toString()); return; } // if (confirmEntity.getSelectedCartIds() == null || "".equals(confirmEntity.getSelectedCartIds())) { // errJson.addProperty("warning", "没有可供生成订单的商品,请从购物车重新勾选商品!"); // json.add("error", errJson); // this.printJson(response, json.toString()); // return; // } } catch (Exception e) { e.printStackTrace(); errJson.addProperty("warning", (e.getCause()!=null? e.getCause().getMessage():e.getMessage())); json.add("error", errJson); this.printJson(response, json.toString()); return; } try { DataSourceEntity dataSourceEntity = MultiDataSource.getDataSourceMap( request) ; SpObserver.setDBtoInstance("_" + dataSourceEntity.getDbId()); FreeEntity freeEntity = freeIfc.getFreeEntity(saveOrderParamters.getFreeId()) ; if (freeEntity!=null && freeEntity.isRequiredAddress() && saveOrderParamters.getPaymentAddress()== null) { errJson.addProperty("warning", "请选择收货地址!"); json.add("error", errJson); this.printJson(response, json.toString()); return; } //微信支付金额,此金额是在 p710806v3 存付过程里计算出来的 double weiXinPayAmount = 0; CurrencyEntity currencyEntity = currencyIfc.getUserCurrency(confirmEntity.getUserCode()); confirmEntity.setCurrency(currencyEntity.getCurrency()); if (saveOrderParamters.getPaymentAddress()!=null) confirmEntity.setPaymentAddress(saveOrderParamters.getPaymentAddress()); if (saveOrderParamters.getFreeId()!=null) confirmEntity.setFreeId(saveOrderParamters.getFreeId()); if (saveOrderParamters.getPanicBuyingDocCode()!=null) confirmEntity.setPanicBuyingDocCode(saveOrderParamters.getPanicBuyingDocCode()); if (saveOrderParamters.getQuantity()!=null) confirmEntity.setQuantity(saveOrderParamters.getQuantity()); if (saveOrderParamters.getComment()!=null) confirmEntity.setComment(saveOrderParamters.getComment()); if (saveOrderParamters.getBalancePayAmount()!=null) confirmEntity.setBalancePayAmount(saveOrderParamters.getBalancePayAmount()); //检查是否超过限购数量 AllowBuyingResult allowBuyingResult = panicBuyingOrderIfc.isAllowBuying( confirmEntity.getPanicBuyingDocCode(),confirmEntity.getOpenId(),confirmEntity.getQuantity()); if (allowBuyingResult!=null&&!allowBuyingResult.isAllowBuying()&& !"".equals(allowBuyingResult.getMessage())) { errJson.addProperty("warning", allowBuyingResult.getMessage()); json.add("error", errJson); this.printJson(response, json.toString()); return; } //提交订单 DocCodeStateEntity docCodeStateEntity = panicBuyingOrderIfc.saveOrder(confirmEntity); if (docCodeStateEntity != null) { //到期未支付的,是定时取消订单 panicBuyingTaskIfc.addCancelOrderTaskForPayingExpired( dataSourceEntity,docCodeStateEntity.getDocCode()) ; //--增加处理单据确认后生成二维码,线程处理 ThreadPoolTaskScheduler threadPoolTaskScheduler = (ThreadPoolTaskScheduler) FactoryBean.getBean("threadPoolTaskScheduler"); threadPoolTaskScheduler.execute(new QrCodeRunable(docCodeStateEntity.getFormId(), docCodeStateEntity.getFormType(),docCodeStateEntity.getDocCode() , confirmEntity.getCltCode(),confirmEntity.getCltName(), dataSourceEntity.getDbId()+"")); docCode = docCodeStateEntity.getDocCode(); json.addProperty("DocStatus", docCodeStateEntity.getDocStatus()); json.addProperty("isAllowPayable", docCodeStateEntity.getIsAllowPayable()); weiXinPayAmount = docCodeStateEntity.getWeiXinPayAmount(); } json.addProperty(SettingKey.DOCCODE, docCode); json.addProperty("formid", "710806"); json.addProperty("weiXinPayAmount", weiXinPayAmount); //如果有月结客户,则不需要支付 json.addProperty("status", "success"); //json.addProperty("redirect", redirect); this.printJson(response, json.toString()); return; } catch (DataAccessException e) { e.printStackTrace(); errJson.addProperty("warning", (e.getCause()!=null?e.getCause().getMessage():e.getMessage())); json.add("error", errJson); this.printJson(response, json.toString()); return; } catch (Exception e) { e.printStackTrace(); errJson.addProperty("warning", (e.getCause()!=null?e.getCause().getMessage():e.getMessage())); json.add("error", errJson); this.printJson(response, json.toString()); return; } finally { SpObserver.setDBtoInstance(); } } /** * 获取活动订单确认时的信息 json 数据格式(小程序需要用到) * @param request * @param response */ @RequestMapping(params = "m=getPanicBuyingConfirmInfo") public void getPanicBuyingConfirmInfo(HttpServletRequest request, HttpServletResponse response) { HttpSession session = request.getSession(); //String dbId = (String) session.getAttribute(SessionKey.SHOPPING_DBID); //String userCode = (session.getAttribute(SessionKey.USERCODE) == null ? "" : (String)session.getAttribute(SessionKey.USERCODE)); String openId = (session.getAttribute(SessionKey.WEIXIN_OPENID) == null ? "" : (String) session.getAttribute(SessionKey.WEIXIN_OPENID) ) ; //String sessionId = session.getId(); String cltCode = (session.getAttribute(SettingKey.CLTCODE) == null?"": (String)session.getAttribute(SettingKey.CLTCODE)) ; String docCode=request.getParameter(SettingKey.DOCCODE)==null?"":request.getParameter(SettingKey.DOCCODE); String hostUrl = SettingKey.getHostUrl(request); JsonObject json = new JsonObject(); JsonObject errJson = new JsonObject(); SettingEntity settingEntity = null; List freeList = null; AddressEntity addressEntity = null ; //boolean isMoblieBrowser = SettingKey.isMoblieBrowser(request); try { ConfirmEntity confirmEntity = new ConfirmEntity(request) ; DataSourceEntity dataSourceEntity = MultiDataSource.getDataSourceMap( request) ; SpObserver.setDBtoInstance("_"+dataSourceEntity.getDbId());//切换数据源 AddressIfc myAddressIfc = (AddressIfc) FactoryBean.getBean("AddressImpl"); try { //交货地址 addressEntity = myAddressIfc.getAddressByAddressId(confirmEntity.getPaymentAddress(), cltCode); }catch (Exception e) { if (e instanceof DataAccessResourceFailureException) { //The connection is closed , 该连接已关闭 try { System.out.println(this.getClass()+" 准备第二次获取地址"); //交货地址,有 The connection is closed 错误时,再执行一次 addressEntity = myAddressIfc.getAddressByAddressId(confirmEntity.getPaymentAddress(), cltCode); } catch (Exception e2) { System.out.println(this.getClass()+" 第二次获取地址时出错:" + ( e2.getCause()!=null? e2.getCause().getMessage():e2.getMessage())); throw e2; } } } settingEntity = settingIfc.getSettingEntity() ; //取网店 shopcccode //ShopCcCodeEntity shopCcCodeEntity = ShopCcCode.getShopCcCode(settingEntity,request); //检查该客户是否存在 if (openId != null && !"".equals(openId)) { CustomerEntity customerEntity = accountIfc.getLoginInfoByMiniAppOpenId(openId); if (customerEntity==null || customerEntity.getCltCode()==null||customerEntity.getCltCode().equals("")) { cltCode = null; session.removeAttribute(SettingKey.CLTCODE); } } //检查该客户是否存在 if (cltCode != null && !"".equals(cltCode)) { CustomerEntity customerEntity = accountIfc.getLoginInfoByCltCode(cltCode,cltCode,cltCode); if (customerEntity==null || customerEntity.getCltCode()==null||customerEntity.getCltCode().equals("")) { cltCode = null; session.removeAttribute(SettingKey.CLTCODE); } } if (cltCode == null || "".equals(cltCode) ) { json.addProperty(SettingKey.CLTCODE, ""); errJson.addProperty("warning", "客户不存在,请重新登录"); json.add("error", errJson); this.printJson(response, json.toString()); return; }else { session.removeAttribute(SettingKey.REDIRECT); } //Gson gson=new Gson(); //DecimalFormat df = new DecimalFormat("0.00"); Calendar calendar=Calendar.getInstance(); calendar.setTime(new Date()); calendar.set(Calendar.DAY_OF_MONTH,calendar.get(Calendar.DAY_OF_MONTH)+ settingEntity.getPreSendDays().intValue());//让日期加3 //SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd"); //SimpleDateFormat sdf2=new SimpleDateFormat("hh:mm"); //收货地址 JsonObject addressEntityJsonObject = new JsonObject(); Integer paymentAddressSeq = null; if (confirmEntity.getPaymentAddress() != null) { paymentAddressSeq = confirmEntity.getPaymentAddress(); }else { paymentAddressSeq = addressEntity!=null?addressEntity.getSeq():0 ; } addressEntityJsonObject.addProperty("paymentaddress",paymentAddressSeq); //交货地址 if (addressEntity != null) { addressEntityJsonObject.addProperty("LinkName", addressEntity.getLinkMan()!= null&& ! "".equals(addressEntity.getLinkMan())?addressEntity.getLinkMan(): addressEntity.getCltName()); addressEntityJsonObject.addProperty("Telephone",addressEntity.getTelephone()!= null&& ! "".equals(addressEntity.getTelephone())?addressEntity.getTelephone() : "" ); addressEntityJsonObject.addProperty("TelephoneMask",StringReplaceUtil.getTelephoneMask(addressEntity.getTelephone()!= null&& ! "".equals(addressEntity.getTelephone())?addressEntity.getTelephone() : "" )); addressEntityJsonObject.addProperty("Address",(addressEntity.getAddress()==null||"".equals(addressEntity.getAddress() )?addressEntity.getPropertyAddress(): addressEntity.getAddress())); addressEntityJsonObject.addProperty("AddressName",addressEntity.getAddressName()); addressEntityJsonObject.addProperty("isSelected", true); //是否选中 addressEntityJsonObject.addProperty("Street",addressEntity.getStreet()); addressEntityJsonObject.addProperty("Longitude",addressEntity.getLongitude()); addressEntityJsonObject.addProperty("Latitude",addressEntity.getLatitude()); addressEntityJsonObject.addProperty("TransDistance", addressEntity.getTransDistance()); //距离 } json.add("Address", addressEntityJsonObject); JsonArray orderListJsonArray = new JsonArray(); PanicBuyingEntity orderEntity = panicBuyingIfc.getPanicBuyingHeader(docCode,openId) ; if (orderEntity != null) { orderListJsonArray.add(PanicBuying.getPanicBuyingJsonObject( orderEntity, hostUrl, dataSourceEntity.getDbId()+"",openId)); } json.add("Data", orderListJsonArray); //获取配送列表 JsonArray freeListJsonArray = new JsonArray(); freeList = freeIfc.getFreeEntityList(settingEntity.getDeliveryMethod(),paymentAddressSeq,cltCode, 0) ; for (int i = 0; freeList!=null&& i < freeList.size(); i++) { FreeEntity freeEntity = freeList.get(i); JsonObject freeEntityJsonObject = new JsonObject(); freeEntityJsonObject.addProperty("FreeId", freeEntity.getFreeId()); freeEntityJsonObject.addProperty("FreeName", freeEntity.getFreeName()); freeEntityJsonObject.addProperty("FreeTotal", freeEntity.getFreeTotal()); //订单总金额超过多少免费 freeEntityJsonObject.addProperty("TransCosts", freeEntity.getTransCosts()); //运费 freeEntityJsonObject.addProperty("TransDistance", freeEntity.getTransDistance()); //距离(米) freeEntityJsonObject.addProperty("isRequiredAddress", freeEntity.isRequiredAddress()); //下单时检查收货地址 freeListJsonArray.add(freeEntityJsonObject); } json.add("FreeList", freeListJsonArray); this.printJson(response, json.toString()); return; } catch (DataAccessException e) { e.printStackTrace(); errJson.addProperty("warning", ( e.getCause()!=null? e.getCause().getMessage():e.getMessage())); json.add("error", errJson); this.printJson(response, json.toString()); return; } catch (Exception e) { e.printStackTrace(); errJson.addProperty("warning", ( e.getCause()!=null? e.getCause().getMessage():e.getMessage())); json.add("error", errJson); this.printJson(response, json.toString()); return; } finally { SpObserver.setDBtoInstance(); } } /** * * 核销活动订单api */ @RequestMapping(params = "m=doHeXiao") public void doHeXiao(HttpServletRequest request, HttpServletResponse response) { HttpSession session = request.getSession(); String openId = (session.getAttribute(SessionKey.WEIXIN_OPENID) == null ? "" : (String) session.getAttribute(SessionKey.WEIXIN_OPENID) ) ; String cltCode = (session.getAttribute(SettingKey.CLTCODE) == null?"": (String)session.getAttribute(SettingKey.CLTCODE)) ; String cltName = (session.getAttribute(SettingKey.CLTNAME) == null?"": (String)session.getAttribute(SettingKey.CLTNAME)) ; JsonObject json = new JsonObject(); JsonObject errJson = new JsonObject(); String docCode=request.getParameter(SettingKey.DOCCODE)==null?"":request.getParameter(SettingKey.DOCCODE); //String qrCode = request.getParameter("qrcode") ; if (docCode == null||"".equals(docCode)) { errJson.addProperty("warning","请传递 doccode 参数【/shopping/panicBuyingOrder.do?m=heXiaoOrder】"); json.add("error", errJson); this.printJson(response, json.toString()); return ; } try { DataSourceEntity dataSourceEntity = MultiDataSource.getDataSourceMap( request) ; SpObserver.setDBtoInstance("_"+dataSourceEntity.getDbId());//切换数据源 //核销活动订单 String docOpenId = panicBuyingOrderIfc.saveHeXiaoAmount( cltCode, cltName, openId, docCode) ; //发送 websocket 消息,通知小程序端,显示:已核销 MessageInfo message=new MessageInfo(); message.setDbId(dataSourceEntity.getDbId()).setUserCode(docOpenId).setUserFromType(UserFromType.miniAppUserForPanicBuying) ; message.setMsgType(MessageType.MINI_APP_VERIFIED).setMsg("已核销"); WebSocketMessageServer.publishMessageToRedis(message); json.addProperty("state", "success"); this.printJson(response, json.toString()); return; } catch (DataAccessException e) { e.printStackTrace(); errJson.addProperty("warning", e.getCause()!=null?e.getCause().getMessage():e.getMessage()); json.add("error", errJson); this.printJson(response, json.toString()); return; } catch (Exception e) { e.printStackTrace(); errJson.addProperty("warning", e.getCause()!=null?e.getCause().getMessage():e.getMessage()); json.add("error", errJson); this.printJson(response, json.toString()); return; } finally { SpObserver.setDBtoInstance(); } } /** * * 由客户发起退款申请 */ @RequestMapping(params = "m=doReturnOrderByCustomer") public void doReturnOrderByCustomer(HttpServletRequest request, HttpServletResponse response) { HttpSession session = request.getSession(); //String hostUrl = SettingKey.getHostUrl(request); // 将微信corpid组装成url //String wxQueryString = SettingKey.getQueryStringByWx(request); //boolean isMoblieBrowser = SettingKey.isMoblieBrowser(request); String soDocCode = request.getParameter(SettingKey.DOCCODE)==null?"":request.getParameter(SettingKey.DOCCODE); //String soRowid = request.getParameter("rowid")==null?"":request.getParameter("rowid"); String ReasonIdStr = request.getParameter("reasonid")==null?"":request.getParameter("reasonid"); String OpenedStr = request.getParameter("opened")==null?"":request.getParameter("opened"); String HDMemo = request.getParameter("hdmemo")==null?"":request.getParameter("hdmemo"); if (OpenedStr == null || OpenedStr.equals("")) { OpenedStr = "0"; } if (ReasonIdStr == null || ReasonIdStr.equals("")) { ReasonIdStr = "0"; } int Opened = Integer.parseInt(OpenedStr); int ReasonId = Integer.parseInt(ReasonIdStr); // int Opened=0; //测试时候用 正式用上面的 String usercode = (String) session.getAttribute(SessionKey.HRCODE); if (usercode == null || "".equals(usercode)) { usercode = (String) session.getAttribute(SessionKey.USERCODE); } if (usercode == null || "".equals(usercode)) { usercode = (String) session.getAttribute(SettingKey.CLTCODE); } JsonObject json = new JsonObject(); JsonObject errJson = new JsonObject(); //String dbId = (String) session.getAttribute(SessionKey.SHOPPING_DBID); // 用户id String openId = (String) session.getAttribute(SessionKey.WEIXIN_OPENID); // 执行存储过程获取退款单号 try { DataSourceEntity dataSourceEntity = MultiDataSource.getDataSourceMap( request) ; SpObserver.setDBtoInstance("_"+dataSourceEntity.getDbId());//切换数据源 // 后三个参数测试写死的 正式使用上面的参数 DocCodeStateEntity returnDocCodeEntity = panicBuyingOrderIfc.saveReturnOrder(usercode, openId, soDocCode, ReasonId, Opened, HDMemo); if (returnDocCodeEntity == null ) { errJson.addProperty("warning", "创建退货订单失败"); json.add("error", errJson); this.printJson(response, json.toString()); return; } json.addProperty("state", "success"); json.addProperty("refundDoccode", returnDocCodeEntity.getDocCode()); json.addProperty("message", "申请已发出,工作人员正在处理请耐心等待!"); this.printJson(response, json.toString()); return; } catch (DataAccessException e) { e.printStackTrace(); errJson.addProperty("warning", e.getCause()!=null?e.getCause().getMessage():e.getMessage()); json.add("error", errJson); this.printJson(response, json.toString()); return; } catch (Exception e) { e.printStackTrace(); errJson.addProperty("warning", e.getCause()!=null?e.getCause().getMessage():e.getMessage()); json.add("error", errJson); this.printJson(response, json.toString()); return; } finally { SpObserver.setDBtoInstance(); } } /** * * 由活动创建者发起团购失败时退款,同时退款到微信钱包(原路返回), */ @RequestMapping(params = "m=doReturnOrderAllByCreator") public void doReturnOrderAllByCreator(HttpServletRequest request, HttpServletResponse response) { HttpSession session = request.getSession(); //String hostUrl = SettingKey.getHostUrl(request); String panicBuyingDocCode = request.getParameter(SettingKey.DOCCODE); String soFormId = request.getParameter(SettingKey.FORMID) ; String userCode = (String) session.getAttribute(SessionKey.HRCODE); if (userCode == null || "".equals(userCode)) { userCode = (String) session.getAttribute(SessionKey.USERCODE); } if (userCode == null || "".equals(userCode)) { userCode = (String) session.getAttribute(SettingKey.CLTCODE); } // 用户id //String openId = (String) session.getAttribute(SessionKey.WEIXIN_OPENID); JsonObject json = new JsonObject(); JsonObject errJson = new JsonObject(); if (soFormId == null||"".equals(soFormId)) { errJson.addProperty("warning","调用api接口【/shopping/panicBuyingOrder.do?m=doReturnOrderAllByCreator】时,必须指定 formid 参数"); json.add("error", errJson); this.printJson(response, json.toString()); return ; } if (soFormId == null||!"710804".equals(soFormId)) { errJson.addProperty("warning","只能团购活动才可以调用api接口【/shopping/panicBuyingOrder.do?m=doReturnOrderAllByCreator】"); json.add("error", errJson); this.printJson(response, json.toString()); return ; } //String formIdStr = request.getParameter(SettingKey.FORMID) ; if (panicBuyingDocCode != null && !"".equals(panicBuyingDocCode)) { panicBuyingDocCode = panicBuyingDocCode.replaceAll("'", ""); } try { DataSourceEntity dataSourceEntity = MultiDataSource.getDataSourceMap( request) ; ReturnOrderResult returnOrderResult = doReturnOrderAll( dataSourceEntity, panicBuyingDocCode); JsonArray orderListJsonArray = new JsonArray(); int returnSuccess = 0,returnFailed = 0 ,returnCount = 0; for (int i = 0;returnOrderResult!=null && i < returnOrderResult.getReturnOrderDetailResultList().size(); i++) { ReturnOrderDetailResult returnOrderDetailResult = returnOrderResult.getReturnOrderDetailResultList().get(i); JsonObject orderJson = new JsonObject(); orderJson.addProperty("SoDocCode", returnOrderDetailResult.getSoDocCode()); orderJson.addProperty("state",returnOrderDetailResult.getState()); json.addProperty("message",returnOrderDetailResult.getMessage()); if (returnOrderDetailResult.getState().equals("success")) { returnSuccess ++; }else { returnFailed ++ ; } returnCount ++ ; orderListJsonArray.add(orderJson); } json.add("returnList", orderListJsonArray); json.addProperty("message", "团购失败,应退款"+ returnCount +"单,实退成功"+ returnSuccess + "单,实退失败"+returnFailed +"单" ); json.addProperty("returnCount", returnCount); //应退总单数 json.addProperty("returnSuccess", returnSuccess); //退款成功单数 json.addProperty("returnFailed", returnFailed); //退款失败单数 json.addProperty("state", returnFailed==0? "success":"failed"); return ; } catch (DataAccessException e) { e.printStackTrace(); errJson.addProperty("warning", e.getCause()!=null?e.getCause().getMessage():e.getMessage()); json.add("error", errJson); this.printJson(response, json.toString()); return; } catch (WxPayException e) { e.printStackTrace(); errJson.addProperty("warning", e.getCause()!=null?e.getCause().getMessage():e.getMessage()); json.add("error", errJson); this.printJson(response, json.toString()); return; } catch (Exception e) { e.printStackTrace(); errJson.addProperty("warning", e.getCause()!=null?e.getCause().getMessage():e.getMessage()); json.add("error", errJson); this.printJson(response, json.toString()); return; } } public static ReturnOrderResult doReturnOrderAll(DataSourceEntity dataSourceEntity,String panicBuyingDocCode) throws Exception { try { //DataSourceEntity dataSourceEntity = MultiDataSource.getDataSourceMap( request) ; SpObserver.setDBtoInstance("_"+dataSourceEntity.getDbId());//切换数据源 //SettingEntity settingEntity = settingIfc.getSettingEntity() ; PanicBuyingIfc panicBuyingIfc = (PanicBuyingIfc) FactoryBean.getBean("PanicBuyingImpl"); panicBuyingIfc.changeGroupBuyingStatus(panicBuyingDocCode,2) ; //1表示【团购成功】,2表示【团购失败】 PanicBuyingEntity panicBuyingEntity = panicBuyingIfc.getPanicBuyingHeader(panicBuyingDocCode); if (panicBuyingEntity == null) { throw new Exception("原活动单号不存在【单号:"+ panicBuyingDocCode + "】"); } PanicBuyingOrderIfc panicBuyingOrderIfc = (PanicBuyingOrderIfc) FactoryBean.getBean("PanicBuyingOrderImpl"); //1.退该活动下的所有订单 List returnDocCodeList = panicBuyingOrderIfc.saveReturnOrderAll(panicBuyingEntity.getCltCode(),panicBuyingEntity.getOpenId(), panicBuyingDocCode, 0, 0, ""); MaSettingIfc maSettingIfc = (MaSettingIfc) FactoryBean.getBean("MaSettingImpl"); //JsonArray orderListJsonArray = new JsonArray(); int returnSuccess = 0,returnFailed = 0 ; ReturnOrderResult returnOrderResult = new ReturnOrderResult(); for (int i = 0;returnDocCodeList!=null&& i < returnDocCodeList.size(); i++) { DocCodeStateEntity returnDocCodeEntity = returnDocCodeList.get(i) ; //2.获取新建退款单据需要退的金额 if (returnDocCodeEntity == null) { throw new Exception("原活动单号不存在【单号:"+ panicBuyingDocCode + "】"); } //3.计算退款单据应退金额 710808 int returnAmount = (new BigDecimal(returnDocCodeEntity.getSoWeiXinPayAmount()*100.00)).setScale(0, BigDecimal.ROUND_HALF_UP).intValue(); if (returnAmount == 0) { throw new Exception("订单"+ returnDocCodeEntity.getDocCode()+"金额为0,无需退款"); } //取出商品明细,用于显示在“退款到账通知” 上的“商品明细”栏位 StringBuffer sb = new StringBuffer() ; sb.append(returnDocCodeEntity.getTitle()); String hostUrl = dataSourceEntity.getCorpURL() ; MaSettingEntity maSettingEntity = maSettingIfc.getMaSettingEntity() ; if (maSettingEntity != null && maSettingEntity.getHttpHost()!=null&&!"".equals(maSettingEntity.getHttpHost())) { hostUrl = maSettingEntity.getHttpHost() ; } //4.计算原始订单金额 710806 int orgAmount = (new BigDecimal(returnDocCodeEntity.getSoWeiXinPayAmount()*100.00)).setScale(0, BigDecimal.ROUND_HALF_UP).intValue() ; //5.执行微信退款 boolean state = Wxtransactions.wxRefundMoney(returnDocCodeEntity.getOpenId() ,returnDocCodeEntity.getRefCode() ,returnDocCodeEntity.getRefFormId(), returnDocCodeEntity.getDocCode(), returnDocCodeEntity.getFormId(),orgAmount,returnAmount, dataSourceEntity, hostUrl+ SettingKey.REFUND_NOTICE_URL_FOR_RETIRN_ORDER,"3",sb.toString()); ReturnOrderDetailResult returnOrderDetailResult = returnOrderResult.new ReturnOrderDetailResult(); returnOrderDetailResult.setSoDocCode(returnDocCodeEntity.getSoDocCode()); if (state) { returnSuccess ++; returnOrderDetailResult.setState("success"); returnOrderDetailResult.setMessage("微信退款成功!"); } else { returnFailed ++; returnOrderDetailResult.setState("failed"); returnOrderDetailResult.setMessage("微信退款失败!"); } returnOrderResult.getReturnOrderDetailResultList().add(returnOrderDetailResult); } returnOrderResult.setMessage("团购失败,应退款"+ (returnDocCodeList!=null?returnDocCodeList.size():0) +"单,实退成功"+ returnSuccess + "单,实退失败"+returnFailed +"单"); returnOrderResult.setReturnCount(returnDocCodeList!=null?returnDocCodeList.size():0); //应退总单数 returnOrderResult.setReturnFailed(returnFailed); //退款失败单数 returnOrderResult.setReturnSuccess(returnSuccess); //退款成功单数 returnOrderResult.setState(returnFailed==0? "success":"failed"); return returnOrderResult ; } catch (DataAccessException e) { throw e ; } catch (WxPayException e) { throw e ; } catch (Exception e) { throw e ; } finally { SpObserver.setDBtoInstance(); } } /** * * 由活动创建者发起退款,同时退款到微信钱包(原路返回), */ @RequestMapping(params = "m=doReturnOrderByCreator") public void doReturnOrderByCreator(HttpServletRequest request, HttpServletResponse response) { HttpSession session = request.getSession(); //String hostUrl = SettingKey.getHostUrl(request); String soDocCode = request.getParameter(SettingKey.DOCCODE); String soFormId = request.getParameter(SettingKey.FORMID) ; //String soDocCode = request.getParameter(SettingKey.DOCCODE)==null?"":request.getParameter(SettingKey.DOCCODE); //String soRowid = request.getParameter("rowid")==null?"":request.getParameter("rowid"); String reasonIdStr = request.getParameter("reasonid")==null?"":request.getParameter("reasonid"); String openedStr = request.getParameter("opened")==null?"":request.getParameter("opened"); String hdMemo = request.getParameter("hdmemo")==null?"":request.getParameter("hdmemo"); if (openedStr == null || openedStr.equals("")) { openedStr = "0"; } if (reasonIdStr == null || reasonIdStr.equals("")) { reasonIdStr = "0"; } int opened = Integer.parseInt(openedStr); int reasonId = Integer.parseInt(reasonIdStr); String userCode = (String) session.getAttribute(SessionKey.HRCODE); if (userCode == null || "".equals(userCode)) { userCode = (String) session.getAttribute(SessionKey.USERCODE); } if (userCode == null || "".equals(userCode)) { userCode = (String) session.getAttribute(SettingKey.CLTCODE); } // 用户id String openId = (String) session.getAttribute(SessionKey.WEIXIN_OPENID); JsonObject json = new JsonObject(); JsonObject errJson = new JsonObject(); if (soFormId == null||"".equals(soFormId)) { errJson.addProperty("warning","调用api接口【/shopping/panicBuyingOrder.do?m=doReturnOrderByCreator】时,必须指定 formid 参数"); json.add("error", errJson); this.printJson(response, json.toString()); return ; } //String formIdStr = request.getParameter(SettingKey.FORMID) ; if (soDocCode != null && !"".equals(soDocCode)) { soDocCode = soDocCode.replaceAll("'", ""); } try { DataSourceEntity dataSourceEntity = MultiDataSource.getDataSourceMap( request) ; SpObserver.setDBtoInstance("_"+dataSourceEntity.getDbId());//切换数据源 //SettingEntity settingEntity = settingIfc.getSettingEntity() ; //检查商家余额是否足够退款 CheckServiceBalanceWhenRefundResult checkServiceBalanceWhenRefundResult = panicBuyingOrderIfc.CheckServiceBalanceWhenRefund(soDocCode); if (checkServiceBalanceWhenRefundResult==null|| !checkServiceBalanceWhenRefundResult.isAllowRefund()) { json.addProperty("state", "failed"); json.addProperty("message", "商家["+checkServiceBalanceWhenRefundResult.getServiceCltName()+"]账户余额[" + (0.0 - checkServiceBalanceWhenRefundResult.getServiceBalance().doubleValue()) + "元]不足以扣除本单退款所需要的金额[" +checkServiceBalanceWhenRefundResult.getPayableAmount() + "元],请与客户协商,退款失败!"); json.addProperty("isAllowRefund", checkServiceBalanceWhenRefundResult.isAllowRefund()) ; json.addProperty("serviceCltCode", checkServiceBalanceWhenRefundResult.getServiceCltCode()) ; json.addProperty("serviceCltName", checkServiceBalanceWhenRefundResult.getServiceCltName()) ; json.addProperty("serviceTelephone", checkServiceBalanceWhenRefundResult.getServiceTelephone()) ; json.addProperty("serviceTelephoneMask",StringReplaceUtil.getTelephoneMask( checkServiceBalanceWhenRefundResult.getServiceTelephone())) ; json.addProperty("sellerId", checkServiceBalanceWhenRefundResult.getSellerId()) ; json.addProperty("sellerName", checkServiceBalanceWhenRefundResult.getSellerName()) ; json.addProperty("sellerTelephone", checkServiceBalanceWhenRefundResult.getSellerTelephone()) ; json.addProperty("sellerTelephoneMask", StringReplaceUtil.getTelephoneMask(checkServiceBalanceWhenRefundResult.getSellerTelephone())) ; json.addProperty("payableAmount", checkServiceBalanceWhenRefundResult.getPayableAmount()) ; json.addProperty("serviceBalance", 0.0 - checkServiceBalanceWhenRefundResult.getServiceBalance().doubleValue()) ; //要转换成负数输出 json.addProperty("paidFeeAmount", checkServiceBalanceWhenRefundResult.getPaidFeeAmount()) ; json.addProperty("customerCltCode", checkServiceBalanceWhenRefundResult.getCustomerCltCode()) ; json.addProperty("customerCltName", checkServiceBalanceWhenRefundResult.getCustomerCltName()) ; json.addProperty("customerTelephone", checkServiceBalanceWhenRefundResult.getCustomerTelephone()) ; json.addProperty("customerTelephoneMask", StringReplaceUtil.getTelephoneMask(checkServiceBalanceWhenRefundResult.getCustomerTelephone())) ; this.printJson(response, json.toString()); return ; } //1.新建退款单据 DocCodeStateEntity returnDocCodeEntity = panicBuyingOrderIfc.saveReturnOrder(userCode, openId, soDocCode, reasonId, opened, hdMemo); //2.获取新建退款单据需要退的金额 if (returnDocCodeEntity == null) { errJson.addProperty("warning", "原活动订单不存在【单号:"+ soDocCode + "】"); json.add("error", errJson); this.printJson(response, json.toString()); return ; } //3.计算退款单据应退金额 710808 int returnAmount = (new BigDecimal(returnDocCodeEntity.getWeiXinPayAmount()*100.00)).setScale(0, BigDecimal.ROUND_HALF_UP).intValue(); if (returnAmount == 0) { errJson.addProperty("warning", "金额为0,无需退款"); json.add("error", errJson); this.printJson(response, json.toString()); return ; } //取出商品明细,用于显示在“退款到账通知” 上的“商品明细”栏位 StringBuffer sb = new StringBuffer() ; sb.append(returnDocCodeEntity.getTitle()); String hostUrl = dataSourceEntity.getCorpURL() ; MaSettingEntity maSettingEntity = maSettingIfc.getMaSettingEntity() ; if (maSettingEntity != null && maSettingEntity.getHttpHost()!=null&&!"".equals(maSettingEntity.getHttpHost())) { hostUrl = maSettingEntity.getHttpHost() ; } //4.计算原始订单金额 710806 int orgAmount = (new BigDecimal(returnDocCodeEntity.getSoWeiXinPayAmount()*100.00)).setScale(0, BigDecimal.ROUND_HALF_UP).intValue() ; //5.执行微信退款 boolean state = Wxtransactions.wxRefundMoney(returnDocCodeEntity.getOpenId() ,soDocCode ,returnDocCodeEntity.getFormId(), returnDocCodeEntity.getDocCode(), Integer.parseInt(soFormId),orgAmount,returnAmount, dataSourceEntity, hostUrl+ SettingKey.REFUND_NOTICE_URL_FOR_RETIRN_ORDER,"3",sb.toString()); if (state) { json.addProperty("state", "success"); json.addProperty("message", "微信退款成功!"); this.printJson(response, json.toString()); return; } else { json.addProperty("state", "failed"); json.addProperty("message", "微信退款失败!"); this.printJson(response, json.toString()); return; } } catch (DataAccessException e) { e.printStackTrace(); errJson.addProperty("warning", e.getCause()!=null?e.getCause().getMessage():e.getMessage()); json.add("error", errJson); this.printJson(response, json.toString()); return; } catch (WxPayException e) { e.printStackTrace(); errJson.addProperty("warning", e.getCause()!=null?e.getCause().getMessage():e.getMessage()); json.add("error", errJson); this.printJson(response, json.toString()); return; } catch (Exception e) { e.printStackTrace(); errJson.addProperty("warning", e.getCause()!=null?e.getCause().getMessage():e.getMessage()); json.add("error", errJson); this.printJson(response, json.toString()); return; } finally { SpObserver.setDBtoInstance(); } } /** * * 获取我的订单列表 */ @RequestMapping(params = "m=getOrderListForMe") public void getOrderListForMe(HttpServletRequest request, HttpServletResponse response) { HttpSession session = request.getSession(); String hostUrl = SettingKey.getHostUrl(request); // 将微信corpid组装成url //String wxQueryString = SettingKey.getQueryStringByWx(request); //boolean isMoblieBrowser = SettingKey.isMoblieBrowser(request); // int Opened=0; //测试时候用 正式用上面的 String usercode = (String) session.getAttribute(SessionKey.HRCODE); if (usercode == null || "".equals(usercode)) { usercode = (String) session.getAttribute(SessionKey.USERCODE); } if (usercode == null || "".equals(usercode)) { usercode = (String) session.getAttribute(SettingKey.CLTCODE); } String cltCode = (session.getAttribute(SettingKey.CLTCODE) == null ? "" : (String) session.getAttribute(SettingKey.CLTCODE)); JsonObject json = new JsonObject(); JsonObject errJson = new JsonObject(); //String dbId = (String) session.getAttribute(SessionKey.SHOPPING_DBID); // 用户id String openId = (String) session.getAttribute(SessionKey.WEIXIN_OPENID); // 执行存储过程获取退款单号 try { DataSourceEntity dataSourceEntity = MultiDataSource.getDataSourceMap( request) ; SpObserver.setDBtoInstance("_"+dataSourceEntity.getDbId());//切换数据源 SettingEntity settingEntity = settingIfc.getSettingEntity() ; // 后三个参数测试写死的 正式使用上面的参数 List dataList = panicBuyingOrderIfc.getOrderHeaderListForMe(cltCode); //待支付(草稿) JsonArray draftListJsonArray = new JsonArray(); List draftList = dataList.stream().filter(s->s.getDocStatus().intValue()==0).collect(Collectors.toList()); for (int i = 0;draftList!=null&& i < draftList.size(); i++) { PanicBuyingOrderEntity orderEntity = draftList.get(i) ; draftListJsonArray.add(getOrderEntityJsonObject( orderEntity, hostUrl, cltCode, settingEntity, dataSourceEntity.getDbId()+"",openId)); } json.add("draftList", draftListJsonArray); //已支付 JsonArray orderListJsonArray = new JsonArray(); List orderList = dataList.stream().filter(s->s.getDocStatus().intValue()==100).collect(Collectors.toList()); for (int i = 0;orderList!=null&& i < orderList.size(); i++) { PanicBuyingOrderEntity orderEntity = orderList.get(i) ; orderListJsonArray.add(getOrderEntityJsonObject( orderEntity, hostUrl, cltCode, settingEntity, dataSourceEntity.getDbId()+"",openId)); } json.add("orderList", orderListJsonArray); //已取消 JsonArray cancelListJsonArray = new JsonArray(); List cancelList = dataList.stream().filter(s->s.getDocStatus().intValue()==-200).collect(Collectors.toList()); for (int i = 0;cancelList!=null&& i < cancelList.size(); i++) { PanicBuyingOrderEntity orderEntity = cancelList.get(i) ; cancelListJsonArray.add(getOrderEntityJsonObject( orderEntity, hostUrl, cltCode, settingEntity, dataSourceEntity.getDbId()+"",openId)); } json.add("cancelList", cancelListJsonArray); this.printJson(response, json.toString()); return; } catch (DataAccessException e) { e.printStackTrace(); errJson.addProperty("warning", e.getCause()!=null?e.getCause().getMessage():e.getMessage()); json.add("error", errJson); this.printJson(response, json.toString()); return; } catch (Exception e) { e.printStackTrace(); errJson.addProperty("warning", e.getCause()!=null?e.getCause().getMessage():e.getMessage()); json.add("error", errJson); this.printJson(response, json.toString()); return; } finally { SpObserver.setDBtoInstance(); } } /** * * 获取我的订单明细 */ @RequestMapping(params = "m=getOrderDetail") public void getOrderDetail(HttpServletRequest request, HttpServletResponse response) { HttpSession session = request.getSession(); String hostUrl = SettingKey.getHostUrl(request); // 将微信corpid组装成url //String wxQueryString = SettingKey.getQueryStringByWx(request); //boolean isMoblieBrowser = SettingKey.isMoblieBrowser(request); // int Opened=0; //测试时候用 正式用上面的 String usercode = (String) session.getAttribute(SessionKey.HRCODE); if (usercode == null || "".equals(usercode)) { usercode = (String) session.getAttribute(SessionKey.USERCODE); } if (usercode == null || "".equals(usercode)) { usercode = (String) session.getAttribute(SettingKey.CLTCODE); } String cltCode = (session.getAttribute(SettingKey.CLTCODE) == null ? "" : (String) session.getAttribute(SettingKey.CLTCODE)); JsonObject json = new JsonObject(); JsonObject errJson = new JsonObject(); String docCode=request.getParameter(SettingKey.DOCCODE)==null?"":request.getParameter(SettingKey.DOCCODE); //String dbId = (String) session.getAttribute(SessionKey.SHOPPING_DBID); // 用户id String openId = (String) session.getAttribute(SessionKey.WEIXIN_OPENID); // 执行存储过程获取退款单号 try { DataSourceEntity dataSourceEntity = MultiDataSource.getDataSourceMap( request) ; SpObserver.setDBtoInstance("_"+dataSourceEntity.getDbId());//切换数据源 SettingEntity settingEntity = settingIfc.getSettingEntity() ; PanicBuyingOrderEntity orderEntity = panicBuyingOrderIfc.getOrderHeaderEntity(docCode); JsonArray orderListJsonArray = new JsonArray(); if (orderEntity!=null) { orderListJsonArray.add(getOrderEntityJsonObject( orderEntity, hostUrl, cltCode, settingEntity, dataSourceEntity.getDbId()+"",openId)); } json.add("list", orderListJsonArray); List orderSellerList = panicBuyingOrderIfc.getPanicBuyingOrderSellerList( docCode); JsonArray orderSellerListJsonArray = new JsonArray(); for (int i = 0; orderSellerList!=null&&i < orderSellerList.size(); i++) { PanicBuyingSellerEntity panicBuyingSellerEntity = orderSellerList.get(i); JsonObject orderSellerEntityJsonObject = new JsonObject(); orderSellerEntityJsonObject.addProperty("DocItem", panicBuyingSellerEntity.getDocItem()); orderSellerEntityJsonObject.addProperty("SellerId", panicBuyingSellerEntity.getSellerId()); orderSellerEntityJsonObject.addProperty("SellerName", panicBuyingSellerEntity.getSellerName()); orderSellerEntityJsonObject.addProperty("Telephone", panicBuyingSellerEntity.getTelephone()); orderSellerEntityJsonObject.addProperty("TelephoneMask",StringReplaceUtil.getTelephoneMask(panicBuyingSellerEntity.getTelephone())); orderSellerEntityJsonObject.addProperty("isDefaultSellerName", panicBuyingSellerEntity.getIsDefaultSellerName()); orderSellerListJsonArray.add(orderSellerEntityJsonObject); } json.add("sellerList", orderSellerListJsonArray); this.printJson(response, json.toString()); return; } catch (DataAccessException e) { e.printStackTrace(); errJson.addProperty("warning", e.getCause()!=null?e.getCause().getMessage():e.getMessage()); json.add("error", errJson); this.printJson(response, json.toString()); return; } catch (Exception e) { e.printStackTrace(); errJson.addProperty("warning", e.getCause()!=null?e.getCause().getMessage():e.getMessage()); json.add("error", errJson); this.printJson(response, json.toString()); return; } finally { SpObserver.setDBtoInstance(); } } /** * * 取消待支付单个订单 */ @RequestMapping(params = "m=cancelExpiredOrder") public void cancelExpiredOrder(HttpServletRequest request, HttpServletResponse response) { JsonObject json = new JsonObject(); JsonObject errJson = new JsonObject(); String docCode=request.getParameter(SettingKey.DOCCODE)==null?"":request.getParameter(SettingKey.DOCCODE); // 执行存储过程获取退款单号 try { DataSourceEntity dataSourceEntity = MultiDataSource.getDataSourceMap( request) ; SpObserver.setDBtoInstance("_"+dataSourceEntity.getDbId());//切换数据源 //SettingEntity settingEntity = settingIfc.getSettingEntity() ; PanicBuyingOrderEntity panicBuyingOrderEntity = panicBuyingOrderIfc.getOrderHeaderEntity(docCode); if (panicBuyingOrderEntity != null) { if (panicBuyingOrderEntity.getDocStatus().intValue() == -200) { errJson.addProperty("warning", "已取消"); json.add("error", errJson); this.printJson(response, json.toString()); return; } if (panicBuyingOrderEntity.getDocStatus().intValue() == 100) { errJson.addProperty("warning", "订单已支付,不能取消"); json.add("error", errJson); this.printJson(response, json.toString()); return; } if (panicBuyingOrderEntity.getDocStatus().intValue() != 0) { errJson.addProperty("warning", "订单不是待支付状态,不能取消"); json.add("error", errJson); this.printJson(response, json.toString()); return; } panicBuyingOrderIfc.deletePayingExpiredOrder(docCode); } json.addProperty("state", "success"); this.printJson(response, json.toString()); return; } catch (DataAccessException e) { e.printStackTrace(); errJson.addProperty("warning", e.getCause()!=null?e.getCause().getMessage():e.getMessage()); json.add("error", errJson); this.printJson(response, json.toString()); return; } catch (Exception e) { e.printStackTrace(); errJson.addProperty("warning", e.getCause()!=null?e.getCause().getMessage():e.getMessage()); json.add("error", errJson); this.printJson(response, json.toString()); return; } finally { SpObserver.setDBtoInstance(); } } /** * * 按扫码获取我的订单明细 */ @RequestMapping(params = "m=getOrderDetailByQrCode") public void getOrderDetailByQrCode(HttpServletRequest request, HttpServletResponse response) { HttpSession session = request.getSession(); String hostUrl = SettingKey.getHostUrl(request); String openId = (session.getAttribute(SessionKey.WEIXIN_OPENID) == null ? "" : (String) session.getAttribute(SessionKey.WEIXIN_OPENID) ) ; String cltCode = (session.getAttribute(SettingKey.CLTCODE) == null?"": (String)session.getAttribute(SettingKey.CLTCODE)) ; //String cltName = (session.getAttribute(SettingKey.CLTNAME) == null?"": (String)session.getAttribute(SettingKey.CLTNAME)) ; JsonObject json = new JsonObject(); JsonObject errJson = new JsonObject(); String qrCode = request.getParameter("qrcode") ; if (qrCode == null||"".equals(qrCode)) { errJson.addProperty("warning","请传递 qrcode 参数【/shopping/panicBuyingOrder.do?m=getOrderDetailByQrCode】"); json.add("error", errJson); this.printJson(response, json.toString()); return ; } try { DataSourceEntity dataSourceEntity = MultiDataSource.getDataSourceMap( request) ; SpObserver.setDBtoInstance("_"+dataSourceEntity.getDbId());//切换数据源 SettingEntity settingEntity = settingIfc.getSettingEntity() ; QrCodeForAppEntity qrCodeForAppEntity = matCodeIfc.getQrCodeEntity( qrCode) ; //获取存在的二维码 if (qrCodeForAppEntity == null) { errJson.addProperty("warning","该二维码不存在!" + qrCode ); json.add("error", errJson); this.printJson(response, json.toString()); return ; } PanicBuyingOrderEntity orderEntity = panicBuyingOrderIfc.getOrderHeaderEntity(qrCodeForAppEntity.getRefDocCode()); JsonArray orderListJsonArray = new JsonArray(); if (orderEntity!=null) { orderListJsonArray.add(getOrderEntityJsonObject( orderEntity, hostUrl, cltCode, settingEntity, dataSourceEntity.getDbId()+"",openId)); } //发送 websocket 消息,通知小程序端,显示:已扫码 MessageInfo message=new MessageInfo(); message.setDbId(dataSourceEntity.getDbId()).setUserCode(orderEntity.getOpenId()).setUserFromType(UserFromType.miniAppUserForPanicBuying); message.setMsgType(MessageType.MINI_APP_VERIFYING).setMsg("已扫码"); WebSocketMessageServer.publishMessageToRedis(message); json.add("list", orderListJsonArray); json.addProperty("state", "success"); this.printJson(response, json.toString()); return; } catch (DataAccessException e) { e.printStackTrace(); errJson.addProperty("warning", e.getCause()!=null?e.getCause().getMessage():e.getMessage()); json.add("error", errJson); this.printJson(response, json.toString()); return; } catch (Exception e) { e.printStackTrace(); errJson.addProperty("warning", e.getCause()!=null?e.getCause().getMessage():e.getMessage()); json.add("error", errJson); this.printJson(response, json.toString()); return; } finally { SpObserver.setDBtoInstance(); } } private JsonObject getOrderEntityJsonObject(PanicBuyingOrderEntity orderEntity,String hostUrl,String cltCode,SettingEntity settingEntity,String dbid,String openId) { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd") ; SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss") ; SimpleDateFormat sdf3 = new SimpleDateFormat("yyyy-MM-dd HH:mm") ; JsonObject orderEntityJsonObject = new JsonObject(); //status : 0 所有,1.待付款,2.待核销,3.已核销,4.已取消 String groupBy = "所有" ; if (orderEntity.getDocStatus()!=null&&orderEntity.getDocStatus().equals(0)) { groupBy = "待付款"; } if (orderEntity.getDocStatus().equals(100) ) { if (orderEntity.getHeXiaoStatus().equals(0)) { groupBy = "待核销"; // orderEntity.getHeXiaoStatus(); //待核销,已核销 }else { groupBy = "已核销"; // orderEntity.getHeXiaoStatus(); //待核销,已核销 } } if (orderEntity.getDocStatus().equals(-200)) { groupBy = "已取消"; } orderEntityJsonObject.addProperty("GroupBy", groupBy); orderEntityJsonObject.addProperty(SettingKey.DOCCODE, orderEntity.getDocCode()); orderEntityJsonObject.addProperty("FormId", orderEntity.getFormId()); orderEntityJsonObject.addProperty("DocType", orderEntity.getDocType()); //单据类型:微信订单 、微信团购订单 orderEntityJsonObject.addProperty("RefCode", orderEntity.getRefCode()); //原始活动单号 //orderEntityJsonObject.addProperty("refFormId", orderEntity.getRefFormId()); //原始活动单号 //orderEntityJsonObject.addProperty("refFormType", orderEntity.getRefFormType()); //原始活动单号 orderEntityJsonObject.addProperty("CcCode", orderEntity.getCcCode()); //商家编号 orderEntityJsonObject.addProperty("CcName", orderEntity.getCcName()); //商家名称 orderEntityJsonObject.addProperty("Street", orderEntity.getStreet()); //门牌地址 orderEntityJsonObject.addProperty("FullAddress", orderEntity.getFullAddress()); //地址 orderEntityJsonObject.addProperty("FullAddressName", orderEntity.getFullAddressName()); //地址名称 orderEntityJsonObject.addProperty("Digit",orderEntity.getDigit()); //订单总数量 orderEntityJsonObject.addProperty("Price",orderEntity.getPrice()); //订单价格 orderEntityJsonObject.addProperty("Amount",orderEntity.getAmount()); //订单金额 orderEntityJsonObject.addProperty("PayableAmount",orderEntity.getPayableAmount()); //优惠后订单总金额 orderEntityJsonObject.addProperty("ReduceMoney",orderEntity.getReduceMoney()); //优惠金额 orderEntityJsonObject.addProperty("TransCosts",orderEntity.getTransCosts()); //运费 orderEntityJsonObject.addProperty("HandlingCharges",orderEntity.getHandlingCharges()); //搬运费 orderEntityJsonObject.addProperty("Currency",orderEntity.getCurrency()); //币种 orderEntityJsonObject.addProperty("CurrencySign",orderEntity.getCurrencySign()); //货币符号 orderEntityJsonObject.addProperty("PropertyAddress",orderEntity.getPropertyAddress()); //地址 orderEntityJsonObject.addProperty("LinkMan",orderEntity.getLinkMan() != null &&!"".equals(orderEntity.getLinkMan())?orderEntity.getLinkMan(): orderEntity.getCltName()); //联系人 orderEntityJsonObject.addProperty("Telephone",orderEntity.getTelephone()); //联系人电话 orderEntityJsonObject.addProperty("TelephoneMask",StringReplaceUtil.getTelephoneMask(orderEntity.getTelephone())); //联系人电话 orderEntityJsonObject.addProperty("DeliveryMethod",orderEntity.getDeliveryMethod()); orderEntityJsonObject.addProperty("DeliveryDate",sdf.format(orderEntity.getDocDate())); orderEntityJsonObject.addProperty("InvoiceType","电子普通发票"); orderEntityJsonObject.addProperty("HDMemo",orderEntity.getHdMemo()); //orderEntityJsonObject.addProperty("LogisticsCode",orderEntity.getLogisticsCode()); //物流单号 //orderEntityJsonObject.addProperty("ShipperCode",orderEntity.getShipperCode()); //快递公司编号 //orderEntityJsonObject.addProperty("ShipperName",orderEntity.getShipperName()); //快递公司名称 orderEntityJsonObject.addProperty("isAllowPayable",orderEntity.getIsAllowPayable()); //是否可支付 isAllowPayable 状态: 0.不能支付,1.可支付,2.已支付 ,3.正在退款,4.已退款,5.无需支付 orderEntityJsonObject.addProperty("DocStatus",orderEntity.getDocStatus()); //单据状态 orderEntityJsonObject.addProperty("DocDate",orderEntity.getDocDate()==null?"":sdf.format(orderEntity.getDocDate())); orderEntityJsonObject.addProperty("EnterDate",orderEntity.getEnterDate()==null?"":sdf2.format(orderEntity.getEnterDate())); //orderEntityJsonObject.addProperty("PreSendDate",orderEntity.getPreSendDate()==null?"":sdf3.format(orderEntity.getPreSendDate())); orderEntityJsonObject.addProperty("PayableSecondBalance",orderEntity.getPayableSecondBalance()!=null&& orderEntity.getPayableSecondBalance().intValue() >0?orderEntity.getPayableSecondBalance():0); //下单后支付倒计时秒数 //orderEntityJsonObject.addProperty("GroupBuyingDocCode",orderEntity.getGroupBuyingDocCode()); //团购订单号 //orderEntityJsonObject.addProperty("GroupBuyingMembers",orderEntity.getGroupBuyingMembers()); //需要多少人成团 //orderEntityJsonObject.addProperty("GroupBuyingHasMembers",orderEntity.getGroupBuyingHasMembers()); //已有多少人成团 //orderEntityJsonObject.addProperty("GroupBuyingMembersBalance",orderEntity.getGroupBuyingMembersBalance()); //还差多少人成团 //获取单据状态描述 String docStatusDesc = getDocStatusDescription(orderEntity) ; orderEntityJsonObject.addProperty("DocStatusDesc", docStatusDesc); //是否显示:支付按钮 orderEntityJsonObject.addProperty("isShowPayButton",cltCode.equals(orderEntity.getCltCode()) && orderEntity.getDocStatus()!=null&&orderEntity.getDocStatus().equals(0) &&orderEntity.getCltCode()!=null&&orderEntity.getCltCode().equals(cltCode) &&orderEntity.getIsAllowPayable()!=null&&orderEntity.getIsAllowPayable().equals(1)?true:false); //是否显示:取消订单按钮 orderEntityJsonObject.addProperty("isShowCancelButton",settingEntity.isAllowReturnOrder()&& cltCode.equals(orderEntity.getCltCode()) && orderEntity.getDocStatus()!=null&&(orderEntity.getDocStatus().equals(100)||orderEntity.getDocStatus().equals(0)) ?true:false); //是否显示:查看物流按钮 //boolean isShowLogisticsButton = (orderEntity.getDocStatus()!=null&&orderEntity.getDocStatus().equals(100)&& orderEntity.getLogisticsCode()!=null && !orderEntity.getLogisticsCode().equals("")?true:false); //orderEntityJsonObject.addProperty("isShowLogisticsButton",(cltCode.equals(orderEntity.getCltCode()))&&isShowLogisticsButton&&orderEntity.getShipperCode()!=null&& !orderEntity.getShipperCode().equals("") ); //是否显示:收货按钮 //orderEntityJsonObject.addProperty("isShowReceiptButton", cltCode.equals(orderEntity.getCltCode()) && isShowLogisticsButton && // orderEntity.getDocStatus()!=null&&orderEntity.getDocStatus().equals(100)&&!orderEntity.isReceiptGoodsByCustomer() ?true:false); //是否显示:删除按钮 orderEntityJsonObject.addProperty("isShowDeleteButton", orderEntity.getDocStatus().equals(0) ||(orderEntity.getDocStatus()!=null &&orderEntity.getDocStatus().equals(-200)) ?true:false); //generateOrderProcess : 0.生成订单可立即支付 ,1.生成订单,导购审核后才能支付(适用于生鲜类卖家,卖牛肉牛排的 ,他们按份卖,客户下单 时1份,假设1份 500克,实际卖出时是按称重卖的,可能有 510克,那最后付款也是按 510克付。) //orderEntityJsonObject.addProperty("generateOrderProcess",settingEntity.getGenerateOrderProcess() ); orderEntityJsonObject.addProperty("title", orderEntity.getTitle()); //活动标题 orderEntityJsonObject.addProperty("coverImage", orderEntity.getCoverImage()); //产品图片 orderEntityJsonObject.addProperty("coverImageUrl", SettingKey.getUrl(hostUrl, orderEntity.getCoverImageUrl(), dbid,null) ); //产品图片 orderEntityJsonObject.addProperty("originalPrice", orderEntity.getOriginalPrice()); //原价 orderEntityJsonObject.addProperty("salesPrice", orderEntity.getSalesPrice()); //秒杀价,报名邀约时是报名费 orderEntityJsonObject.addProperty("numberOfTeam", orderEntity.getNumberOfTeam()); //成团人数 orderEntityJsonObject.addProperty("effectiveStartDate",orderEntity.getEffectiveStartDate()==null?"":sdf3.format(orderEntity.getEffectiveStartDate())); //开始时间 orderEntityJsonObject.addProperty("effectiveEndDate",orderEntity.getEffectiveEndDate()==null?"":sdf3.format(orderEntity.getEffectiveEndDate())); //结束时间 orderEntityJsonObject.addProperty("effectiveStartDateBalance", orderEntity.getEffectiveStartDateBalance()); //开始时间 秒数 orderEntityJsonObject.addProperty("effectiveEndDateBalance", orderEntity.getEffectiveEndDateBalance()); //结束时间 秒数 orderEntityJsonObject.addProperty("quantity", orderEntity.getQuantity()); //秒杀总量 orderEntityJsonObject.addProperty("restrictBuyingQuantity", orderEntity.getRestrictBuyingQuantity()); //限购数量 orderEntityJsonObject.addProperty("freeId", orderEntity.getFreeId()); //配送方式 : 到店自提,送货上门 orderEntityJsonObject.addProperty("freeName", orderEntity.getFreeName()); //配送方式 : 到店自提,送货上门 orderEntityJsonObject.addProperty("isRequiredAddress", orderEntity.isRequiredAddress()); //下单时检查收货地址,如果需要检查,则有可能填写了配送费,需要在页面上显示给客户看 orderEntityJsonObject.addProperty("transCosts", orderEntity.getTransCosts()); //送货上门时 配送费 orderEntityJsonObject.addProperty("description", orderEntity.getDescription()); //活动规则图片描述 orderEntityJsonObject.addProperty("images", orderEntity.getImages()); //活动规则图片描述 //取出导购上传的图片 JsonArray orderDetailImageJsonArray = new JsonArray(); if (orderEntity.getImagesUrl()!=null && !orderEntity.getImagesUrl().equals("")) { String images[] = orderEntity.getImagesUrl().split(";") ; for (int j = 0; j < images.length; j++) { orderDetailImageJsonArray.add(SettingKey.getUrl(hostUrl, images[j], dbid,null) ); } } orderEntityJsonObject.add("imagesUrl",orderDetailImageJsonArray) ; //活动规则图片描述 orderEntityJsonObject.addProperty("visiteTimes", orderEntity.getVisiteTimes()); //浏览人数 orderEntityJsonObject.addProperty("sharedTimes", orderEntity.getSharedTimes()); //转发次数 orderEntityJsonObject.addProperty("orderTimes", orderEntity.getOrderTimes()); //订单次数 orderEntityJsonObject.addProperty("orderDigit", orderEntity.getOrderDigit()); //订单数量 orderEntityJsonObject.addProperty("orderAmount", orderEntity.getOrderAmount()); //订单金额 orderEntityJsonObject.addProperty("panicBuyingStatus", orderEntity.getPanicBuyingStatus()); //活动状态: 0 未发布; 1 未开始; 2 进行中; 3 已停止; String statusName = "起草" ; switch (orderEntity.getPanicBuyingStatus()) { case 0: statusName = "起草" ; break; case 1: statusName = "未开始" ; break; case 2: statusName = "进行中" ; break; case 3: statusName = "已停止" ; break; default: break; } orderEntityJsonObject.addProperty("panicBuyingStatusName", statusName); //活动状态: 0 未发布; 1 未开始; 2 进行中; 3 已停止; String formName = "秒杀"; switch (orderEntity.getRefFormId().intValue()) { case 710802: formName = "秒杀"; break; case 710803: formName = "邀约"; break; case 710804: formName = "拼团"; break; default: break; } orderEntityJsonObject.addProperty("formName", formName); orderEntityJsonObject.addProperty("qrCode", orderEntity.getQrCode()); orderEntityJsonObject.addProperty("HeXiaoStatus",orderEntity.getHeXiaoStatus()); orderEntityJsonObject.addProperty("HeXiaoStatusName",orderEntity.getHeXiaoStatusName()); orderEntityJsonObject.addProperty("isShowHeXiaoButton",((openId).equals(orderEntity.getPanicBuyingOpenId()) || (openId).equals(orderEntity.getSellerOpenId()))&&orderEntity.getHeXiaoStatus()!=null&&orderEntity.getHeXiaoStatus().equals(0)); orderEntityJsonObject.addProperty("SellerId",orderEntity.getSellerId()); orderEntityJsonObject.addProperty("SellerName",orderEntity.getSellerName()); orderEntityJsonObject.addProperty("SellerTelephone",orderEntity.getSellerTelephone()); orderEntityJsonObject.addProperty("SellerTelephoneMask",StringReplaceUtil.getTelephoneMask(orderEntity.getSellerTelephone())); orderEntityJsonObject.addProperty("ReturnStatus",orderEntity.getReturnStatus()); orderEntityJsonObject.addProperty("ReturnStatusName",orderEntity.getReturnStatusName()); orderEntityJsonObject.addProperty("isShowReturnButton",((openId).equals(orderEntity.getPanicBuyingOpenId()) || (openId).equals(orderEntity.getSellerOpenId()))&orderEntity.getReturnStatus()!=null&&orderEntity.getReturnStatus().equals(0)); return orderEntityJsonObject ; } private String getDocStatusDescription(PanicBuyingOrderEntity orderEntity) { if (orderEntity.getDocStatus().equals(-200)) { return "已取消"; } if (orderEntity.getDocStatus().equals(0)) { //是否可支付 isAllowPayable 状态: 0.不能支付,1.可支付,2.已支付 ,3.正在退款,4.已退款,5.无需支付 if (orderEntity.getIsAllowPayable().equals(0)) { return "待卖家确认"; }else if (orderEntity.getIsAllowPayable().equals(1)) { return "待买家付款"; }else if (orderEntity.getIsAllowPayable().equals(2)) { return "待商家发货" ; // "买家已付款"; }else if (orderEntity.getIsAllowPayable().equals(3)) { return "正在退款"; }else if (orderEntity.getIsAllowPayable().equals(4)){ return "已退款"; }else if (orderEntity.getIsAllowPayable().equals(5)){ return "待商家发货"; } } if (orderEntity.getDocStatus().equals(100)) { if (orderEntity.getTuiHuoRuKuDigit() >= orderEntity.getDigit()) { return "已退款"; }if (orderEntity.getHeXiaoStatus().intValue() == 1) { return "已核销"; }else if (orderEntity.getIsAllowPayable().equals(2)) { return "买家已付款"; }else if (orderEntity.getIsAllowPayable().equals(3)) { return "正在退款"; }else if (orderEntity.getIsAllowPayable().equals(4)){ return "已退款"; }else if (orderEntity.getIsAllowPayable().equals(5)){ return "待商家发货"; } } return "交易完成" ; } /** * 修改单据状态,客户删单,单据对客户隐藏 * * @param request * @param response */ @RequestMapping(params = "m=deleteOrder") public void orderDelete(HttpServletRequest request, HttpServletResponse response) { HttpSession session = request.getSession(); JsonObject json = new JsonObject(); JsonObject errJson = new JsonObject(); String docCode=request.getParameter(SettingKey.DOCCODE)==null?"":request.getParameter(SettingKey.DOCCODE); String cltCode = (session.getAttribute(SettingKey.CLTCODE)==null?"":(String)session.getAttribute(SettingKey.CLTCODE)); String hostUrl = SettingKey.getHostUrl(request); // 将微信corpid组装成url String wxQueryString = SettingKey.getQueryStringByWx(request); boolean isMoblieBrowser = SettingKey.isMoblieBrowser(request); //String dbId = (String) session.getAttribute(SessionKey.SHOPPING_DBID); try { DataSourceEntity dataSourceEntity = MultiDataSource.getDataSourceMap( request) ; SpObserver.setDBtoInstance("_"+dataSourceEntity.getDbId());//切换数据源 Boolean issuccess = panicBuyingOrderIfc.deletePanicBuyingOrder(cltCode,docCode); if (!issuccess) { json.addProperty("success", "删除订单失败!"); json.addProperty("state", issuccess ? "success": "falied"); this.printJson(response, json.toString()); return; } json.addProperty("state", issuccess ? "success": "falied"); json.addProperty("success", "删除订单成功!"); String redirect = hostUrl + "/shopping/" + (isMoblieBrowser ? "mobile/" : "") + "account/order.jsp?"+ SettingKey.DOCCODE + "=" + docCode + "" + (wxQueryString == null || "".equals(wxQueryString) ? "" : "&" + wxQueryString); json.addProperty("redirect", redirect); this.printJson(response, json.toString()); return; } catch (DataAccessException e) { e.printStackTrace(); errJson.addProperty("warning", e.getCause() != null ?e.getCause().getMessage():e.getMessage()); json.add("error", errJson); this.printJson(response, json.toString()); return; } catch (Exception e) { e.printStackTrace(); errJson.addProperty("warning", e.getCause() != null ?e.getCause().getMessage():e.getMessage()); json.add("error", errJson); this.printJson(response, json.toString()); return; } finally { SpObserver.setDBtoInstance(); } } /** * 展示需核销订单的二维码 * @param request * @param response */ @RequestMapping(params = "m=getQrCode") public void getQrCode(HttpServletRequest request, HttpServletResponse response) { //HttpSession session = request.getSession(); JsonObject json = new JsonObject(); JsonObject errJson = new JsonObject(); String qrCode = request.getParameter("qrcode") ; if (qrCode == null||"".equals(qrCode)) { errJson.addProperty("warning","请传递 qrcode 参数【/shopping/panicBuyingOrder.do?m=getQrCode】"); json.add("error", errJson); this.printJson(response, json.toString()); return ; } try { DataSourceEntity dataSourceEntity = MultiDataSource.getDataSourceMap( request) ; SpObserver.setDBtoInstance("_" + dataSourceEntity.getDbId());// 切换数据源 SettingEntity settingEntity = settingIfc.getSettingEntity(); QrCodeForAppEntity qrCodeForAppEntity = matCodeIfc.getQrCodeEntity( qrCode) ; //获取存在的二维码 if (qrCodeForAppEntity == null) { errJson.addProperty("warning","该二维码不存在!" + qrCode ); json.add("error", errJson); this.printJson(response, json.toString()); return ; } String qrCodeImage = imgData.getImageUrl(qrCodeForAppEntity.getQrCodeUnid(), settingEntity.getImagePopupWidth(), settingEntity.getImagePopupHeight(), settingEntity.isShowPopupOrgImage(), settingEntity.isFromCached(), request); json.addProperty("qrCodeImage", qrCodeImage); json.addProperty("state", "success"); this.printJson(response, json.toString()); return; } catch (DataAccessException e) { e.printStackTrace(); errJson.addProperty("warning", (e.getCause()!=null?e.getCause().getMessage():e.getMessage())); json.add("error", errJson); this.printJson(response, json.toString()); return; }catch (Exception e ){ e.printStackTrace(); errJson.addProperty("warning",(e.getCause()!=null?e.getCause().getMessage():e.getMessage())); json.add("error", errJson); this.printJson(response, json.toString()); return; } finally { SpObserver.setDBtoInstance(); } } }