package com.ruoyi.web.controller.sf;

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.sf.SfBean;
import com.ruoyi.system.domain.sf.SfRespVo;
import com.sf.csim.express.service.CallExpressServiceTools;
import com.sf.csim.express.service.HttpClientUtil;
import com.sf.csim.express.service.IServiceCodeStandard;
import com.sf.csim.express.service.code.ExpressServiceCodeEnum;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import java.util.stream.Collectors;

/**
 * @Author: tjf
 * @Date: 2024/1/18 14:54
 * @Describe:
 */
@RestController
@RequestMapping("/sf")
public class SfExpressController {

    @Resource
    private SfBean sfBean;

    @PostMapping("/callSFApi")
    public  AjaxResult callSfApi(@RequestBody SfRespVo sfRespVo) throws Exception {
        //验证请求参数
        if (StringUtils.isEmpty(sfRespVo.getCheckPhoneNo()) || StringUtils.isEmpty(sfRespVo.getTrackingType())) {
            throw new Exception("参数不能为空!");
        }
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("language", "zh-CN");
        //查询号类别 1:根据顺丰运单号查询,trackingNumber将被当作顺丰运单号处理
        jsonObject.put("trackingType", "1");
        //快递单号
        jsonObject.put("trackingNumber", sfRespVo.getTrackingType());
        //1:标准路由查询
        jsonObject.put("methodType", "1");
        //收件人手机号码后4位
        jsonObject.put("checkPhoneNo", sfRespVo.getCheckPhoneNo());
        String msgData = jsonObject.toString();
        IServiceCodeStandard standardService = ExpressServiceCodeEnum.EXP_RECE_SEARCH_ROUTES;
        // set common header
        Map<String, String> params = new HashMap<String, String>();
        String timeStamp = String.valueOf(System.currentTimeMillis());
        // 顾客编码 ，对应丰桥上获取的clientCode
        params.put("partnerID", sfBean.getClientCode());
        //请求唯一号UUID
        params.put("requestID", UUID.randomUUID().toString().replace("-", ""));
        // 接口服务码
        params.put("serviceCode", standardService.getCode());
        //调用接口时间戳
        params.put("timestamp", timeStamp);
        //报文
        params.put("msgData", msgData);
        //数字签名
        params.put("msgDigest", CallExpressServiceTools.getMsgDigest(msgData, timeStamp, sfBean.getCheckWord()));
        long startTime = System.currentTimeMillis();
        System.out.println("====调用实际请求：" + params);
        String result = HttpClientUtil.post(sfBean.getCallUrlProd(), params);
        System.out.println("====调用丰桥的接口服务代码：" + String.valueOf(standardService.getCode()) + " 接口耗时：" + String.valueOf(System.currentTimeMillis() - startTime) + "====");
        System.out.println("===调用地址 ===" + sfBean.getCallUrlProd());
        System.out.println("===顾客编码 ===" + sfBean.getClientCode());
        System.out.println("===返回结果：" + result);
        JSONObject apiResultData = JSONUtil.parseObj(JSONUtil.parseObj(result).get("apiResultData"));
        if (!apiResultData.getBool("success")) {
            throw new Exception("暂无物流信息");
        }
        JSONObject resultMsgData = JSONUtil.parseObj(apiResultData.get("msgData"));
        JSONArray routeResps = JSONUtil.parseArray(resultMsgData.get("routeResps"));
        JSONArray routes = routeResps.getJSONObject(0).getJSONArray("routes");
        if (routes.isEmpty()) {
            throw new Exception("暂无物流信息");
        }
        //根据时间翻转
        Collections.reverse(routes);
        routeResps.getJSONObject(0).put("routes",routes);
        return AjaxResult.success(routeResps);
    }
}
