package com.ruoyi.jarvis.service.impl;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.utils.http.HttpUtils;
import com.ruoyi.jarvis.domain.JDOrder;
import com.ruoyi.jarvis.service.ILogisticsService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.net.URLEncoder;
import java.util.concurrent.TimeUnit;

/**
 * 物流信息服务实现类
 */
@Service
public class LogisticsServiceImpl implements ILogisticsService {
    private static final Logger logger = LoggerFactory.getLogger(LogisticsServiceImpl.class);
    
    private static final String REDIS_WAYBILL_KEY_PREFIX = "logistics:waybill:order:";
    private static final String EXTERNAL_API_URL = "http://192.168.8.88:5001/fetch_logistics?tracking_url=";
    private static final String PUSH_URL = "https://wxts.van333.cn/wx/send/pdd";
    private static final String PUSH_TOKEN = "super_token_b62190c26";
    
    @Resource
    private StringRedisTemplate stringRedisTemplate;
    
    @Override
    public boolean isOrderProcessed(Long orderId) {
        if (orderId == null) {
            return false;
        }
        String redisKey = REDIS_WAYBILL_KEY_PREFIX + orderId;
        return stringRedisTemplate.hasKey(redisKey);
    }
    
    @Override
    public boolean fetchLogisticsAndPush(JDOrder order) {
        if (order == null || order.getId() == null) {
            logger.warn("订单信息为空，无法获取物流信息");
            return false;
        }
        
        // 检查物流链接
        String logisticsLink = order.getLogisticsLink();
        if (logisticsLink == null || logisticsLink.trim().isEmpty()) {
            logger.info("订单暂无物流链接，跳过处理 - 订单ID: {}", order.getId());
            return false;
        }
        
        try {
            // 构建外部接口URL
            String externalUrl = EXTERNAL_API_URL + URLEncoder.encode(logisticsLink, "UTF-8");
            logger.info("调用外部接口获取物流信息 - 订单ID: {}, URL: {}", order.getId(), externalUrl);
            
            // 在服务端执行HTTP请求
            String result = HttpUtils.sendGet(externalUrl);
            
            if (result == null || result.trim().isEmpty()) {
                logger.warn("外部接口返回空结果 - 订单ID: {}", order.getId());
                return false;
            }
            
            logger.info("外部接口调用成功 - 订单ID: {}, 返回数据长度: {}", order.getId(), result.length());
            
            // 解析返回结果
            JSONObject parsedData = null;
            try {
                Object parsed = JSON.parse(result);
                if (parsed instanceof JSONObject) {
                    parsedData = (JSONObject) parsed;
                } else {
                    logger.warn("返回数据不是JSON对象格式 - 订单ID: {}", order.getId());
                    return false;
                }
            } catch (Exception e) {
                logger.warn("解析返回数据失败 - 订单ID: {}, 错误: {}", order.getId(), e.getMessage());
                return false;
            }
            
            // 检查waybill_no
            JSONObject dataObj = parsedData.getJSONObject("data");
            if (dataObj == null) {
                logger.info("返回数据中没有data字段 - 订单ID: {}", order.getId());
                return false;
            }
            
            String waybillNo = dataObj.getString("waybill_no");
            if (waybillNo == null || waybillNo.trim().isEmpty()) {
                logger.info("waybill_no为空，无需处理 - 订单ID: {}", order.getId());
                return false;
            }
            
            logger.info("检测到waybill_no: {} - 订单ID: {}", waybillNo, order.getId());
            
            // 保存运单号到Redis（避免重复处理）
            String redisKey = REDIS_WAYBILL_KEY_PREFIX + order.getId();
            stringRedisTemplate.opsForValue().set(redisKey, waybillNo, 30, TimeUnit.DAYS);
            
            // 调用企业应用推送
            sendEnterprisePushNotification(order, waybillNo);
            
            logger.info("物流信息获取并推送成功 - 订单ID: {}, waybill_no: {}", order.getId(), waybillNo);
            return true;
            
        } catch (Exception e) {
            logger.error("获取物流信息失败 - 订单ID: {}, 错误: {}", order.getId(), e.getMessage(), e);
            return false;
        }
    }
    
    /**
     * 调用企业应用推送逻辑
     * @param order 订单信息
     * @param waybillNo 运单号
     */
    private void sendEnterprisePushNotification(JDOrder order, String waybillNo) {
        try {
            // 构建推送消息内容
            StringBuilder pushContent = new StringBuilder();
            
            // 第一行：分销标识（F或PDD）
            String distributionMark = order.getDistributionMark() != null ? order.getDistributionMark() : "未知";
            pushContent.append(distributionMark).append("\n");
            
            // PDD订单包含第三方单号，F订单不包含
            if ("PDD".equals(distributionMark)) {
                String thirdPartyOrderNo = order.getThirdPartyOrderNo();
                if (thirdPartyOrderNo != null && !thirdPartyOrderNo.trim().isEmpty()) {
                    pushContent.append("第三方单号：").append(thirdPartyOrderNo).append("\n");
                }
            }
            
            // 型号
            pushContent.append("型号：").append(order.getModelNumber() != null ? order.getModelNumber() : "无").append("\n");
            // 收货地址
            pushContent.append("收货地址：").append(order.getAddress() != null ? order.getAddress() : "无").append("\n");
            // 运单号
            pushContent.append("运单号：").append(waybillNo).append("\n");
            
            // 调用企业微信推送接口
            JSONObject pushParam = new JSONObject();
            pushParam.put("title", "JD物流信息推送");
            pushParam.put("text", pushContent.toString());
            
            // 使用支持自定义header的HTTP请求
            String pushResult = sendPostWithHeaders(PUSH_URL, pushParam.toJSONString(), PUSH_TOKEN);
            logger.info("企业应用推送调用结果 - 订单ID: {}, waybill_no: {}, 推送结果: {}", 
                order.getId(), waybillNo, pushResult);
            
        } catch (Exception e) {
            logger.error("调用企业应用推送失败 - 订单ID: {}, waybill_no: {}, 错误: {}", 
                order.getId(), waybillNo, e.getMessage(), e);
            // 不抛出异常，避免影响主流程
        }
    }
    
    /**
     * 发送POST请求，支持自定义header（用于企业微信推送）
     * @param url 请求URL
     * @param jsonBody JSON请求体
     * @param token 认证token
     * @return 响应结果
     */
    private String sendPostWithHeaders(String url, String jsonBody, String token) {
        java.io.BufferedReader in = null;
        java.io.PrintWriter out = null;
        StringBuilder result = new StringBuilder();
        try {
            java.net.URL realUrl = new java.net.URL(url);
            java.net.URLConnection conn = realUrl.openConnection();
            
            // 设置请求头
            conn.setRequestProperty("accept", "*/*");
            conn.setRequestProperty("connection", "Keep-Alive");
            conn.setRequestProperty("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64)");
            conn.setRequestProperty("Accept-Charset", "utf-8");
            conn.setRequestProperty("Content-Type", "application/json");
            conn.setRequestProperty("vanToken", token);
            conn.setRequestProperty("source", "XZJ_UBUNTU");
            
            conn.setDoOutput(true);
            conn.setDoInput(true);
            
            // 发送请求体
            out = new java.io.PrintWriter(conn.getOutputStream());
            out.print(jsonBody);
            out.flush();
            
            // 读取响应
            in = new java.io.BufferedReader(new java.io.InputStreamReader(conn.getInputStream(), java.nio.charset.StandardCharsets.UTF_8));
            String line;
            while ((line = in.readLine()) != null) {
                result.append(line);
            }
            
            logger.info("企业微信推送请求成功 - URL: {}, 响应: {}", url, result.toString());
        } catch (java.net.ConnectException e) {
            logger.error("企业微信推送连接失败 - URL: {}", url, e);
            throw new RuntimeException("推送连接失败: " + e.getMessage(), e);
        } catch (java.net.SocketTimeoutException e) {
            logger.error("企业微信推送超时 - URL: {}", url, e);
            throw new RuntimeException("推送请求超时: " + e.getMessage(), e);
        } catch (java.io.IOException e) {
            logger.error("企业微信推送IO异常 - URL: {}", url, e);
            throw new RuntimeException("推送IO异常: " + e.getMessage(), e);
        } catch (Exception e) {
            logger.error("企业微信推送异常 - URL: {}", url, e);
            throw new RuntimeException("推送异常: " + e.getMessage(), e);
        } finally {
            try {
                if (out != null) {
                    out.close();
                }
                if (in != null) {
                    in.close();
                }
            } catch (java.io.IOException ex) {
                logger.error("关闭流异常", ex);
            }
        }
        return result.toString();
    }
}

