package com.prolog.cs.biz.inbound.service.impl.failure;

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.prolog.cs.biz.inbound.dao.OmsAsnBillHMapper;
import com.prolog.cs.biz.inbound.model.OmsAsnBillH;
import com.prolog.cs.biz.inbound.remote.BaseInventoryRemoteCaller;
import com.prolog.cs.biz.inbound.service.OrderInboundStatusBackService;
import com.prolog.cs.biz.inbound.vo.OrderStatusBackVO;
import com.prolog.framework.common.message.RestMessage;
import com.prolog.framework.core.exception.BizException;
import com.prolog.framework.utils.*;
import com.prolog.upcloud.base.inventory.vo.AskIntegrationVo;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;

@Service
public class OrderStatusBackServiceImpl implements OrderInboundStatusBackService {

	@Resource
	private OmsAsnBillHMapper omsAsnBillHMapper;

	@Resource
	private BaseInventoryRemoteCaller baseInventoryRemoteCaller;

//	@Value("${oms.orderStatusBack}")
//	private String orderStatusBack;


	@Override
	public void sendInboundOrderStatusOms(String billno, String remark, String holdCode, String packedBy, String extendProps, String doStatus) {
		try {
			OmsAsnBillH omsAsnBillH = omsAsnBillHMapper.findByMap(MapUtils.put("billno",billno).getMap(),OmsAsnBillH.class).get(0);
			OrderStatusBackVO orderStatusBackVO = new OrderStatusBackVO();
			orderStatusBackVO.setId(omsAsnBillH.getBillno());
			orderStatusBackVO.setTenantCode(omsAsnBillH.getpEnterpriseid());
			orderStatusBackVO.setDoStatus(doStatus);
			orderStatusBackVO.setDistributionSupplierId(omsAsnBillH.getCarrierId());
			orderStatusBackVO.setOrderOutOfInvTime(DateUtils.format(new Date(),"yyyy-MM-dd HH:mm:ss"));
			orderStatusBackVO.setExpressNbr("");
			orderStatusBackVO.setOrderType(omsAsnBillH.getBilltype());
			if(omsAsnBillH.getBilltype().equals("1")){
				orderStatusBackVO.setOrderType("CGRK");
			}
			if(omsAsnBillH.getBilltype().equals("2")){
				orderStatusBackVO.setOrderType("XSTH");
			}
			if(omsAsnBillH.getBilltype().equals("3")){
				orderStatusBackVO.setOrderType("QTRK");
			}
			orderStatusBackVO.setOwnerCode(omsAsnBillH.getConsignorcode());
			orderStatusBackVO.setWareHouseId(omsAsnBillH.getBranchcode());
			orderStatusBackVO.setCasesShpd(BigDecimal.ZERO);
			orderStatusBackVO.setNotes(remark);
			orderStatusBackVO.setHoldCode(holdCode);
			orderStatusBackVO.setDoOperateTime(DateUtils.format(new Date(),"yyyy-MM-dd HH:mm:ss"));
			orderStatusBackVO.setPackedBy(packedBy);
//			if(StringUtils.isEmpty(orderStatusBack)){
//				throw new BizException("请求路径配置为空");
//			}
//			System.out.println(JSON.toJSONString(orderStatusBackVO));
//			String url = orderStatusBack;
//			String result = HttpUtil.post(url, JSONObject.toJSONString(orderStatusBackVO));
			AskIntegrationVo vo = new AskIntegrationVo();
			vo.setIntegrationCode("orderStatusBack");
			vo.setParam(orderStatusBackVO);
			RestMessage<?> msg = baseInventoryRemoteCaller.askIntegration(vo);
		}catch (Exception e){
			e.printStackTrace();
		}
	}
}
