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

import com.alibaba.fastjson.JSON;
import com.huaheng.common.exception.service.ServiceException;
import com.huaheng.common.utils.StringUtils;
import com.huaheng.common.utils.http.HttpUtils;
import com.huaheng.pc.config.address.domain.Address;
import com.huaheng.pc.config.address.service.AddressService;
import com.huaheng.pc.shipment.kuaidiHeader.domain.KuaidiHeader;
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 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,null);
        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 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();
            }
        }
      /*SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String dateString = dateFormat.format(kuaidiHeader.getCreated());
        根据搜寻条件拼接地址
        if(!StringUtils.isEmpty(kuaidiHeader.getOrderBill()) && StringUtils.isEmpty(kuaidiHeader.getFromCode())) {
                url= url + "token=" + token + "&ds_sequence=3&rows_per_page=1000&code_begin=" + kuaidiHeader.getOrderBill() + "&code_end=" + kuaidiHeader.getOrderBill();
        }
        if(!StringUtils.isEmpty(kuaidiHeader.getOrderBill()) && StringUtils.isNotEmpty(kuaidiHeader.getFromCode())) {
                url= url + "token=" + token + "&ds_sequence=3&rows_per_page=1000&&auditdate="+dateString;
        }
        if(!StringUtils.isEmpty(kuaidiHeader.getSourceCode())) {
                url = url + "token=" + token + "&ds_sequence=3&rows_per_page=1000&subconsignmentcode=" + kuaidiHeader.getSourceCode();
        }*/
        //获取信息
        String result = HttpUtils.sendGet(url,null);
        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());
        }
    }





}