package com.huazheng.project.greenplum.service.impl;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

import com.huazheng.project.greenplum.mapper.GPMapper;
import com.huazheng.project.hana.mapper.SapMapper;
import com.huazheng.project.hana.model.Afko;
import com.huazheng.project.hana.model.Afpo;
import com.huazheng.project.hana.model.Aufk;
import com.huazheng.project.hana.model.Bkpf;
import com.huazheng.project.hana.model.Kna1;

import cn.hutool.core.thread.ThreadUtil;
import lombok.extern.log4j.Log4j2;

@Log4j2
@Service
public class CheckDeleteServiceImpl {

	@Autowired
	private RedisTemplate<String, String> redis1Template;
	
	@Autowired
	private GPMapper gpMapper;
	
	@Autowired
	private SapMapper sapMapper;
	
	public String getErrorInfoFromException(Exception e) {
		try {
			StringWriter sw = new StringWriter();
			PrintWriter pw = new PrintWriter(sw);
			e.printStackTrace(pw);
			return "\r\n" + sw.toString() + "\r\n";
		} catch (Exception e2) {
			return "bad getErrorInfoFromException";
		}
	}
	
	public void selectAufkCheckByDelete() {
		try {
			ValueOperations<String, String> opsForValue = redis1Template.opsForValue();
			opsForValue.setIfAbsent("huazheng:checkDelete:Aufk:rowNum", "0");
			String rowNum = opsForValue.get("huazheng:checkDelete:Aufk:rowNum");
			Aufk build = Aufk.builder().rowNum(rowNum).build();
			
			List<Aufk> list = gpMapper.selectAufkCheck(build); // 从数仓中查询一组数据
			if (list.size() == 0) {
				redis1Template.opsForValue().set("huazheng:checkDelete:Aufk:rowNum", "0"); // 计数器复位
				ThreadUtil.sleep(1000); // 没有数据了，休眠一下
			}
			
			list.forEach(target -> { // 遍历要检查的数据
				Aufk source = sapMapper.selectAufkById(target); // 根据主键查询源库中的数据
				String operator = "none";
				if (source == null) { // 如果源库中没有数据
					gpMapper.deleteAufk(target); // 删除数仓中的数据
					operator = "delete";
				}
				redis1Template.opsForValue().set("huazheng:checkDelete:Aufk:rowNum", target.getRowNum());
				if (!operator.equals("none")) {
					log.info(String.format("selectAufkcheckDelete --> rowNum:%s, operator:%s", target.getRowNum(), operator));
				}
			});
		} catch (Exception e) {
			redis1Template.opsForValue().set("huazheng:checkDeleteError:Aufk:rowNum", getErrorInfoFromException(e));
		}
	}
	
	public void selectAfkoCheckByDelete() {
		try {
			ValueOperations<String, String> opsForValue = redis1Template.opsForValue();
			opsForValue.setIfAbsent("huazheng:checkDelete:Afko:rowNum", "0");
			String rowNum = opsForValue.get("huazheng:checkDelete:Afko:rowNum");
			Afko build = Afko.builder().rowNum(rowNum).build();
			
			List<Afko> list = gpMapper.selectAfkoCheck(build); // 从数仓中查询一组数据
			if (list.size() == 0) {
				redis1Template.opsForValue().set("huazheng:checkDelete:Afko:rowNum", "0"); // 计数器复位
				ThreadUtil.sleep(1000); // 没有数据了，休眠一下
			}
			
			list.forEach(target -> { // 遍历要检查的数据
				Afko source = sapMapper.selectAfkoById(target); // 根据主键查询源库中的数据
				String operator = "none";
				if (source == null) { // 如果源库中没有数据
					gpMapper.deleteAfko(target); // 删除数仓中的数据
					operator = "delete";
				}
				redis1Template.opsForValue().set("huazheng:checkDelete:Afko:rowNum", target.getRowNum());
				if (!operator.equals("none")) {
					log.info(String.format("selectAfkocheckDelete --> rowNum:%s, operator:%s", target.getRowNum(), operator));
				}
			});
		} catch (Exception e) {
			redis1Template.opsForValue().set("huazheng:checkDeleteError:Afko:rowNum", getErrorInfoFromException(e));
		}
	}
	
	public void selectAfpoCheckByDelete() {
		try {
			ValueOperations<String, String> opsForValue = redis1Template.opsForValue();
			opsForValue.setIfAbsent("huazheng:checkDelete:Afpo:rowNum", "0");
			String rowNum = opsForValue.get("huazheng:checkDelete:Afpo:rowNum");
			Afpo build = Afpo.builder().rowNum(rowNum).build();
			
			List<Afpo> list = gpMapper.selectAfpoCheck(build); // 从数仓中查询一组数据
			if (list.size() == 0) {
				redis1Template.opsForValue().set("huazheng:checkDelete:Afpo:rowNum", "0"); // 计数器复位
				ThreadUtil.sleep(1000); // 没有数据了，休眠一下
			}
			
			list.forEach(target -> { // 遍历要检查的数据
				Afpo source = sapMapper.selectAfpoById(target); // 根据主键查询源库中的数据
				String operator = "none";
				if (source == null) { // 如果源库中没有数据
					gpMapper.deleteAfpo(target); // 删除数仓中的数据
					operator = "delete";
				}

				redis1Template.opsForValue().set("huazheng:checkDelete:Afpo:rowNum", target.getRowNum());
				if (!operator.equals("none")) {
					log.info(String.format("selectAfpocheckDelete --> rowNum:%s, operator:%s", target.getRowNum(), operator));
				}
			});
		} catch (Exception e) {
			redis1Template.opsForValue().set("huazheng:checkDeleteError:Afpo:rowNum", getErrorInfoFromException(e));
		}
	}
	
	public void selectBkpfCheckByDelete() {
		try {
			ValueOperations<String, String> opsForValue = redis1Template.opsForValue();
			opsForValue.setIfAbsent("huazheng:checkDelete:Bkpf:rowNum", "0");
			String rowNum = opsForValue.get("huazheng:checkDelete:Bkpf:rowNum");
			Bkpf build = Bkpf.builder().rowNum(rowNum).build();
			
			List<Bkpf> list = gpMapper.selectBkpfCheck(build); // 从数仓中查询一组数据
			if (list.size() == 0) {
				redis1Template.opsForValue().set("huazheng:checkDelete:Bkpf:rowNum", "0"); // 计数器复位
				ThreadUtil.sleep(1000); // 没有数据了，休眠一下
			}
			
			list.forEach(target -> { // 遍历要检查的数据
				Bkpf source = sapMapper.selectBkpfById(target); // 根据主键查询源库中的数据
				String operator = "none";
				if (source == null) { // 如果源库中没有数据
					gpMapper.deleteBkpf(target); // 删除数仓中的数据
					operator = "delete";
				}

				redis1Template.opsForValue().set("huazheng:checkDelete:Bkpf:rowNum", target.getRowNum());
				if (!operator.equals("none")) {
					log.info(String.format("selectBkpfcheckDelete --> rowNum:%s, operator:%s", target.getRowNum(), operator));
				}
			});
		} catch (Exception e) {
			redis1Template.opsForValue().set("huazheng:checkDeleteError:Bkpf:rowNum", getErrorInfoFromException(e));
		}
	}
	
	public void selectKna1CheckByDelete() {
		try {
			ValueOperations<String, String> opsForValue = redis1Template.opsForValue();
			opsForValue.setIfAbsent("huazheng:checkDelete:Kna1:rowNum", "0");
			String rowNum = opsForValue.get("huazheng:checkDelete:Kna1:rowNum");
			Kna1 build = Kna1.builder().rowNum(rowNum).build();
			
			List<Kna1> list = gpMapper.selectKna1Check(build); // 从数仓中查询一组数据
			if (list.size() == 0) {
				redis1Template.opsForValue().set("huazheng:checkDelete:Kna1:rowNum", "0"); // 计数器复位
				ThreadUtil.sleep(1000); // 没有数据了，休眠一下
			}
			
			list.forEach(target -> { // 遍历要检查的数据
				Kna1 source = sapMapper.selectKna1ById(target); // 根据主键查询源库中的数据
				String operator = "none";
				if (source == null) { // 如果源库中没有数据
					gpMapper.deleteKna1(target); // 删除数仓中的数据
					operator = "delete";
				}

				redis1Template.opsForValue().set("huazheng:checkDelete:Kna1:rowNum", target.getRowNum());
				if (!operator.equals("none")) {
					log.info(String.format("selectKna1checkDelete --> rowNum:%s, operator:%s", target.getRowNum(), operator));
				}
			});
		} catch (Exception e) {
			redis1Template.opsForValue().set("huazheng:checkDeleteError:Kna1:rowNum", getErrorInfoFromException(e));
		}
	}
	
}
