package com.ruoyi.logistics.service;

import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.ruoyi.common.core.exception.ServiceException;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.redis.service.RedisService;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.logistics.constant.RedisCacheConstants;
import com.ruoyi.logistics.domain.BizMonthQrcodeRecord;
import com.ruoyi.logistics.domain.BizWaybillOrder;
import com.ruoyi.logistics.dto.MonthQRCodeDTO;
import com.ruoyi.logistics.service.factory.LogisticsFactory;
import com.ruoyi.logistics.util.QRCodeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.UnsupportedEncodingException;
import java.util.concurrent.TimeUnit;

@Service
public class LogisticsOrderService {
    @Autowired
    private LogisticsFactory logisticsFactory;

    @Autowired
    private RedisService redisService;

    @Autowired
    private IBizMonthQrcodeRecordService bizMonthQrcodeRecordService;


    public JSONObject precheckOrder(BizWaybillOrder bizWaybillOrder) throws UnsupportedEncodingException {
        // 根据订单中的物流公司类型获取对应服务
        LogisticsService logisticsService = logisticsFactory.getLogisticsService(bizWaybillOrder.getOrderType()==1?"JD":"SF");
        JSONObject  precheckOrder= logisticsService.precheckOrder(bizWaybillOrder);
        return precheckOrder;
    }


    public JSONObject createOrder(BizWaybillOrder bizWaybillOrder) throws UnsupportedEncodingException, JsonProcessingException {
        // 根据订单中的物流公司类型获取对应服务
        LogisticsService logisticsService = logisticsFactory.getLogisticsService(bizWaybillOrder.getOrderType()==1?"JD":"SF");
        JSONObject  createOrder= logisticsService.createOrder(bizWaybillOrder);
        return createOrder;
    }

    public JSONObject cancelOrder(BizWaybillOrder bizWaybillOrder) throws UnsupportedEncodingException {
        // 根据订单中的物流公司类型获取对应服务
        LogisticsService logisticsService = logisticsFactory.getLogisticsService(bizWaybillOrder.getOrderType()==1?"JD":"SF");
        JSONObject  cancelOrder= logisticsService.cancelOrder(bizWaybillOrder);
        return cancelOrder;
    }

    public JSONObject getQRCode(Integer source, Integer type) {
        // 根据订单中的物流公司类型获取对应服务
        LogisticsService logisticsService = logisticsFactory.getLogisticsService(source==1?"JD":"SF");
        JSONObject qrCode = logisticsService.getMonthQRCode(type);

        if(!qrCode.getBooleanValue("success")){
            throw new ServiceException("获取二维码失败！ 原因:"+qrCode.getString("msg"));
        }

        String qrcodeContent = qrCode.getString("msg");

        BizMonthQrcodeRecord bizMonthQrcodeRecord = new BizMonthQrcodeRecord();
        bizMonthQrcodeRecord.setQrcodeContent(qrcodeContent);
        bizMonthQrcodeRecord.setUserId(SecurityUtils.getUserId());
        bizMonthQrcodeRecord.setDeptId(SecurityUtils.getLoginUser().getSysUser().getDeptId());
        bizMonthQrcodeRecord.setSource(source);
        bizMonthQrcodeRecord.setIsVerify(0);
        bizMonthQrcodeRecord.setCreateTime(DateUtils.getNowDate());
        bizMonthQrcodeRecord.setCreateBy(SecurityUtils.getLoginUser().getSysUser().getNickName());

        bizMonthQrcodeRecordService.insertBizMonthQrcodeRecord(bizMonthQrcodeRecord);

        MonthQRCodeDTO monthQRCodeDTO = new MonthQRCodeDTO();
        monthQRCodeDTO.setQrcodeId(bizMonthQrcodeRecord.getQrcodeId());
        monthQRCodeDTO.setQrCodeContent(qrcodeContent);
        monthQRCodeDTO.setUserId(SecurityUtils.getUserId());
        monthQRCodeDTO.setDeptId(SecurityUtils.getLoginUser().getSysUser().getDeptId());
        monthQRCodeDTO.setSource(source);
        monthQRCodeDTO.setCreateTime(bizMonthQrcodeRecord.getCreateTime());
        monthQRCodeDTO.setProcessNum(0);

        redisService.setCacheObject(RedisCacheConstants.MONTH_QRCODE_CACHE_KEY + bizMonthQrcodeRecord.getQrcodeId(), monthQRCodeDTO, 660L, TimeUnit.SECONDS);

        // 生成二维码图片
        String qrCodeContent = qrCode.getString("msg");
        return getQRCodeImage(qrCodeContent);
    }

    public JSONObject getQRCodeImage(String content) {
        // 生成二维码图片并返回Base64编码的图片数据
        try {
            String base64Image = QRCodeUtils.generateQRCodeBase64(content, 300, 300);
            JSONObject result = new JSONObject();
            result.put("success", true);
            result.put("qrCodeImage", "data:image/png;base64," + base64Image);
            result.put("msg", "二维码生成成功");
            return result;
        } catch (Exception e) {
            JSONObject result = new JSONObject();
            result.put("success", false);
            result.put("msg", "生成二维码失败: " + e.getMessage());
            return result;
        }
    }

    public JSONObject queryOrderByQRCode(Integer orderType, String qrCode, Integer qrcodeType) {
        // 根据订单中的物流公司类型获取对应服务
        LogisticsService logisticsService = logisticsFactory.getLogisticsService(orderType==1?"JD":"SF");
        JSONObject jsonObject = logisticsService.queryOrderByQRCode(qrCode, qrcodeType);
        return jsonObject;
    }

    public JSONObject queryOrderDetailByWaybillCode(Integer orderType, String waybillCode) {
        // 根据订单中的物流公司类型获取对应服务
        LogisticsService logisticsService = logisticsFactory.getLogisticsService(orderType==1?"JD":"SF");
        JSONObject jsonObject = logisticsService.queryOrderDetailByWaybillCode(waybillCode);
        return jsonObject;
    }

    public JSONObject queryOrderFeeInfoByWaybillCode(Integer orderType, String waybillCode) {
        // 根据订单中的物流公司类型获取对应服务
        LogisticsService logisticsService = logisticsFactory.getLogisticsService(orderType==1?"JD":"SF");
        JSONObject jsonObject = logisticsService.queryOrderFeeInfo(waybillCode);
        return jsonObject;
    }

    public JSONObject queryWaybillPdf(BizWaybillOrder bizWaybillOrder) {
        // 根据订单中的物流公司类型获取对应服务
        LogisticsService logisticsService = logisticsFactory.getLogisticsService(bizWaybillOrder.getOrderType()==1?"JD":"SF");
        JSONObject jsonObject = logisticsService.queryWaybillPdf(bizWaybillOrder);
        return jsonObject;
    }
}

