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

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.huaheng.api.WAYBILLNO.service.KuaidiService;
import com.huaheng.common.exception.service.ServiceException;
import com.huaheng.common.utils.StringUtils;
import com.huaheng.common.utils.Wrappers;
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.domain.KuaidiHeader;
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.*;


/**
 * @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;

    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
    public AjaxResult test(@RequestBody JSONObject body) throws Exception {
        String json = JSON.toJSONString(body);
        logger.info("获取快递单号:{}",json);
        System.out.println(body);
        String type =body.getString("type");
        if(StringUtils.isNotEmpty(type)){
            if(type.equals("SEND")){
                JSONObject data =body.getJSONObject("data");
                String number =data.getString("orderNum");
                System.out.println(number);
            }
            if(type.equals("UPDATE")){
                JSONObject data =body.getJSONObject("data");
                String number =data.getString("orderNum");
                System.out.println(number);
            }
            if(type.equals("FILLEXPNUM")){
                JSONObject data = body.getJSONArray("data").getJSONObject(0);
                String number =data.getString("orderNum");
                System.out.println(number);
                String dingdan = data.getString("kuaidinum");
                LambdaQueryWrapper<KuaidiHeader> queryWrapper = Wrappers.lambdaQueryNoWarehouse();
                queryWrapper.eq(KuaidiHeader::getKuaidiNum,number);
                queryWrapper.last("limit 1");
                KuaidiHeader kuaidiHeader =kuaidiHeaderService.getOne(queryWrapper);
                if(kuaidiHeader != null){
                    if(StringUtils.isNotEmpty(kuaidiHeader.getTrackCode())){
                        kuaidiHeader.setTrackCode(dingdan + "," +kuaidiHeader.getTrackCode());
                    }else {
                        kuaidiHeader.setTrackCode(dingdan);
                    }
                    kuaidiHeader.setDeleted(true);
                    kuaidiHeader.setLastUpdated(new Date());
                    kuaidiHeaderService.updateById(kuaidiHeader);
                }
                System.out.println(dingdan);
            }

        }else {
        }
        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("成功");
    }

}