package abacus.springboot.example.esb;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSONObject;

import abacus.commons.context.interceptor.UserContext;
import abacus.commons.exception.BusinessException;
import abacus.config.ext.AbacusExtConfig;
import abacus.springboot.example.dao.JcBill;
import abacus.springboot.example.dao.JcBillHg;
import abacus.springboot.example.dao.JcBillInItem;
import abacus.springboot.example.dao.JcBillPhoto;
import abacus.springboot.example.dao.JcBillSourceItem;
import abacus.springboot.example.dao.JcSourceItem;
import abacus.springboot.example.pi.JcBillAccessIF;
import abacus.springboot.example.repository.JcBillHgRepository;
import abacus.springboot.example.repository.JcBillInItemRepository;
import abacus.springboot.example.repository.JcBillPhotoRepository;
import abacus.springboot.example.repository.JcBillRepository;
import abacus.springboot.example.repository.JcBillSourceItemRepository;
import abacus.springboot.example.repository.JcSourceItemRepository;
import abacus.springboot.example.util.RequestUtil;
import abacus.springboot.example.vo.PMSConfigKey;
import abacus.springboot.example.vo.SiDepositlocation;
import abacus.springboot.example.wsi.JcBillServiceIF;

@Repository
public class JcBillService implements JcBillServiceIF{
	
	private static final Logger LOG = LoggerFactory.getLogger(JcBillService.class);
	
	@Autowired
	private JcBillAccessIF jcBillAccess;
	
	@Autowired
	private JcBillRepository jcBillRepository;
	
	@Autowired
	private JcBillSourceItemRepository jcBillSourceItemRepository;
	
	@Autowired
	private JcBillHgRepository jcBillHgRepository;
	
	@Autowired
	private JcSourceItemRepository jcSourceItemRepository;
	
	@Autowired
	private JcBillInItemRepository jcBillInItemRepository;
	
	@Autowired
	private JcBillPhotoRepository jcBillPhotoRepository;
	
	@Autowired
	private AbacusExtConfig abacusExtConfig;
	
	@Override
	public Page<JcBill> queryJcBill(String fromDate, String thruDate, String id, String applicant, String goods,
			String billin, String goodsid, String product, String status, int typ, Pageable pageable) throws RuntimeException {
		try {
			return this.jcBillAccess.queryJcBill(fromDate, thruDate, applicant, goods, id, billin, goodsid, product, status, typ, pageable);
		} catch (RuntimeException e) {
			e.printStackTrace();
			LOG.error("服务异常", e.getMessage());
			throw new RuntimeException("服务异常：", e);
		}
	}

	@Override
	public JcBill queryJcBillById(String billId) throws RuntimeException {
		try {
			Optional<JcBill> optional = jcBillRepository.findById(billId);
			
			return optional.isPresent() ? optional.get() : null;
		} catch (RuntimeException e) {
			e.printStackTrace();
			LOG.error("服务异常", e.getMessage());
			throw new RuntimeException("服务异常：", e);
		}
	}

	@Override
	public List<JcBillSourceItem> queryJcBillSourceItemByBillId(String billId) throws RuntimeException {
		try {
			List<JcBillSourceItem> list = jcBillSourceItemRepository.findByBillid(billId);
			
			return (list != null && list.size() >= 1) ? list : null;
		} catch (RuntimeException e) {
			e.printStackTrace();
			LOG.error("服务异常", e.getMessage());
			throw new RuntimeException("服务异常：", e);
		}
	}

	@Override
	public JcBillHg queryJcBillHg(String billHg) throws RuntimeException {
		try {
			Optional<JcBillHg> optional = jcBillHgRepository.findById(billHg);
			
			return optional.isPresent() ? optional.get() : null;
		} catch (RuntimeException e) {
			e.printStackTrace();
			LOG.error("服务异常", e.getMessage());
			throw new RuntimeException("服务异常：", e);
		}
	}

	@Override
	public List<JcSourceItem> queryJcSourceItemByBillId(String billId) throws RuntimeException {
		try {
			return jcBillAccess.queryJcSourceItemByBillId(billId);
		} catch (RuntimeException e) {
			e.printStackTrace();
			LOG.error("服务异常", e.getMessage());
			throw new RuntimeException("服务异常：", e);
		}
	}

	@Override
	public Map<String, List<JcBillPhoto>> queryJcBillPhoto(String srcid) throws RuntimeException {
		try {
			List<JcBillPhoto> billPhotos = jcBillAccess.queryJcBillPhoto(srcid, null, null);
			
			if(billPhotos == null) return new HashMap<String, List<JcBillPhoto>>();
			
			Map<String, List<JcBillPhoto>> map = billPhotos.stream()
					.collect(Collectors.groupingBy(e -> {
						return e.getTypephoto() + "#" + e.getSrcitemid();
					}, 
					Collectors.collectingAndThen(Collectors.toList(), value -> value)
				));
			
			return map;
		} catch (RuntimeException e) {
			e.printStackTrace();
			LOG.error("服务异常", e.getMessage());
			throw new RuntimeException("服务异常：", e);
		}
	}

	@Override
	public List<JcSourceItem> queryJcSourceItemByBillId(String billId, String srcitemid) throws RuntimeException {
		try {
			return jcSourceItemRepository.findByBillidAndSrcitemid(billId, srcitemid);
		} catch (RuntimeException e) {
			e.printStackTrace();
			LOG.error("服务异常", e.getMessage());
			throw new RuntimeException("服务异常：", e);
		}
	}

	@Override
	public JcBillSourceItem queryJcBillSourceItemById(Long id) throws RuntimeException {
		try {
			Optional<JcBillSourceItem> optional = jcBillSourceItemRepository.findById(id);
			
			return optional.isPresent() ? optional.get() : null;
		} catch (RuntimeException e) {
			e.printStackTrace();
			LOG.error("服务异常", e.getMessage());
			throw new RuntimeException("服务异常：", e);
		}
	}

	@Override
	public JcSourceItem queryJcSourceItemById(Long id) throws RuntimeException {
		try {
			Optional<JcSourceItem> optional = jcSourceItemRepository.findById(id);
			
			return optional.isPresent() ? optional.get() : null;
		} catch (RuntimeException e) {
			e.printStackTrace();
			LOG.error("服务异常", e.getMessage());
			throw new RuntimeException("服务异常：", e);
		}
	}

	@Transactional(isolation = Isolation.DEFAULT, propagation = Propagation.REQUIRED, rollbackFor = {Exception.class})
	@Override
	public void saveQuerenBillSourceItem(JcBillSourceItem billItem, JcSourceItem sourceItem) throws RuntimeException {
		try {
			
			jcBillSourceItemRepository.save(billItem);
			
			jcSourceItemRepository.save(sourceItem);
			
		} catch (RuntimeException e) {
			e.printStackTrace();
			LOG.error("服务异常", e.getMessage());
			throw new RuntimeException("服务异常：", e);
		}
	}

	@Transactional(isolation = Isolation.DEFAULT, propagation = Propagation.REQUIRED, rollbackFor = {Exception.class})
	@Override
	public void saveRejectJcSourceItems(JcBillSourceItem billItem, JcBill jcBill) throws RuntimeException {
		try {
			
			jcBillSourceItemRepository.save(billItem);
			
			jcBillRepository.save(jcBill);
			
		} catch (RuntimeException e) {
			e.printStackTrace();
			LOG.error("服务异常", e.getMessage());
			throw new RuntimeException("服务异常：", e);
		}
	}

	@Transactional(isolation = Isolation.DEFAULT, propagation = Propagation.REQUIRED, rollbackFor = {Exception.class})
	@Override
	public void saveCompleteJcBill(List<JcBillInItem> inItems, JcBill jcBill) throws RuntimeException {
		try {
			
			jcBillInItemRepository.saveAll(inItems);
			
			jcBillRepository.save(jcBill);
			
		} catch (RuntimeException e) {
			e.printStackTrace();
			LOG.error("服务异常", e.getMessage());
			throw new RuntimeException("服务异常：", e);
		}
	}

	@Transactional(isolation = Isolation.DEFAULT, propagation = Propagation.REQUIRED, rollbackFor = {Exception.class})
	@Override
	public void updateWarehouse(JcBill jcBill) throws RuntimeException {
		try {
			
			jcBillRepository.save(jcBill);
			
		} catch (RuntimeException e) {
			e.printStackTrace();
			LOG.error("服务异常", e.getMessage());
			throw new RuntimeException("服务异常：", e);
		}
	}

	@Override
	public List<JcBillInItem> queryJcBillInItemByBillId(String billId, int status) throws RuntimeException {
		try {
			List<JcBillInItem> list = jcBillInItemRepository.findByBillidAndStatus(billId, status);
			
			return (list != null && list.size() >= 1) ? list : null;
		} catch (RuntimeException e) {
			e.printStackTrace();
			LOG.error("服务异常", e.getMessage());
			throw new RuntimeException("服务异常：", e);
		}
	}
	
	@Override
	public List<JcBillInItem> queryJcBillInItemByBillId(String billId) throws RuntimeException {
		try {
			List<JcBillInItem> list = jcBillInItemRepository.findByBillid(billId);
			
			return (list != null && list.size() >= 1) ? list : null;
		} catch (RuntimeException e) {
			e.printStackTrace();
			LOG.error("服务异常", e.getMessage());
			throw new RuntimeException("服务异常：", e);
		}
	}

	@Transactional(isolation = Isolation.DEFAULT, propagation = Propagation.REQUIRED, rollbackFor = {Exception.class})
	@Override
	public void createStockIn(JcBill jcBill) throws RuntimeException {
		try {
			
			jcBillRepository.save(jcBill);
			
			jcBillAccess.saveEvent(jcBill.getId(), "稽查收货", "其他入库");
			
		} catch (RuntimeException e) {
			e.printStackTrace();
			LOG.error("服务异常", e.getMessage());
			throw new RuntimeException("服务异常：", e);
		}
	}

	@Override
	public JcBillInItem queryJcBillInItemById(Long id) {
		try {
			Optional<JcBillInItem> optional = jcBillInItemRepository.findById(id);
			
			return optional.isPresent() ? optional.get() : null;
		} catch (RuntimeException e) {
			e.printStackTrace();
			LOG.error("服务异常", e.getMessage());
			throw new RuntimeException("服务异常：", e);
		}
	}

	@Transactional(isolation = Isolation.DEFAULT, propagation = Propagation.REQUIRED, rollbackFor = {Exception.class})
	@Override
	public void saveTerminateHgInItem(JcBillInItem inItem) throws RuntimeException {
		try {
		
			jcBillInItemRepository.save(inItem);
			
		} catch (RuntimeException e) {
			e.printStackTrace();
			LOG.error("服务异常", e.getMessage());
			throw new RuntimeException("服务异常：", e);
		}
	}

	@Override
	public List<JcBillInItem> findAllById(List<Long> ids) throws RuntimeException {
		try {
			return jcBillInItemRepository.findAllById(ids);
		} catch (RuntimeException e) {
			e.printStackTrace();
			LOG.error("服务异常", e.getMessage());
			throw new RuntimeException("服务异常：", e);
		}
	}
	
	@Transactional(isolation = Isolation.DEFAULT, propagation = Propagation.REQUIRED, rollbackFor = {Exception.class})
	@Override
	public void saveInformHg(List<JcBillInItem> inItems, JcBill jcBill, JcBillHg jcBillHg, String photoUrl) throws RuntimeException {
		try {
			
			JcBillPhoto photo = new JcBillPhoto();
			photo.setSrcid(jcBillHg.getId());
			photo.setSrcitemid(jcBillHg.getId());
			photo.setTypephoto(JcBillPhoto.HG_TZH);
			photo.setUrlphoto(photoUrl);
			
			jcBillPhotoRepository.save(photo);
			
			jcBillInItemRepository.saveAll(inItems);
			
			jcBillRepository.save(jcBill);
			
			jcBillHgRepository.save(jcBillHg);
			
			jcBillAccess.saveEvent(jcBillHg.getId(), "回购订单", "回购通知");
			
		} catch (RuntimeException e) {
			e.printStackTrace();
			LOG.error("服务异常", e.getMessage());
			throw new RuntimeException("服务异常：", e);
		}
	}

	@Override
	public Page<JcBillHg> queryJcBillHg(String fromDate, String thruDate, String account, String salearea, String id,
			String billId, String status, String product, String manager, int typ, Pageable pageable)
			throws RuntimeException {
		try {
			
			return jcBillAccess.queryJcBillHg(fromDate, thruDate, account, salearea, id, billId, status, product, manager, typ, pageable);
		} catch (RuntimeException e) {
			e.printStackTrace();
			LOG.error("服务异常", e.getMessage());
			throw new RuntimeException("服务异常：", e);
		}
	}

	@Override
	public List<JcBillInItem> queryJcBillInItemByBillHg(String billHgId) throws RuntimeException {
		try {
			List<JcBillInItem> list = jcBillInItemRepository.findByBillhg(billHgId);
			
			return (list != null && list.size() >= 1) ? list : null;
		} catch (RuntimeException e) {
			e.printStackTrace();
			LOG.error("服务异常", e.getMessage());
			throw new RuntimeException("服务异常：", e);
		}
	}

	@Transactional(isolation = Isolation.DEFAULT, propagation = Propagation.REQUIRED, rollbackFor = {Exception.class})
	@Override
	public void saveRejectBillHg(int status, int typ, JcBill jcBill, JcBillHg billHg, List<JcBillInItem> inItems) throws RuntimeException {
		try {
			if(JcBillHg.STATUS_ONE == status) {
				
				jcBillRepository.save(jcBill);
				jcBillHgRepository.save(billHg);
				jcBillInItemRepository.saveAll(inItems);
				
				
			} else if(JcBillHg.STATUS_TWO == status) {
				
				jcBillHgRepository.save(billHg);
				
				
			} else if(JcBillHg.STATUS_THREE == status || JcBillHg.STATUS_FOUR == status) {
				
				jcBillHgRepository.save(billHg);
				
			} else if (JcBillHg.STATUS_FIVE == status){
				
				// 删除对应的事件
				jcBillAccess.deleteEvent(billHg.getId(), "回购分仓", "回购分仓");
			} else {
				throw new BusinessException("未知状态不能驳回！");
			}
		} catch (RuntimeException e) {
			e.printStackTrace();
			LOG.error("服务异常", e.getMessage());
			throw new RuntimeException("服务异常：", e);
		}
	}

	@Transactional(isolation = Isolation.DEFAULT, propagation = Propagation.REQUIRED, rollbackFor = {Exception.class})
	@Override
	public void saveQuerenSk(JcBillHg billHg, boolean fcFlag) throws RuntimeException {
		try {
			billHg.setAudit(UserContext.getUserId());
			billHg.setAuditname(UserContext.getUserName());
			billHg.setAuditdt(new Date());
			jcBillHgRepository.save(billHg);
			
			if(fcFlag) {
				
				jcBillAccess.saveEvent(billHg.getId(), "回购分仓", "回购分仓");
				
			}
		} catch (RuntimeException e) {
			e.printStackTrace();
			LOG.error("服务异常", e.getMessage());
			throw new RuntimeException("服务异常：", e);
		}
	}

	@Transactional(isolation = Isolation.DEFAULT, propagation = Propagation.REQUIRED, rollbackFor = {Exception.class})
	@Override
	public void savecreateFc(JcBillHg billHg) throws RuntimeException {
		try {
			
			jcBillHgRepository.save(billHg);
			
			jcBillAccess.saveEvent(billHg.getId(), "回购分仓", "回购分仓");
			
		} catch (RuntimeException e) {
			e.printStackTrace();
			LOG.error("服务异常", e.getMessage());
			throw new RuntimeException("服务异常：", e);
		}
	}

	@Transactional(isolation = Isolation.DEFAULT, propagation = Propagation.REQUIRED, rollbackFor = {Exception.class})
	@Override
	public void updateFcWarehouse(String oldJsonStr, JcBillHg billHg) throws RuntimeException {
		try {
			
			jcBillHgRepository.save(billHg);
			
		} catch (RuntimeException e) {
			e.printStackTrace();
			LOG.error("服务异常", e.getMessage());
			throw new RuntimeException("服务异常：", e);
		}
	}

	@Transactional(isolation = Isolation.DEFAULT, propagation = Propagation.REQUIRED, rollbackFor = {Exception.class})
	@Override
	public JcSourceItem saveJcSourceItem(JcSourceItem item) throws RuntimeException {
		try {
			
			JcSourceItem tempItem = jcSourceItemRepository.save(item);
			
			
			return tempItem;
		} catch (RuntimeException e) {
			e.printStackTrace();
			LOG.error("服务异常", e.getMessage());
			throw new RuntimeException("服务异常：", e);
		}
	}

	@Transactional(isolation = Isolation.DEFAULT, propagation = Propagation.REQUIRED, rollbackFor = {Exception.class})
	@Override
	public void deleteJcSourceItem(List<Long> ids) throws RuntimeException {
		try {
			
			jcBillAccess.deleteJcSourceItem(ids);
			
//			/*操作日志*/
//			OperationLog log = new OperationLog("jc_bill", item.getBillid(), "删除溯源确认信息", "", "", JSON.toJSONString(item));
//			log.setOperator(UserContext.getUserId());
//    		log.setOperatorName(UserContext.getUserName());
//			BusinessESUtil.checkService(this.operationLogService).saveOperationLog(log);
			
		} catch (RuntimeException e) {
			e.printStackTrace();
			LOG.error("服务异常", e.getMessage());
			throw new RuntimeException("服务异常：", e);
		}
	}

	@Transactional(isolation = Isolation.DEFAULT, propagation = Propagation.REQUIRED, rollbackFor = {Exception.class})
	@Override
	public void updateHgprice(JcBillInItem inItem) throws RuntimeException {
		try {
			jcBillInItemRepository.save(inItem);
			
		} catch (RuntimeException e) {
			e.printStackTrace();
			LOG.error("服务异常", e.getMessage());
			throw new RuntimeException("服务异常：", e);
		}
	}

	@Transactional(isolation = Isolation.DEFAULT, propagation = Propagation.REQUIRED, rollbackFor = {Exception.class})
	@Override
	public void updateRemark(String oldJsonStr, JcBillHg billHg) throws RuntimeException {
		try {
			
			jcBillHgRepository.save(billHg);
			
		} catch (RuntimeException e) {
			e.printStackTrace();
			LOG.error("服务异常", e.getMessage());
			throw new RuntimeException("服务异常：", e);
		}
	}
	
	@Transactional(isolation = Isolation.DEFAULT, propagation = Propagation.REQUIRED, rollbackFor = {Exception.class})
	@Override
	public void updatePac(String oldJsonStr, JcBillHg billHg) throws RuntimeException {
		try {
			
			jcBillHgRepository.save(billHg);
			
		} catch (RuntimeException e) {
			e.printStackTrace();
			LOG.error("服务异常", e.getMessage());
			throw new RuntimeException("服务异常：", e);
		}
	}

	@Transactional(isolation = Isolation.DEFAULT, propagation = Propagation.REQUIRED, rollbackFor = {Exception.class})
	@Override
	public void updateOperatorname(JcBillInItem inItem, String oldStr) throws RuntimeException {
		try {
			jcBillInItemRepository.save(inItem);
			
		} catch (RuntimeException e) {
			e.printStackTrace();
			LOG.error("服务异常", e.getMessage());
			throw new RuntimeException("服务异常：", e);
		}
	}

	@Override
	public void saveJcBillInItem(List<JcBillInItem> items) throws RuntimeException {
		jcBillInItemRepository.saveAll(items);
	}

	@Override
	public List<JcBillSourceItem> queryJcBillSourceItemInBillId(List<String> billIds) throws RuntimeException {
		try {
			return jcBillAccess.queryJcBillSourceItemInBillId(billIds);
		} catch (RuntimeException e) {
			e.printStackTrace();
			LOG.error("服务异常", e.getMessage());
			throw new RuntimeException("服务异常：", e);
		}
	}

	@Transactional(isolation = Isolation.DEFAULT, propagation = Propagation.REQUIRED, rollbackFor = {Exception.class})
	@Override
	public void saveRejectBill(int status, JcBill jcBill, List<JcBillInItem> inItems, List<JcBillSourceItem> sourceItems) throws RuntimeException {
		try {
				
			jcBillRepository.save(jcBill);
			jcBillSourceItemRepository.saveAll(sourceItems);
			if(inItems != null) jcBillInItemRepository.deleteAll(inItems);
			
			if(JcBill.STATUS_FOUR == status) {// 入库待审核   进行撤回
				
				// 进行事件删除
				jcBillAccess.deleteEvent(jcBill.getId(), "稽查收货", "其他入库");
				
			}
		} catch (RuntimeException e) {
			e.printStackTrace();
			LOG.error("服务异常", e.getMessage());
			throw new RuntimeException("服务异常：", e);
		}
	}

	@Override
	public List<JcSourceItem> queryJcSourceItemByBillId(String billId, String srcitemid, int status)
			throws RuntimeException {
		try {
			return jcSourceItemRepository.findByBillidAndSrcitemidAndStatus(billId, srcitemid, status);
		} catch (RuntimeException e) {
			e.printStackTrace();
			LOG.error("服务异常", e.getMessage());
			throw new RuntimeException("服务异常：", e);
		}
	}

	@Transactional(isolation = Isolation.DEFAULT, propagation = Propagation.REQUIRED, rollbackFor = {Exception.class})
	@Override
	public void updateSourceItemAccount(String sourceItemLogJson, String billSourceItemLogJson, String billSourceItemNewLogJson,
			JcSourceItem sourceItem, List<JcBillSourceItem> billSourceItems,
			List<JcBillInItem> oldInItems, List<JcBillInItem> inItems) throws RuntimeException {
		try {
			String biillId = sourceItem.getBillid();
			
			jcSourceItemRepository.save(sourceItem);
			jcBillSourceItemRepository.saveAll(billSourceItems);
			jcBillInItemRepository.deleteAll(oldInItems);
			jcBillInItemRepository.saveAll(inItems);
		} catch (RuntimeException e) {
			e.printStackTrace();
			LOG.error("服务异常", e.getMessage());
			throw new RuntimeException("服务异常：", e);
		}
	}
	@Override
	public SiDepositlocation querySiDepositlocation(String srcid) throws Exception {
		return jcBillAccess.querySiDepositlocation(srcid);
	}
	@Override
	public SiDepositlocation querySiDepositlocationHGfc(String srcid) throws Exception {
		return jcBillAccess.querySiDepositlocationHGfc(srcid);
	}

	@Override
	public JSONObject gwisSubCancelOrder(String orderCode, String orderType) throws Exception {
		try {
			
			Map<String, String> configValues = this.abacusExtConfig.getAbacusConfigSetByKeyGroupByKey(
					PMSConfigKey.KEY_GROUP, PMSConfigKey.PMS_WMSCONF);
			if (configValues == null || configValues.isEmpty())
				throw new Exception(PMSConfigKey.PMS_WMSCONF + "为空！");
			
			// 判断key是否存在
			if (!configValues.containsKey(PMSConfigKey.PMS_WMSCONF_URL))
				throw new Exception(PMSConfigKey.PMS_WMSCONF + "配置中" + PMSConfigKey.PMS_WMSCONF_URL + "不存在！");
			if (!configValues.containsKey(PMSConfigKey.PMS_WMSCONF_APPKEY))
				throw new Exception(PMSConfigKey.PMS_WMSCONF + "配置中" + PMSConfigKey.PMS_WMSCONF_APPKEY + "不存在！");
			if (!configValues.containsKey(PMSConfigKey.PMS_WMSCONF_SESSIONKEY))
				throw new Exception(PMSConfigKey.PMS_WMSCONF + "配置中" + PMSConfigKey.PMS_WMSCONF_SESSIONKEY + "不存在！");
			
			// 判断值是否为空
			String url = configValues.get(PMSConfigKey.PMS_WMSCONF_URL);
			if (StringUtils.isBlank(url))
				throw new Exception(PMSConfigKey.PMS_WMSCONF + "配置中" + PMSConfigKey.PMS_WMSCONF_URL + "为空！");
			
			String appkey = configValues.get(PMSConfigKey.PMS_WMSCONF_APPKEY);
			if (StringUtils.isBlank(appkey))
				throw new Exception(PMSConfigKey.PMS_WMSCONF + "配置中" + PMSConfigKey.PMS_WMSCONF_APPKEY + "为空！");
			
			String sessionKey = configValues.get(PMSConfigKey.PMS_WMSCONF_SESSIONKEY);
			if (StringUtils.isBlank(sessionKey))
				throw new Exception(PMSConfigKey.PMS_WMSCONF + "配置中" + PMSConfigKey.PMS_WMSCONF_SESSIONKEY + "为空！");
			
			String goodsOwner = configValues.get(PMSConfigKey.PMS_WMSCONF_GOODSOWNER);
			if (StringUtils.isBlank(goodsOwner))
				throw new Exception(PMSConfigKey.PMS_WMSCONF + "配置中" + PMSConfigKey.PMS_WMSCONF_GOODSOWNER + "为空！");
			
			JSONObject head = new JSONObject();
	        head.put("orderCode", orderCode);  // 唯一订单号
	        head.put("orderType", orderType);
	        head.put("goodsOwner", goodsOwner);

	        // 2. 计算签名 secret = MD5(contentJson + sessionKey)
	        String secret = md5(head.toJSONString() + sessionKey);

	        // 3. 拼接完整URL（带上method、appkey、secret）
	        String fullUrl = url 
	            + "?method=gwisSubCancelOrder"
	            + "&appkey=" + appkey
	            + "&secret=" + secret;
			
	        LOG.info("WMS取消订单>>>orderCode>>>{}>>>orderType>>>{}>>>请求参数>>>{}", orderCode, orderType, head.toJSONString());
	        String returnJson = RequestUtil.send(fullUrl, head.toJSONString(), orderCode);
	        LOG.info("WMS取消订单>>>orderCode>>>{}>>>orderType>>>{}>>>响应参数>>>{}", orderCode, orderType, returnJson);
	        
	        JSONObject jsonObj = JSONObject.parseObject(returnJson);
	        
	        return jsonObj;
		} catch (RuntimeException e) {
			e.printStackTrace();
			LOG.error("WMS取消订单接口服务异常", e.getMessage());
			throw new RuntimeException("WMS取消订单接口服务异常：", e);
		}
	}
	
	private static String md5(String input) {
        try {
            java.security.MessageDigest md = java.security.MessageDigest.getInstance("MD5");
            byte[] digest = md.digest(input.getBytes("UTF-8"));
            StringBuilder sb = new StringBuilder();
            for (byte b : digest) {
                sb.append(String.format("%02X", b));  // 大写十六进制
            }
            return sb.toString();
        } catch (Exception e) {
            throw new RuntimeException("MD5计算失败", e);
        }
    }
	
}
