package com.zhongshu.card.server.core.time;

import com.github.microservice.components.data.mongo.token.service.ResourceTokenService;
import com.github.microservice.net.ResultContent;
import com.github.microservice.net.ResultState;
import com.zhongshu.card.server.core.domain.payment.RefundRecord;
import com.zhongshu.card.server.core.model.pay.UnionFrictionlessPayFinishModel;
import com.zhongshu.card.server.core.service.pay.ChinaumsSenselessPayService;
import com.zhongshu.card.server.core.service.payment.ExpenseFlowServiceImpl;
import com.zhongshu.card.server.core.service.payment.PayCallService;
import com.zhongshu.card.server.core.service.payment.RefundRecordService;
import lombok.Cleanup;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;

@Slf4j
@Component
public class ComplexTimer {

    @Autowired
    RefundRecordService refundRecordService;

    @Autowired
    ChinaumsSenselessPayService chinaumsSenselessPayService;

    @Autowired
    PayCallService payCallService;

    @Autowired
    ResourceTokenService resourceTokenService;

//    @Scheduled(fixedDelay = 2*1000L)
    @SneakyThrows
    public void queryChinaumsRefund(){
        @Cleanup ResourceTokenService.Token token =  resourceTokenService.token("queryChinaumsRefund");
        //获取退款中的订单
        List<RefundRecord> refundingRecord = refundRecordService.getTop10RefundingRecord();
        if (refundingRecord!=null && !refundingRecord.isEmpty()){
            for (RefundRecord refundRecord : refundingRecord){
                ResultContent<UnionFrictionlessPayFinishModel> refundQueryResult = chinaumsSenselessPayService.refundQuery(refundRecord.getProjectOid(), refundRecord.getCreateUserId(), refundRecord.getOid(), refundRecord.getRefundNo(), true);
                if (refundQueryResult.getState().equals(ResultState.Success)){
                    UnionFrictionlessPayFinishModel unionFrictionlessPayFinishModel = refundQueryResult.getContent();
                    payCallService.unionFrictionlessPayRefundFinish(unionFrictionlessPayFinishModel);
                }
                Thread.sleep(3000);
            }
        }

    }
}
