admin
2024-07-27 65aaf1c05bd06cefa82ebc40cc3e01cf4ac233c0
src/main/java/com/taoke/autopay/utils/order/DYOrderApi.java
@@ -1,12 +1,24 @@
package com.taoke.autopay.utils.order;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.taoke.autopay.dto.DYOrderDto;
import com.taoke.autopay.dto.DYSkuOrderDto;
import com.taoke.autopay.dto.DYSubsidyDto;
import com.taoke.autopay.exception.KeyOrderException;
import com.taoke.autopay.utils.Constant;
import com.taoke.autopay.utils.HttpUtil;
import com.taoke.autopay.utils.JsonUtil;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
 * @author hxh
@@ -17,26 +29,110 @@
public class DYOrderApi {
    private static Logger logger = LoggerFactory.getLogger("dyorderApiLogger");
    private static Gson gson=JsonUtil.getSimpleGson();
    private static String requestByOrderNo1(String orderNo) {
        String url = String.format("https://api.youihuo.com/open/order.getFreeOrder?apiKey=sTIFFTyunIFZfp5i4V6g19PN9biudl4v&orderId=%s", orderNo);
        String result = HttpUtil.get(url);
        System.out.println(result);
        return result;
    }
    public static DYOrderDto getOrderDetail(String orderNo) throws KeyOrderException {
    private static String requestByOrderNo2(String orderNo) {
        String url = String.format("https://api.bpshe.com/mall/douyinOMS/getSubsidyOrderInfo?orderId=%s", orderNo);
        Map<String,String> headers=new HashMap<>();
        headers.put("Accept","application/json;charset=utf-8");
        String result = HttpUtil.get(url, headers);
        return result;
    }
    private static DYOrderDto getOrderDetail1(String orderNo) throws KeyOrderException {
        String result = requestByOrderNo1(orderNo);
        JSONObject root = JSONObject.fromObject(result);
        if (root.optInt("code") != 1000) {
            logger.error(String.format("抖音订单查询出错:%s - %s",orderNo, result));
            logger.error(String.format("抖音订单查询出错(1):%s - %s",orderNo, result));
            throw new KeyOrderException(root.optString("message"));
        }
       JSONObject data = root.optJSONObject("data");
       return  JsonUtil.getSimpleGson().fromJson(data.toString(),DYOrderDto.class);
        JSONObject data = root.optJSONObject("data");
        DYOrderDto dto =  JsonUtil.getSimpleGson().fromJson(data.toString(),DYOrderDto.class);
        dto.setOrderChannel(Constant.ORDER_CHANNEL_CYX);
        return dto;
    }
    private static DYOrderDto getOrderDetail2(String orderNo) throws KeyOrderException {
        String result = requestByOrderNo2(orderNo);
        JSONObject root = JSONObject.fromObject(result);
        if (root.optInt("errCode") != 0) {
            logger.error(String.format("抖音订单查询出错(2):%s - %s",orderNo, result));
            throw new KeyOrderException(root.optString("errMsg"));
        }
        JSONObject data = root.optJSONObject("data");
        if(data==null){
            logger.error(String.format("抖音订单查询无数据(2):%s - %s",orderNo, result));
            throw new KeyOrderException("订单查询无数据");
        }
        JSONObject orderDetailData = data.optJSONObject("orderDetail");
        JSONArray subsidyDetailData = data.optJSONArray("subsidyDetail");
        if(orderDetailData==null||subsidyDetailData==null){
            logger.error(String.format("订单与补贴无数据(2):%s - %s",orderNo, result));
            throw new KeyOrderException("订单与补贴无数据");
        }
        orderDetailData = orderDetailData.optJSONObject("shop_order_detail");
        DYOrderDto dyOrder =   gson.fromJson(orderDetailData.toString(),DYOrderDto.class);
        List<DYSubsidyDto> subsidyList = gson.fromJson(subsidyDetailData.toString(), new TypeToken<List<DYSubsidyDto>>(){}.getType());
        if(subsidyList.size()!=dyOrder.getSku_order_list().size()){
            throw new KeyOrderException("订单商品与补贴商品不相等");
        }
        Map<String, DYSkuOrderDto> skuMap=new HashMap<>();
        for(DYSkuOrderDto d: dyOrder.getSku_order_list()){
            skuMap.put(d.getProduct_id()+"", d);
        }
        for(DYSubsidyDto d:subsidyList){
            if(skuMap.get(d.getGoodsId())==null){
                throw new KeyOrderException("补贴商品没在订单商品中");
            }
            if(skuMap.get(d.getGoodsId()).getPay_amount()> d.getSubsidyAmount()){
                throw new KeyOrderException("订单商品金额高于补贴金额");
            }
            if(!d.getStatus().equalsIgnoreCase("online")){
                throw new KeyOrderException("补贴下线");
            }
        }
        dyOrder.setOrderChannel(Constant.ORDER_CHANNEL_BPS);
        return  dyOrder;
    }
    public static DYOrderDto getOrderDetail(String orderNo) throws KeyOrderException {
        DYOrderDto dto=null;
        List<KeyOrderException> exceptions=new ArrayList<>();
        try {
            dto = getOrderDetail1(orderNo);
        }catch(KeyOrderException e){
            exceptions.add(e);
        }
        if(dto==null){
            try {
                dto = getOrderDetail2(orderNo);
            }catch(KeyOrderException e){
                exceptions.add(e);
            }
        }
        if(dto!=null){
            return dto;
        }
        String exception="";
        for(KeyOrderException e:exceptions){
            exception+=e.getMessage()+";";
        }
       throw new KeyOrderException(exception);
    }
    public static void main(String[] args) throws Exception {
        DYOrderDto dto = (DYOrderApi.getOrderDetail("6931134387948820382"));
        System.out.println("123123");
//        DYOrderDto dto = (DYOrderApi.getOrderDetail("6932591080266339994"));
        DYOrderDto result = getOrderDetail("6932591080266471066");
        System.out.println(result);
    }
}