IcsKuaidiService.java 7.36 KB
package com.huaheng.pc.u8.service;

import com.alibaba.fastjson.JSON;
import com.huaheng.common.constant.HttpConstant;
import com.huaheng.common.constant.QuantityConstant;
import com.huaheng.common.exception.service.ServiceException;
import com.huaheng.common.utils.StringUtils;
import com.huaheng.common.utils.http.HttpUtils;
import com.huaheng.common.utils.http.OkHttpUtils;
import com.huaheng.framework.aspectj.ApiLogAspect;
import com.huaheng.framework.web.domain.AjaxResult;
import com.huaheng.pc.config.address.domain.Address;
import com.huaheng.pc.config.address.service.AddressService;
import com.huaheng.pc.monitor.apilog.domain.ApiLog;
import com.huaheng.pc.shipment.kuaidiHeader.domain.KuaidiHeader;
import com.huaheng.pc.u8.domain.KuaidiU8Request;
import org.springframework.http.HttpHeaders;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName icsKuaidiService
 * @Description TODO
 * @Author Administrator
 * @Date 2021/5/3116:16
 */
@Service
public class IcsKuaidiService {

    private static String param = "kd";

    private static String paramV1 = "kd-new";

    private static String tokenUrl = "https://api.yonyouup.com/system/token?from_account=huaheng2019&app_key=opa7271824ba6ce3541&app_secret=8425b85f0fea43bca6c14f149fe18b21";
    @Resource
    private AddressService addressService;


    //获取token
    public String getToken() {
        String result = HttpUtils.sendGet(tokenUrl);
        if (StringUtils.isEmpty(result)) {
            throw new ServiceException("U8系统接口地址错误或服务器连接不到或返回为空");
        }
        Map map = JSON.parseObject(result, HashMap.class);
        String accessToken = map.get("token").toString();

        Map hashMap = JSON.parseObject(accessToken, HashMap.class);
        String token = hashMap.get("id").toString();
        return token;
    }

    public String getUrl() {
        //获取url
        Address address = new Address();
        address.setParam(param);
        address.setWarehouseCode("KS0001");
        address = addressService.selectEntity(address);
        if (address == null) {
            throw new ServiceException("查询不到U8接口地址");
        }
        return address.getUrl();
    }

    public String getUrlNew() {
        //获取url
        Address address = new Address();
        address.setParam(paramV1);
        address.setWarehouseCode("KS0001");
        address = addressService.selectEntity(address);
        if (address == null) {
            throw new ServiceException("查询不到U8接口地址");
        }
        return address.getUrl();
    }

    //通过单号获取出库单信息
    public List<Map<String, String>> getKDMeseeage(KuaidiHeader kuaidiHeader) {
        //获取url和获取token
        String url = this.getUrl();
        String token = this.getToken();
        if (!StringUtils.isEmpty(kuaidiHeader.getOrderBill()) && StringUtils.isEmpty(kuaidiHeader.getFromCode())) {
            if (kuaidiHeader.getOrderBill().substring(0, 2).equals("AA")) {
                url
                        = url + "token=" + token + "&ds_sequence=1&rows_per_page=1000&code_begin=" + kuaidiHeader.getOrderBill() + "&code_end=" + kuaidiHeader.getOrderBill();
            } else {
                url
                        = url + "token=" + token + "&ds_sequence=2&rows_per_page=1000&code_begin=" + kuaidiHeader.getOrderBill() + "&code_end=" + kuaidiHeader.getOrderBill();
            }
        }

        if (!StringUtils.isEmpty(kuaidiHeader.getOrderBill()) && StringUtils.isNotEmpty(kuaidiHeader.getFromCode())) {
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
            String dateString = dateFormat.format(kuaidiHeader.getCreated());
            if (kuaidiHeader.getOrderBill().substring(0, 2).equals("AA")) {
                url
                        = url + "token=" + token + "&ds_sequence=1&rows_per_page=1000&auditdate=" + dateString;
            } else {
                url
                        = url + "token=" + token + "&ds_sequence=2&rows_per_page=1000&auditdate=" + dateString;
            }
        }
        if (!StringUtils.isEmpty(kuaidiHeader.getSourceCode())) {
            if (kuaidiHeader.getSourceCode().substring(0, 2).equals("AH")) {
                url
                        = url + "token=" + token + "&ds_sequence=1&rows_per_page=1000&subconsignmentcode=" + kuaidiHeader.getSourceCode();
            } else {
                url = url + "token=" + token + "&ds_sequence=2&rows_per_page=1000&subconsignmentcode=" + kuaidiHeader.getSourceCode();
            }
        }

        //获取信息
        ApiLog log = null;
        String result = null;
        HttpHeaders headers = new HttpHeaders();
        try {
            log = ApiLogAspect.initApiLog("get", url,
                    null, null, QuantityConstant.WAREHOUSE_KS);
            log.setWarehouseCode(QuantityConstant.WAREHOUSE_KS);
            log.setApiName("快递获取u8销售出库单");
            log.setRequestBody(url);
            result = HttpUtils.sendGet(url);
            log.setResponseBody(result);
        } catch (Exception e) {
//            e.printStackTrace();
            ApiLogAspect.setApiLogException(log, e);
        } finally {
            ApiLogAspect.finishApiLog(log, headers, result);
        }

        if (StringUtils.isEmpty(result)) {
            throw new ServiceException("U8系统接口地址错误或服务器连接不到或返回为空");
        }

        Map map = JSON.parseObject(result, HashMap.class);
        String code = map.get("errcode").toString();
        if ("0".equals(code)) {
            List<Map<String, String>> lists = new ArrayList<>();
            if (StringUtils.isNull(map.get("saleoutlistall"))) {
                throw new ServiceException("获取的U8信息错误,为空");
            }
            String msg = map.get("saleoutlistall").toString();
            lists = JSON.parseObject(msg, ArrayList.class);
            return lists;
        } else {
            throw new ServiceException(map.get("errmsg").toString());
        }
    }

    //通过单号获取出库单信息--新版
    //type  888 昆山股份公司   807 长沙公司  801 徐州公司
    public List<Map<String, String>> getKDMeseeageV1(KuaidiHeader kuaidiHeader, String type, String warehouseCode) {
        //获取url和获取token
        String url = this.getUrlNew();

        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String dateString = dateFormat.format(kuaidiHeader.getCreated());
        KuaidiU8Request kuaidiU8Request = new KuaidiU8Request();
        kuaidiU8Request.setType(type);
        kuaidiU8Request.setVerifydate(dateString);
        String jsonParam = JSON.toJSONString(kuaidiU8Request);
        String result = OkHttpUtils.bodypost(url, jsonParam, warehouseCode);
        if (StringUtils.isNull(result) || StringUtils.isEmpty(result)) {
            throw new ServiceException("获取快递单:接口地址错误或返回为空");
        }
        AjaxResult ajaxResult = JSON.parseObject(result, AjaxResult.class);
        if (ajaxResult.getCode() != HttpConstant.OK) {
            return null;
        }
        List<Map<String, String>> map = new ArrayList<>();
        map = (List<Map<String, String>>) ajaxResult.getData();
        return map;
    }


}