package com.gree.mall.manager.schedule;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gree.mall.manager.enums.*;
import com.gree.mall.manager.enums.material.PayTypeEnum;
import com.gree.mall.manager.plus.entity.Coupon;
import com.gree.mall.manager.plus.entity.EsOrderInfo;
import com.gree.mall.manager.plus.entity.PgIncreOrder;
import com.gree.mall.manager.plus.entity.SettlementOrder;
import com.gree.mall.manager.plus.service.CouponService;
import com.gree.mall.manager.plus.service.EsOrderInfoService;
import com.gree.mall.manager.plus.service.PgIncreOrderService;
import com.gree.mall.manager.plus.service.SettlementOrderService;
import com.gree.mall.manager.utils.DateUtils;
import net.bytebuddy.asm.Advice;
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 org.springframework.transaction.annotation.Transactional;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.text.ParseException;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@ConditionalOnProperty(name = "schedule.enable", havingValue = "true", matchIfMissing = true)
@Component
public class SettlementSchedule {

    @Resource
    SettlementOrderService settlementOrderService;

    @Resource
    PgIncreOrderService pgIncreOrderService;

    @Autowired
    EsOrderInfoService esOrderInfoService;

    /**到达提现时间变为可提现
     *
     */
    @Scheduled(fixedDelay = 60 * 1000)
    public void task() {
        settlementOrderService.lambdaUpdate()
                .lt(SettlementOrder::getChangeTime,new Date())
                .eq(SettlementOrder::getStatus,SettlementStatusEnum.WAIT.getKey())
                .set(SettlementOrder::getStatus, SettlementStatusEnum.WAIT_ING.getKey()).update();
    }


    /**服务结束改为服务结束
     *
     */
    @Scheduled(fixedDelay = 60 * 1000)
    public void incre() {
        pgIncreOrderService.lambdaUpdate()
                .lt(PgIncreOrder::getServiceEndTime,new Date())
                .eq(PgIncreOrder::getPayStatus, PayStatusEnum.PAID.getKey())
                .set(PgIncreOrder::getPayStatus, PayStatusEnum.EXPIRE.getKey())
                .update();
    }



    /**超时取消
     *
     */
    @Scheduled(fixedDelay = 60 * 1000)
    public void cancelOrder() throws ParseException {
        timeoutOrder();
    }



    public void timeoutOrder() throws ParseException {

        Date timeByMin = DateUtils.getTimeByMin(new Date(), -10);
        //1次至多处理50单
        List<EsOrderInfo> list = esOrderInfoService.lambdaQuery()
                .select(EsOrderInfo::getEsOrderId)
                .le(EsOrderInfo::getCreateTime, timeByMin)
                .eq(EsOrderInfo::getStatus, ExamineStatusEnum.SAVE.getKey())
                .eq(EsOrderInfo::getPayType, PayTypeEnum.WECHAT.getKey())
                .page(new Page<>(1, 50, false))
                .getRecords();
        if (CollectionUtil.isEmpty(list)) {
            return;
        }
        List<String> orderIds = list.stream().map(EsOrderInfo::getEsOrderId).collect(Collectors.toList());
        this.cancel(orderIds);
    }


    /**
     * 取消订单
     */
    @Transactional
    public void cancel(List<String> orderIds) {
        esOrderInfoService.lambdaUpdate()
                .in(EsOrderInfo::getEsOrderId,orderIds)
                .set(EsOrderInfo::getStatus, "TIME_OUT")
                .update();
    }

}
