package com.ruoyi.front.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.front.service.impl.Kuaidi100Service;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;


/**
 * 快递100调用查询接口
 *
 * @author RuiJing
 * @date 2026-01-29
 */
@RestController
@RequestMapping("/kd100/wxmini")
public class Kd100Controller {
    private static final Logger logger = LoggerFactory.getLogger(Kd100Controller.class);

   @Autowired
   Kuaidi100Service kuaidi100Service;
   @Autowired
   RedisTemplate redisTemplate;

    @PostMapping("/queryExpres")
    public AjaxResult queryExpres(@RequestBody Map param)  {
        try {
            logger.info("调用快递100查询实时快递接口  运单号: {}", param.get("number"));
            param.put("resultv2","4");
            Object  redisResult= redisTemplate.opsForValue().get(param.get("number"));
            if(redisResult==null){
                String   result = kuaidi100Service.queryExpress(param);
                ObjectMapper objectMapper = new ObjectMapper();
                Map<String, Object> map = objectMapper.readValue(result, Map.class);
                if("ok".equals(map.get("message"))){
                    //通过返回的公司编码返回物流公司名称
                    List<Map<String,Object>> comList=kuaidi100Service.getComList(map.get("com").toString());
                    if(comList.size()!=0){
                        map.put("compamyName",comList.get(0).get("company_name"));
                    }
                    else{
                        map.put("compamyName",null);
                    }
                    redisTemplate.opsForValue().set(param.get("number"), map, 30, TimeUnit.MINUTES);;
                    return AjaxResult.success(map);
                }
                else{
                   return  AjaxResult.error(map.get("message").toString());
                }
            }

            return AjaxResult.success(redisResult);
        } catch (Exception e) {
            return AjaxResult.error("快递100查寻接口异常! 原因：{}" ,e.getMessage());
        }


    }



    @PostMapping("/queryIcrExpres")
    public Map queryIcrExpres(@RequestBody Map param)  {
        try {
            if(StringUtils.isBlank(param.get("param").toString())){
                return AjaxResult.error("智能识别文字不能为空！");

            }
                Map   result = kuaidi100Service.queryIcrExpres(param);
                return  result;
        } catch (Exception e) {
            return AjaxResult.error("智能识别文字出错！");
        }


    }






    @PostMapping("/queryExpresPlus")
    public AjaxResult queryExpresPlus(@RequestBody Map param)  {
        try {
            logger.info("调用快递100查询实时快递接口  运单号: {}", param.get("number"));
            param.put("resultv2","4");
            Object  redisResult= redisTemplate.opsForValue().get(param.get("number"));
            if(redisResult==null){
                String   result = kuaidi100Service.queryExpresPlus(param);
                ObjectMapper objectMapper = new ObjectMapper();
                Map<String, Object> map = objectMapper.readValue(result, Map.class);
                if("ok".equals(map.get("message"))){
                    //通过返回的公司编码返回物流公司名称
                    List<Map<String,Object>> comList=kuaidi100Service.getComList(map.get("com").toString());
                    if(comList.size()!=0){
                        map.put("compamyName",comList.get(0).get("company_name"));
                    }
                    else{
                        map.put("compamyName",null);
                    }
                    redisTemplate.opsForValue().set(param.get("number"), map, 30, TimeUnit.MINUTES);;
                    return AjaxResult.success(map);
                }
                else{
                    return  AjaxResult.error(map.get("message").toString());
                }
            }

            return AjaxResult.success(redisResult);
        } catch (Exception e) {
            return AjaxResult.error("快递100查寻接口异常! 原因：{}" ,e.getMessage());
        }


    }
}
