package com.yc.open.wms.controller; import com.alibaba.fastjson.JSON; import com.yc.entity.AttachmentConfig; import com.yc.entity.DataSourceEntity; import com.yc.exception.ApplicationException; import com.yc.exception.CallBackMessage; import com.yc.multiData.MultiDataSource; import com.yc.multiData.SpObserver; import com.yc.open.controller.BaseController; import com.yc.open.deli.service.PostDataBaseService; import com.yc.open.jinwu.entity.T110565Entity; import com.yc.open.mutual.entity.PushEntity; import com.yc.open.wms.entity.*; import com.yc.open.wms.schedule.BaseThread; import com.yc.open.wms.service.WMSServiceIfc; import com.yc.service.build.type.T_22_Ifc; import com.yc.service.panel.SqlDBHelperIfc; import com.yc.utils.SessionKey; import org.apache.commons.lang.RandomStringUtils; import org.apache.commons.lang3.StringUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.RestController; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.net.URLDecoder; import java.util.ArrayList; import java.util.List; import java.util.Map; /** * 与 WMS对接 */ @RestController public class WMSController extends BaseController { @Autowired T_22_Ifc t22ifc; @Autowired SqlDBHelperIfc mSqlDBHelperIfc; @Autowired WMSServiceIfc wmsServiceIfc; @Autowired PostDataBaseService mPostDataBaseService; @Autowired BaseController baseController; //=================接收模块=================== /** * 接收回写单据状态 * * @return */ @RequestMapping(value = "/open/jinwu/callBackfromWms.do") @ResponseBody public Object callBackfromWms(HttpServletRequest request, HttpServletResponse response) { //验证已在filter里面做,这里只需要处理业务逻辑 CallBackMessage callBackMessage = new CallBackMessage(); String data = null; CallBackDataEntity entity = new CallBackDataEntity(); try { String dbid = request.getAttribute(SessionKey.SHOPPING_DBID) + ""; SpObserver.setDBtoInstance("_" + dbid); data = URLDecoder.decode(request.getParameter("data"), "utf-8"); log.info("回写单据状态数据:" + data); entity = JSON.parseObject(data, CallBackDataEntity.class); wmsServiceIfc.doLogBy110565(new T110565Entity(entity.getType(), null, RandomStringUtils.randomAlphanumeric(6), "接收回写单据状态接口数据", data)); if (entity.getType() == null || entity.getType() == 0) { throw new ApplicationException("回写单据状态类型错误,正确取值范围【140204,140223,140104,140130】"); } wmsServiceIfc.doCallBackfromWms(entity); log.info("回写成功"); callBackMessage.sendSuccessMessageByDefault(); return callBackMessage.toJSONObject(); } catch (Exception e) { //写错误日志 e.printStackTrace(); this.sendErrorLog(entity.getType(), null, e); String errorMsg = (e.getCause() != null ? (e.getCause().getMessage()).trim() : e.getMessage()); callBackMessage.sendErrorMessage(errorMsg); return callBackMessage.toJSONObject(); } finally { SpObserver.setDBtoInstance(); } } /** * 接收采购收货单 * * @return */ @RequestMapping(value = "/open/jinwu/pull140101.do") @ResponseBody public Object pull140101(HttpServletRequest request, HttpServletResponse response) { //验证已在filter里面做,这里只需要处理业务逻辑 CallBackMessage callBackMessage = new CallBackMessage(); String data = null; List list = new ArrayList<>(); try { String dbid = request.getAttribute(SessionKey.SHOPPING_DBID) + ""; SpObserver.setDBtoInstance("_" + dbid); data = URLDecoder.decode(request.getParameter("data"), "utf-8"); log.info("采购收货单数据:" + data); wmsServiceIfc.doLogBy110565(new T110565Entity(140101, null, RandomStringUtils.randomAlphanumeric(6), "接收采购收货接口数据", data)); list = JSON.parseArray(data, T140101Entity.class); if (list.size() > 0) { wmsServiceIfc.do140101(list); callBackMessage.sendSuccessMessageByDefault(); return callBackMessage.toJSONObject(); } else { throw new ApplicationException("采购收货数据不能空-" + data); } } catch (Exception e) { //写错误日志 e.printStackTrace(); this.sendErrorLog(140101, null, e); String errorMsg = (e.getCause() != null ? (e.getCause().getMessage()).trim() : e.getMessage()); callBackMessage.sendErrorMessage(errorMsg); return callBackMessage.toJSONObject(); } finally { SpObserver.setDBtoInstance(); } } /** * 接收投诉件入库单 * * @return */ @RequestMapping(value = "/open/jinwu/pull140123.do") @ResponseBody public Object pull140123(HttpServletRequest request, HttpServletResponse response) { //验证已在filter里面做,这里只需要处理业务逻辑 CallBackMessage callBackMessage = new CallBackMessage(); String data = null; List list = new ArrayList<>(); try { String dbid = request.getAttribute(SessionKey.SHOPPING_DBID) + ""; SpObserver.setDBtoInstance("_" + dbid); data = URLDecoder.decode(request.getParameter("data"), "utf-8"); log.info("投诉件入库数据:" + data); wmsServiceIfc.doLogBy110565(new T110565Entity(140123, null, RandomStringUtils.randomAlphanumeric(6), "接收投诉件入库接口数据", data)); list = JSON.parseArray(data, T140123Entity.class); if (list.size() > 0) { wmsServiceIfc.do140123(list); callBackMessage.sendSuccessMessageByDefault(); return callBackMessage.toJSONObject(); } else { throw new ApplicationException("投诉件入库数据不能空-" + data); } } catch (Exception e) { //写错误日志 e.printStackTrace(); this.sendErrorLog(140123, null, e); String errorMsg = (e.getCause() != null ? (e.getCause().getMessage()).trim() : e.getMessage()); callBackMessage.sendErrorMessage(errorMsg); return callBackMessage.toJSONObject(); } finally { SpObserver.setDBtoInstance(); } } /** * 接收商品发货单 * * @return */ @RequestMapping(value = "/open/jinwu/pull140201.do") @ResponseBody public Object pull140201(HttpServletRequest request, HttpServletResponse response) { //验证已在filter里面做,这里只需要处理业务逻辑 CallBackMessage callBackMessage = new CallBackMessage(); String data = null; List list = new ArrayList<>(); try { String dbid = request.getAttribute(SessionKey.SHOPPING_DBID) + ""; SpObserver.setDBtoInstance("_" + dbid); data = URLDecoder.decode(request.getParameter("data"), "utf-8"); log.info("采购收货单数据:" + data); wmsServiceIfc.doLogBy110565(new T110565Entity(140201, null, RandomStringUtils.randomAlphanumeric(6), "接收商品发货单接口数据", data)); list = JSON.parseArray(data, T140201Entity.class); if (list.size() > 0) { wmsServiceIfc.do140201(list); callBackMessage.sendSuccessMessageByDefault(); return callBackMessage.toJSONObject(); } else { throw new ApplicationException("商品发货单数据不能空-" + data); } } catch (Exception e) { //写错误日志 e.printStackTrace(); this.sendErrorLog(140201, null, e); String errorMsg = (e.getCause() != null ? (e.getCause().getMessage()).trim() : e.getMessage()); callBackMessage.sendErrorMessage(errorMsg); return callBackMessage.toJSONObject(); } finally { SpObserver.setDBtoInstance(); } } //==========推送模块============================== /** * 推送120380=电商开单数据 * * @return */ @RequestMapping(value = "/jinwu/push120380.do") public void push120380(HttpServletRequest request, HttpServletResponse response) { String docCode = null;//单号 String formid = null;//功能号 CallBackMessage callBackMessage=new CallBackMessage(); try { Map map = null; DataSourceEntity dataSourceEntity = MultiDataSource.getDataSourceMap(request); try { SpObserver.setDBtoInstance("_" + dataSourceEntity.getDbId());//切换数据源 map = t22ifc.getParamMap(request); } catch (Exception e) { e.printStackTrace(); log.error(e.getCause() != null ? e.getCause().getMessage() : e.getMessage()); this.printJson(response, callBackMessage.sendErrorMessage(this.getErrorMsg(e))); return; } finally { SpObserver.setDBtoInstance(); } if (map != null) { docCode = map.get("doccode"); formid = map.get("formid"); } if (docCode == null) { if (log.isDebugEnabled()) { log.error("通过22类型传进来组装出来的map:" + map.toString()); } this.printJson(response, callBackMessage.sendErrorMessage("单号不能为空")); return; } if (formid == null) { if (log.isDebugEnabled()) { log.error("通过22类型传进来组装出来的map:" + map.toString()); } this.printJson(response, callBackMessage.sendErrorMessage("功能号不能为空")); return; } SpObserver.setDBtoInstance("_" + dataSourceEntity.getDbId()); PushEntity pushEntity = new PushEntity(); pushEntity.setFormId(Integer.parseInt(formid)); pushEntity.setType(5); pushEntity.setDocCode(docCode); pushEntity.setDbId(dataSourceEntity.getDbId() + ""); Sales120380XMLEntity salesData = wmsServiceIfc.getData120380(pushEntity); if (salesData != null) { final APIConfig apiConfig = BaseThread.getAPIConfig(dataSourceEntity.getDbId() + ""); salesData.getDeliveryOrder().setWarehouseCode(apiConfig.warehouseCode); String data = BaseThread.createXmlData(salesData); log.info(data); String url = apiConfig.PUSH_URL + baseController.getSignByWms(data, "SyncSalesOrderInfoJinWu", apiConfig.APP_KEY, apiConfig.SECRET, apiConfig.CUSTOMERID); final String msg = baseController.doPostByParameterNotNameByXml(url, data); log.info("msg:" + msg); if (StringUtils.isNotBlank(msg)) { ResponseEntity responseEntity = BaseThread.createDataByXml(msg); if ("success".equalsIgnoreCase(responseEntity.getFlag())) { //--更新状态 wmsServiceIfc.updateDocStatus(pushEntity, responseEntity.getDeliveryOrderId()); this.printJson(response, callBackMessage.toJSONStringByMemo("推送成功")); } else { log.info("" + responseEntity.getCode() + ":" + responseEntity.getMessage()); throw new ApplicationException(responseEntity.getCode() + "-" + responseEntity.getMessage()); } } else { throw new ApplicationException("接收返回结果数据为空"); } }else{ this.printJson(response, callBackMessage.toJSONStringByMemo("没有需要推送的数据:"+docCode)); } } catch (Exception e) { //写错误日志 e.printStackTrace(); this.sendErrorLog(Integer.parseInt(formid), docCode, e); String errorMsg = (e.getCause() != null ? (e.getCause().getMessage()).trim() : e.getMessage()); this.printJson(response, callBackMessage.sendErrorMessage(errorMsg)); } finally { SpObserver.setDBtoInstance(); } } /** * 推送开单出库单数据 * 120381=分销开单,120382=工程开单,120383=V服开单,120384=维修开单,120385=家装开单,120386=橱柜开单,120301=销售开单,140206=调拨出库 ,140223=投诉件出库,140240=不良品出库,140204=采购退货出库单 * * @return */ @RequestMapping(value = "/jinwu/pushSale.do") public void pushSale(HttpServletRequest request, HttpServletResponse response) { String docCode = null;//单号 String formid = null;//功能号 CallBackMessage callBackMessage=new CallBackMessage(); try { Map map = null; DataSourceEntity dataSourceEntity = MultiDataSource.getDataSourceMap(request); try { SpObserver.setDBtoInstance("_" + dataSourceEntity.getDbId());//切换数据源 map = t22ifc.getParamMap(request); } catch (Exception e) { e.printStackTrace(); log.error(e.getCause() != null ? e.getCause().getMessage() : e.getMessage()); this.printJson(response, callBackMessage.sendErrorMessage(this.getErrorMsg(e))); return; } finally { SpObserver.setDBtoInstance(); } if (map != null) { docCode = map.get("doccode"); formid = map.get("formid"); } if (docCode == null) { if (log.isDebugEnabled()) { log.error("通过22类型传进来组装出来的map:" + map.toString()); } this.printJson(response, callBackMessage.sendErrorMessage("单号不能为空")); return; } if (formid == null) { if (log.isDebugEnabled()) { log.error("通过22类型传进来组装出来的map:" + map.toString()); } this.printJson(response, callBackMessage.sendErrorMessage("功能号不能为空")); return; } if ("120380".equals(formid)) { this.printJson(response, callBackMessage.sendErrorMessage("120380功能号不能调用本接口进行推送")); return; } SpObserver.setDBtoInstance("_" + dataSourceEntity.getDbId()); PushEntity pushEntity = new PushEntity(); pushEntity.setFormId(Integer.parseInt(formid)); pushEntity.setType(5); pushEntity.setDocCode(docCode); pushEntity.setDbId(dataSourceEntity.getDbId() + ""); SalesXMLEntity salesData = null; switch (pushEntity.getFormId()) { case 140206: salesData = wmsServiceIfc.getData140206(pushEntity); break; case 140223: case 140240: salesData = wmsServiceIfc.getData140223And140240(pushEntity); break; case 140204: salesData = wmsServiceIfc.getData140204(pushEntity); break; default: salesData = wmsServiceIfc.getSalesData(pushEntity); break; } if (salesData != null) { final APIConfig apiConfig = BaseThread.getAPIConfig(dataSourceEntity.getDbId() + ""); salesData.getDeliveryOrder().setWarehouseCode(apiConfig.warehouseCode); String data = BaseThread.createXmlData(salesData); log.info(data); String url = apiConfig.PUSH_URL + baseController.getSignByWms(data, "stockout.create", apiConfig.APP_KEY, apiConfig.SECRET, apiConfig.CUSTOMERID); final String msg = baseController.doPostByParameterNotNameByXml(url, data); log.info("msg:" + msg); if (StringUtils.isNotBlank(msg)) { ResponseEntity responseEntity = BaseThread.createDataByXml(msg); if ("success".equalsIgnoreCase(responseEntity.getFlag())) { //--更新状态 wmsServiceIfc.updateDocStatus(pushEntity, responseEntity.getDeliveryOrderId()); this.printJson(response, callBackMessage.toJSONStringByMemo("推送成功")); } else { log.info("" + responseEntity.getCode() + ":" + responseEntity.getMessage()); throw new ApplicationException(responseEntity.getCode() + "-" + responseEntity.getMessage()); } } else { throw new ApplicationException("接收返回结果数据为空"); } }else{ this.printJson(response, callBackMessage.toJSONStringByMemo("没有需要推送的数据:"+docCode)); } } catch (Exception e) { //写错误日志 e.printStackTrace(); this.sendErrorLog(Integer.parseInt(formid), docCode, e); String errorMsg = (e.getCause() != null ? (e.getCause().getMessage()).trim() : e.getMessage()); this.printJson(response, callBackMessage.sendErrorMessage(errorMsg)); } finally { SpObserver.setDBtoInstance(); } } /** * 推送入库单数据 * 130301=采购订单,140315=投诉申请单,140130=不良品入库,140107=调拨入库,140104=销售退货入库 * * @return */ @RequestMapping(value = "/jinwu/pushOrder.do") public void pushorder(HttpServletRequest request, HttpServletResponse response) { String docCode = null;//单号 String formid = null;//功能号 CallBackMessage callBackMessage=new CallBackMessage(); try { Map map = null; DataSourceEntity dataSourceEntity = MultiDataSource.getDataSourceMap(request); try { SpObserver.setDBtoInstance("_" + dataSourceEntity.getDbId());//切换数据源 map = t22ifc.getParamMap(request); } catch (Exception e) { e.printStackTrace(); log.error(e.getCause() != null ? e.getCause().getMessage() : e.getMessage()); this.printJson(response, callBackMessage.sendErrorMessage(this.getErrorMsg(e))); return; } finally { SpObserver.setDBtoInstance(); } if (map != null) { docCode = map.get("doccode"); formid = map.get("formid"); } if (docCode == null) { if (log.isDebugEnabled()) { log.error("通过22类型传进来组装出来的map:" + map.toString()); } this.printJson(response, callBackMessage.sendErrorMessage("单号不能为空")); return; } SpObserver.setDBtoInstance("_" + dataSourceEntity.getDbId()); PushEntity pushEntity = new PushEntity(); pushEntity.setFormId(Integer.parseInt(formid)); pushEntity.setType(5); pushEntity.setDocCode(docCode); pushEntity.setDbId(dataSourceEntity.getDbId() + ""); Order130301XMLEntity salesData = null; if (pushEntity.getFormId() == 130301) { salesData = wmsServiceIfc.getData130301(pushEntity); } else if (pushEntity.getFormId() == 140104) { salesData = wmsServiceIfc.getData140104(pushEntity); } else if (pushEntity.getFormId() == 140107) { salesData = wmsServiceIfc.getData140107(pushEntity); } else { salesData = wmsServiceIfc.getData140315And140130(pushEntity); } if (salesData != null) { final APIConfig apiConfig = BaseThread.getAPIConfig(dataSourceEntity.getDbId() + ""); salesData.getEntryOrder().setWarehouseCode(apiConfig.warehouseCode); String data = BaseThread.createXmlData(salesData); log.info(data); String url = apiConfig.PUSH_URL + baseController.getSignByWms(data, "entryorder.create", apiConfig.APP_KEY, apiConfig.SECRET, apiConfig.CUSTOMERID); final String msg = baseController.doPostByParameterNotNameByXml(url, data); log.info("msg:" + msg); if (StringUtils.isNotBlank(msg)) { ResponseEntity responseEntity = BaseThread.createDataByXml(msg); if ("success".equalsIgnoreCase(responseEntity.getFlag())) { //--更新状态 wmsServiceIfc.updateDocStatus(pushEntity, responseEntity.getEntryOrderId()); this.printJson(response, callBackMessage.toJSONStringByMemo("推送成功")); } else { log.info("" + responseEntity.getCode() + ":" + responseEntity.getMessage()); throw new ApplicationException(responseEntity.getCode() + "-" + responseEntity.getMessage()); } } else { throw new ApplicationException("接收返回结果数据为空"); } }else{ this.printJson(response, callBackMessage.toJSONStringByMemo("没有需要推送的数据:"+docCode)); } } catch (Exception e) { //写错误日志 e.printStackTrace(); this.sendErrorLog(Integer.parseInt(formid), docCode, e); String errorMsg = (e.getCause() != null ? (e.getCause().getMessage()).trim() : e.getMessage()); this.printJson(response, callBackMessage.sendErrorMessage(errorMsg)); } finally { SpObserver.setDBtoInstance(); } } /** * 推送物料主数据 * * @return */ @RequestMapping(value = "/jinwu/push110514.do") public void push110514(HttpServletRequest request, HttpServletResponse response) { String docCode = null;//单号 CallBackMessage callBackMessage=new CallBackMessage(); try { Map map = null; DataSourceEntity dataSourceEntity = MultiDataSource.getDataSourceMap(request); try { SpObserver.setDBtoInstance("_" + dataSourceEntity.getDbId());//切换数据源 map = t22ifc.getParamMap(request); } catch (Exception e) { e.printStackTrace(); log.error(e.getCause() != null ? e.getCause().getMessage() : e.getMessage()); this.printJson(response, this.getErrorMsg(e)); return; } finally { SpObserver.setDBtoInstance(); } if (map != null) { docCode = map.get("doccode"); } if (StringUtils.isBlank(docCode)) { if (log.isDebugEnabled()) { log.error("通过22类型传进来组装出来的map:" + map.toString()); } this.printJson(response, callBackMessage.sendErrorMessage("单号不能为空")); return; } SpObserver.setDBtoInstance("_" + dataSourceEntity.getDbId()); List list = wmsServiceIfc.get110503(docCode); for (MatCodeEntity entity : list) { if (entity != null) { final APIConfig apiConfig = BaseThread.getAPIConfig(dataSourceEntity.getDbId() + ""); MatCodeXmlEntity matCodeXmlEntity = new MatCodeXmlEntity(); matCodeXmlEntity.setItem(entity); String data = BaseThread.createXmlData(matCodeXmlEntity); log.info(data); String url = apiConfig.PUSH_URL + getSignByWms(data, "singleitem.synchronize", apiConfig.APP_KEY, apiConfig.SECRET, apiConfig.CUSTOMERID); final String msg = doPostByParameterNotNameByXml(url, data); log.info("msg:" + msg); if (StringUtils.isNotBlank(msg)) { ResponseEntity responseEntity = BaseThread.createDataByXml(msg); if ("success".equalsIgnoreCase(responseEntity.getFlag())) { //--更新状态 PushEntity pushEntity = new PushEntity(); pushEntity.setDocCode(entity.getDocCode()); wmsServiceIfc.updateDocStatus110514(pushEntity); } else { log.info("" + responseEntity.getCode() + ":" + responseEntity.getMessage()); throw new ApplicationException(responseEntity.getCode() + "-" + responseEntity.getMessage()); } } else { throw new ApplicationException("接收返回结果数据为空"); } } } if (list != null && list.size() > 0) { this.printJson(response, callBackMessage.toJSONStringByMemo("推送成功")); } } catch (Exception e) { //写错误日志 e.printStackTrace(); this.sendErrorLog(110514, docCode, e); String errorMsg = (e.getCause() != null ? (e.getCause().getMessage()).trim() : e.getMessage()); this.printJson(response, callBackMessage.sendErrorMessage(errorMsg)); } finally { SpObserver.setDBtoInstance(); } } /** * 推送取消确认 */ @RequestMapping(value = "/jinwu/pushCancel.do") public void proccessCancelProc(String dbid, int formid, String docCode, HttpServletResponse response) throws Exception { try { String isWmsRunSchedule = AttachmentConfig.get("isWmsRunSchedule");//只在设置了定时任务的服务器上运行 if ("0".equals(isWmsRunSchedule)) { //不执行自动推送 return; } final APIConfig apiConfig = BaseThread.getAPIConfig(dbid); //指定的功能号才能执行 if (apiConfig.CANEL_TOTAL_LIST.contains("," + formid + ",")) { SpObserver.setDBtoInstance("_" + dbid); PushEntity pushEntity = new PushEntity(); pushEntity.setDocCode(docCode); pushEntity.setFormId(formid); final CancelXMLEntity cancelXMLEntity = wmsServiceIfc.cancelToWms(pushEntity); //不需要执行wms接口 if (cancelXMLEntity == null) { return; } String data = BaseThread.createXmlData(cancelXMLEntity); String url = apiConfig.PUSH_URL + getSignByWms(data, "order.cancel", apiConfig.APP_KEY, apiConfig.SECRET, apiConfig.CUSTOMERID); final String msg = doPostByParameterNotNameByXml(url, data); log.info("msg:" + msg); if (StringUtils.isNotBlank(msg)) { ResponseEntity responseEntity = BaseThread.createDataByXml(msg); if ("success".equalsIgnoreCase(responseEntity.getFlag())) { //--更新状态,清空之前推送成功的信息,还原成可以继续推送 wmsServiceIfc.updateDocStatusByCancel(pushEntity); //this.printJson(response, "取消确认成功"); } else { throw new ApplicationException(responseEntity.getCode() + ":" + responseEntity.getMessage()); } } else { throw new ApplicationException("接收返回结果数据为空"); } } } finally { SpObserver.setDBtoInstance(); } } }