package com.iformall.schedule;

import com.iformall.domain.po.WxCouponChannel;
import com.iformall.domain.po.WxGame;
import com.iformall.enums.EnumCouponChannelStatus;
import com.iformall.enums.EnumCouponChannelType;
import com.iformall.enums.EnumGameStatus;
import com.iformall.mapper.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Component
public class CouponExpiringSchedule {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private WxCouponMapper wxCouponMapper;

    @Autowired
    private WxCouponChannelMapper wxCouponChannelMapper;

    @Autowired
    private WxCouponSendMapper wxCouponSendMapper;

    @Autowired
    private WxScreenAdMapper wxScreenAdMapper;

    @Autowired
    private WxGameMapper wxGameMapper;


    @Scheduled(cron = "0 5 0 * * ?") // 每天凌晨00:05 作废过期券,商户禁用券
    //@Scheduled(cron = "*/10 * * * * ?") // 测试10秒中一次
    public void couponExpiringSchedule() {

        wxCouponMapper.offExpiriedCouponByValidDate();
        wxCouponMapper.offExpiriedCouponByMerchantStatus();

        wxCouponSendMapper.offExpiriedCouponSendByCouponStatus();
        wxCouponChannelMapper.offExpiriedCouponChannelByCouponStatus();
    }

    @Scheduled(cron = "0 10 0 * * ?") // 每天凌晨00:10 下架定向发放
    //@Scheduled(cron = "*/10 * * * * ?") // 测试10秒中一次
    public void couponSendExpiringSchedule() {
        wxCouponSendMapper.offExpiriedCouponSendByCouponStatus();
    }

    @Scheduled(cron = "0 10 0 * * ?") // 每天凌晨00:10 下架主动领取
    //@Scheduled(cron = "*/10 * * * * ?") // 测试10秒中一次
    public void couponChannelExpiringSchedule() {

        wxCouponChannelMapper.offExpiriedCouponChannelByEndTime();
        wxCouponChannelMapper.offExpiriedCouponChannelByValidDate();
        wxCouponChannelMapper.offExpiriedCouponChannelByCouponStatus();
    }

    @Scheduled(cron = "0 15 0 * * ?") // 每天凌晨00:15 下架券广告
    //@Scheduled(cron = "*/10 * * * * ?") // 测试10秒中一次
    public void screenAdExpiringSchedule() {
        wxScreenAdMapper.offScreeAdByCouponChannelStatus();
    }

    @Scheduled(cron = "0 0,1,2,3 0/1 * * ?") // 每小时 0, 1 ,2 ,3 分钟(整点)执行//限时抢购
    //@Scheduled(cron = "*/10 * * * * ?") // 测试10秒中一次
    public void couponChannel2ExpiringSchedule() {
        wxCouponChannelMapper.offExpiriedCouponChannel2ByEndTime();
    }

    @Scheduled(cron = "0 20 0 * * ?") // 每天凌晨00:20 下架游戏
    //@Scheduled(cron = "*/10 * * * * ?") // 测试10秒中一次
    public void gameExpiringSchedule() {
        WxGame wxGame = new WxGame();
        wxGame.setStatus(EnumGameStatus.STATUS_THROW_IN.getCode());

        List<WxGame> list = wxGameMapper.findList(wxGame);
        list.stream().forEach(g->{
            WxCouponChannel couponChannel = new WxCouponChannel();
            couponChannel.setTenantId(g.getTenantId());
            couponChannel.setTargetAd(EnumCouponChannelType.COUPON_CHANNEL_ID_GAME.getCode());
            couponChannel.setSubTargetId(g.getId());
            couponChannel.setStatus(EnumCouponChannelStatus.STATUS_THROW_IN.getCode());
            int count = wxCouponChannelMapper.countCouponChannel(couponChannel);
            if (count == 0) {
                WxGame record = new WxGame();
                record.setId(g.getId());
                record.setStatus(EnumGameStatus.STATUS_TAKE_OFFF.getCode());
                wxGameMapper.updateById(record);
            }
        });
    }


}