kuaidiController.java 5 KB
package com.huaheng.api.WAYBILLNO.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.huaheng.api.WAYBILLNO.service.KuaidiCallBackService;
import com.huaheng.api.WAYBILLNO.service.KuaidiService;
import com.huaheng.common.constant.QuantityConstant;
import com.huaheng.common.exception.service.ServiceException;
import com.huaheng.framework.aspectj.lang.annotation.ApiLogger;
import com.huaheng.framework.web.controller.BaseController;
import com.huaheng.framework.web.domain.AjaxResult;
import com.huaheng.pc.config.KDCertification.domain.KDCertification;
import com.huaheng.pc.config.KDCertification.service.KDCertificationService;
import com.huaheng.pc.shipment.kuaidiHeader.service.KuaidiHeaderService;
import io.swagger.annotations.ApiOperation;
import org.json.JSONArray;
import org.json.JSONTokener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;


/**
 * @ClassName kuaidiController
 * @Description TODO
 * @Author Administrator
 * @Date 2020/2/2514:29
 */
@RestController
@RequestMapping("/api/kuaidi")
public class kuaidiController extends BaseController {

    @Autowired
    private KuaidiService kuaidiService;
    @Autowired
    private KDCertificationService kdCertificationService;
    @Autowired
    private KuaidiHeaderService
            kuaidiHeaderService;
    @Autowired
    private KuaidiCallBackService kuaidiCallBackService;

    private final static Logger logger = LoggerFactory.getLogger(kuaidiController.class);


    @PostMapping("/confim")
    @ApiOperation("kuaidi接口")
    @ResponseBody
    public AjaxResult confim() {
        //整合快递信息
        Map<String, String> data = new TreeMap<>();
        data.put("recName", "张三");
        data.put("recMobile", "12345678999");
        data.put("recAddr", "湖南长沙岳麓区山水新城7栋701");
        data.put("recCompany", "东睿有限公司");
        data.put("sendName", "李四");
        data.put("sendMobile", "18475748484");
        data.put("sendAddr", "湖南省长沙市浏阳高新技术产业开发区鼎盛路21号");
        data.put("sendCompany", "长沙华恒机器人");
        data.put("kuaidiCom", "shunfeng");
        data.put("orderNum", "202012050002");
        String jsonObject = JSONObject.toJSONString(data);

        KDCertification kdCertification = new KDCertification();
        String accessToken = kdCertificationService.getOne(kdCertification).getAccessToken();
        try {
            String message = kuaidiService.send(accessToken, jsonObject);
            System.out.println(message);
            JSONObject object = JSONObject.parseObject(message);
            String code = object.getString("status");
            if (!"200".equals(code)) {
                return AjaxResult.error(object.getString("message"));
            }
        } catch (Exception e) {
            throw new ServiceException("快递信息send异常");
        }
        return AjaxResult.success("成功");
    }

    @GetMapping("/print")
    @ApiOperation("kuaidi接口")
    @ResponseBody
    public String print() {
        //整合快递信息
        String message = null;
        KDCertification kdCertification = new KDCertification();
        String accessToken = kdCertificationService.getOne(kdCertification).getAccessToken();
        try {
            message = kuaidiService.quickPrint(accessToken, "202012050002");
            System.out.println(message);
        } catch (Exception e) {
            throw new ServiceException("快递信息send异常");
        }
        return message;
    }


    @PostMapping("/test")
    @ApiOperation("kuaidi接口")
    @ResponseBody
    @ApiLogger(apiName = "kuaidi接口", from = "KD")
    public AjaxResult test(@RequestBody JSONObject body) throws Exception {
        //快递100回传接口,更新快递单号
        kuaidiCallBackService.kuaidiCallback(body, QuantityConstant.WAREHOUSE_KS);
        logger.info("返回值:{}", "成功");
        return AjaxResult.success("成功");
    }

    @PostMapping("/tests")
    @ApiOperation("kuaidi接口")
    @ResponseBody
    public AjaxResult test(@RequestBody String json) throws Exception {
        AjaxResult ajaxResult = JSON.parseObject(json, AjaxResult.class);

        Object object = new JSONTokener(ajaxResult.getMsg()).nextValue();
        if (object instanceof JSONObject) {
            return AjaxResult.success(ajaxResult.getMsg());
        } else if (object instanceof JSONArray) {
            List<Map> list = JSON.parseObject(ajaxResult.getMsg(), ArrayList.class);
            for (Map map : list) {
                String code = map.get("Code").toString();
                String irowno = map.get("irowno").toString();
                String AutoID = map.get("AutoID").toString().substring(7, 10);
                String ll = "";
            }
        }
        return AjaxResult.success("成功");
    }

}