package org.springblade.modules.quartz.task;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springblade.modules.distribution.entity.DistributionConfig;
import org.springblade.modules.distribution.service.IAppointService;
import org.springblade.modules.distribution.service.IDistributionConfigService;
import org.springblade.modules.groupon.service.IEmbersService;
import org.springblade.modules.platform.service.UserAppService;
import org.springblade.modules.system.entity.UserApp;
import org.springblade.modules.system.service.IUserService;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Objects;

@Component("unbundleTask")
public class UnbundleTask {

    @Lazy
    @Resource
    private IEmbersService embersService;

    @Lazy
    @Resource
    private IDistributionConfigService distributionConfigService;

    public void  unbundleUser(){
        DistributionConfig config = distributionConfigService.lambdaQuery()
                .eq(DistributionConfig::getIsDelete, 0)
                .one();
        if (Objects.isNull(config)){
            return;
        }
        Integer bindingDays = config.getBindingDays();
        if (Objects.isNull(bindingDays)){
            return;
        }
        //获取应当解绑的时间
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.add(calendar.DATE,-bindingDays);
        Date time = calendar.getTime();
        embersService.unbundleUser(time);
    }

    public static void main(String[] args) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.add(calendar.DATE,-3);
        Date time = calendar.getTime();
        System.out.println(time);
    }


}
