package cn.pluss.platform.job;

import cn.pluss.platform.activityActivate.ActivityActivateService;
import cn.pluss.platform.activityConsumReturn.ActivityConsumReturnService;
import cn.pluss.platform.activityRecharge.ActivityRechargeService;
import cn.pluss.platform.activityRecommend.ActivityRecommendService;
import cn.pluss.platform.entity.ActivityConsumReturn;
import cn.pluss.platform.entity.ActivityRecharge;
import cn.pluss.platform.util.DateUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.Scheduled;

import java.util.Date;
import java.util.List;

/**
 * @author yuchen
 *
 * @Description 检查活动是否到期 到期删除
 */
@Configuration
@ConditionalOnProperty(prefix = "scheduling", name = "enabled", havingValue = "true")
public class ExamineActityIsExistJob {
	
	@Autowired
	private ActivityConsumReturnService activityConsumReturnService;
	
	@Autowired
	private ActivityRechargeService activityRechargeService;

	/**
	 *	每天凌晨12点执行一次 判断活动是否过期 过期活动删除
	 */
	@Scheduled(cron = "05 00 0 * * ?")
	private void configureTasks() {
		Integer timeType = 1;
		String nowDate = DateUtils.currentYourDate();
		String lastSql = " and (date_format(endDt,'%Y-%m-%d')) < '" + nowDate + "'";
		QueryWrapper<ActivityConsumReturn> returnWrapper = new QueryWrapper<ActivityConsumReturn>()
				.eq("timeType", timeType).last(lastSql);
		List<ActivityConsumReturn> returnList = activityConsumReturnService.list(returnWrapper);
		if (!returnList.isEmpty()) {
			for (ActivityConsumReturn r : returnList) {
				activityConsumReturnService.removeById(r.getId());
			}
		}
		QueryWrapper<ActivityRecharge> rechargeWrapper = new QueryWrapper<ActivityRecharge>()
				.eq("timeType", timeType).last(lastSql);
		List<ActivityRecharge> rechargeList = activityRechargeService.list(rechargeWrapper);
		if (!rechargeList.isEmpty()) {
			for (ActivityRecharge r : rechargeList) {
				activityRechargeService.removeById(r.getId());
			}
		}
	}
}

