package com.prolog.cs.biz.outbound.service.impl;

import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.prolog.cs.biz.inbound.vo.OrderStatusBackVO;
import com.prolog.cs.biz.outbound.dao.OmsDoBillHMapper;
import com.prolog.cs.biz.outbound.model.OmsDoBillHDO;
import com.prolog.cs.biz.outbound.service.OrderOutboundStatusBackService;
import com.prolog.framework.core.exception.BizException;
import com.prolog.framework.utils.DateUtils;
import com.prolog.framework.utils.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
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 OrderOutboundStatusBackService {

	private static Logger log = LoggerFactory.getLogger(OrderStatusBackServiceImpl.class);

	@Resource
	private OmsDoBillHMapper omsDoBillHMapper;

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

	@Override
	public void sendOutboundOrderStatusOms(String billid, String remark, String holdCode, String packedBy, String extendProps, String doStatus,BigDecimal casesShpd) {
		OmsDoBillHDO omsDoBillHDO = omsDoBillHMapper.findById(billid,OmsDoBillHDO.class);
		OrderStatusBackVO orderStatusBackVO = new OrderStatusBackVO();
		orderStatusBackVO.setId(omsDoBillHDO.getBillno());
		orderStatusBackVO.setTenantCode(omsDoBillHDO.getEnterpriseId());
		orderStatusBackVO.setDoStatus(doStatus);
		orderStatusBackVO.setDistributionSupplierId(omsDoBillHDO.getCarrierId());
		orderStatusBackVO.setOrderOutOfInvTime(DateUtils.format(new Date(),"yyyy-MM-dd HH:mm:ss"));
		orderStatusBackVO.setExpressNbr(omsDoBillHDO.getShipBillNo());
		orderStatusBackVO.setOrderType(omsDoBillHDO.getBilltype());
		if(omsDoBillHDO.getBilltype().equals("1")){
			orderStatusBackVO.setOrderType("XSCK");
		}
		if(omsDoBillHDO.getBilltype().equals("4")){
			orderStatusBackVO.setOrderType("CGTH");
		}
		if(omsDoBillHDO.getBilltype().equals("6")){
			orderStatusBackVO.setOrderType("QTRK");
		}
		orderStatusBackVO.setOwnerCode(omsDoBillHDO.getConsignorcode());
		orderStatusBackVO.setWareHouseId(omsDoBillHDO.getBranchcode());
		orderStatusBackVO.setCasesShpd(casesShpd);
		orderStatusBackVO.setNotes(remark);
		orderStatusBackVO.setHoldCode(holdCode);
		orderStatusBackVO.setDoOperateTime(DateUtils.format(new Date(),"yyyy-MM-dd HH:mm:ss"));
		orderStatusBackVO.setPackedBy(packedBy);
		orderStatusBackVO.setExtendProps(extendProps);
		try {
			if (StringUtils.isEmpty(orderStatusBack)) {
				throw new BizException("请求路径配置为空");
			}
			log.info("回传URL为:{}", JSONUtil.toJsonStr(orderStatusBack));
			log.info("回传参数为:{}", JSONUtil.toJsonStr(orderStatusBackVO));
			String url = orderStatusBack;
			String result = HttpUtil.post(url, JSONObject.toJSONString(orderStatusBackVO));
			log.info("回传结果为:{}", JSONUtil.toJsonStr(result));
		} catch (Exception e) {
			log.info("回传失败信息为:{}", e);
		}
	}
}
