package com.xunwang.payment.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.google.gson.Gson;
import com.xunwang.payment.entity.OrderInfo;
import com.xunwang.payment.entity.Refund;
import com.xunwang.payment.enums.wxpay.WxRefundStatus;
import com.xunwang.payment.mapper.RefundMapper;
import com.xunwang.payment.service.OrderInfoService;
import com.xunwang.payment.service.RefundService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xunwang.payment.util.OrderNoUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.Duration;
import java.time.Instant;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 退款表 服务实现类
 * </p>
 *
 * @author Long
 * @since 2023-02-26
 */
@Service
public class RefundServiceImpl extends ServiceImpl<RefundMapper, Refund> implements RefundService {

    @Resource
    private OrderInfoService orderInfoService;

    @Override
    public Refund createRefundByOrderNo(String orderNo, String reason) {
        // 根据订单号获取订单信息
        OrderInfo orderInfo = orderInfoService.getOne(new LambdaQueryWrapper<OrderInfo>().eq(OrderInfo::getOrderId, orderNo));

        //根据订单号生成退款订单
        Refund refundInfo = new Refund();
        refundInfo.setOrderNo(orderNo); // 订单编号
        refundInfo.setRefundNo(OrderNoUtils.getRefundNo()); // 退款单编号
        refundInfo.setTotalFee(orderInfo.getRequireSalary());    // 原订单金额（分）
        refundInfo.setRefund(orderInfo.getRequireSalary());  // 退款金额（分）
        refundInfo.setReason(reason);   // 退款原因

        // 保存退款订单
        baseMapper.insert(refundInfo);

        return refundInfo;
    }

    @Override
    public void updateRefund(String content) {

        // 将json字符串转换成Map
        Gson gson = new Gson();
        Map<String, String> resultMap = gson.fromJson(content, HashMap.class);

        // 设置要修改的字段
        Refund refundInfo = new Refund();

        refundInfo.setRefundId(resultMap.get("refund_id")); // 微信支付退款单号

        // 查询退款和申请退款中的返回参数
        if (resultMap.get("status") != null) {
            refundInfo.setRefundStatus(resultMap.get("status"));    // 退款状态
            refundInfo.setContentReturn(content);   // 将全部响应结果存入数据库的content字段
        }

        // 退款回调中（退款通知）的回调参数
        if (resultMap.get("refund_status") != null) {
            refundInfo.setRefundStatus(resultMap.get("refund_status")); // 退款状态
            refundInfo.setContentNotify(content);   // 将全部响应结果存入数据库的content字段
        }

        // 根据退款单编号修改退款单
        String refundNo = resultMap.get("out_refund_no");
        refundInfo.setRefundNo(refundNo);

        // 更新退款单
        baseMapper.update(refundInfo, new LambdaQueryWrapper<Refund>().eq(Refund::getRefundNo, refundNo));
    }

    @Override
    public List<Refund> getNoRefundOrderByDuration(int minutes) {
        // minutes分钟之前的时间
        Instant instant = Instant.now().minus(Duration.ofMinutes(minutes));

        Refund refund = new Refund();
        refund.setRefundStatus(WxRefundStatus.PROCESSING.getType());
        refund.setCreateTime(instant.toString());

        List<Refund> refundInfoList = baseMapper.selectList(new LambdaQueryWrapper<Refund>()
                .eq(Refund::getRefundStatus, refund.getRefundStatus())
                .le(Refund::getCreateTime, refund.getCreateTime()));
        return refundInfoList;
    }
}
