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

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.greenplum.utils.SomeUtils;
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 com.huazheng.project.hana.model.Likp;
import com.huazheng.project.hana.model.Lips;
import com.huazheng.project.hana.model.Pa0002;
import com.huazheng.project.hana.model.Vbak;
import com.huazheng.project.hana.model.Vbrk;

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 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", SomeUtils.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", SomeUtils.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", SomeUtils.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", SomeUtils.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", SomeUtils.getErrorInfoFromException(e));
		}
	}
	public void selectLikpCheckByDelete() {
		try {
			ValueOperations<String, String> opsForValue = redis1Template.opsForValue();
			opsForValue.setIfAbsent("huazheng:checkDelete:Likp:rowNum", "0");
			String rowNum = opsForValue.get("huazheng:checkDelete:Likp:rowNum");
			Likp build = Likp.builder().rowNum(rowNum).build();
			
			List<Likp> list = gpMapper.selectLikpCheck(build); // 从数仓中查询一组数据
			if (list.size() == 0) { // rowid已完成，则转为由更新时间字段开始同步
				redis1Template.opsForValue().set("huazheng:checkDelete:Likp:rowNum", "0"); // 计数器复位
				ThreadUtil.sleep(1000); // 没有数据了，休眠一下
			}
			
			list.forEach(target -> { // 遍历要检查的数据
				Likp source = sapMapper.selectLikpById(target); // 根据主键查询源库中的数据
				String operator = "none";
				if (source == null) { // 如果源库中没有数据
					gpMapper.deleteLikp(target); // 删除数仓中的数据
					operator = "delete";
				}

				redis1Template.opsForValue().set("huazheng:checkDelete:Likp:rowNum", target.getRowNum());
				if (!operator.equals("none")) {
					log.info(String.format("selectLikpcheckDelete --> rowNum:%s, operator:%s", target.getRowNum(), operator));
				}
			});
		} catch (Exception e) {
			redis1Template.opsForValue().set("huazheng:checkDeleteError:Likp:rowNum", SomeUtils.getErrorInfoFromException(e));
		}
	}
	public void selectLipsCheckByDelete() {
		try {
			ValueOperations<String, String> opsForValue = redis1Template.opsForValue();
			opsForValue.setIfAbsent("huazheng:checkDelete:Lips:rowNum", "0");
			String rowNum = opsForValue.get("huazheng:checkDelete:Lips:rowNum");
			Lips build = Lips.builder().rowNum(rowNum).build();

			List<Lips> list = gpMapper.selectLipsCheck(build); // 从数仓中查询一组数据
			if (list.size() == 0) { // rowid已完成，则转为由更新时间字段开始同步
				redis1Template.opsForValue().set("huazheng:checkDelete:Lips:rowNum", "0"); // 计数器复位
				ThreadUtil.sleep(1000); // 没有数据了，休眠一下
			}

			list.forEach(target -> { // 遍历要检查的数据
				Lips source = sapMapper.selectLipsById(target); // 根据主键查询源库中的数据
				String operator = "none";
				if (source == null) { // 如果源库中没有数据
					gpMapper.deleteLips(target); // 删除数仓中的数据
					operator = "delete";
				}

				redis1Template.opsForValue().set("huazheng:checkDelete:Lips:rowNum", target.getRowNum());
				if (!operator.equals("none")) {
					log.info(String.format("selectLipscheckDelete --> rowNum:%s, operator:%s", target.getRowNum(), operator));
				}
			});
		} catch (Exception e) {
			redis1Template.opsForValue().set("huazheng:checkDeleteError:Lips:rowNum", SomeUtils.getErrorInfoFromException(e));
		}
	}
	public void selectPa0002CheckByDelete() {
		try {
			ValueOperations<String, String> opsForValue = redis1Template.opsForValue();
			opsForValue.setIfAbsent("huazheng:checkDelete:Pa0002:rowNum", "0");
			String rowNum = opsForValue.get("huazheng:checkDelete:Pa0002:rowNum");
			Pa0002 build = Pa0002.builder().rowNum(rowNum).build();

			List<Pa0002> list = gpMapper.selectPa0002Check(build); // 从数仓中查询一组数据
			if (list.size() == 0) { // rowid已完成，则转为由更新时间字段开始同步
				redis1Template.opsForValue().set("huazheng:checkDelete:Pa0002:rowNum", "0"); // 计数器复位
				ThreadUtil.sleep(1000); // 没有数据了，休眠一下
			}

			list.forEach(target -> { // 遍历要检查的数据
				Pa0002 source = sapMapper.selectPa0002ById(target); // 根据主键查询源库中的数据
				String operator = "none";
				if (source == null) { // 如果源库中没有数据
					gpMapper.deletePa0002(target); // 删除数仓中的数据
					operator = "delete";
				}

				redis1Template.opsForValue().set("huazheng:checkDelete:Pa0002:rowNum", target.getRowNum());
				if (!operator.equals("none")) {
					log.info(String.format("selectPa0002checkDelete --> rowNum:%s, operator:%s", target.getRowNum(), operator));
				}
			});
		} catch (Exception e) {
			redis1Template.opsForValue().set("huazheng:checkDeleteError:Pa0002:rowNum", SomeUtils.getErrorInfoFromException(e));
		}
	}
	public void selectVbakCheckByDelete() {
		try {
			ValueOperations<String, String> opsForValue = redis1Template.opsForValue();
			opsForValue.setIfAbsent("huazheng:checkDelete:Vbak:rowNum", "0");
			String rowNum = opsForValue.get("huazheng:checkDelete:Vbak:rowNum");
			Vbak build = Vbak.builder().rowNum(rowNum).build();

			List<Vbak> list = gpMapper.selectVbakCheck(build); // 从数仓中查询一组数据
			if (list.size() == 0) {
				redis1Template.opsForValue().set("huazheng:checkDelete:Vbak:rowNum", "0"); // 计数器复位
				ThreadUtil.sleep(1000); // 没有数据了，休眠一下
			}

			list.forEach(target -> { // 遍历要检查的数据
				Vbak source = sapMapper.selectVbakById(target); // 根据主键查询源库中的数据
				String operator = "none";
				if (source == null) { // 如果源库中没有数据
					gpMapper.deleteVbak(target); // 删除数仓中的数据
					operator = "delete";
				}

				redis1Template.opsForValue().set("huazheng:checkDelete:Vbak:rowNum", target.getRowNum());
				if (!operator.equals("none")) {
					log.info(String.format("selectVbakcheckDelete --> rowNum:%s, operator:%s", target.getRowNum(), operator));
				}
			});
		} catch (Exception e) {
			redis1Template.opsForValue().set("huazheng:checkDeleteError:Vbak:rowNum", SomeUtils.getErrorInfoFromException(e));
		}
	}
	public void selectVbrkCheckByDelete() {
		try {
			ValueOperations<String, String> opsForValue = redis1Template.opsForValue();
			opsForValue.setIfAbsent("huazheng:checkDelete:Vbrk:rowNum", "0");
			String rowNum = opsForValue.get("huazheng:checkDelete:Vbrk:rowNum");
			Vbrk build = Vbrk.builder().rowNum(rowNum).build();

			List<Vbrk> list = gpMapper.selectVbrkCheck(build); // 从数仓中查询一组数据
			if (list.size() == 0) { // rowid已完成，则转为由更新时间字段开始同步
				redis1Template.opsForValue().set("huazheng:checkDelete:Vbrk:rowNum", "0"); // 计数器复位
				ThreadUtil.sleep(1000); // 没有数据了，休眠一下
			}

			list.forEach(target -> { // 遍历要检查的数据
				Vbrk source = sapMapper.selectVbrkById(target); // 根据主键查询源库中的数据
				String operator = "none";
				if (source == null) { // 如果源库中没有数据
					gpMapper.deleteVbrk(target); // 删除数仓中的数据
					operator = "delete";
				}

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