package com.hzs.third.pay.provider;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.hzs.common.core.domain.R;
import com.hzs.common.core.enums.EPayChannel;
import com.hzs.common.domain.third.pay.TOnlinePayment;
import com.hzs.third.pay.ITOnlinePaymentServiceApi;
import com.hzs.third.pay.service.IRefundService;
import com.hzs.third.pay.service.ITOnlinePaymentService;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Objects;

@DubboService
public class ITOnlinePaymentServiceProvider implements ITOnlinePaymentServiceApi {
    @Autowired
    private ITOnlinePaymentService itOnlinePaymentService;
    @Autowired
    private IRefundService iRefundService;
    @Override
    public R<TOnlinePayment> getOnlinePayment(String orderNo) {
        LambdaQueryWrapper<TOnlinePayment> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(TOnlinePayment::getBusinessCode, orderNo);
        return R.ok(itOnlinePaymentService.getOne(queryWrapper, false));
    }

    @Override
    public R<String> refund(String orderNo, Long userId) {
        LambdaQueryWrapper<TOnlinePayment> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(TOnlinePayment::getBusinessCode, orderNo);
        TOnlinePayment tOnlinePayment = itOnlinePaymentService.getOne(queryWrapper, false);
        if(ObjectUtil.isNotEmpty(tOnlinePayment)){
            if (Objects.requireNonNull(EPayChannel.getEnumByValue(tOnlinePayment.getPayChannel())) == EPayChannel.JD) {
                return R.ok(iRefundService.jdRefundDivision(orderNo, userId));
            }
        }
        return R.fail();
    }
}
