package com.platform.api;

import com.alibaba.fastjson.JSONObject;
import com.platform.entity.GoodsVo;
import com.platform.service.ApiGoodsService;
import com.platform.util.ApiBaseAction;
import com.platform.utils.HttpUtils;
import com.platform.utils.LogisticsUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by Hb_zzZ on 2020/3/9.
 */
@Api(tags = "收货地址")
@RestController
@RequestMapping("/api/logistics")
public class LogisticsController extends ApiBaseAction {

    @Autowired
    private ApiGoodsService goodsService;

    /**
     * 获取物流详情
     */
    @ApiOperation(value = "获取物流详情", response = Map.class)
    @PostMapping("detail")
    public Object detail(@RequestParam(value = "no", defaultValue = "") String no,
                         @RequestParam(value = "type", defaultValue = "") String type,
                         @RequestParam(value = "goodsId", defaultValue = "") String goodsId) {
        JSONObject result = new JSONObject();
        try {
            String msg = LogisticsUtil.getDetail(no, type, "/kdi");
            result = JSONObject.parseObject(msg);
            //status 0:正常查询 201:快递单号错误 203:快递公司不存在 204:快递公司识别失败 205:没有信息 207:该单号被限制，错误单号 */
            String status = result.getString("status");
            if("201".equals(status)){
                return toResponsFail("快递单号错误");
            }
            if("203".equals(status)){
                return toResponsFail("快递公司不存在");
            }
            if("204".equals(status)){
                return toResponsFail("快递公司识别失败");
            }
            if("205".equals(status)){
                return toResponsFail("没有信息");
            }
            if("206".equals(status)){
                return toResponsFail("该单号被限制，错误单号");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        JSONObject data = result.getJSONObject("result");
        if(data == null){
            data = new JSONObject();
        }
        //查询商品图片
        if(!StringUtils.isEmpty(goodsId)){
            GoodsVo goodsVo = goodsService.queryObject(Integer.valueOf(goodsId));
            if(goodsVo != null){
                data.put("goodsPic", goodsVo.getPrimary_pic_url());
            }
        }
        return toResponsSuccess(data);
    }

    /**
     * 获取物流详情
     */
    @ApiOperation(value = "获取物流详情", response = Map.class)
    @PostMapping("list")
    public Object list( @RequestParam(value = "type", defaultValue = "") String type) {
        JSONObject result = new JSONObject();
        try {
            //System.out.println(response.toString());如不输出json, 请打开这行代码，打印调试头部状态码。
            //状态码: 200 正常；400 URL无效；401 appCode错误； 403 次数用完； 500 API网管错误
            //获取response的body
            String msg = LogisticsUtil.getDetail(null, type, "/kdi");
            logger.info(msg); //输出json
            result = JSONObject.parseObject(msg);
            String status = result.getString("status");
            if("200".equals(status)){
                return toResponsSuccess(result.get("result"));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return toResponsFail("查询失败");
    }
}
