/**
*
* Copyright (c) behosoft Co.,Ltd.
* All Rights Reserved.
*
* This software is the confidential and proprietary information of behosoft.
* (Social Security Department). You shall not disclose such
* Confidential Information and shall use it only in accordance with
* the terms of the license agreement you entered into with behosoft.
*
* Distributable under GNU LGPL license by gnu.org
*/
package com.behosoft.lis.common.subscriber;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import com.behosoft.lis.crm.customer.service.CustomerService;
import com.behosoft.lis.model.bo.AccountConfig;
import com.behosoft.lis.model.bo.OmsPickInbound;
import com.behosoft.lis.model.bo.ShelvesTask;
import com.behosoft.lis.model.bo.SnInLocation;
import com.behosoft.lis.model.vo.CustomerVO;
import com.behosoft.lis.oms.pickinbound.service.OmsPickInboundService;
import com.behosoft.lis.sys.accountconfig.service.AccountConfigService;
import com.behosoft.lis.wms.shelves.service.ShelvesTaskService;
import com.behosoft.lis.wms.sninlocation.service.SnInLocationService;
import com.behosoft.util.HttpUtils;
import com.wondersgroup.saas.message.common.SerializableMessage;
import com.wondersgroup.saas.message.common.WrappedMessage;
import com.wondersgroup.saas.message.subscribe.AbstractDataSubscriber;
import com.wondersgroup.saas.util.JSONUtil;
/**
*
* Title: 百弘电商物流标准版_[]_[模块名]
*
*
* Description: [百胜采购订单反馈]
*
*
* @author chenxinglong
* @version 1.0 2019-01-31
* @author (lastest modification by )
* @since 1.0
*/
public class BsOmsPickInboundFeedbackSubscriber extends AbstractDataSubscriber {
private final Log logger = LogFactory.getLog(this.getClass());
@Autowired
private AccountConfigService accountConfigService;
@Autowired
private CustomerService customerService;
@Autowired
private ShelvesTaskService shelvesTaskService;
@Autowired
private SnInLocationService snInLocationService;
@Autowired
private OmsPickInboundService omsPickInboundService;
@Override
public void receive(WrappedMessage param) {
List serializables = ((SerializableMessage) param).getFilteredObjects();
Date date = (Date) serializables.get(0);
logger.info("enter BsOmsPickInboundFeedbackSubscriber get date is:" + date);
List accountConfigList = accountConfigService.getValueByKey("BS_CUSTOMER_CODE");
if (CollectionUtils.isEmpty(accountConfigList)) {
logger.info("百胜采购订单反馈,账户配置表(sys_account_config)未配置:BS_CUSTOMER_CODE");
return;
}
//查询JSBS货主
List customerList = customerService.findCustomerByCustomerCode(accountConfigList.get(0).getAttributeValue());
if (CollectionUtils.isEmpty(customerList)) {
logger.info("百胜采购订单反馈,未配置货主:江苏百胜");
return;
}
CustomerVO customer = customerList.get(0);
JSONObject jsonObject = accountConfigService.getMapByKey3("BS_OMSPICKINBOUND_FEEDBACK");
if(jsonObject != null){
if(!jsonObject.containsKey("URL") || jsonObject.get("URL") == null){
logger.info("百胜采购订单反馈,账户配置表(sys_account_config)未配置:URL");
return;
}
//获取百胜ERP服务端信息
String url = jsonObject.getString("URL");
//组装请求报文,并发送请求
packingRequestData(customer, url);
}
else{
logger.info("百胜采购订单反馈,账户配置表(sys_account_config)未配置:BS_OMSPICKINBOUND_FEEDBACK");
return;
}
logger.info("exit BsOmsPickInboundFeedbackSubscriber");
}
/**
* 组装请求报文
*/
private void packingRequestData(CustomerVO customer, String url){
Map params = new HashMap();
params.put("url", url);
//采购订单状态集合
List pickInboundStatusList = new ArrayList();
//入储单状态集合
List shelvesStatusList = new ArrayList();
//采购订单号集合
List pickInboundIdList = new ArrayList();
//已完成收货
pickInboundStatusList.add("4704");
//已入储
shelvesStatusList.add("6402");
//查询符合反馈条件的入储单
List shelvesTaskList =
shelvesTaskService.queryByPickInboundId(customer.getAccountId(), customer.getCustomerId(), pickInboundStatusList, shelvesStatusList);
if (CollectionUtils.isNotEmpty(shelvesTaskList)) {
//收集符合条件的采购订单号
for (ShelvesTask shelvesTask : shelvesTaskList) {
pickInboundIdList.add(shelvesTask.getPickInboundId());
}
//map:采购订单号
Map> inboundIdMap = new HashMap>();
//查询所有符合条件的sn入库记录
List allSnInLocationList = snInLocationService.queryByPickInboundId(pickInboundIdList);
if (CollectionUtils.isNotEmpty(allSnInLocationList)) {
//根据采购订单号分组
for (SnInLocation snInLocation : allSnInLocationList) {
if (!inboundIdMap.containsKey(snInLocation.getInboundId())) {
List temp = new ArrayList();
temp.add(snInLocation);
inboundIdMap.put(snInLocation.getInboundId(), temp);
}
else{
List temp = inboundIdMap.get(snInLocation.getInboundId());
temp.add(snInLocation);
inboundIdMap.put(snInLocation.getInboundId(), temp);
}
}
//map:商品id
Map> itemIdMap = new HashMap>();
for (String inboundId : inboundIdMap.keySet()) {
//同一采购订单下的sn入库记录
List snInLocationList = inboundIdMap.get(inboundId);
//根据商品id分组
for (SnInLocation snInLocation : snInLocationList) {
if (!itemIdMap.containsKey(snInLocation.getItemId())) {
List temp = new ArrayList();
temp.add(snInLocation);
itemIdMap.put(snInLocation.getItemId(), temp);
}
else{
List temp = itemIdMap.get(snInLocation.getItemId());
temp.add(snInLocation);
itemIdMap.put(snInLocation.getItemId(), temp);
}
}
//查询采购订单
OmsPickInbound omsPickInbound = omsPickInboundService.getEntityById(inboundId, OmsPickInbound.class);
//组装请求报文
if (null != omsPickInbound) {
JSONObject request = new JSONObject();
request.put("customerRefferenceId", omsPickInbound.getCustomerRefferenceId());
request.put("warehouseCode", omsPickInbound.getWarehouseName());
request.put("senderCode", omsPickInbound.getSenderCode());
JSONArray itemListJsonArray = new JSONArray();
//组装itemList节点报文
for (String itemId : itemIdMap.keySet()) {
List itemList = itemIdMap.get(itemId);
StringBuffer snListSb = new StringBuffer();
for (int i = 0; i < itemList.size(); i++) {
if (i == (itemList.size() - 1)) {
snListSb.append(itemList.get(i).getSn());
}
else{
snListSb.append(itemList.get(i).getSn()).append(",");
}
}
JSONObject itemJsonObject = new JSONObject();
itemJsonObject.put("itemCode", itemList.get(0).getItemCode());
itemJsonObject.put("snList", snListSb.toString());
itemListJsonArray.add(itemJsonObject);
}
request.put("itemList", itemListJsonArray);
params.put("requestData", request.toString());
}
//发送请求,并获取响应
Map result = HttpUtils.httpPost(params);
if (result.containsKey("response") && result.get("response") != null) {
JSONObject json = JSONObject.fromObject(result.get("response"), JSONUtil.getJsonConfig());
//如果响应成功,更新采购单是否反馈标识为是
if (json.containsKey("resultCode") && json.get("resultCode") != null && "0".equals(json.get("resultCode").toString())) {
omsPickInbound.setIsFeedback(1);
omsPickInboundService.updateEntity(omsPickInbound);
}
}
}
}
}
}
}