package com.gree.mall.manager.schedule;

import com.gree.mall.manager.logic.ExpressLogic;
import com.gree.mall.manager.plus.entity.OrderRefund;
import com.gree.mall.manager.plus.service.OrderRefundService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
@ConditionalOnProperty(name = "schedule.enable", havingValue = "true", matchIfMissing = true)
public class OrderRefundSchedule {
    @Autowired
    OrderRefundService orderRefundService;
    @Autowired
    ExpressLogic expressLogic;

    @Scheduled(fixedDelay = 5 * 60 * 1000)
    public void orderRefund(){
        // id = 1    status = false  ..
        // id = 2    status = false
        // id = 3    status = true
        List<OrderRefund> OrderRefundList =orderRefundService.lambdaQuery()
                .eq(OrderRefund::getSubscribeStatus,false)
                .list();
        for (OrderRefund orderRefund :OrderRefundList){
            if (StringUtils.isNotBlank(orderRefund.getLogisticsNo())){
                //1.订阅
                expressLogic.subscribeExpress(orderRefund.getLogisticsNo(),null,orderRefund.getRecePhone());
                //2.标记为已订阅
                orderRefundService.lambdaUpdate()
                        .eq(OrderRefund::getOrderRefundId,orderRefund.getOrderRefundId())
                        .set(OrderRefund::getSubscribeStatus,true)
                        .update();
            }
        }


    }



}
