package com.tofly.qualitycontrol.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tofly.qualitycontrol.common.QualityControlConstant;
import com.tofly.qualitycontrol.config.ScanScheduler;
import com.tofly.qualitycontrol.entity.ScanFrequencyConfig;
import com.tofly.qualitycontrol.mapper.ScanFrequencyConfigMapper;
import com.tofly.qualitycontrol.service.IScanFrequencyConfigService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author B
 */
@Service
@RequiredArgsConstructor
public class ScanFrequencyConfigServiceImpl extends ServiceImpl<ScanFrequencyConfigMapper,ScanFrequencyConfig> implements IScanFrequencyConfigService, QualityControlConstant {

	private final ScanScheduler scanScheduler;

	@Override
	@Transactional(rollbackFor = Exception.class)
	public Boolean configScanFrequency(Long id) {
		Boolean flag1 = update(null,Wrappers.<ScanFrequencyConfig>lambdaUpdate().eq(ScanFrequencyConfig::getStatus,1).set(ScanFrequencyConfig::getStatus,0));

		Boolean flag2 = update(null,Wrappers.<ScanFrequencyConfig>lambdaUpdate().eq(ScanFrequencyConfig::getId,id).set(ScanFrequencyConfig::getStatus,1));

		String expression = getById(id).getExpression();

		Boolean flag3 = true;
		try {
			//设置定时器执行时间
			scanScheduler.setCron(expression);
		} catch (Exception e) {
			flag3 = false;
		}

		return flag1 && flag2 && flag3;
	}

}
